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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//! BUSCTRL (Bus Control) HAL for RP2350
//!
//! This module provides access to the bus fabric control signals
//! and performance counters for monitoring bus activity.
//!
//! ## Features
//!
//! - Bus arbitration priority control
//! - 4 performance counters for bus activity monitoring
//! - Configurable event selection for each counter
//!
//! ## Example
//!
//! ```ignore
//! use rp2350::busctrl::{Busctrl, PerfEvent};
//!
//! let busctrl = Busctrl::init(dp.BUSCTRL);
//!
//! // Enable performance counters
//! busctrl.enable_perf_counters(true);
//!
//! // Configure counter 0 to count PROC0 access events
//! busctrl.set_perfsel0(PerfEvent::SiobProc0Access);
//!
//! // Read counter value
//! let count = busctrl.perfctr0();
//! ```
use crate::Steal;
use pastey::paste;
use rp2350_pac::BUSCTRL;
/// Performance counter event selection.
///
/// Each downstream port of the main crossbar exposes four events:
/// `StallUpstream`, `StallDownstream`, `AccessContested` and `Access`. All 17
/// ports (SIOB PROC0/1, APB, fast peripherals, SRAM0–9, XIP main0/1 and ROM)
/// are represented.
macro_rules! impl_perf_events {
($(($port:ident, $base:literal)),+ $(,)?) => {
paste! {
#[doc = "Performance counter event selection."]
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u32)]
pub enum PerfEvent {
$(
#[doc = concat!(stringify!($port), " stall upstream")]
[<$port StallUpstream>] = $base,
#[doc = concat!(stringify!($port), " stall downstream")]
[<$port StallDownstream>] = $base + 1,
#[doc = concat!(stringify!($port), " access contested")]
[<$port AccessContested>] = $base + 2,
#[doc = concat!(stringify!($port), " access")]
[<$port Access>] = $base + 3,
)+
}
}
};
}
impl_perf_events!(
(SiobProc1, 0),
(SiobProc0, 4),
(Apb, 8),
(Fastperi, 12),
(Sram9, 16),
(Sram8, 20),
(Sram7, 24),
(Sram6, 28),
(Sram5, 32),
(Sram4, 36),
(Sram3, 40),
(Sram2, 44),
(Sram1, 48),
(Sram0, 52),
(XipMain1, 56),
(XipMain0, 60),
(Rom, 64),
);
/// BUSCTRL peripheral wrapper
///
/// Provides access to bus fabric control and performance counters.
pub struct Busctrl {
regs: rp2350_pac::BUSCTRL,
}
impl Busctrl {
/// Initialize the BUSCTRL driver.
///
/// Takes ownership of the BUSCTRL peripheral.
#[inline]
pub fn init(regs: BUSCTRL) -> Self {
Self { regs }
}
/// Obtain a handle to the BUSCTRL hardware peripheral.
///
/// # Safety
///
/// The caller must ensure the peripheral has been initialised
/// and that no data races occur.
#[inline]
pub unsafe fn steal() -> Steal<Self> {
Steal(Self {
regs: unsafe { BUSCTRL::steal() },
})
}
/// Get reference to the register block
#[inline]
pub fn regs(&self) -> &rp2350_pac::busctrl::RegisterBlock {
&self.regs
}
// ========================================================================
// Bus Priority
// ========================================================================
// Per-master priority accessors are generated by `impl_bus_priority!` below.
/// Check if bus priority has been acknowledged by all arbiters
#[inline]
pub fn is_priority_ack(&self) -> bool {
self.regs.bus_priority_ack.bus_priority_ack()
}
// ========================================================================
// Performance Counters
// ========================================================================
/// Enable or disable all performance counters
#[inline]
pub fn enable_perf_counters(&self, enable: bool) {
if enable {
self.regs.perfctr_en.set_perfctr_en();
} else {
self.regs.perfctr_en.clr_perfctr_en();
}
}
/// Check if performance counters are enabled
#[inline]
pub fn is_perf_counters_enabled(&self) -> bool {
self.regs.perfctr_en.perfctr_en()
}
// Per-counter accessors are generated by `impl_perf_counters!` below.
// ======== Utility Functions ========
/// Clear all performance counters
#[inline]
pub fn clear_all_perfctrs(&self) {
self.regs.perfctr0.write_perfctr0(0);
self.regs.perfctr1.write_perfctr1(0);
self.regs.perfctr2.write_perfctr2(0);
self.regs.perfctr3.write_perfctr3(0);
}
/// Get all performance counter values as an array
#[inline]
pub fn all_perfctrs(&self) -> [u32; 4] {
[
self.perfctr0(),
self.perfctr1(),
self.perfctr2(),
self.perfctr3(),
]
}
}
// ============================================================================
// Generated accessors
// ============================================================================
macro_rules! impl_bus_priority {
($(($name:ident, $set:ident, $clr:ident, $get:ident)),+ $(,)?) => {
paste! {
$(
impl Busctrl {
#[doc = concat!("Set ", stringify!($name), " to high priority.")]
#[inline]
pub fn [<set_ $name _high_priority>](&self, high: bool) {
if high {
self.regs.bus_priority.$set();
} else {
self.regs.bus_priority.$clr();
}
}
#[doc = concat!("Check if ", stringify!($name), " has high priority.")]
#[inline]
pub fn [<is_ $name _high_priority>](&self) -> bool {
self.regs.bus_priority.$get()
}
}
)+
}
};
}
impl_bus_priority!(
(proc0, set_proc0, clr_proc0, proc0),
(proc1, set_proc1, clr_proc1, proc1),
(dma_r, set_dma_r, clr_dma_r, dma_r),
(dma_w, set_dma_w, clr_dma_w, dma_w),
);
macro_rules! impl_perf_counters {
($($n:literal),+ $(,)?) => {
paste! {
$(
impl Busctrl {
#[doc = concat!("Get performance counter ", $n, " value.")]
#[inline]
pub fn [<perfctr $n>](&self) -> u32 {
self.regs.[<perfctr $n>].[<perfctr $n>]()
}
#[doc = concat!("Clear performance counter ", $n, ".")]
#[inline]
pub fn [<clear_perfctr $n>](&self) {
self.regs.[<perfctr $n>].[<write_perfctr $n>](0);
}
#[doc = concat!("Set performance counter ", $n, " event selection.")]
#[inline]
pub fn [<set_perfsel $n>](&self, event: PerfEvent) {
self.regs.[<perfsel $n>].[<write_perfsel $n>](event as u32);
}
#[doc = concat!("Get performance counter ", $n, " event selection.")]
#[inline]
pub fn [<perfsel $n>](&self) -> u32 {
self.regs.[<perfsel $n>].[<perfsel $n>]()
}
}
)+
}
};
}
impl_perf_counters!(0, 1, 2, 3);