Skip to content

Commit bcb76ef

Browse files
committed
itm: derive serde for LocalTimestampOptions, impl gated TryFrom<u8>
1 parent ae1d2a6 commit bcb76ef

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/peripheral/itm.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ use volatile_register::{RO, RW, WO};
1010
use crate::peripheral::ITM;
1111
use bitfield::bitfield;
1212

13+
#[cfg(feature = "serde")]
14+
use serde::{Deserialize, Serialize};
15+
1316
/// Register block
1417
#[repr(C)]
1518
pub struct RegisterBlock {
@@ -91,6 +94,7 @@ impl Stim {
9194

9295
/// The possible local timestamp options.
9396
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
97+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
9498
pub enum LocalTimestampOptions {
9599
/// Disable local timestamps.
96100
Disabled,
@@ -107,6 +111,24 @@ pub enum LocalTimestampOptions {
107111
EnabledDiv64,
108112
}
109113

114+
#[cfg(feature = "std-map")]
115+
impl core::convert::TryFrom<u8> for LocalTimestampOptions {
116+
type Error = ();
117+
118+
/// Converts an integer value to an enabled [LocalTimestampOptions]
119+
/// variant. Accepted values are: 1, 4, 16, 64. Any other value
120+
/// yields `Err(())`.
121+
fn try_from(value: u8) -> Result<Self, Self::Error> {
122+
match value {
123+
1 => Ok(Self::Enabled),
124+
4 => Ok(Self::EnabledDiv4),
125+
16 => Ok(Self::EnabledDiv16),
126+
64 => Ok(Self::EnabledDiv64),
127+
_ => Err(()),
128+
}
129+
}
130+
}
131+
110132
/// The possible global timestamp options.
111133
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
112134
pub enum GlobalTimestampOptions {

xtask/src/lib.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ pub fn check_blobs() {
211211

212212
// Check that serde and PartialOrd works with VectActive
213213
pub fn check_host_side() {
214-
use cortex_m::peripheral::scb::VectActive;
214+
use cortex_m::peripheral::{itm::LocalTimestampOptions, scb::VectActive};
215215

216216
// check serde
217217
{
@@ -220,6 +220,12 @@ pub fn check_host_side() {
220220
let deser_v: VectActive =
221221
serde_json::from_str(&json).expect("Failed to deserialize VectActive");
222222
assert_eq!(deser_v, v);
223+
224+
let lts = LocalTimestampOptions::EnabledDiv4;
225+
let json = serde_json::to_string(&lts).expect("Failed to serialize LocalTimestampOptions");
226+
let deser_lts: LocalTimestampOptions =
227+
serde_json::from_str(&json).expect("Failed to deserilaize LocalTimestampOptions");
228+
assert_eq!(deser_lts, lts);
223229
}
224230

225231
// check PartialOrd
@@ -228,4 +234,15 @@ pub fn check_host_side() {
228234
let b = VectActive::from(20).unwrap();
229235
assert_eq!(a < b, true);
230236
}
237+
238+
// check TryFrom
239+
{
240+
use core::convert::TryInto;
241+
use std::convert::TryFrom;
242+
243+
let lts: LocalTimestampOptions = (16 as u8).try_into().unwrap();
244+
assert_eq!(lts, LocalTimestampOptions::EnabledDiv16);
245+
246+
assert!(LocalTimestampOptions::try_from(42).is_err());
247+
}
231248
}

0 commit comments

Comments
 (0)