1#![no_std]
10#![no_main]
11#![deny(missing_docs)]
12
13use components::gpio::GpioComponent;
14use kernel::capabilities;
15use kernel::component::Component;
16use kernel::hil::gpio::Configure;
17use kernel::platform::{KernelResources, SyscallDriverLookup};
18use kernel::process::ProcessArray;
19use kernel::scheduler::round_robin::RoundRobinSched;
20use kernel::{create_capability, debug, static_init};
21
22pub mod io;
24
25const NUM_PROCS: usize = 4;
27
28static mut PROCESSES: Option<&'static ProcessArray<NUM_PROCS>> = None;
30
31static mut CHIP: Option<&'static msp432::chip::Msp432<msp432::chip::Msp432DefaultPeripherals>> =
33 None;
34static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
36 None;
37
38const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
40 capsules_system::process_policies::PanicFaultPolicy {};
41
42#[no_mangle]
44#[link_section = ".stack_buffer"]
45static mut STACK_MEMORY: [u8; 0x1000] = [0; 0x1000];
46
47struct MspExp432P401R {
50 led: &'static capsules_core::led::LedDriver<
51 'static,
52 kernel::hil::led::LedHigh<'static, msp432::gpio::IntPin<'static>>,
53 3,
54 >,
55 console: &'static capsules_core::console::Console<'static>,
56 button: &'static capsules_core::button::Button<'static, msp432::gpio::IntPin<'static>>,
57 gpio: &'static capsules_core::gpio::GPIO<'static, msp432::gpio::IntPin<'static>>,
58 alarm: &'static capsules_core::alarm::AlarmDriver<
59 'static,
60 capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<
61 'static,
62 msp432::timer::TimerA<'static>,
63 >,
64 >,
65 ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
66 adc: &'static capsules_core::adc::AdcDedicated<'static, msp432::adc::Adc<'static>>,
67 wdt: &'static msp432::wdt::Wdt,
68 scheduler: &'static RoundRobinSched<'static>,
69 systick: cortexm4::systick::SysTick,
70}
71
72impl KernelResources<msp432::chip::Msp432<'static, msp432::chip::Msp432DefaultPeripherals<'static>>>
73 for MspExp432P401R
74{
75 type SyscallDriverLookup = Self;
76 type SyscallFilter = ();
77 type ProcessFault = ();
78 type Scheduler = RoundRobinSched<'static>;
79 type SchedulerTimer = cortexm4::systick::SysTick;
80 type WatchDog = msp432::wdt::Wdt;
81 type ContextSwitchCallback = ();
82
83 fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
84 self
85 }
86 fn syscall_filter(&self) -> &Self::SyscallFilter {
87 &()
88 }
89 fn process_fault(&self) -> &Self::ProcessFault {
90 &()
91 }
92 fn scheduler(&self) -> &Self::Scheduler {
93 self.scheduler
94 }
95 fn scheduler_timer(&self) -> &Self::SchedulerTimer {
96 &self.systick
97 }
98 fn watchdog(&self) -> &Self::WatchDog {
99 self.wdt
100 }
101 fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
102 &()
103 }
104}
105
106impl SyscallDriverLookup for MspExp432P401R {
108 fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
109 where
110 F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
111 {
112 match driver_num {
113 capsules_core::led::DRIVER_NUM => f(Some(self.led)),
114 capsules_core::console::DRIVER_NUM => f(Some(self.console)),
115 capsules_core::button::DRIVER_NUM => f(Some(self.button)),
116 capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
117 capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
118 kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
119 capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
120 _ => f(None),
121 }
122 }
123}
124
125unsafe fn startup_intilialisation() {
132 msp432::init();
133
134 let wdt = msp432::wdt::Wdt::new();
139 let sysctl = msp432::sysctl::SysCtl::new();
140 let flctl = msp432::flctl::FlCtl::new();
141 let pcm = msp432::pcm::Pcm::new();
142
143 wdt.disable();
148 sysctl.enable_all_sram_banks();
149 pcm.set_high_power();
150 flctl.set_waitstates(msp432::flctl::WaitStates::_1);
151 flctl.set_buffering(true);
152}
153
154unsafe fn setup_adc_pins(gpio: &msp432::gpio::GpioManager) {
157 use msp432::gpio::{IntPinNr, PinNr};
158 gpio.int_pins[IntPinNr::P05_5 as usize].enable_tertiary_function(); gpio.int_pins[IntPinNr::P05_4 as usize].enable_tertiary_function(); gpio.int_pins[IntPinNr::P05_3 as usize].enable_tertiary_function(); gpio.int_pins[IntPinNr::P05_2 as usize].enable_tertiary_function(); gpio.int_pins[IntPinNr::P05_1 as usize].enable_tertiary_function(); gpio.int_pins[IntPinNr::P05_0 as usize].enable_tertiary_function(); gpio.int_pins[IntPinNr::P04_7 as usize].enable_tertiary_function(); gpio.int_pins[IntPinNr::P04_6 as usize].enable_tertiary_function(); gpio.int_pins[IntPinNr::P04_5 as usize].enable_tertiary_function(); gpio.int_pins[IntPinNr::P04_4 as usize].enable_tertiary_function(); gpio.int_pins[IntPinNr::P04_3 as usize].enable_tertiary_function(); gpio.int_pins[IntPinNr::P04_2 as usize].enable_tertiary_function(); gpio.int_pins[IntPinNr::P04_1 as usize].enable_tertiary_function(); gpio.int_pins[IntPinNr::P04_0 as usize].enable_tertiary_function(); gpio.int_pins[IntPinNr::P06_1 as usize].enable_tertiary_function(); gpio.int_pins[IntPinNr::P06_0 as usize].enable_tertiary_function(); gpio.pins[PinNr::P09_1 as usize].enable_tertiary_function(); gpio.pins[PinNr::P09_0 as usize].enable_tertiary_function(); gpio.pins[PinNr::P08_7 as usize].enable_tertiary_function(); gpio.pins[PinNr::P08_6 as usize].enable_tertiary_function(); gpio.pins[PinNr::P08_5 as usize].enable_tertiary_function(); gpio.pins[PinNr::P08_4 as usize].enable_tertiary_function(); }
186
187#[inline(never)]
191unsafe fn start() -> (
192 &'static kernel::Kernel,
193 MspExp432P401R,
194 &'static msp432::chip::Msp432<'static, msp432::chip::Msp432DefaultPeripherals<'static>>,
195) {
196 startup_intilialisation();
197
198 let peripherals = static_init!(
199 msp432::chip::Msp432DefaultPeripherals,
200 msp432::chip::Msp432DefaultPeripherals::new()
201 );
202 peripherals.init();
203
204 peripherals.gpio.pins[msp432::gpio::PinNr::PJ_2 as usize].enable_primary_function();
206 peripherals.gpio.pins[msp432::gpio::PinNr::PJ_3 as usize].enable_primary_function();
207
208 peripherals.gpio.pins[msp432::gpio::PinNr::PJ_0 as usize].enable_primary_function();
210 peripherals.gpio.pins[msp432::gpio::PinNr::PJ_1 as usize].enable_primary_function();
211
212 peripherals.cs.setup_clocks();
214
215 let dbg_gpio0 = &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P01_0 as usize];
217 let dbg_gpio1 = &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P03_5 as usize];
218 let dbg_gpio2 = &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P03_7 as usize];
219 dbg_gpio0.make_output();
220 dbg_gpio1.make_output();
221 dbg_gpio2.make_output();
222 debug::assign_gpios(
223 Some(dbg_gpio0), Some(dbg_gpio1),
225 Some(dbg_gpio2),
226 );
227
228 peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P01_2 as usize].enable_primary_function();
230 peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P01_3 as usize].enable_primary_function();
231
232 let processes = components::process_array::ProcessArrayComponent::new()
234 .finalize(components::process_array_component_static!(NUM_PROCS));
235 PROCESSES = Some(processes);
236
237 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
239
240 let chip = static_init!(
241 msp432::chip::Msp432<msp432::chip::Msp432DefaultPeripherals>,
242 msp432::chip::Msp432::new(peripherals)
243 );
244 CHIP = Some(chip);
245
246 let button = components::button::ButtonComponent::new(
248 board_kernel,
249 capsules_core::button::DRIVER_NUM,
250 components::button_component_helper!(
251 msp432::gpio::IntPin,
252 (
253 &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P01_1 as usize],
254 kernel::hil::gpio::ActivationMode::ActiveLow,
255 kernel::hil::gpio::FloatingState::PullUp
256 ),
257 (
258 &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P01_4 as usize],
259 kernel::hil::gpio::ActivationMode::ActiveLow,
260 kernel::hil::gpio::FloatingState::PullUp
261 )
262 ),
263 )
264 .finalize(components::button_component_static!(msp432::gpio::IntPin));
265
266 let leds = components::led::LedsComponent::new().finalize(components::led_component_static!(
268 kernel::hil::led::LedHigh<'static, msp432::gpio::IntPin>,
269 kernel::hil::led::LedHigh::new(
270 &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P02_0 as usize]
271 ),
272 kernel::hil::led::LedHigh::new(
273 &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P02_1 as usize]
274 ),
275 kernel::hil::led::LedHigh::new(
276 &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P02_2 as usize]
277 ),
278 ));
279
280 let gpio = GpioComponent::new(
282 board_kernel,
283 capsules_core::gpio::DRIVER_NUM,
284 components::gpio_component_helper!(
285 msp432::gpio::IntPin<'static>,
286 1 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P03_2 as usize],
289 2 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P03_3 as usize],
290 5 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P01_5 as usize],
293 7 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P06_5 as usize],
295 8 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P06_4 as usize],
296 17 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P02_7 as usize],
307 18 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P02_6 as usize],
308 19 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P02_4 as usize],
309 20 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P05_6 as usize],
310 21 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P06_6 as usize],
311 22 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P06_7 as usize],
312 23 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P02_3 as usize],
313 27 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P02_5 as usize],
318 28 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P03_0 as usize],
319 29 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P05_7 as usize],
320 30 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P01_6 as usize],
321 31 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P01_7 as usize],
322 34 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P03_6 as usize]
325 ),
326 )
327 .finalize(components::gpio_component_static!(
328 msp432::gpio::IntPin<'static>
329 ));
330
331 let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
332 let process_management_capability =
333 create_capability!(capabilities::ProcessManagementCapability);
334
335 let uart_mux = components::console::UartMuxComponent::new(&peripherals.uart0, 115200)
337 .finalize(components::uart_mux_component_static!());
338
339 let console = components::console::ConsoleComponent::new(
341 board_kernel,
342 capsules_core::console::DRIVER_NUM,
343 uart_mux,
344 )
345 .finalize(components::console_component_static!());
346 components::debug_writer::DebugWriterComponent::new(
348 uart_mux,
349 create_capability!(capabilities::SetDebugWriterCapability),
350 )
351 .finalize(components::debug_writer_component_static!());
352
353 let timer0 = &peripherals.timer_a0;
355 let mux_alarm = components::alarm::AlarmMuxComponent::new(timer0).finalize(
356 components::alarm_mux_component_static!(msp432::timer::TimerA),
357 );
358 let alarm = components::alarm::AlarmDriverComponent::new(
359 board_kernel,
360 capsules_core::alarm::DRIVER_NUM,
361 mux_alarm,
362 )
363 .finalize(components::alarm_component_static!(msp432::timer::TimerA));
364
365 setup_adc_pins(&peripherals.gpio);
367
368 let adc_channels = static_init!(
369 [msp432::adc::Channel; 24],
370 [
371 msp432::adc::Channel::Channel0, msp432::adc::Channel::Channel1, msp432::adc::Channel::Channel2, msp432::adc::Channel::Channel3, msp432::adc::Channel::Channel4, msp432::adc::Channel::Channel5, msp432::adc::Channel::Channel6, msp432::adc::Channel::Channel7, msp432::adc::Channel::Channel8, msp432::adc::Channel::Channel9, msp432::adc::Channel::Channel10, msp432::adc::Channel::Channel11, msp432::adc::Channel::Channel12, msp432::adc::Channel::Channel13, msp432::adc::Channel::Channel14, msp432::adc::Channel::Channel15, msp432::adc::Channel::Channel16, msp432::adc::Channel::Channel17, msp432::adc::Channel::Channel18, msp432::adc::Channel::Channel19, msp432::adc::Channel::Channel20, msp432::adc::Channel::Channel21, msp432::adc::Channel::Channel22, msp432::adc::Channel::Channel23, ]
396 );
397 let adc = components::adc::AdcDedicatedComponent::new(
398 &peripherals.adc,
399 adc_channels,
400 board_kernel,
401 capsules_core::adc::DRIVER_NUM,
402 )
403 .finalize(components::adc_dedicated_component_static!(
404 msp432::adc::Adc
405 ));
406
407 peripherals
409 .adc_ref
410 .select_ref_voltage(msp432::ref_module::ReferenceVoltage::Volt2_5);
411 peripherals.adc_ref.enable_temp_sensor(true);
413
414 let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
415 .finalize(components::round_robin_component_static!(NUM_PROCS));
416
417 let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
418 .finalize(components::process_printer_text_component_static!());
419 PROCESS_PRINTER = Some(process_printer);
420
421 let msp_exp432p4014 = MspExp432P401R {
422 led: leds,
423 console,
424 button,
425 gpio,
426 alarm,
427 ipc: kernel::ipc::IPC::new(
428 board_kernel,
429 kernel::ipc::DRIVER_NUM,
430 &memory_allocation_capability,
431 ),
432 adc,
433 scheduler,
434 systick: cortexm4::systick::SysTick::new_with_calibration(48_000_000),
435 wdt: &peripherals.wdt,
436 };
437
438 debug!("Initialization complete. Entering main loop");
439
440 extern "C" {
442 static _sapps: u8;
444 static _eapps: u8;
446 static mut _sappmem: u8;
448 static _eappmem: u8;
450 }
451
452 kernel::process::load_processes(
453 board_kernel,
454 chip,
455 core::slice::from_raw_parts(
456 core::ptr::addr_of!(_sapps),
457 core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
458 ),
459 core::slice::from_raw_parts_mut(
460 core::ptr::addr_of_mut!(_sappmem),
461 core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
462 ),
463 &FAULT_RESPONSE,
464 &process_management_capability,
465 )
466 .unwrap();
467
468 (board_kernel, msp_exp432p4014, chip)
474}
475
476#[no_mangle]
478pub unsafe fn main() {
479 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
480
481 let (board_kernel, board, chip) = start();
482 board_kernel.kernel_loop(&board, chip, Some(&board.ipc), &main_loop_capability);
483}