1#![no_std]
10#![no_main]
11#![deny(missing_docs)]
12
13use components::gpio::GpioComponent;
14use kernel::capabilities;
15use kernel::component::Component;
16use kernel::debug::PanicResources;
17use kernel::hil::gpio::Configure;
18use kernel::platform::{KernelResources, SyscallDriverLookup};
19use kernel::scheduler::round_robin::RoundRobinSched;
20use kernel::utilities::single_thread_value::SingleThreadValue;
21use kernel::{create_capability, debug, static_init};
22
23pub mod io;
25
26const NUM_PROCS: usize = 4;
28
29type ChipHw = msp432::chip::Msp432<'static, msp432::chip::Msp432DefaultPeripherals<'static>>;
30type ProcessPrinterInUse = capsules_system::process_printer::ProcessPrinterText;
31
32static PANIC_RESOURCES: SingleThreadValue<PanicResources<ChipHw, ProcessPrinterInUse>> =
34 SingleThreadValue::new(PanicResources::new());
35
36const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
38 capsules_system::process_policies::PanicFaultPolicy {};
39
40kernel::stack_size! {0x1000}
41
42struct MspExp432P401R {
45 led: &'static capsules_core::led::LedDriver<
46 'static,
47 kernel::hil::led::LedHigh<'static, msp432::gpio::IntPin<'static>>,
48 3,
49 >,
50 console: &'static capsules_core::console::Console<'static>,
51 button: &'static capsules_core::button::Button<'static, msp432::gpio::IntPin<'static>>,
52 gpio: &'static capsules_core::gpio::GPIO<'static, msp432::gpio::IntPin<'static>>,
53 alarm: &'static capsules_core::alarm::AlarmDriver<
54 'static,
55 capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm<
56 'static,
57 msp432::timer::TimerA<'static>,
58 >,
59 >,
60 ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
61 adc: &'static capsules_core::adc::AdcDedicated<'static, msp432::adc::Adc<'static>>,
62 wdt: &'static msp432::wdt::Wdt,
63 scheduler: &'static RoundRobinSched<'static>,
64 systick: cortexm4::systick::SysTick,
65}
66
67impl KernelResources<msp432::chip::Msp432<'static, msp432::chip::Msp432DefaultPeripherals<'static>>>
68 for MspExp432P401R
69{
70 type SyscallDriverLookup = Self;
71 type SyscallFilter = ();
72 type ProcessFault = ();
73 type Scheduler = RoundRobinSched<'static>;
74 type SchedulerTimer = cortexm4::systick::SysTick;
75 type WatchDog = msp432::wdt::Wdt;
76 type ContextSwitchCallback = ();
77
78 fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
79 self
80 }
81 fn syscall_filter(&self) -> &Self::SyscallFilter {
82 &()
83 }
84 fn process_fault(&self) -> &Self::ProcessFault {
85 &()
86 }
87 fn scheduler(&self) -> &Self::Scheduler {
88 self.scheduler
89 }
90 fn scheduler_timer(&self) -> &Self::SchedulerTimer {
91 &self.systick
92 }
93 fn watchdog(&self) -> &Self::WatchDog {
94 self.wdt
95 }
96 fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
97 &()
98 }
99}
100
101impl SyscallDriverLookup for MspExp432P401R {
103 fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
104 where
105 F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
106 {
107 match driver_num {
108 capsules_core::led::DRIVER_NUM => f(Some(self.led)),
109 capsules_core::console::DRIVER_NUM => f(Some(self.console)),
110 capsules_core::button::DRIVER_NUM => f(Some(self.button)),
111 capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
112 capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
113 kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
114 capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
115 _ => f(None),
116 }
117 }
118}
119
120unsafe fn startup_intilialisation() {
127 msp432::init();
128
129 let wdt = msp432::wdt::Wdt::new();
134 let sysctl = msp432::sysctl::SysCtl::new();
135 let flctl = msp432::flctl::FlCtl::new();
136 let pcm = msp432::pcm::Pcm::new();
137
138 wdt.disable();
143 sysctl.enable_all_sram_banks();
144 pcm.set_high_power();
145 flctl.set_waitstates(msp432::flctl::WaitStates::_1);
146 flctl.set_buffering(true);
147}
148
149unsafe fn setup_adc_pins(gpio: &msp432::gpio::GpioManager) {
152 use msp432::gpio::{IntPinNr, PinNr};
153 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(); }
181
182#[inline(never)]
186unsafe fn start() -> (
187 &'static kernel::Kernel,
188 MspExp432P401R,
189 &'static msp432::chip::Msp432<'static, msp432::chip::Msp432DefaultPeripherals<'static>>,
190) {
191 startup_intilialisation();
192
193 kernel::deferred_call::initialize_deferred_call_state::<
195 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
196 >();
197
198 PANIC_RESOURCES.bind_to_thread::<<ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider>();
200
201 let peripherals = static_init!(
202 msp432::chip::Msp432DefaultPeripherals,
203 msp432::chip::Msp432DefaultPeripherals::new()
204 );
205 peripherals.init();
206
207 peripherals.gpio.pins[msp432::gpio::PinNr::PJ_2 as usize].enable_primary_function();
209 peripherals.gpio.pins[msp432::gpio::PinNr::PJ_3 as usize].enable_primary_function();
210
211 peripherals.gpio.pins[msp432::gpio::PinNr::PJ_0 as usize].enable_primary_function();
213 peripherals.gpio.pins[msp432::gpio::PinNr::PJ_1 as usize].enable_primary_function();
214
215 peripherals.cs.setup_clocks();
217
218 let dbg_gpio0 = &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P01_0 as usize];
220 let dbg_gpio1 = &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P03_5 as usize];
221 let dbg_gpio2 = &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P03_7 as usize];
222 dbg_gpio0.make_output();
223 dbg_gpio1.make_output();
224 dbg_gpio2.make_output();
225 let debug_gpios = static_init!(
226 [&'static dyn kernel::hil::gpio::Pin; 3],
227 [
228 dbg_gpio0, dbg_gpio1, dbg_gpio2
230 ]
231 );
232 kernel::debug::initialize_debug_gpio::<
233 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
234 >();
235 kernel::debug::assign_gpios(debug_gpios);
236
237 peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P01_2 as usize].enable_primary_function();
239 peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P01_3 as usize].enable_primary_function();
240
241 let processes = components::process_array::ProcessArrayComponent::new()
243 .finalize(components::process_array_component_static!(NUM_PROCS));
244 PANIC_RESOURCES.get().map(|resources| {
245 resources.processes.put(processes.as_slice());
246 });
247
248 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
250
251 let chip = static_init!(
252 msp432::chip::Msp432<msp432::chip::Msp432DefaultPeripherals>,
253 msp432::chip::Msp432::new(peripherals)
254 );
255 PANIC_RESOURCES.get().map(|resources| {
256 resources.chip.put(chip);
257 });
258
259 let button = components::button::ButtonComponent::new(
261 board_kernel,
262 capsules_core::button::DRIVER_NUM,
263 components::button_component_helper!(
264 msp432::gpio::IntPin,
265 (
266 &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P01_1 as usize],
267 kernel::hil::gpio::ActivationMode::ActiveLow,
268 kernel::hil::gpio::FloatingState::PullUp
269 ),
270 (
271 &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P01_4 as usize],
272 kernel::hil::gpio::ActivationMode::ActiveLow,
273 kernel::hil::gpio::FloatingState::PullUp
274 )
275 ),
276 )
277 .finalize(components::button_component_static!(msp432::gpio::IntPin));
278
279 let leds = components::led::LedsComponent::new().finalize(components::led_component_static!(
281 kernel::hil::led::LedHigh<'static, msp432::gpio::IntPin>,
282 kernel::hil::led::LedHigh::new(
283 &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P02_0 as usize]
284 ),
285 kernel::hil::led::LedHigh::new(
286 &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P02_1 as usize]
287 ),
288 kernel::hil::led::LedHigh::new(
289 &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P02_2 as usize]
290 ),
291 ));
292
293 let gpio = GpioComponent::new(
295 board_kernel,
296 capsules_core::gpio::DRIVER_NUM,
297 components::gpio_component_helper!(
298 msp432::gpio::IntPin<'static>,
299 1 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P03_2 as usize],
302 2 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P03_3 as usize],
303 5 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P01_5 as usize],
306 7 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P06_5 as usize],
308 8 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P06_4 as usize],
309 17 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P02_7 as usize],
320 18 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P02_6 as usize],
321 19 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P02_4 as usize],
322 20 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P05_6 as usize],
323 21 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P06_6 as usize],
324 22 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P06_7 as usize],
325 23 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P02_3 as usize],
326 27 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P02_5 as usize],
331 28 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P03_0 as usize],
332 29 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P05_7 as usize],
333 30 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P01_6 as usize],
334 31 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P01_7 as usize],
335 34 => &peripherals.gpio.int_pins[msp432::gpio::IntPinNr::P03_6 as usize]
338 ),
339 )
340 .finalize(components::gpio_component_static!(
341 msp432::gpio::IntPin<'static>
342 ));
343
344 let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
345 let process_management_capability =
346 create_capability!(capabilities::ProcessManagementCapability);
347
348 let uart_mux = components::console::UartMuxComponent::new(&peripherals.uart0, 115200)
350 .finalize(components::uart_mux_component_static!());
351
352 let console = components::console::ConsoleComponent::new(
354 board_kernel,
355 capsules_core::console::DRIVER_NUM,
356 uart_mux,
357 )
358 .finalize(components::console_component_static!());
359 components::debug_writer::DebugWriterComponent::new::<
361 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
362 >(
363 uart_mux,
364 create_capability!(capabilities::SetDebugWriterCapability),
365 )
366 .finalize(components::debug_writer_component_static!());
367
368 let timer0 = &peripherals.timer_a0;
370 let mux_alarm = components::alarm::AlarmMuxComponent::new(timer0).finalize(
371 components::alarm_mux_component_static!(msp432::timer::TimerA),
372 );
373 let alarm = components::alarm::AlarmDriverComponent::new(
374 board_kernel,
375 capsules_core::alarm::DRIVER_NUM,
376 mux_alarm,
377 )
378 .finalize(components::alarm_component_static!(msp432::timer::TimerA));
379
380 setup_adc_pins(&peripherals.gpio);
382
383 let adc_channels = static_init!(
384 [msp432::adc::Channel; 24],
385 [
386 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, ]
411 );
412 let adc = components::adc::AdcDedicatedComponent::new(
413 &peripherals.adc,
414 adc_channels,
415 board_kernel,
416 capsules_core::adc::DRIVER_NUM,
417 )
418 .finalize(components::adc_dedicated_component_static!(
419 msp432::adc::Adc
420 ));
421
422 peripherals
424 .adc_ref
425 .select_ref_voltage(msp432::ref_module::ReferenceVoltage::Volt2_5);
426 peripherals.adc_ref.enable_temp_sensor(true);
428
429 let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
430 .finalize(components::round_robin_component_static!(NUM_PROCS));
431
432 let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
433 .finalize(components::process_printer_text_component_static!());
434 PANIC_RESOURCES.get().map(|resources| {
435 resources.printer.put(process_printer);
436 });
437
438 let msp_exp432p4014 = MspExp432P401R {
439 led: leds,
440 console,
441 button,
442 gpio,
443 alarm,
444 ipc: kernel::ipc::IPC::new(
445 board_kernel,
446 kernel::ipc::DRIVER_NUM,
447 &memory_allocation_capability,
448 ),
449 adc,
450 scheduler,
451 systick: cortexm4::systick::SysTick::new_with_calibration(48_000_000),
452 wdt: &peripherals.wdt,
453 };
454
455 debug!("Initialization complete. Entering main loop");
456
457 extern "C" {
459 static _sapps: u8;
461 static _eapps: u8;
463 static mut _sappmem: u8;
465 static _eappmem: u8;
467 }
468
469 kernel::process::load_processes(
470 board_kernel,
471 chip,
472 core::slice::from_raw_parts(
473 core::ptr::addr_of!(_sapps),
474 core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
475 ),
476 core::slice::from_raw_parts_mut(
477 core::ptr::addr_of_mut!(_sappmem),
478 core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
479 ),
480 &FAULT_RESPONSE,
481 &process_management_capability,
482 )
483 .unwrap();
484
485 (board_kernel, msp_exp432p4014, chip)
491}
492
493#[no_mangle]
495pub unsafe fn main() {
496 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
497
498 let (board_kernel, board, chip) = start();
499 board_kernel.kernel_loop(&board, chip, Some(&board.ipc), &main_loop_capability);
500}