Skip to content

Commit cde498d

Browse files
bors[bot]tmplt
andauthored
Merge #366
366: itm: derive serde for `LocalTimestampOptions`, impl gated `TryFrom<u8>` r=adamgreig a=tmplt This PR is an upstream push of more std-features required by `cargo-rtic-scope`. If required, the `TryFrom<u8>` impl can be kept downstream. Co-authored-by: Viktor Sonesten <[email protected]>
2 parents 42065a6 + 3b44533 commit cde498d

File tree

5 files changed

+44
-5
lines changed

5 files changed

+44
-5
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ cm7 = []
3131
cm7-r0p1 = ["cm7"]
3232
inline-asm = []
3333
linker-plugin-lto = []
34-
std-map = []
34+
std = []
3535

3636
[workspace]
3737
members = ["xtask", "cortex-m-semihosting", "panic-semihosting", "panic-itm"]

src/peripheral/itm.rs

+22
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")]
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 {

src/peripheral/scb.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl SCB {
197197
/// Processor core exceptions (internal interrupts)
198198
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
199199
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
200-
#[cfg_attr(feature = "std-map", derive(PartialOrd, Hash))]
200+
#[cfg_attr(feature = "std", derive(PartialOrd, Hash))]
201201
pub enum Exception {
202202
/// Non maskable interrupt
203203
NonMaskableInt,
@@ -264,7 +264,7 @@ impl Exception {
264264
/// Active exception number
265265
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
266266
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
267-
#[cfg_attr(feature = "std-map", derive(PartialOrd, Hash))]
267+
#[cfg_attr(feature = "std", derive(PartialOrd, Hash))]
268268
pub enum VectActive {
269269
/// Thread mode
270270
ThreadMode,

xtask/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ harness = false
1111

1212
[dependencies]
1313
ar = "0.8.0"
14-
cortex-m = { path = "../", features = ["serde", "std-map"] }
14+
cortex-m = { path = "../", features = ["serde", "std"] }
1515
serde_json = "1"

xtask/src/lib.rs

+18-1
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)