1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
mod decoder;
mod publisher;
pub use decoder::{Decoder, ExceptionAction, ExceptionType, TracePacket};
pub use publisher::{SwoPublisher, UpdaterChannel};
use crate::Error;
#[derive(Debug, Copy, Clone)]
pub enum SwoMode {
UART,
Manchester,
}
#[derive(Debug, Copy, Clone)]
pub struct SwoConfig {
mode: SwoMode,
baud: u32,
tpiu_clk: u32,
tpiu_continuous_formatting: bool,
}
impl SwoConfig {
pub fn new(tpiu_clk: u32) -> Self {
SwoConfig {
mode: SwoMode::UART,
baud: 1_000_000,
tpiu_clk,
tpiu_continuous_formatting: false,
}
}
pub fn set_baud(mut self, baud: u32) -> Self {
self.baud = baud;
self
}
pub fn set_mode(mut self, mode: SwoMode) -> Self {
self.mode = mode;
self
}
pub fn set_mode_uart(mut self) -> Self {
self.mode = SwoMode::UART;
self
}
pub fn set_mode_manchester(mut self) -> Self {
self.mode = SwoMode::Manchester;
self
}
pub fn set_continuous_formatting(mut self, enabled: bool) -> Self {
self.tpiu_continuous_formatting = enabled;
self
}
pub fn mode(&self) -> SwoMode {
self.mode
}
pub fn baud(&self) -> u32 {
self.baud
}
pub fn tpiu_clk(&self) -> u32 {
self.tpiu_clk
}
pub fn tpiu_continuous_formatting(&self) -> bool {
self.tpiu_continuous_formatting
}
}
pub trait SwoAccess {
fn enable_swo(&mut self, config: &SwoConfig) -> Result<(), Error>;
fn disable_swo(&mut self) -> Result<(), Error>;
fn read_swo(&mut self) -> Result<Vec<u8>, Error> {
self.read_swo_timeout(std::time::Duration::from_millis(10))
}
fn read_swo_timeout(&mut self, timeout: std::time::Duration) -> Result<Vec<u8>, Error>;
fn swo_poll_interval_hint(&mut self, config: &SwoConfig) -> Option<std::time::Duration> {
match self.swo_buffer_size() {
Some(size) => poll_interval_from_buf_size(config, size),
None => None,
}
}
fn swo_buffer_size(&mut self) -> Option<usize> {
None
}
}
pub(crate) fn poll_interval_from_buf_size(
config: &SwoConfig,
buf_size: usize,
) -> Option<std::time::Duration> {
let time_to_full_ms = match config.mode() {
SwoMode::UART => (1000 * buf_size as u32) / (config.baud() / 10),
SwoMode::Manchester => (500 * buf_size as u32) / (config.baud() / 8),
};
Some(std::time::Duration::from_millis(time_to_full_ms as u64 / 4))
}