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))
}