1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//! USB OTG full-speed peripheral
//!
//! Requires the `usb_fs` feature.
//! Only one of the `usb_fs`/`usb_hs` features can be selected at the same time.

use crate::pac;

use crate::gpio::alt::otg_fs as alt;
use crate::rcc::{Clocks, Enable, Reset};
use fugit::HertzU32 as Hertz;

pub use synopsys_usb_otg::UsbBus;
use synopsys_usb_otg::UsbPeripheral;

pub struct USB {
    pub usb_global: pac::OTG_FS_GLOBAL,
    pub usb_device: pac::OTG_FS_DEVICE,
    pub usb_pwrclk: pac::OTG_FS_PWRCLK,
    pub pin_dm: alt::Dm,
    pub pin_dp: alt::Dp,
    pub hclk: Hertz,
}

impl USB {
    pub fn new(
        periphs: (pac::OTG_FS_GLOBAL, pac::OTG_FS_DEVICE, pac::OTG_FS_PWRCLK),
        pins: (impl Into<alt::Dm>, impl Into<alt::Dp>),
        clocks: &Clocks,
    ) -> Self {
        Self {
            usb_global: periphs.0,
            usb_device: periphs.1,
            usb_pwrclk: periphs.2,
            pin_dm: pins.0.into(),
            pin_dp: pins.1.into(),
            hclk: clocks.hclk(),
        }
    }
}

unsafe impl Sync for USB {}

unsafe impl UsbPeripheral for USB {
    const REGISTERS: *const () = pac::OTG_FS_GLOBAL::ptr() as *const ();

    const HIGH_SPEED: bool = false;
    const FIFO_DEPTH_WORDS: usize = 320;

    #[cfg(any(
        feature = "gpio-f401",
        feature = "gpio-f411",
        feature = "gpio-f417",
        feature = "gpio-f427",
    ))]
    const ENDPOINT_COUNT: usize = 4;
    #[cfg(any(
        feature = "gpio-f412",
        feature = "gpio-f413",
        feature = "gpio-f446",
        feature = "gpio-f469",
    ))]
    const ENDPOINT_COUNT: usize = 6;

    fn enable() {
        cortex_m::interrupt::free(|_| {
            // Enable USB peripheral
            unsafe {
                pac::OTG_FS_GLOBAL::enable_unchecked();
                pac::OTG_FS_GLOBAL::reset_unchecked();
            }
        });
    }

    fn ahb_frequency_hz(&self) -> u32 {
        self.hclk.raw()
    }
}

pub type UsbBusType = UsbBus<USB>;