From bcb76ef24cda4b4b06830d7e63b55cf64b3148da Mon Sep 17 00:00:00 2001 From: Viktor Sonesten Date: Mon, 6 Dec 2021 17:23:14 +0100 Subject: [PATCH 1/2] itm: derive serde for LocalTimestampOptions, impl gated TryFrom --- src/peripheral/itm.rs | 22 ++++++++++++++++++++++ xtask/src/lib.rs | 19 ++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/peripheral/itm.rs b/src/peripheral/itm.rs index 4d0aa220..f1555400 100644 --- a/src/peripheral/itm.rs +++ b/src/peripheral/itm.rs @@ -10,6 +10,9 @@ use volatile_register::{RO, RW, WO}; use crate::peripheral::ITM; use bitfield::bitfield; +#[cfg(feature = "serde")] +use serde::{Deserialize, Serialize}; + /// Register block #[repr(C)] pub struct RegisterBlock { @@ -91,6 +94,7 @@ impl Stim { /// The possible local timestamp options. #[derive(Debug, Eq, PartialEq, Copy, Clone)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub enum LocalTimestampOptions { /// Disable local timestamps. Disabled, @@ -107,6 +111,24 @@ pub enum LocalTimestampOptions { EnabledDiv64, } +#[cfg(feature = "std-map")] +impl core::convert::TryFrom for LocalTimestampOptions { + type Error = (); + + /// Converts an integer value to an enabled [LocalTimestampOptions] + /// variant. Accepted values are: 1, 4, 16, 64. Any other value + /// yields `Err(())`. + fn try_from(value: u8) -> Result { + match value { + 1 => Ok(Self::Enabled), + 4 => Ok(Self::EnabledDiv4), + 16 => Ok(Self::EnabledDiv16), + 64 => Ok(Self::EnabledDiv64), + _ => Err(()), + } + } +} + /// The possible global timestamp options. #[derive(Debug, Eq, PartialEq, Copy, Clone)] pub enum GlobalTimestampOptions { diff --git a/xtask/src/lib.rs b/xtask/src/lib.rs index c3d83560..ddbb88bd 100644 --- a/xtask/src/lib.rs +++ b/xtask/src/lib.rs @@ -211,7 +211,7 @@ pub fn check_blobs() { // Check that serde and PartialOrd works with VectActive pub fn check_host_side() { - use cortex_m::peripheral::scb::VectActive; + use cortex_m::peripheral::{itm::LocalTimestampOptions, scb::VectActive}; // check serde { @@ -220,6 +220,12 @@ pub fn check_host_side() { let deser_v: VectActive = serde_json::from_str(&json).expect("Failed to deserialize VectActive"); assert_eq!(deser_v, v); + + let lts = LocalTimestampOptions::EnabledDiv4; + let json = serde_json::to_string(<s).expect("Failed to serialize LocalTimestampOptions"); + let deser_lts: LocalTimestampOptions = + serde_json::from_str(&json).expect("Failed to deserilaize LocalTimestampOptions"); + assert_eq!(deser_lts, lts); } // check PartialOrd @@ -228,4 +234,15 @@ pub fn check_host_side() { let b = VectActive::from(20).unwrap(); assert_eq!(a < b, true); } + + // check TryFrom + { + use core::convert::TryInto; + use std::convert::TryFrom; + + let lts: LocalTimestampOptions = (16 as u8).try_into().unwrap(); + assert_eq!(lts, LocalTimestampOptions::EnabledDiv16); + + assert!(LocalTimestampOptions::try_from(42).is_err()); + } } From 3b44533324499d355445b502dfff987da8546f1c Mon Sep 17 00:00:00 2001 From: Viktor Sonesten Date: Tue, 7 Dec 2021 12:53:40 +0100 Subject: [PATCH 2/2] feature gates: std-map -> std The alternative is to create another std-* gate for each std-related functionality. I do not think this will scale well, and makes the crate difficult to use. If the end-user wants to use host-side functionality they will only need to specify "std". --- Cargo.toml | 2 +- src/peripheral/itm.rs | 2 +- src/peripheral/scb.rs | 4 ++-- xtask/Cargo.toml | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4769aba3..35db73c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,7 +31,7 @@ cm7 = [] cm7-r0p1 = ["cm7"] inline-asm = [] linker-plugin-lto = [] -std-map = [] +std = [] [workspace] members = ["xtask", "cortex-m-semihosting", "panic-semihosting", "panic-itm"] diff --git a/src/peripheral/itm.rs b/src/peripheral/itm.rs index f1555400..f8e9e25d 100644 --- a/src/peripheral/itm.rs +++ b/src/peripheral/itm.rs @@ -111,7 +111,7 @@ pub enum LocalTimestampOptions { EnabledDiv64, } -#[cfg(feature = "std-map")] +#[cfg(feature = "std")] impl core::convert::TryFrom for LocalTimestampOptions { type Error = (); diff --git a/src/peripheral/scb.rs b/src/peripheral/scb.rs index 28cfca86..6c16149b 100644 --- a/src/peripheral/scb.rs +++ b/src/peripheral/scb.rs @@ -197,7 +197,7 @@ impl SCB { /// Processor core exceptions (internal interrupts) #[derive(Clone, Copy, Debug, Eq, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "std-map", derive(PartialOrd, Hash))] +#[cfg_attr(feature = "std", derive(PartialOrd, Hash))] pub enum Exception { /// Non maskable interrupt NonMaskableInt, @@ -264,7 +264,7 @@ impl Exception { /// Active exception number #[derive(Clone, Copy, Debug, Eq, PartialEq)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -#[cfg_attr(feature = "std-map", derive(PartialOrd, Hash))] +#[cfg_attr(feature = "std", derive(PartialOrd, Hash))] pub enum VectActive { /// Thread mode ThreadMode, diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 8742f9b1..b5b5c5f4 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -11,5 +11,5 @@ harness = false [dependencies] ar = "0.8.0" -cortex-m = { path = "../", features = ["serde", "std-map"] } +cortex-m = { path = "../", features = ["serde", "std"] } serde_json = "1"