Skip to content

Commit 6b481a7

Browse files
bors[bot]bradjc
andcommitted
Merge tock#1046
1046: Refactor debug.rs to not be implemented as a kernel process r=alevy a=bradjc ### Pull Request Overview This pull request changes the debug module to use a normal `uart::UART` interface rather than acting as a process. This simplifies some kernel data structures as they no longer have to handle the case where an app can be a normal app or a kernel app. It also allows boards to easily use a different uart interface (like segger RTT) without having to implement the `Driver` interface as well. ~~This is blocked on tock#1045 but you can help test anyway!~~ ### Testing Strategy This pull request was tested by running hail app and doing debug!()s from a capsule. ### TODO or Help Wanted n/a ### Documentation Updated - [x] Updated the relevant files in `/docs`, or no updates are required. ### Formatting - [x] Ran `make formatall`. Co-authored-by: Brad Campbell <bradjc5@gmail.com>
2 parents 2fb6eb0 + 72cca90 commit 6b481a7

File tree

11 files changed

+563
-458
lines changed

11 files changed

+563
-458
lines changed

boards/ek-tm4c1294xl/src/main.rs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extern crate cortexm4;
1212
extern crate tm4c129x;
1313

1414
use capsules::virtual_alarm::{MuxAlarm, VirtualMuxAlarm};
15+
use capsules::virtual_uart::{UartDevice, UartMux};
1516
use kernel::hil;
1617
use kernel::hil::Controller;
1718
use kernel::Platform;
@@ -43,7 +44,7 @@ pub static mut STACK_MEMORY: [u8; 0x1000] = [0; 0x1000];
4344
/// A structure representing this platform that holds references to all
4445
/// capsules for this platform.
4546
struct EkTm4c1294xl {
46-
console: &'static capsules::console::Console<'static, tm4c129x::uart::UART>,
47+
console: &'static capsules::console::Console<'static, UartDevice<'static>>,
4748
alarm: &'static capsules::alarm::AlarmDriver<
4849
'static,
4950
VirtualMuxAlarm<'static, tm4c129x::gpt::AlarmTimer>,
@@ -80,19 +81,49 @@ pub unsafe fn reset_handler() {
8081
tm4c129x::sysctl::PSYSCTLM
8182
.setup_system_clock(tm4c129x::sysctl::SystemClockSource::PllPioscAt120MHz);
8283

84+
// Create a shared UART channel for the console and for kernel debug.
85+
let uart_mux = static_init!(
86+
UartMux<'static>,
87+
UartMux::new(&tm4c129x::uart::UART0, &mut capsules::virtual_uart::RX_BUF)
88+
);
89+
hil::uart::UART::set_client(&tm4c129x::uart::UART0, uart_mux);
90+
91+
// Create a UartDevice for the console.
92+
let console_uart = static_init!(UartDevice, UartDevice::new(uart_mux, true));
93+
console_uart.setup();
94+
8395
let console = static_init!(
84-
capsules::console::Console<tm4c129x::uart::UART>,
96+
capsules::console::Console<UartDevice>,
8597
capsules::console::Console::new(
86-
&tm4c129x::uart::UART0,
98+
console_uart,
8799
115200,
88100
&mut capsules::console::WRITE_BUF,
89101
&mut capsules::console::READ_BUF,
90102
kernel::Grant::create()
91103
)
92104
);
93-
hil::uart::UART::set_client(&tm4c129x::uart::UART0, console);
105+
hil::uart::UART::set_client(console_uart, console);
94106
tm4c129x::uart::UART0.specify_pins(&tm4c129x::gpio::PA[0], &tm4c129x::gpio::PA[1]);
95107

108+
// Create virtual device for kernel debug.
109+
let debugger_uart = static_init!(UartDevice, UartDevice::new(uart_mux, false));
110+
debugger_uart.setup();
111+
let debugger = static_init!(
112+
kernel::debug::DebugWriter,
113+
kernel::debug::DebugWriter::new(
114+
debugger_uart,
115+
&mut kernel::debug::OUTPUT_BUF,
116+
&mut kernel::debug::INTERNAL_BUF,
117+
)
118+
);
119+
hil::uart::UART::set_client(debugger_uart, debugger);
120+
121+
let debug_wrapper = static_init!(
122+
kernel::debug::DebugWriterWrapper,
123+
kernel::debug::DebugWriterWrapper::new(debugger)
124+
);
125+
kernel::debug::set_debug_writer_wrapper(debug_wrapper);
126+
96127
// Alarm
97128
let alarm_timer = &tm4c129x::gpt::TIMER0;
98129
let mux_alarm = static_init!(
@@ -193,10 +224,6 @@ pub unsafe fn reset_handler() {
193224

194225
tm4c1294.console.initialize();
195226

196-
// Attach the kernel debug interface to this console
197-
let kc = static_init!(capsules::console::App, capsules::console::App::default());
198-
kernel::debug::assign_console_driver(Some(tm4c1294.console), kc);
199-
200227
debug!("Initialization complete. Entering main loop...\r");
201228

202229
let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new());

boards/hail/src/main.rs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ extern crate sam4l;
1818
use capsules::virtual_alarm::{MuxAlarm, VirtualMuxAlarm};
1919
use capsules::virtual_i2c::{I2CDevice, MuxI2C};
2020
use capsules::virtual_spi::{MuxSpiMaster, VirtualSpiMasterDevice};
21+
use capsules::virtual_uart::{UartDevice, UartMux};
2122
use kernel::hil;
2223
use kernel::hil::spi::SpiMaster;
2324
use kernel::hil::Controller;
@@ -60,7 +61,7 @@ pub static mut STACK_MEMORY: [u8; 0x1000] = [0; 0x1000];
6061
/// A structure representing this platform that holds references to all
6162
/// capsules for this platform.
6263
struct Hail {
63-
console: &'static capsules::console::Console<'static, sam4l::usart::USART>,
64+
console: &'static capsules::console::Console<'static, UartDevice<'static>>,
6465
gpio: &'static capsules::gpio::GPIO<'static, sam4l::gpio::GPIOPin>,
6566
alarm: &'static capsules::alarm::AlarmDriver<
6667
'static,
@@ -202,17 +203,28 @@ pub unsafe fn reset_handler() {
202203

203204
// Initialize USART0 for Uart
204205
sam4l::usart::USART0.set_mode(sam4l::usart::UsartMode::Uart);
206+
207+
// Create a shared UART channel for the console and for kernel debug.
208+
let uart_mux = static_init!(
209+
UartMux<'static>,
210+
UartMux::new(&sam4l::usart::USART0, &mut capsules::virtual_uart::RX_BUF)
211+
);
212+
hil::uart::UART::set_client(&sam4l::usart::USART0, uart_mux);
213+
214+
// Create a UartDevice for the console.
215+
let console_uart = static_init!(UartDevice, UartDevice::new(uart_mux, true));
216+
console_uart.setup();
205217
let console = static_init!(
206-
capsules::console::Console<sam4l::usart::USART>,
218+
capsules::console::Console<UartDevice>,
207219
capsules::console::Console::new(
208-
&sam4l::usart::USART0,
220+
console_uart,
209221
115200,
210222
&mut capsules::console::WRITE_BUF,
211223
&mut capsules::console::READ_BUF,
212224
kernel::Grant::create()
213225
)
214226
);
215-
hil::uart::UART::set_client(&sam4l::usart::USART0, console);
227+
hil::uart::UART::set_client(console_uart, console);
216228

217229
// Initialize USART3 for Uart
218230
sam4l::usart::USART3.set_mode(sam4l::usart::UsartMode::Uart);
@@ -472,9 +484,25 @@ pub unsafe fn reset_handler() {
472484
};
473485

474486
hail.console.initialize();
475-
// Attach the kernel debug interface to this console
476-
let kc = static_init!(capsules::console::App, capsules::console::App::default());
477-
kernel::debug::assign_console_driver(Some(hail.console), kc);
487+
488+
// Create virtual device for kernel debug.
489+
let debugger_uart = static_init!(UartDevice, UartDevice::new(uart_mux, false));
490+
debugger_uart.setup();
491+
let debugger = static_init!(
492+
kernel::debug::DebugWriter,
493+
kernel::debug::DebugWriter::new(
494+
debugger_uart,
495+
&mut kernel::debug::OUTPUT_BUF,
496+
&mut kernel::debug::INTERNAL_BUF,
497+
)
498+
);
499+
hil::uart::UART::set_client(debugger_uart, debugger);
500+
501+
let debug_wrapper = static_init!(
502+
kernel::debug::DebugWriterWrapper,
503+
kernel::debug::DebugWriterWrapper::new(debugger)
504+
);
505+
kernel::debug::set_debug_writer_wrapper(debug_wrapper);
478506

479507
// Reset the nRF and setup the UART bus.
480508
hail.nrf51822.reset();
@@ -483,7 +511,7 @@ pub unsafe fn reset_handler() {
483511
// Uncomment to measure overheads for TakeCell and MapCell:
484512
// test_take_map_cell::test_take_map_cell();
485513

486-
// debug!("Initialization complete. Entering main loop");
514+
debug!("Initialization complete. Entering main loop");
487515

488516
let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new());
489517

boards/imix/src/components/console.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,47 +18,64 @@
1818
#![allow(dead_code)] // Components are intended to be conditionally included
1919

2020
use capsules::console;
21+
use capsules::virtual_uart::{UartDevice, UartMux};
2122
use hil;
2223
use kernel;
2324
use kernel::component::Component;
2425
use kernel::Grant;
25-
use sam4l;
2626

2727
pub struct ConsoleComponent {
28-
uart: &'static sam4l::usart::USART,
28+
uart_mux: &'static UartMux<'static>,
2929
baud_rate: u32,
3030
}
3131

3232
impl ConsoleComponent {
33-
pub fn new(uart: &'static sam4l::usart::USART, rate: u32) -> ConsoleComponent {
33+
pub fn new(uart_mux: &'static UartMux, rate: u32) -> ConsoleComponent {
3434
ConsoleComponent {
35-
uart: uart,
35+
uart_mux: uart_mux,
3636
baud_rate: rate,
3737
}
3838
}
3939
}
4040

4141
impl Component for ConsoleComponent {
42-
type Output = &'static console::Console<'static, sam4l::usart::USART>;
42+
type Output = &'static console::Console<'static, UartDevice<'static>>;
4343

4444
unsafe fn finalize(&mut self) -> Self::Output {
45-
sam4l::usart::USART3.set_mode(sam4l::usart::UsartMode::Uart);
45+
// Create virtual device for console.
46+
let console_uart = static_init!(UartDevice, UartDevice::new(self.uart_mux, true));
47+
console_uart.setup();
4648
let console = static_init!(
47-
console::Console<sam4l::usart::USART>,
49+
console::Console<UartDevice>,
4850
console::Console::new(
49-
self.uart,
51+
console_uart,
5052
self.baud_rate,
5153
&mut console::WRITE_BUF,
5254
&mut console::READ_BUF,
5355
Grant::create()
5456
)
5557
);
56-
hil::uart::UART::set_client(self.uart, console);
58+
hil::uart::UART::set_client(console_uart, console);
5759
console.initialize();
5860

59-
// Attach the kernel debug interface to this console
60-
let kc = static_init!(console::App, console::App::default());
61-
kernel::debug::assign_console_driver(Some(console), kc);
61+
// Create virtual device for kernel debug.
62+
let debugger_uart = static_init!(UartDevice, UartDevice::new(self.uart_mux, false));
63+
debugger_uart.setup();
64+
let debugger = static_init!(
65+
kernel::debug::DebugWriter,
66+
kernel::debug::DebugWriter::new(
67+
debugger_uart,
68+
&mut kernel::debug::OUTPUT_BUF,
69+
&mut kernel::debug::INTERNAL_BUF,
70+
)
71+
);
72+
hil::uart::UART::set_client(debugger_uart, debugger);
73+
74+
let debug_wrapper = static_init!(
75+
kernel::debug::DebugWriterWrapper,
76+
kernel::debug::DebugWriterWrapper::new(debugger)
77+
);
78+
kernel::debug::set_debug_writer_wrapper(debug_wrapper);
6279

6380
console
6481
}

boards/imix/src/main.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use capsules::alarm::AlarmDriver;
2323
use capsules::virtual_alarm::{MuxAlarm, VirtualMuxAlarm};
2424
use capsules::virtual_i2c::MuxI2C;
2525
use capsules::virtual_spi::{MuxSpiMaster, VirtualSpiMasterDevice};
26+
use capsules::virtual_uart::{UartDevice, UartMux};
2627
use kernel::component::Component;
2728
use kernel::hil;
2829
use kernel::hil::radio;
@@ -94,7 +95,7 @@ static mut PROCESSES: [Option<&'static mut kernel::procs::Process<'static>>; NUM
9495
pub static mut STACK_MEMORY: [u8; 0x1000] = [0; 0x1000];
9596

9697
struct Imix {
97-
console: &'static capsules::console::Console<'static, sam4l::usart::USART>,
98+
console: &'static capsules::console::Console<'static, UartDevice<'static>>,
9899
gpio: &'static capsules::gpio::GPIO<'static, sam4l::gpio::GPIOPin>,
99100
alarm: &'static AlarmDriver<'static, VirtualMuxAlarm<'static, sam4l::ast::Ast<'static>>>,
100101
temp: &'static capsules::temperature::TemperatureSensor<'static>,
@@ -254,7 +255,15 @@ pub unsafe fn reset_handler() {
254255
trng: true,
255256
});
256257

257-
let console = ConsoleComponent::new(&sam4l::usart::USART3, 115200).finalize();
258+
// Create a shared UART channel for the console and for kernel debug.
259+
sam4l::usart::USART3.set_mode(sam4l::usart::UsartMode::Uart);
260+
let uart_mux = static_init!(
261+
UartMux<'static>,
262+
UartMux::new(&sam4l::usart::USART3, &mut capsules::virtual_uart::RX_BUF)
263+
);
264+
hil::uart::UART::set_client(&sam4l::usart::USART3, uart_mux);
265+
266+
let console = ConsoleComponent::new(uart_mux, 115200).finalize();
258267

259268
// Allow processes to communicate over BLE through the nRF51822
260269
let nrf_serialization =

boards/launchxl/src/main.rs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ extern crate cc26xx;
1212
#[macro_use(debug, debug_gpio, static_init)]
1313
extern crate kernel;
1414

15+
use capsules::virtual_uart::{UartDevice, UartMux};
1516
use cc26x2::aon;
1617
use cc26x2::prcm;
18+
use kernel::hil;
1719

1820
#[macro_use]
1921
pub mod io;
@@ -38,7 +40,7 @@ pub static mut STACK_MEMORY: [u8; 0x1000] = [0; 0x1000];
3840
pub struct Platform {
3941
gpio: &'static capsules::gpio::GPIO<'static, cc26xx::gpio::GPIOPin>,
4042
led: &'static capsules::led::LED<'static, cc26xx::gpio::GPIOPin>,
41-
console: &'static capsules::console::Console<'static, cc26xx::uart::UART>,
43+
console: &'static capsules::console::Console<'static, UartDevice<'static>>,
4244
button: &'static capsules::button::Button<'static, cc26xx::gpio::GPIOPin>,
4345
alarm: &'static capsules::alarm::AlarmDriver<
4446
'static,
@@ -125,23 +127,51 @@ pub unsafe fn reset_handler() {
125127
}
126128

127129
// UART
130+
131+
// Create a shared UART channel for the console and for kernel debug.
132+
let uart_mux = static_init!(
133+
UartMux<'static>,
134+
UartMux::new(&cc26xx::uart::UART0, &mut capsules::virtual_uart::RX_BUF)
135+
);
136+
hil::uart::UART::set_client(&cc26xx::uart::UART0, uart_mux);
137+
138+
// Create a UartDevice for the console.
139+
let console_uart = static_init!(UartDevice, UartDevice::new(uart_mux, true));
140+
console_uart.setup();
141+
128142
cc26xx::uart::UART0.initialize_and_set_pins(3, 2);
143+
129144
let console = static_init!(
130-
capsules::console::Console<cc26xx::uart::UART>,
145+
capsules::console::Console<UartDevice>,
131146
capsules::console::Console::new(
132-
&cc26xx::uart::UART0,
147+
console_uart,
133148
115200,
134149
&mut capsules::console::WRITE_BUF,
135150
&mut capsules::console::READ_BUF,
136151
kernel::Grant::create()
137152
)
138153
);
139-
kernel::hil::uart::UART::set_client(&cc26xx::uart::UART0, console);
154+
kernel::hil::uart::UART::set_client(console_uart, console);
140155
console.initialize();
141156

142-
// Attach the kernel debug interface to this console
143-
let kc = static_init!(capsules::console::App, capsules::console::App::default());
144-
kernel::debug::assign_console_driver(Some(console), kc);
157+
// Create virtual device for kernel debug.
158+
let debugger_uart = static_init!(UartDevice, UartDevice::new(uart_mux, false));
159+
debugger_uart.setup();
160+
let debugger = static_init!(
161+
kernel::debug::DebugWriter,
162+
kernel::debug::DebugWriter::new(
163+
debugger_uart,
164+
&mut kernel::debug::OUTPUT_BUF,
165+
&mut kernel::debug::INTERNAL_BUF,
166+
)
167+
);
168+
hil::uart::UART::set_client(debugger_uart, debugger);
169+
170+
let debug_wrapper = static_init!(
171+
kernel::debug::DebugWriterWrapper,
172+
kernel::debug::DebugWriterWrapper::new(debugger)
173+
);
174+
kernel::debug::set_debug_writer_wrapper(debug_wrapper);
145175

146176
// Setup for remaining GPIO pins
147177
let gpio_pins = static_init!(

0 commit comments

Comments
 (0)