1#![no_std]
10#![no_main]
11#![deny(missing_docs)]
12
13use core::ptr::addr_of_mut;
14
15use capsules_core::virtualizers::virtual_alarm::VirtualMuxAlarm;
16use components::gpio::GpioComponent;
17use kernel::capabilities;
18use kernel::component::Component;
19use kernel::hil::led::LedLow;
20use kernel::platform::{KernelResources, SyscallDriverLookup};
21use kernel::process::ProcessArray;
22use kernel::scheduler::round_robin::RoundRobinSched;
23use kernel::{create_capability, debug, static_init};
24
25use stm32f401cc::chip_specs::Stm32f401Specs;
26use stm32f401cc::clocks::hsi::HSI_FREQUENCY_MHZ;
27use stm32f401cc::interrupt_service::Stm32f401ccDefaultPeripherals;
28
29pub mod io;
31
32const NUM_PROCS: usize = 4;
34
35type ChipHw = stm32f401cc::chip::Stm32f4xx<'static, Stm32f401ccDefaultPeripherals<'static>>;
36
37static mut PROCESSES: Option<&'static ProcessArray<NUM_PROCS>> = None;
39static mut CHIP: Option<&'static ChipHw> = None;
40static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
41 None;
42
43const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
45 capsules_system::process_policies::PanicFaultPolicy {};
46
47kernel::stack_size! {0x2000}
48
49struct WeactF401CC {
52 console: &'static capsules_core::console::Console<'static>,
53 ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
54 led: &'static capsules_core::led::LedDriver<
55 'static,
56 LedLow<'static, stm32f401cc::gpio::Pin<'static>>,
57 1,
58 >,
59 button: &'static capsules_core::button::Button<'static, stm32f401cc::gpio::Pin<'static>>,
60 adc: &'static capsules_core::adc::AdcVirtualized<'static>,
61 alarm: &'static capsules_core::alarm::AlarmDriver<
62 'static,
63 VirtualMuxAlarm<'static, stm32f401cc::tim2::Tim2<'static>>,
64 >,
65 gpio: &'static capsules_core::gpio::GPIO<'static, stm32f401cc::gpio::Pin<'static>>,
66 scheduler: &'static RoundRobinSched<'static>,
67 systick: cortexm4::systick::SysTick,
68}
69
70impl SyscallDriverLookup for WeactF401CC {
72 fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
73 where
74 F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
75 {
76 match driver_num {
77 capsules_core::console::DRIVER_NUM => f(Some(self.console)),
78 capsules_core::led::DRIVER_NUM => f(Some(self.led)),
79 capsules_core::button::DRIVER_NUM => f(Some(self.button)),
80 capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
81 capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
82 kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
83 capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
84 _ => f(None),
85 }
86 }
87}
88
89impl KernelResources<stm32f401cc::chip::Stm32f4xx<'static, Stm32f401ccDefaultPeripherals<'static>>>
90 for WeactF401CC
91{
92 type SyscallDriverLookup = Self;
93 type SyscallFilter = ();
94 type ProcessFault = ();
95 type Scheduler = RoundRobinSched<'static>;
96 type SchedulerTimer = cortexm4::systick::SysTick;
97 type WatchDog = ();
98 type ContextSwitchCallback = ();
99
100 fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
101 self
102 }
103 fn syscall_filter(&self) -> &Self::SyscallFilter {
104 &()
105 }
106 fn process_fault(&self) -> &Self::ProcessFault {
107 &()
108 }
109 fn scheduler(&self) -> &Self::Scheduler {
110 self.scheduler
111 }
112 fn scheduler_timer(&self) -> &Self::SchedulerTimer {
113 &self.systick
114 }
115 fn watchdog(&self) -> &Self::WatchDog {
116 &()
117 }
118 fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
119 &()
120 }
121}
122
123unsafe fn setup_dma(
125 dma: &stm32f401cc::dma::Dma1,
126 dma_streams: &'static [stm32f401cc::dma::Stream<stm32f401cc::dma::Dma1>; 8],
127 usart2: &'static stm32f401cc::usart::Usart<stm32f401cc::dma::Dma1>,
128) {
129 use stm32f401cc::dma::Dma1Peripheral;
130 use stm32f401cc::usart;
131
132 dma.enable_clock();
133
134 let usart2_tx_stream = &dma_streams[Dma1Peripheral::USART2_TX.get_stream_idx()];
135 let usart2_rx_stream = &dma_streams[Dma1Peripheral::USART2_RX.get_stream_idx()];
136
137 usart2.set_dma(
138 usart::TxDMA(usart2_tx_stream),
139 usart::RxDMA(usart2_rx_stream),
140 );
141
142 usart2_tx_stream.set_client(usart2);
143 usart2_rx_stream.set_client(usart2);
144
145 usart2_tx_stream.setup(Dma1Peripheral::USART2_TX);
146 usart2_rx_stream.setup(Dma1Peripheral::USART2_RX);
147
148 cortexm4::nvic::Nvic::new(Dma1Peripheral::USART2_TX.get_stream_irqn()).enable();
149 cortexm4::nvic::Nvic::new(Dma1Peripheral::USART2_RX.get_stream_irqn()).enable();
150}
151
152unsafe fn set_pin_primary_functions(
154 syscfg: &stm32f401cc::syscfg::Syscfg,
155 gpio_ports: &'static stm32f401cc::gpio::GpioPorts<'static>,
156) {
157 use kernel::hil::gpio::Configure;
158 use stm32f401cc::gpio::{AlternateFunction, Mode, PinId, PortId};
159
160 syscfg.enable_clock();
161
162 gpio_ports.get_port_from_port_id(PortId::A).enable_clock();
163
164 gpio_ports.get_pin(PinId::PA00).map(|pin| {
166 pin.enable_interrupt();
167 });
168
169 gpio_ports.get_pin(PinId::PC14).map(|pin| {
171 pin.enable_interrupt();
172 });
173
174 gpio_ports.get_pin(PinId::PA02).map(|pin| {
176 pin.set_mode(Mode::AlternateFunctionMode);
177 pin.set_alternate_function(AlternateFunction::AF7);
179 });
180 gpio_ports.get_pin(PinId::PA03).map(|pin| {
181 pin.set_mode(Mode::AlternateFunctionMode);
182 pin.set_alternate_function(AlternateFunction::AF7);
184 });
185
186 gpio_ports.get_port_from_port_id(PortId::C).enable_clock();
187
188 gpio_ports.get_pin(PinId::PC13).map(|pin| {
190 pin.make_output();
191 let debug_gpios = static_init!([&'static dyn kernel::hil::gpio::Pin; 1], [pin]);
193 kernel::debug::initialize_debug_gpio::<
194 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
195 >();
196 kernel::debug::assign_gpios(debug_gpios);
197 });
198
199 gpio_ports.get_port_from_port_id(PortId::B).enable_clock();
202}
203
204unsafe fn setup_peripherals(tim2: &stm32f401cc::tim2::Tim2) {
206 cortexm4::nvic::Nvic::new(stm32f401cc::nvic::USART2).enable();
208
209 tim2.enable_clock();
211 tim2.start();
212 cortexm4::nvic::Nvic::new(stm32f401cc::nvic::TIM2).enable();
213}
214
215#[inline(never)]
221unsafe fn start() -> (
222 &'static kernel::Kernel,
223 WeactF401CC,
224 &'static stm32f401cc::chip::Stm32f4xx<'static, Stm32f401ccDefaultPeripherals<'static>>,
225) {
226 stm32f401cc::init();
227
228 kernel::deferred_call::initialize_deferred_call_state::<
230 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
231 >();
232
233 let rcc = static_init!(stm32f401cc::rcc::Rcc, stm32f401cc::rcc::Rcc::new());
235 let clocks = static_init!(
236 stm32f401cc::clocks::Clocks<Stm32f401Specs>,
237 stm32f401cc::clocks::Clocks::new(rcc)
238 );
239 let syscfg = static_init!(
240 stm32f401cc::syscfg::Syscfg,
241 stm32f401cc::syscfg::Syscfg::new(clocks)
242 );
243 let exti = static_init!(
244 stm32f401cc::exti::Exti,
245 stm32f401cc::exti::Exti::new(syscfg)
246 );
247 let dma1 = static_init!(stm32f401cc::dma::Dma1, stm32f401cc::dma::Dma1::new(clocks));
248 let dma2 = static_init!(stm32f401cc::dma::Dma2, stm32f401cc::dma::Dma2::new(clocks));
249
250 let peripherals = static_init!(
251 Stm32f401ccDefaultPeripherals,
252 Stm32f401ccDefaultPeripherals::new(clocks, exti, dma1, dma2)
253 );
254
255 peripherals.init();
256 let base_peripherals = &peripherals.stm32f4;
257
258 setup_peripherals(&base_peripherals.tim2);
259
260 set_pin_primary_functions(syscfg, &base_peripherals.gpio_ports);
261
262 setup_dma(
263 dma1,
264 &base_peripherals.dma1_streams,
265 &base_peripherals.usart2,
266 );
267
268 let processes = components::process_array::ProcessArrayComponent::new()
270 .finalize(components::process_array_component_static!(NUM_PROCS));
271 PROCESSES = Some(processes);
272
273 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
275
276 let chip = static_init!(
277 stm32f401cc::chip::Stm32f4xx<Stm32f401ccDefaultPeripherals>,
278 stm32f401cc::chip::Stm32f4xx::new(peripherals)
279 );
280 CHIP = Some(chip);
281
282 base_peripherals.usart2.enable_clock();
286 let uart_mux = components::console::UartMuxComponent::new(&base_peripherals.usart2, 115200)
287 .finalize(components::uart_mux_component_static!());
288
289 (*addr_of_mut!(io::WRITER)).set_initialized();
290
291 let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
294 let process_management_capability =
295 create_capability!(capabilities::ProcessManagementCapability);
296
297 let console = components::console::ConsoleComponent::new(
299 board_kernel,
300 capsules_core::console::DRIVER_NUM,
301 uart_mux,
302 )
303 .finalize(components::console_component_static!());
304 components::debug_writer::DebugWriterComponent::new::<
306 <ChipHw as kernel::platform::chip::Chip>::ThreadIdProvider,
307 >(
308 uart_mux,
309 create_capability!(capabilities::SetDebugWriterCapability),
310 )
311 .finalize(components::debug_writer_component_static!());
312
313 let gpio_ports = &base_peripherals.gpio_ports;
316
317 let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
318 LedLow<'static, stm32f401cc::gpio::Pin>,
319 LedLow::new(gpio_ports.get_pin(stm32f401cc::gpio::PinId::PC13).unwrap()),
320 ));
321
322 let button = components::button::ButtonComponent::new(
324 board_kernel,
325 capsules_core::button::DRIVER_NUM,
326 components::button_component_helper!(
327 stm32f401cc::gpio::Pin,
328 (
329 gpio_ports.get_pin(stm32f401cc::gpio::PinId::PA00).unwrap(),
330 kernel::hil::gpio::ActivationMode::ActiveLow,
331 kernel::hil::gpio::FloatingState::PullUp
332 )
333 ),
334 )
335 .finalize(components::button_component_static!(stm32f401cc::gpio::Pin));
336
337 let tim2 = &base_peripherals.tim2;
340 let mux_alarm = components::alarm::AlarmMuxComponent::new(tim2).finalize(
341 components::alarm_mux_component_static!(stm32f401cc::tim2::Tim2),
342 );
343
344 let alarm = components::alarm::AlarmDriverComponent::new(
345 board_kernel,
346 capsules_core::alarm::DRIVER_NUM,
347 mux_alarm,
348 )
349 .finalize(components::alarm_component_static!(stm32f401cc::tim2::Tim2));
350
351 let gpio = GpioComponent::new(
353 board_kernel,
354 capsules_core::gpio::DRIVER_NUM,
355 components::gpio_component_helper!(
356 stm32f401cc::gpio::Pin,
357 3 => gpio_ports.pins[2][14].as_ref().unwrap(), 4 => gpio_ports.pins[2][15].as_ref().unwrap(), 11 => gpio_ports.pins[0][1].as_ref().unwrap(), 12 => gpio_ports.pins[0][2].as_ref().unwrap(), 13 => gpio_ports.pins[0][3].as_ref().unwrap(), 14 => gpio_ports.pins[0][4].as_ref().unwrap(), 15 => gpio_ports.pins[0][5].as_ref().unwrap(), 16 => gpio_ports.pins[0][6].as_ref().unwrap(), 17 => gpio_ports.pins[0][7].as_ref().unwrap(), 18 => gpio_ports.pins[1][0].as_ref().unwrap(), 19 => gpio_ports.pins[1][1].as_ref().unwrap(), 20 => gpio_ports.pins[1][2].as_ref().unwrap(), 21 => gpio_ports.pins[1][10].as_ref().unwrap(), 25 => gpio_ports.pins[1][12].as_ref().unwrap(), 26 => gpio_ports.pins[1][13].as_ref().unwrap(), 27 => gpio_ports.pins[1][14].as_ref().unwrap(), 28 => gpio_ports.pins[1][15].as_ref().unwrap(), 29 => gpio_ports.pins[0][8].as_ref().unwrap(), 30 => gpio_ports.pins[0][9].as_ref().unwrap(), 31 => gpio_ports.pins[0][10].as_ref().unwrap(), 32 => gpio_ports.pins[0][11].as_ref().unwrap(), 33 => gpio_ports.pins[0][12].as_ref().unwrap(), 34 => gpio_ports.pins[0][13].as_ref().unwrap(), 37 => gpio_ports.pins[0][14].as_ref().unwrap(), 38 => gpio_ports.pins[0][15].as_ref().unwrap(), 39 => gpio_ports.pins[1][3].as_ref().unwrap(), 40 => gpio_ports.pins[1][4].as_ref().unwrap(), 41 => gpio_ports.pins[1][5].as_ref().unwrap(), 42 => gpio_ports.pins[1][6].as_ref().unwrap(), 43 => gpio_ports.pins[1][7].as_ref().unwrap(), 45 => gpio_ports.pins[1][8].as_ref().unwrap(), 46 => gpio_ports.pins[1][9].as_ref().unwrap(), ),
392 )
393 .finalize(components::gpio_component_static!(stm32f401cc::gpio::Pin));
394
395 let adc_mux = components::adc::AdcMuxComponent::new(&base_peripherals.adc1)
397 .finalize(components::adc_mux_component_static!(stm32f401cc::adc::Adc));
398
399 let adc_channel_0 =
400 components::adc::AdcComponent::new(adc_mux, stm32f401cc::adc::Channel::Channel3)
401 .finalize(components::adc_component_static!(stm32f401cc::adc::Adc));
402
403 let adc_channel_1 =
404 components::adc::AdcComponent::new(adc_mux, stm32f401cc::adc::Channel::Channel10)
405 .finalize(components::adc_component_static!(stm32f401cc::adc::Adc));
406
407 let adc_channel_2 =
408 components::adc::AdcComponent::new(adc_mux, stm32f401cc::adc::Channel::Channel13)
409 .finalize(components::adc_component_static!(stm32f401cc::adc::Adc));
410
411 let adc_channel_3 =
412 components::adc::AdcComponent::new(adc_mux, stm32f401cc::adc::Channel::Channel9)
413 .finalize(components::adc_component_static!(stm32f401cc::adc::Adc));
414
415 let adc_channel_4 =
416 components::adc::AdcComponent::new(adc_mux, stm32f401cc::adc::Channel::Channel15)
417 .finalize(components::adc_component_static!(stm32f401cc::adc::Adc));
418
419 let adc_channel_5 =
420 components::adc::AdcComponent::new(adc_mux, stm32f401cc::adc::Channel::Channel8)
421 .finalize(components::adc_component_static!(stm32f401cc::adc::Adc));
422
423 let adc_syscall =
424 components::adc::AdcVirtualComponent::new(board_kernel, capsules_core::adc::DRIVER_NUM)
425 .finalize(components::adc_syscall_component_helper!(
426 adc_channel_0,
427 adc_channel_1,
428 adc_channel_2,
429 adc_channel_3,
430 adc_channel_4,
431 adc_channel_5
432 ));
433
434 let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
435 .finalize(components::process_printer_text_component_static!());
436 PROCESS_PRINTER = Some(process_printer);
437
438 let process_console = components::process_console::ProcessConsoleComponent::new(
440 board_kernel,
441 uart_mux,
442 mux_alarm,
443 process_printer,
444 Some(cortexm4::support::reset),
445 )
446 .finalize(components::process_console_component_static!(
447 stm32f401cc::tim2::Tim2
448 ));
449 let _ = process_console.start();
450
451 let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
452 .finalize(components::round_robin_component_static!(NUM_PROCS));
453
454 let weact_f401cc = WeactF401CC {
455 console,
456 ipc: kernel::ipc::IPC::new(
457 board_kernel,
458 kernel::ipc::DRIVER_NUM,
459 &memory_allocation_capability,
460 ),
461 adc: adc_syscall,
462 led,
463 button,
464 alarm,
465 gpio,
466 scheduler,
467 systick: cortexm4::systick::SysTick::new_with_calibration(
468 (HSI_FREQUENCY_MHZ * 1_000_000) as u32,
469 ),
470 };
471
472 debug!("Initialization complete. Entering main loop");
473
474 extern "C" {
476 static _sapps: u8;
478 static _eapps: u8;
480 static mut _sappmem: u8;
482 static _eappmem: u8;
484 }
485
486 kernel::process::load_processes(
487 board_kernel,
488 chip,
489 core::slice::from_raw_parts(
490 core::ptr::addr_of!(_sapps),
491 core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
492 ),
493 core::slice::from_raw_parts_mut(
494 core::ptr::addr_of_mut!(_sappmem),
495 core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
496 ),
497 &FAULT_RESPONSE,
498 &process_management_capability,
499 )
500 .unwrap_or_else(|err| {
501 debug!("Error loading processes!");
502 debug!("{:?}", err);
503 });
504
505 (board_kernel, weact_f401cc, chip)
511}
512
513#[no_mangle]
515pub unsafe fn main() {
516 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
517
518 let (board_kernel, platform, chip) = start();
519 board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
520}