Skip to content

itm: derive serde for LocalTimestampOptions, impl gated TryFrom<u8> #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
22 changes: 22 additions & 0 deletions src/peripheral/itm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -107,6 +111,24 @@ pub enum LocalTimestampOptions {
EnabledDiv64,
}

#[cfg(feature = "std")]
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 {
Expand Down
4 changes: 2 additions & 2 deletions src/peripheral/scb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion xtask/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
19 changes: 18 additions & 1 deletion xtask/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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(&lts).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
Expand All @@ -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());
}
}