diff options
author | 2021-12-06 17:23:14 +0100 | |
---|---|---|
committer | 2021-12-07 13:07:16 +0100 | |
commit | bcb76ef24cda4b4b06830d7e63b55cf64b3148da (patch) | |
tree | 5733fd26d3afe858e3eeb7d9714592070fa6022d | |
parent | ae1d2a62d895dc458784c465e021a010cb75a8b1 (diff) | |
download | cortex-m-bcb76ef24cda4b4b06830d7e63b55cf64b3148da.tar.gz cortex-m-bcb76ef24cda4b4b06830d7e63b55cf64b3148da.tar.zst cortex-m-bcb76ef24cda4b4b06830d7e63b55cf64b3148da.zip |
itm: derive serde for LocalTimestampOptions, impl gated TryFrom<u8>
-rw-r--r-- | src/peripheral/itm.rs | 22 | ||||
-rw-r--r-- | xtask/src/lib.rs | 19 |
2 files changed, 40 insertions, 1 deletions
diff --git a/src/peripheral/itm.rs b/src/peripheral/itm.rs index 4d0aa22..f155540 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<u8> 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<Self, Self::Error> { + 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 c3d8356..ddbb88b 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()); + } } |