nucleo_f446re/
lib.rs

1#![doc = include_str!("../README.md")]
2#![deny(missing_docs)]
3#![deny(unsafe_code)]
4#![no_std]
5
6/// Work with the on-board user button
7pub mod button;
8/// Work with the on-board user LED
9pub mod led;
10/// Work with the virtual serial port through the on-board ST-Link
11pub mod serial;
12
13use self::{
14    button::Button,
15    led::{LedBuilder, LedDigital},
16    serial::SerialPort,
17};
18use stm32f4xx_hal::{prelude::*, timer::SysDelay};
19
20/// The batteries-included way to work with the Nucleo board.
21///
22/// This struct is marked as non_exhaustive to make it unconstructable, thereby forcing the user to go through init().
23#[non_exhaustive]
24pub struct Nucleo<LED = LedDigital> {
25    /// The user LED.
26    pub user_led: LED,
27    /// The user button.
28    pub user_button: Button,
29    /// The virtual serial port through the on-board ST-Link debugger.
30    pub vcom: SerialPort,
31    /// Gives the ability to delay (blocking) with millisecond resolution.
32    pub delay: SysDelay,
33}
34
35impl<LED: LedBuilder> Nucleo<LED> {
36    /// Initialize the Nucleo board.
37    ///
38    /// Use this if you don't have any other hardware needs, since the peripheral struct is taken and dropped
39    /// internally and can't be used elsewhere. After the first call, always returns None.
40    ///
41    /// The LED generic parameter allows the user to specify what type of LED should control the LED hardware.
42    pub fn init() -> Option<Self> {
43        if let (Some(dp), Some(cp)) = (
44            stm32f4xx_hal::pac::Peripherals::take(),
45            cortex_m::peripheral::Peripherals::take(),
46        ) {
47            let rcc = dp.RCC.constrain();
48            let clocks = rcc.cfgr.freeze();
49
50            let gpioa = dp.GPIOA.split();
51            let gpioc = dp.GPIOC.split();
52
53            let user_led = LED::build(gpioa.pa5, dp.TIM2, &clocks);
54            let user_button = Button::new(gpioc.pc13);
55            let vcom = SerialPort::new(gpioa.pa2, gpioa.pa3, dp.USART2, &clocks);
56            let delay = cp.SYST.delay(&clocks);
57
58            Some(Self {
59                user_led,
60                user_button,
61                vcom,
62                delay,
63            })
64        } else {
65            None
66        }
67    }
68}