Skip to content

Commit 5a3755f

Browse files
committed
tpiu: use bitfield for SPPR
1 parent c37f80b commit 5a3755f

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

src/peripheral/tpiu.rs

+41-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct RegisterBlock {
1919
pub acpr: RW<u32>,
2020
reserved1: [u32; 55],
2121
/// Selected Pin Control
22-
pub sppr: RW<u32>,
22+
pub sppr: RW<Sppr>,
2323
reserved2: [u32; 132],
2424
/// Formatter and Flush Control
2525
pub ffcr: RW<Ffcr>,
@@ -52,7 +52,16 @@ bitfield! {
5252
nrzvalid, _: 11;
5353
}
5454

55+
bitfield! {
56+
/// Selected pin protocol register.
57+
#[repr(C)]
58+
#[derive(Clone, Copy)]
59+
pub struct Sppr(u32);
60+
u8, txmode, set_txmode: 1, 0;
61+
}
62+
5563
/// The available protocols for the trace output.
64+
#[repr(u8)]
5665
pub enum TraceProtocol {
5766
/// Parallel trace port mode
5867
Parallel = 0b00,
@@ -61,6 +70,21 @@ pub enum TraceProtocol {
6170
/// Asynchronous SWO, using NRZ encoding
6271
AsyncSWONRZ = 0b10,
6372
}
73+
impl core::convert::TryFrom<u8> for TraceProtocol {
74+
type Error = ();
75+
76+
/// Tries to convert from a `TXMODE` field value. Fails if the set mode is
77+
/// unknown (and thus unpredictable).
78+
#[inline]
79+
fn try_from(value: u8) -> Result<Self, Self::Error> {
80+
match value {
81+
0b00 => Ok(Self::Parallel),
82+
0b01 => Ok(Self::AsyncSWOManchester),
83+
0b10 => Ok(Self::AsyncSWONRZ),
84+
_ => Err(()), // unknown and unpredictable mode
85+
}
86+
}
87+
}
6488

6589
/// The SWO options supported by the TPIU.
6690
#[allow(dead_code)]
@@ -86,10 +110,25 @@ impl TPIU {
86110
}
87111
}
88112

113+
/// The used protocol for the trace output. Return `None` if an
114+
/// unknown (and thus unpredicable mode) is configured by means
115+
/// other than
116+
/// [`trace_output_protocol`](Self::set_trace_output_protocol).
117+
#[inline]
118+
pub fn trace_output_protocol(&self) -> Option<TraceProtocol> {
119+
use core::convert::TryInto;
120+
self.sppr.read().txmode().try_into().ok()
121+
}
122+
89123
/// Sets the used protocol for the trace output.
90124
#[inline]
91125
pub fn set_trace_output_protocol(&mut self, proto: TraceProtocol) {
92-
unsafe { self.sppr.write(proto as u32) }
126+
unsafe {
127+
self.sppr.modify(|mut r| {
128+
r.set_txmode(proto as u8);
129+
r
130+
});
131+
}
93132
}
94133

95134
/// Whether to enable the formatter. If disabled, only ITM and DWT

0 commit comments

Comments
 (0)