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::gpio::Configure;
20use kernel::hil::led::LedHigh;
21use kernel::platform::{KernelResources, SyscallDriverLookup};
22use kernel::process::ProcessArray;
23use kernel::scheduler::round_robin::RoundRobinSched;
24use kernel::{create_capability, debug, static_init};
25use stm32f446re::chip_specs::Stm32f446Specs;
26use stm32f446re::clocks::hsi::HSI_FREQUENCY_MHZ;
27use stm32f446re::gpio::{AlternateFunction, Mode, PinId, PortId};
28use stm32f446re::interrupt_service::Stm32f446reDefaultPeripherals;
29
30pub mod io;
32
33#[allow(dead_code)]
35mod virtual_uart_rx_test;
36
37const NUM_PROCS: usize = 4;
39
40static mut PROCESSES: Option<&'static ProcessArray<NUM_PROCS>> = None;
42
43static mut CHIP: Option<&'static stm32f446re::chip::Stm32f4xx<Stm32f446reDefaultPeripherals>> =
45 None;
46static mut PROCESS_PRINTER: Option<&'static capsules_system::process_printer::ProcessPrinterText> =
48 None;
49
50const FAULT_RESPONSE: capsules_system::process_policies::PanicFaultPolicy =
52 capsules_system::process_policies::PanicFaultPolicy {};
53
54kernel::stack_size! {0x2000}
55
56type TemperatureSTMSensor = components::temperature_stm::TemperatureSTMComponentType<
57 capsules_core::virtualizers::virtual_adc::AdcDevice<'static, stm32f446re::adc::Adc<'static>>,
58>;
59type TemperatureDriver = components::temperature::TemperatureComponentType<TemperatureSTMSensor>;
60
61struct NucleoF446RE {
64 console: &'static capsules_core::console::Console<'static>,
65 ipc: kernel::ipc::IPC<{ NUM_PROCS as u8 }>,
66 led: &'static capsules_core::led::LedDriver<
67 'static,
68 LedHigh<'static, stm32f446re::gpio::Pin<'static>>,
69 1,
70 >,
71 button: &'static capsules_core::button::Button<'static, stm32f446re::gpio::Pin<'static>>,
72 adc: &'static capsules_core::adc::AdcVirtualized<'static>,
73 alarm: &'static capsules_core::alarm::AlarmDriver<
74 'static,
75 VirtualMuxAlarm<'static, stm32f446re::tim2::Tim2<'static>>,
76 >,
77
78 temperature: &'static TemperatureDriver,
79 gpio: &'static capsules_core::gpio::GPIO<'static, stm32f446re::gpio::Pin<'static>>,
80
81 scheduler: &'static RoundRobinSched<'static>,
82 systick: cortexm4::systick::SysTick,
83}
84
85impl SyscallDriverLookup for NucleoF446RE {
87 fn with_driver<F, R>(&self, driver_num: usize, f: F) -> R
88 where
89 F: FnOnce(Option<&dyn kernel::syscall::SyscallDriver>) -> R,
90 {
91 match driver_num {
92 capsules_core::console::DRIVER_NUM => f(Some(self.console)),
93 capsules_core::led::DRIVER_NUM => f(Some(self.led)),
94 capsules_core::button::DRIVER_NUM => f(Some(self.button)),
95 capsules_core::adc::DRIVER_NUM => f(Some(self.adc)),
96 capsules_core::alarm::DRIVER_NUM => f(Some(self.alarm)),
97 capsules_extra::temperature::DRIVER_NUM => f(Some(self.temperature)),
98 capsules_core::gpio::DRIVER_NUM => f(Some(self.gpio)),
99 kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
100 _ => f(None),
101 }
102 }
103}
104
105impl
106 KernelResources<
107 stm32f446re::chip::Stm32f4xx<
108 'static,
109 stm32f446re::interrupt_service::Stm32f446reDefaultPeripherals<'static>,
110 >,
111 > for NucleoF446RE
112{
113 type SyscallDriverLookup = Self;
114 type SyscallFilter = ();
115 type ProcessFault = ();
116 type Scheduler = RoundRobinSched<'static>;
117 type SchedulerTimer = cortexm4::systick::SysTick;
118 type WatchDog = ();
119 type ContextSwitchCallback = ();
120
121 fn syscall_driver_lookup(&self) -> &Self::SyscallDriverLookup {
122 self
123 }
124 fn syscall_filter(&self) -> &Self::SyscallFilter {
125 &()
126 }
127 fn process_fault(&self) -> &Self::ProcessFault {
128 &()
129 }
130 fn scheduler(&self) -> &Self::Scheduler {
131 self.scheduler
132 }
133 fn scheduler_timer(&self) -> &Self::SchedulerTimer {
134 &self.systick
135 }
136 fn watchdog(&self) -> &Self::WatchDog {
137 &()
138 }
139 fn context_switch_callback(&self) -> &Self::ContextSwitchCallback {
140 &()
141 }
142}
143
144unsafe fn setup_dma(
146 dma: &stm32f446re::dma::Dma1,
147 dma_streams: &'static [stm32f446re::dma::Stream<stm32f446re::dma::Dma1>; 8],
148 usart2: &'static stm32f446re::usart::Usart<stm32f446re::dma::Dma1>,
149) {
150 use stm32f446re::dma::Dma1Peripheral;
151 use stm32f446re::usart;
152
153 dma.enable_clock();
154
155 let usart2_tx_stream = &dma_streams[Dma1Peripheral::USART2_TX.get_stream_idx()];
156 let usart2_rx_stream = &dma_streams[Dma1Peripheral::USART2_RX.get_stream_idx()];
157
158 usart2.set_dma(
159 usart::TxDMA(usart2_tx_stream),
160 usart::RxDMA(usart2_rx_stream),
161 );
162
163 usart2_tx_stream.set_client(usart2);
164 usart2_rx_stream.set_client(usart2);
165
166 usart2_tx_stream.setup(Dma1Peripheral::USART2_TX);
167 usart2_rx_stream.setup(Dma1Peripheral::USART2_RX);
168
169 cortexm4::nvic::Nvic::new(Dma1Peripheral::USART2_TX.get_stream_irqn()).enable();
170 cortexm4::nvic::Nvic::new(Dma1Peripheral::USART2_RX.get_stream_irqn()).enable();
171}
172
173unsafe fn set_pin_primary_functions(
175 syscfg: &stm32f446re::syscfg::Syscfg,
176 gpio_ports: &'static stm32f446re::gpio::GpioPorts<'static>,
177) {
178 syscfg.enable_clock();
179
180 gpio_ports.get_port_from_port_id(PortId::A).enable_clock();
181 gpio_ports.get_port_from_port_id(PortId::B).enable_clock();
182
183 gpio_ports.get_pin(PinId::PA05).map(|pin| {
185 pin.make_output();
186
187 kernel::debug::assign_gpios(Some(pin), None, None);
189 });
190
191 gpio_ports.get_pin(PinId::PA02).map(|pin| {
193 pin.set_mode(Mode::AlternateFunctionMode);
194 pin.set_alternate_function(AlternateFunction::AF7);
196 });
197 gpio_ports.get_pin(PinId::PA03).map(|pin| {
198 pin.set_mode(Mode::AlternateFunctionMode);
199 pin.set_alternate_function(AlternateFunction::AF7);
201 });
202
203 gpio_ports.get_port_from_port_id(PortId::C).enable_clock();
204
205 gpio_ports.get_pin(PinId::PC13).map(|pin| {
207 pin.enable_interrupt();
208 });
209
210 gpio_ports.get_pin(PinId::PA10).map(|pin| {
212 pin.enable_interrupt();
213 });
214
215 gpio_ports.get_pin(PinId::PA00).map(|pin| {
217 pin.set_mode(stm32f446re::gpio::Mode::AnalogMode);
218 });
219
220 gpio_ports.get_pin(PinId::PA01).map(|pin| {
222 pin.set_mode(stm32f446re::gpio::Mode::AnalogMode);
223 });
224
225 gpio_ports.get_pin(PinId::PA04).map(|pin| {
227 pin.set_mode(stm32f446re::gpio::Mode::AnalogMode);
228 });
229
230 gpio_ports.get_pin(PinId::PB00).map(|pin| {
232 pin.set_mode(stm32f446re::gpio::Mode::AnalogMode);
233 });
234
235 gpio_ports.get_pin(PinId::PC01).map(|pin| {
237 pin.set_mode(stm32f446re::gpio::Mode::AnalogMode);
238 });
239
240 gpio_ports.get_pin(PinId::PC00).map(|pin| {
242 pin.set_mode(stm32f446re::gpio::Mode::AnalogMode);
243 });
244}
245
246unsafe fn setup_peripherals(tim2: &stm32f446re::tim2::Tim2) {
248 cortexm4::nvic::Nvic::new(stm32f446re::nvic::USART2).enable();
250
251 tim2.enable_clock();
253 tim2.start();
254 cortexm4::nvic::Nvic::new(stm32f446re::nvic::TIM2).enable();
255}
256
257#[inline(never)]
261unsafe fn start() -> (
262 &'static kernel::Kernel,
263 NucleoF446RE,
264 &'static stm32f446re::chip::Stm32f4xx<'static, Stm32f446reDefaultPeripherals<'static>>,
265) {
266 stm32f446re::init();
267
268 let rcc = static_init!(stm32f446re::rcc::Rcc, stm32f446re::rcc::Rcc::new());
270 let clocks = static_init!(
271 stm32f446re::clocks::Clocks<Stm32f446Specs>,
272 stm32f446re::clocks::Clocks::new(rcc)
273 );
274
275 let syscfg = static_init!(
276 stm32f446re::syscfg::Syscfg,
277 stm32f446re::syscfg::Syscfg::new(clocks)
278 );
279 let exti = static_init!(
280 stm32f446re::exti::Exti,
281 stm32f446re::exti::Exti::new(syscfg)
282 );
283 let dma1 = static_init!(stm32f446re::dma::Dma1, stm32f446re::dma::Dma1::new(clocks));
284 let dma2 = static_init!(stm32f446re::dma::Dma2, stm32f446re::dma::Dma2::new(clocks));
285
286 let peripherals = static_init!(
287 Stm32f446reDefaultPeripherals,
288 Stm32f446reDefaultPeripherals::new(clocks, exti, dma1, dma2)
289 );
290 peripherals.init();
291 let base_peripherals = &peripherals.stm32f4;
292
293 setup_peripherals(&base_peripherals.tim2);
294
295 set_pin_primary_functions(syscfg, &base_peripherals.gpio_ports);
296
297 setup_dma(
298 dma1,
299 &base_peripherals.dma1_streams,
300 &base_peripherals.usart2,
301 );
302
303 let processes = components::process_array::ProcessArrayComponent::new()
305 .finalize(components::process_array_component_static!(NUM_PROCS));
306 PROCESSES = Some(processes);
307
308 let board_kernel = static_init!(kernel::Kernel, kernel::Kernel::new(processes.as_slice()));
310
311 let chip = static_init!(
312 stm32f446re::chip::Stm32f4xx<Stm32f446reDefaultPeripherals>,
313 stm32f446re::chip::Stm32f4xx::new(peripherals)
314 );
315 CHIP = Some(chip);
316
317 base_peripherals.usart2.enable_clock();
321 let uart_mux = components::console::UartMuxComponent::new(&base_peripherals.usart2, 115200)
322 .finalize(components::uart_mux_component_static!());
323
324 (*addr_of_mut!(io::WRITER)).set_initialized();
327
328 let memory_allocation_capability = create_capability!(capabilities::MemoryAllocationCapability);
331 let process_management_capability =
332 create_capability!(capabilities::ProcessManagementCapability);
333
334 let console = components::console::ConsoleComponent::new(
336 board_kernel,
337 capsules_core::console::DRIVER_NUM,
338 uart_mux,
339 )
340 .finalize(components::console_component_static!());
341 components::debug_writer::DebugWriterComponent::new(
343 uart_mux,
344 create_capability!(capabilities::SetDebugWriterCapability),
345 )
346 .finalize(components::debug_writer_component_static!());
347
348 let gpio_ports = &base_peripherals.gpio_ports;
350
351 let led = components::led::LedsComponent::new().finalize(components::led_component_static!(
353 LedHigh<'static, stm32f446re::gpio::Pin>,
354 LedHigh::new(gpio_ports.get_pin(stm32f446re::gpio::PinId::PA05).unwrap()),
355 ));
356
357 let button = components::button::ButtonComponent::new(
359 board_kernel,
360 capsules_core::button::DRIVER_NUM,
361 components::button_component_helper!(
362 stm32f446re::gpio::Pin,
363 (
364 gpio_ports.get_pin(stm32f446re::gpio::PinId::PC13).unwrap(),
365 kernel::hil::gpio::ActivationMode::ActiveLow,
366 kernel::hil::gpio::FloatingState::PullNone
367 )
368 ),
369 )
370 .finalize(components::button_component_static!(stm32f446re::gpio::Pin));
371
372 let tim2 = &base_peripherals.tim2;
374 let mux_alarm = components::alarm::AlarmMuxComponent::new(tim2).finalize(
375 components::alarm_mux_component_static!(stm32f446re::tim2::Tim2),
376 );
377
378 let alarm = components::alarm::AlarmDriverComponent::new(
379 board_kernel,
380 capsules_core::alarm::DRIVER_NUM,
381 mux_alarm,
382 )
383 .finalize(components::alarm_component_static!(stm32f446re::tim2::Tim2));
384
385 let adc_mux = components::adc::AdcMuxComponent::new(&base_peripherals.adc1)
387 .finalize(components::adc_mux_component_static!(stm32f446re::adc::Adc));
388
389 let temp_sensor = components::temperature_stm::TemperatureSTMComponent::new(
390 adc_mux,
391 stm32f446re::adc::Channel::Channel18,
392 2.5,
393 0.76,
394 )
395 .finalize(components::temperature_stm_adc_component_static!(
396 stm32f446re::adc::Adc
397 ));
398
399 let temp = components::temperature::TemperatureComponent::new(
400 board_kernel,
401 capsules_extra::temperature::DRIVER_NUM,
402 temp_sensor,
403 )
404 .finalize(components::temperature_component_static!(
405 TemperatureSTMSensor
406 ));
407
408 let adc_channel_0 =
409 components::adc::AdcComponent::new(adc_mux, stm32f446re::adc::Channel::Channel0)
410 .finalize(components::adc_component_static!(stm32f446re::adc::Adc));
411
412 let adc_channel_1 =
413 components::adc::AdcComponent::new(adc_mux, stm32f446re::adc::Channel::Channel1)
414 .finalize(components::adc_component_static!(stm32f446re::adc::Adc));
415
416 let adc_channel_2 =
417 components::adc::AdcComponent::new(adc_mux, stm32f446re::adc::Channel::Channel4)
418 .finalize(components::adc_component_static!(stm32f446re::adc::Adc));
419
420 let adc_channel_3 =
421 components::adc::AdcComponent::new(adc_mux, stm32f446re::adc::Channel::Channel8)
422 .finalize(components::adc_component_static!(stm32f446re::adc::Adc));
423
424 let adc_channel_4 =
425 components::adc::AdcComponent::new(adc_mux, stm32f446re::adc::Channel::Channel11)
426 .finalize(components::adc_component_static!(stm32f446re::adc::Adc));
427
428 let adc_channel_5 =
429 components::adc::AdcComponent::new(adc_mux, stm32f446re::adc::Channel::Channel10)
430 .finalize(components::adc_component_static!(stm32f446re::adc::Adc));
431
432 let adc_syscall =
433 components::adc::AdcVirtualComponent::new(board_kernel, capsules_core::adc::DRIVER_NUM)
434 .finalize(components::adc_syscall_component_helper!(
435 adc_channel_0,
436 adc_channel_1,
437 adc_channel_2,
438 adc_channel_3,
439 adc_channel_4,
440 adc_channel_5
441 ));
442
443 let process_printer = components::process_printer::ProcessPrinterTextComponent::new()
444 .finalize(components::process_printer_text_component_static!());
445 PROCESS_PRINTER = Some(process_printer);
446
447 let gpio = GpioComponent::new(
449 board_kernel,
450 capsules_core::gpio::DRIVER_NUM,
451 components::gpio_component_helper!(
452 stm32f446re::gpio::Pin,
453 2 => gpio_ports.get_pin(PinId::PA10).unwrap(), 3 => gpio_ports.get_pin(PinId::PB03).unwrap(), 4 => gpio_ports.get_pin(PinId::PB05).unwrap(), 5 => gpio_ports.get_pin(PinId::PB04).unwrap(), 6 => gpio_ports.get_pin(PinId::PB10).unwrap(), 7 => gpio_ports.get_pin(PinId::PA08).unwrap(), 8 => gpio_ports.get_pin(PinId::PA09).unwrap(), 9 => gpio_ports.get_pin(PinId::PC07).unwrap(), 10 => gpio_ports.get_pin(PinId::PB06).unwrap(), 11 => gpio_ports.get_pin(PinId::PA07).unwrap(), 12 => gpio_ports.get_pin(PinId::PA06).unwrap(), 13 => gpio_ports.get_pin(PinId::PA05).unwrap(), 14 => gpio_ports.get_pin(PinId::PB09).unwrap(), 15 => gpio_ports.get_pin(PinId::PB08).unwrap(), ),
480 )
481 .finalize(components::gpio_component_static!(stm32f446re::gpio::Pin));
482
483 let process_console = components::process_console::ProcessConsoleComponent::new(
485 board_kernel,
486 uart_mux,
487 mux_alarm,
488 process_printer,
489 Some(cortexm4::support::reset),
490 )
491 .finalize(components::process_console_component_static!(
492 stm32f446re::tim2::Tim2
493 ));
494 let _ = process_console.start();
495
496 let scheduler = components::sched::round_robin::RoundRobinComponent::new(processes)
497 .finalize(components::round_robin_component_static!(NUM_PROCS));
498
499 let nucleo_f446re = NucleoF446RE {
500 console,
501 ipc: kernel::ipc::IPC::new(
502 board_kernel,
503 kernel::ipc::DRIVER_NUM,
504 &memory_allocation_capability,
505 ),
506 led,
507 button,
508 adc: adc_syscall,
509 alarm,
510
511 temperature: temp,
512 gpio,
513
514 scheduler,
515 systick: cortexm4::systick::SysTick::new_with_calibration(
516 (HSI_FREQUENCY_MHZ * 1_000_000) as u32,
517 ),
518 };
519
520 debug!("Initialization complete. Entering main loop");
526
527 extern "C" {
529 static _sapps: u8;
531 static _eapps: u8;
533 static mut _sappmem: u8;
535 static _eappmem: u8;
537 }
538
539 kernel::process::load_processes(
540 board_kernel,
541 chip,
542 core::slice::from_raw_parts(
543 core::ptr::addr_of!(_sapps),
544 core::ptr::addr_of!(_eapps) as usize - core::ptr::addr_of!(_sapps) as usize,
545 ),
546 core::slice::from_raw_parts_mut(
547 core::ptr::addr_of_mut!(_sappmem),
548 core::ptr::addr_of!(_eappmem) as usize - core::ptr::addr_of!(_sappmem) as usize,
549 ),
550 &FAULT_RESPONSE,
551 &process_management_capability,
552 )
553 .unwrap_or_else(|err| {
554 debug!("Error loading processes!");
555 debug!("{:?}", err);
556 });
557
558 (board_kernel, nucleo_f446re, chip)
564}
565
566#[no_mangle]
568pub unsafe fn main() {
569 let main_loop_capability = create_capability!(capabilities::MainLoopCapability);
570
571 let (board_kernel, platform, chip) = start();
572 board_kernel.kernel_loop(&platform, chip, Some(&platform.ipc), &main_loop_capability);
573}