From bcb76ef24cda4b4b06830d7e63b55cf64b3148da Mon Sep 17 00:00:00 2001 From: Viktor Sonesten Date: Mon, 6 Dec 2021 17:23:14 +0100 Subject: itm: derive serde for LocalTimestampOptions, impl gated TryFrom --- src/peripheral/itm.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'src/peripheral/itm.rs') 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 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 { -- cgit v1.2.3