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
use std::sync::Arc;
use super::{ArmDebugSequence, ArmDebugSequenceError};
use crate::architecture::arm::{
component::TraceSink, memory::CoresightComponent, ArmError, ArmProbeInterface,
};
#[derive(thiserror::Error, Debug)]
pub enum ComponentError {
#[error("Nordic does not support TPIU CLK value of {0}")]
NordicUnsupportedTPUICLKValue(u32),
#[error("nRF52 devices do not have a trace buffer")]
NordicNoTraceMem,
}
pub struct Nrf52 {}
impl Nrf52 {
pub fn create() -> Arc<Self> {
Arc::new(Self {})
}
}
mod clock {
use crate::architecture::arm::{memory::adi_v5_memory_interface::ArmProbe, ArmError};
use bitfield::bitfield;
const CLOCK: u64 = 0x4000_0000;
bitfield! {
pub struct TraceConfig(u32);
impl Debug;
pub u8, traceportspeed, set_traceportspeed: 1, 0;
pub u8, tracemux, set_tracemux: 17, 16;
}
impl TraceConfig {
const ADDRESS: u64 = 0x55C;
pub fn read(memory: &mut dyn ArmProbe) -> Result<Self, ArmError> {
let contents = memory.read_word_32(CLOCK + Self::ADDRESS)?;
Ok(Self(contents))
}
pub fn write(&mut self, memory: &mut dyn ArmProbe) -> Result<(), ArmError> {
memory.write_word_32(CLOCK + Self::ADDRESS, self.0)
}
}
}
impl ArmDebugSequence for Nrf52 {
fn trace_start(
&self,
interface: &mut dyn ArmProbeInterface,
components: &[CoresightComponent],
sink: &TraceSink,
) -> Result<(), ArmError> {
let tpiu_clock = match sink {
TraceSink::TraceMemory => {
tracing::error!("nRF52 does not have a trace buffer");
return Err(ArmError::from(ComponentError::NordicNoTraceMem));
}
TraceSink::Tpiu(config) => config.tpiu_clk(),
TraceSink::Swo(config) => config.tpiu_clk(),
};
let portspeed = match tpiu_clock {
4_000_000 => 3,
8_000_000 => 2,
16_000_000 => 1,
32_000_000 => 0,
tpiu_clk => {
let e = ComponentError::NordicUnsupportedTPUICLKValue(tpiu_clk);
tracing::error!("{:?}", e);
return Err(ArmError::from(e));
}
};
let mut memory = interface.memory_interface(components[0].ap)?;
let mut config = clock::TraceConfig::read(&mut *memory)?;
config.set_traceportspeed(portspeed);
if matches!(sink, TraceSink::Tpiu(_)) {
config.set_tracemux(2);
} else {
config.set_tracemux(1);
}
config.write(&mut *memory)?;
Ok(())
}
}
impl From<ComponentError> for ArmError {
fn from(value: ComponentError) -> ArmError {
ArmError::DebugSequence(ArmDebugSequenceError::custom(value))
}
}