forked from tock/tock
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdebug_writer.rs
More file actions
272 lines (245 loc) · 9.67 KB
/
Copy pathdebug_writer.rs
File metadata and controls
272 lines (245 loc) · 9.67 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.
//! Component for DebugWriter, the implementation for `debug!()`.
//!
//! This provides components for attaching the kernel debug output (for panic!,
//! print!, debug!, etc.) to the output. `DebugWriterComponent` uses a UART mux,
//! and `DebugWriterNoMuxComponent` just uses a UART interface directly.
//!
//! Usage
//! -----
//! ```rust
//! let debug_wrapper = components::debug_writer::DebugWriterComponent::new(
//! uart_mux,
//! create_capability!(kernel::capabilities::SetDebugWriterCapability),
//! )
//! .finalize(components::debug_writer_component_static!());
//!
//! let debug_wrapper = components::debug_writer::DebugWriterNoMuxComponent::new(
//! &nrf52::uart::UARTE0,
//! create_capability!(kernel::capabilities::SetDebugWriterCapability),
//! )
//! .finalize(components::debug_writer_no_mux_component_static!());
//! ```
// Author: Brad Campbell <bradjc@virginia.edu>
// Last modified: 11/07/2019
use capsules_core::virtualizers::selection_policy::SelectionPolicy;
use capsules_core::virtualizers::virtual_uart::{MuxUart, UartDevice};
use capsules_system::debug_writer::uart_debug_writer::UartDebugWriter;
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::capabilities::SetDebugWriterCapability;
use kernel::collections::ring_buffer::RingBuffer;
use kernel::component::Component;
use kernel::hil;
use kernel::hil::uart;
// The sum of the output_buf and internal_buf is set to a multiple of 1024 bytes in order to avoid excessive
// padding between kernel memory and application memory (which often needs to be aligned to at
// least a 1 KiB boundary). This is not _semantically_ critical, but helps keep buffers on 1 KiB
// boundaries in some cases. Of course, these definitions are only advisory, and individual boards
// can choose to pass in their own buffers with different lengths.
pub const DEFAULT_DEBUG_BUFFER_KBYTE: usize = 2;
// Bytes [0, DEBUG_BUFFER_SPLIT) are used for output_buf while bytes
// [DEBUG_BUFFER_SPLIT, DEFAULT_DEBUG_BUFFER_KBYTE * 1024) are used for internal_buf.
const DEBUG_BUFFER_SPLIT: usize = 64;
/// The optional argument to this macro allows boards to specify the size of the in-RAM
/// buffer used for storing debug messages.
///
/// Increase this value to be able to send more debug messages in
/// quick succession.
#[macro_export]
macro_rules! debug_writer_component_static {
($BUF_SIZE_KB:expr, $P: ty) => {{
let uart = kernel::static_buf!(capsules_core::virtualizers::virtual_uart::UartDevice<$P>);
let ring = kernel::static_buf!(kernel::collections::ring_buffer::RingBuffer<'static, u8>);
let buffer = kernel::static_buf!([u8; 1024 * $BUF_SIZE_KB]);
let debug =
kernel::static_buf!(capsules_system::debug_writer::uart_debug_writer::UartDebugWriter);
(uart, ring, buffer, debug)
};};
($P: ty) => {{
$crate::debug_writer_component_static!($crate::debug_writer::DEFAULT_DEBUG_BUFFER_KBYTE, $P)
};};
($BUF_SIZE_KB:expr) => {{
$crate::debug_writer_component_static!(
$BUF_SIZE_KB,
capsules_core::virtualizers::selection_policy::InsertionFirstPolicy
)
};};
() => {{
$crate::debug_writer_component_static!(
$crate::debug_writer::DEFAULT_DEBUG_BUFFER_KBYTE,
capsules_core::virtualizers::selection_policy::InsertionFirstPolicy
)
};};
}
/// The optional argument to this macro allows boards to specify the size of the in-RAM
/// buffer used for storing debug messages.
///
/// Increase this value to be able to send more debug messages in
/// quick succession.
#[macro_export]
macro_rules! debug_writer_no_mux_component_static {
($BUF_SIZE_KB:expr) => {{
let ring = kernel::static_buf!(kernel::collections::ring_buffer::RingBuffer<'static, u8>);
let buffer = kernel::static_buf!([u8; 1024 * $BUF_SIZE_KB]);
let debug =
kernel::static_buf!(capsules_system::debug_writer::uart_debug_writer::UartDebugWriter);
(ring, buffer, debug)
};};
() => {{
use $crate::debug_writer::DEFAULT_DEBUG_BUFFER_KBYTE;
$crate::debug_writer_no_mux_component_static!(DEFAULT_DEBUG_BUFFER_KBYTE)
};};
}
// Allow dead code because we need the `Chip` type but don't use `chip`.
#[allow(dead_code)]
pub struct DebugWriterComponent<
const BUF_SIZE_BYTES: usize,
C: SetDebugWriterCapability,
P: SelectionPolicy<&'static UartDevice<'static, P>> + 'static,
> {
uart_mux: &'static MuxUart<'static, P>,
marker: core::marker::PhantomData<[u8; BUF_SIZE_BYTES]>,
capability: C,
}
impl<
const BUF_SIZE_BYTES: usize,
C: SetDebugWriterCapability,
SP: SelectionPolicy<&'static UartDevice<'static, SP>> + 'static,
> DebugWriterComponent<BUF_SIZE_BYTES, C, SP>
{
/// Create a debug writer component while binding the global variable used
/// by debug.rs to the main thread.
#[cfg(target_has_atomic = "ptr")]
pub fn new<P: kernel::platform::chip::ThreadIdProvider>(
uart_mux: &'static MuxUart<SP>,
capability: C,
) -> Self {
kernel::debug::initialize_debug_writer_wrapper::<P>();
Self {
uart_mux,
marker: core::marker::PhantomData,
capability,
}
}
/// Create a debug writer component and bind the global variable used by
/// debug.rs to the main thread, but require that the caller(i.e., main.rs)
/// provides the actual init call.
///
/// This allows moving the unsafe call to main.rs instead of being
/// encapsulated in a component.
///
/// The resulting use of this component for platforms without atomics
/// support looks like this:
///
/// ```
/// components::debug_writer::DebugWriterComponent::new(
/// ...,
/// || unsafe {
/// kernel::debug::initialize_debug_writer_wrapper_unsafe::<
/// <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
/// >();
/// })
/// .finalize(components::debug_writer_component_static!());
/// ```
pub fn new_unsafe<F>(
uart_mux: &'static MuxUart<SP>,
capability: C,
bind_debug_global: F,
) -> Self
where
F: FnOnce(),
{
bind_debug_global();
Self {
uart_mux,
marker: core::marker::PhantomData,
capability,
}
}
}
pub struct Capability;
unsafe impl capabilities::ProcessManagementCapability for Capability {}
impl<
const BUF_SIZE_BYTES: usize,
C: SetDebugWriterCapability,
P: SelectionPolicy<&'static UartDevice<'static, P>> + 'static,
> Component for DebugWriterComponent<BUF_SIZE_BYTES, C, P>
{
type StaticInput = (
&'static mut MaybeUninit<UartDevice<'static, P>>,
&'static mut MaybeUninit<RingBuffer<'static, u8>>,
&'static mut MaybeUninit<[u8; BUF_SIZE_BYTES]>,
&'static mut MaybeUninit<UartDebugWriter>,
);
type Output = ();
fn finalize(self, s: Self::StaticInput) -> Self::Output {
let buf = s.2.write([0; BUF_SIZE_BYTES]);
let (output_buf, internal_buf) = buf.split_at_mut(DEBUG_BUFFER_SPLIT);
// Create virtual device for kernel debug.
let debugger_uart = s.0.write(UartDevice::new(self.uart_mux, false));
debugger_uart.setup();
let ring_buffer = s.1.write(RingBuffer::new(internal_buf));
let debugger =
s.3.write(UartDebugWriter::new(debugger_uart, output_buf, ring_buffer));
hil::uart::Transmit::set_transmit_client(debugger_uart, debugger);
kernel::debug::set_debug_writer_wrapper(debugger, self.capability);
}
}
// Allow dead code because we need the `Chip` type but don't use `chip`.
#[allow(dead_code)]
pub struct DebugWriterNoMuxComponent<
U: uart::Uart<'static> + uart::Transmit<'static> + 'static,
const BUF_SIZE_BYTES: usize,
C: SetDebugWriterCapability,
> {
uart: &'static U,
marker: core::marker::PhantomData<[u8; BUF_SIZE_BYTES]>,
capability: C,
}
impl<
U: uart::Uart<'static> + uart::Transmit<'static> + 'static,
const BUF_SIZE_BYTES: usize,
C: SetDebugWriterCapability,
> DebugWriterNoMuxComponent<U, BUF_SIZE_BYTES, C>
{
pub fn new(uart: &'static U, capability: C) -> Self {
Self {
uart,
marker: core::marker::PhantomData,
capability,
}
}
}
impl<
U: uart::Uart<'static> + uart::Transmit<'static> + 'static,
const BUF_SIZE_BYTES: usize,
C: SetDebugWriterCapability,
> Component for DebugWriterNoMuxComponent<U, BUF_SIZE_BYTES, C>
{
type StaticInput = (
&'static mut MaybeUninit<RingBuffer<'static, u8>>,
&'static mut MaybeUninit<[u8; BUF_SIZE_BYTES]>,
&'static mut MaybeUninit<UartDebugWriter>,
);
type Output = ();
fn finalize(self, s: Self::StaticInput) -> Self::Output {
let buf = s.1.write([0; BUF_SIZE_BYTES]);
let (output_buf, internal_buf) = buf.split_at_mut(DEBUG_BUFFER_SPLIT);
// Create virtual device for kernel debug.
let ring_buffer = s.0.write(RingBuffer::new(internal_buf));
let debugger =
s.2.write(UartDebugWriter::new(self.uart, output_buf, ring_buffer));
hil::uart::Transmit::set_transmit_client(self.uart, debugger);
kernel::debug::set_debug_writer_wrapper(debugger, self.capability);
let _ = self.uart.configure(uart::Parameters {
baud_rate: 115200,
width: uart::Width::Eight,
stop_bits: uart::StopBits::One,
parity: uart::Parity::None,
hw_flow_control: false,
});
}
}