From f27c6f97eb95fdbc9a5b0d743e7295f94e0c5ba8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D0=BE=D0=BC=D0=B0=D0=BD=20=D0=9A=D1=80=D0=B8=D0=B2?= =?UTF-8?q?=D0=B5=D0=BD=D0=BA=D0=BE=D0=B2?= Date: Tue, 2 Jul 2024 11:12:54 +0400 Subject: [PATCH] Remove alloc for no_std feature. Update dependencies. Update to 2021 edition. --- Cargo.toml | 20 ++++++++------ src/commands.rs | 5 ++++ src/lib.rs | 18 ++---------- src/packet.rs | 31 +++++++++++---------- src/prelude/no_std.rs | 19 ------------- src/prelude/std.rs | 20 -------------- src/structs.rs | 64 ++++++++++++++++++++++++++++++++++++++----- 7 files changed, 92 insertions(+), 85 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1cf47c2..f688fc1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 ", "Ilya Guterman "] +authors = [ + "Rudi Benkovic ", + "Ilya Guterman ", +] 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 = [] \ No newline at end of file +suppport_int32_setting_type = [] diff --git a/src/commands.rs b/src/commands.rs index 9f075a3..4c7a7da 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,3 +1,5 @@ +use packed_struct_codegen::PrimitiveEnum; + #[derive(PrimitiveEnum, Debug, Copy, Clone, PartialEq)] #[allow(non_camel_case_types)] @@ -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, @@ -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, diff --git a/src/lib.rs b/src/lib.rs index 5c3cc41..31314d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/packet.rs b/src/packet.rs index 4afca15..924bbef 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -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)] @@ -41,7 +43,7 @@ impl MspPacketDirection { pub struct MspPacket { pub cmd: u16, pub direction: MspPacketDirection, - pub data: Vec, + pub data: Vec, } #[derive(Copy, Clone, PartialEq, Debug)] @@ -65,7 +67,7 @@ enum MSPVersion { V2, } -#[derive(Debug)] +// #[derive(Debug)] /// Parser that can find packets from a raw byte stream pub struct MspParser { state: MspParserState, @@ -73,7 +75,7 @@ pub struct MspParser { packet_direction: MspPacketDirection, packet_cmd: u16, packet_data_length_remaining: usize, - packet_data: Vec, + packet_data: Vec, packet_crc: u8, packet_crc_v2: CRCu8, } @@ -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::()]; + let mut s = [0u8; core::mem::size_of::()]; s.copy_from_slice(&self.packet_data); self.packet_cmd = u16::from_le_bytes(s); @@ -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::()]; + let mut s = [0u8; core::mem::size_of::()]; 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; @@ -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 { @@ -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 => { @@ -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; @@ -263,7 +264,7 @@ impl MspParser { } } -impl Default for ::MspParser { +impl Default for MspParser { fn default() -> Self { Self::new() } diff --git a/src/prelude/no_std.rs b/src/prelude/no_std.rs index c60eab1..23e6f03 100644 --- a/src/prelude/no_std.rs +++ b/src/prelude/no_std.rs @@ -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; diff --git a/src/prelude/std.rs b/src/prelude/std.rs index d0e84e0..e868bbe 100644 --- a/src/prelude/std.rs +++ b/src/prelude/std.rs @@ -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; diff --git a/src/structs.rs b/src/structs.rs index ffb8d11..d234bac 100644 --- a/src/structs.rs +++ b/src/structs.rs @@ -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 { @@ -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, @@ -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)] @@ -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 { @@ -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)] @@ -554,7 +603,7 @@ pub struct MspSetOsdLayoutItem { pub struct MspOsdSettings { pub osd_support: u8, pub config: MspOsdConfig, - pub item_positions: Vec, + pub item_positions: Vec, } #[derive(PackedStruct, Debug, Copy, Clone)] @@ -654,6 +703,7 @@ impl TryFrom<&str> for Baudrate { } } +#[cfg(feature = "std")] impl From for String { fn from(value: Baudrate) -> Self { match value {