Skip to content

The no_std feature update #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,23 @@ name = "multiwii_serial_protocol_v2"
description = "A Multiwii Serial Protocol (MSP) implementation for Rust"
repository = "https://github.com/amfern/multiwii_serial_protocol.rs"
version = "0.1.12"
authors = ["Rudi Benkovic <[email protected]>", "Ilya Guterman <[email protected]>"]
authors = [
"Rudi Benkovic <[email protected]>",
"Ilya Guterman <[email protected]>",
]
license = "MIT OR Apache-2.0"
readme = "README.md"
edition = "2021"

[dependencies]
packed_struct = "0.4"
packed_struct_codegen = "0.4"
serde = "1.0"
serde_derive = "1.0"
crc-any = "2.3"
packed_struct = { version = "0.10", default-features = false }
packed_struct_codegen = { version = "0.10", default-features = false }
serde = { version = "1.0", default-features = false, features = ["derive"] }
crc-any = { version = "2.5", default-features = false }
heapless = "0.8"

[features]
default = ["std"]
std = []
std = ["packed_struct/std"]
no_std = []
suppport_int32_setting_type = []
suppport_int32_setting_type = []
5 changes: 5 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use packed_struct_codegen::PrimitiveEnum;

#[derive(PrimitiveEnum, Debug, Copy, Clone, PartialEq)]
#[allow(non_camel_case_types)]

Expand Down Expand Up @@ -70,6 +72,7 @@ pub enum MspCommandCode {
MSP_BATTERY_STATE = 130,

MSP_MOTOR_CONFIG = 131,
MSP_MOTOR_TELEMETRY = 139,

// OSD commands
MSP_OSD_VIDEO_CONFIG = 180,
Expand Down Expand Up @@ -105,6 +108,8 @@ pub enum MspCommandCode {
MSP_RC_DEADBAND = 125,
MSP_SENSOR_ALIGNMENT = 126,

MSP_ESC_SENSOR_DATA = 134,

MSP_SET_RAW_RC = 200,
MSP_SET_RAW_GPS = 201,
MSP_SET_PID = 202,
Expand Down
18 changes: 2 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,9 @@
//! Incomplete. Includes some structures from Cleanflight and Betaflight.

#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(not(feature = "std"), feature(alloc))]

#[cfg(not(feature = "std"))]
#[macro_use]
extern crate alloc;

extern crate packed_struct;

extern crate crc_any;

#[macro_use]
extern crate packed_struct_codegen;

#[macro_use]
extern crate serde_derive;

extern crate serde;
pub use packed_struct;
pub use serde;

mod prelude;

Expand Down
31 changes: 16 additions & 15 deletions src/packet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crc_any::CRCu8;
use prelude::v1::*;
use heapless::Vec;

use crate::prelude::v1::*;

/// Packet parsing error
#[derive(Copy, Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -41,7 +43,7 @@ impl MspPacketDirection {
pub struct MspPacket {
pub cmd: u16,
pub direction: MspPacketDirection,
pub data: Vec<u8>,
pub data: Vec<u8, 255>,
}

#[derive(Copy, Clone, PartialEq, Debug)]
Expand All @@ -65,15 +67,15 @@ enum MSPVersion {
V2,
}

#[derive(Debug)]
// #[derive(Debug)]
/// Parser that can find packets from a raw byte stream
pub struct MspParser {
state: MspParserState,
packet_version: MSPVersion,
packet_direction: MspPacketDirection,
packet_cmd: u16,
packet_data_length_remaining: usize,
packet_data: Vec<u8>,
packet_data: Vec<u8, 255>,
packet_crc: u8,
packet_crc_v2: CRCu8,
}
Expand Down Expand Up @@ -143,15 +145,15 @@ impl MspParser {
MspParserState::FlagV2 => {
// uint8, flag, usage to be defined (set to zero)
self.state = MspParserState::CommandV2;
self.packet_data = Vec::with_capacity(2);
self.packet_data.clear();
self.packet_crc_v2.digest(&[input]);
}

MspParserState::CommandV2 => {
self.packet_data.push(input);
self.packet_data.push(input).unwrap();

if self.packet_data.len() == 2 {
let mut s = [0u8; size_of::<u16>()];
let mut s = [0u8; core::mem::size_of::<u16>()];
s.copy_from_slice(&self.packet_data);
self.packet_cmd = u16::from_le_bytes(s);

Expand All @@ -162,15 +164,14 @@ impl MspParser {
}

MspParserState::DataLengthV2 => {
self.packet_data.push(input);
self.packet_data.push(input).unwrap();

if self.packet_data.len() == 2 {
let mut s = [0u8; size_of::<u16>()];
let mut s = [0u8; core::mem::size_of::<u16>()];
s.copy_from_slice(&self.packet_data);
self.packet_data_length_remaining = u16::from_le_bytes(s).into();
self.packet_crc_v2.digest(&self.packet_data);
self.packet_data =
Vec::with_capacity(self.packet_data_length_remaining as usize);
self.packet_data.clear();

if self.packet_data_length_remaining == 0 {
self.state = MspParserState::Crc;
Expand All @@ -181,7 +182,7 @@ impl MspParser {
}

MspParserState::DataV2 => {
self.packet_data.push(input);
self.packet_data.push(input).unwrap();
self.packet_data_length_remaining -= 1;

if self.packet_data_length_remaining == 0 {
Expand All @@ -193,7 +194,7 @@ impl MspParser {
self.packet_data_length_remaining = input as usize;
self.state = MspParserState::Command;
self.packet_crc ^= input;
self.packet_data = Vec::with_capacity(input as usize);
self.packet_data.clear();
}

MspParserState::Command => {
Expand All @@ -209,7 +210,7 @@ impl MspParser {
}

MspParserState::Data => {
self.packet_data.push(input);
self.packet_data.push(input).unwrap();
self.packet_data_length_remaining -= 1;

self.packet_crc ^= input;
Expand Down Expand Up @@ -263,7 +264,7 @@ impl MspParser {
}
}

impl Default for ::MspParser {
impl Default for MspParser {
fn default() -> Self {
Self::new()
}
Expand Down
19 changes: 0 additions & 19 deletions src/prelude/no_std.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
pub use core::cell::RefCell;
pub use core::cmp::*;
pub use core::fmt;
pub use core::fmt::Debug;
pub use core::fmt::Error as FmtError;
pub use core::fmt::Write as FmtWrite;
pub use core::intrinsics::write_bytes;
pub use core::iter;
pub use core::marker::PhantomData;
pub use core::mem;
pub use core::num::Wrapping;
pub use core::ops::Deref;
pub use core::ops::Range;

pub use alloc::arc::Arc;
pub use alloc::borrow::Cow;
pub use alloc::boxed::Box;
pub use alloc::fmt::format as format_to_string;
pub use alloc::fmt::{Display, Formatter};
pub use alloc::rc::Rc;
pub use alloc::str::{from_utf8, FromStr};
pub use alloc::string::*;
pub use alloc::vec::Vec;
20 changes: 0 additions & 20 deletions src/prelude/std.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
pub use std::borrow::Cow;
pub use std::cell::RefCell;
pub use std::cmp::{max, min};
pub use std::convert::TryFrom;
pub use std::fmt;
pub use std::fmt::format as format_to_string;
pub use std::fmt::Error as FmtError;
pub use std::fmt::Formatter;
pub use std::fmt::Write as FmtWrite;
pub use std::fmt::{Debug, Display};
pub use std::io;
pub use std::io::Write;
pub use std::iter;
pub use std::marker::PhantomData;
pub use std::mem;
pub use std::mem::size_of;
pub use std::num::Wrapping;
pub use std::ops::Deref;
pub use std::ops::Range;
pub use std::prelude::v1::*;
pub use std::ptr::write_bytes;
pub use std::rc::Rc;
pub use std::str::from_utf8;
pub use std::str::FromStr;
pub use std::sync::Arc;
64 changes: 57 additions & 7 deletions src/structs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! MSP structures
use heapless::Vec;
use packed_struct_codegen::{PackedStruct, PrimitiveEnum};
use serde::{Deserialize, Serialize};

use prelude::v1::*;
use crate::prelude::v1::*;

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
pub struct MspApiVersion {
Expand Down Expand Up @@ -42,7 +45,7 @@ pub struct MspUniqueId {
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(bytes = "1", endian = "lsb", bit_numbering = "msb0")]
#[packed_struct(bytes = "2", endian = "lsb", bit_numbering = "msb0")]
pub struct MspAvailableSensors {
#[packed_field(bits = "2")]
pub sonar: bool,
Expand All @@ -61,12 +64,16 @@ pub struct MspAvailableSensors {
pub struct MspStatus {
pub cycle_time: u16,
pub i2c_errors: u16,
#[packed_field(size_bits = "8")]
pub sensors: MspAvailableSensors,
pub null1: u8,
pub sensors: u16,
pub flight_mode: u32,
pub profile: u8,
pub system_load: u16,
pub gyro_cycle_time: u16,
pub flight_mode_flag_bits: u8,
pub flight_mode_flags: u16,
pub arming_disable_flags_count: u8,
pub arming_disable_flags: u32,
pub config_state_flags: u8,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
Expand Down Expand Up @@ -190,6 +197,47 @@ pub struct MspAltitude {
pub vario: i16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MotorStatus {
/// temperature
pub temperature: u8,
/// rpm
pub rpm: u16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MotorTelemetry {
/// rpm
pub rpm: u32,
/// invalid pct
pub invalid_pct: u16,
pub temperature: u8,
pub voltage: u16,
pub current: u16,
pub cinsuption: u16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspMotorTelemetry {
/// Motor count
pub motor_count: u8,
/// motors [temp:u8, rpm: u16]
#[packed_field(element_size_bits = "104")]
pub motor_sensor_data: [MotorTelemetry; 4],
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspEscSensorData {
/// Motor count
pub motor_count: u8,
/// motors [temp:u8, rpm: u16]
#[packed_field(element_size_bits = "24")]
pub motor_sensor_data: [MotorStatus; 4],
}
#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspBatteryConfig {
Expand All @@ -204,11 +252,12 @@ pub struct MspBatteryConfig {
#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspAnalog {
pub battery_voltage: u8,
pub leagcy_battery_voltage: u8,
pub mah_drawn: u16,
pub rssi: u16,
/// Current in 0.01A steps, range is -320A to 320A
pub amperage: i16,
pub battery_voltage: u16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
Expand Down Expand Up @@ -554,7 +603,7 @@ pub struct MspSetOsdLayoutItem {
pub struct MspOsdSettings {
pub osd_support: u8,
pub config: MspOsdConfig,
pub item_positions: Vec<MspOsdItemPosition>,
pub item_positions: Vec<MspOsdItemPosition, 32>,
}

#[derive(PackedStruct, Debug, Copy, Clone)]
Expand Down Expand Up @@ -654,6 +703,7 @@ impl TryFrom<&str> for Baudrate {
}
}

#[cfg(feature = "std")]
impl From<Baudrate> for String {
fn from(value: Baudrate) -> Self {
match value {
Expand Down