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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//! USB support, including for simulated COM ports. This module is a thin wrapper required to work with
//! the `usbd` crate.
//!
//! Requires the `usbotg_fs` or `usbotg_hs` features.
//! Used on F4, L4x6, and H7. Others use the `usb` module.

// Based on `stm3h7xx-hal`

#[cfg(all(feature = "h7", feature = "usbotg_fs"))]
compile_error!("target only supports usbotg_hs feature");
#[cfg(all(not(feature = "h7"), feature = "usbotg_hs"))]
compile_error!("target only supports usbotg_fs feature");

use crate::{
    gpio::Pin,
    pac::{self, PWR, RCC},
};

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

cfg_if! {
    if #[cfg(feature = "usbotg_hs")] {
        // On older H7s (H743 etc), OTG1 maps to pisn PB14 and PB15.
        type Usb1GlobalRegType = pac::OTG1_HS_GLOBAL;
        type Usb1DeviceRegType = pac::OTG1_HS_DEVICE;
        type Usb1PwrclkRegType = pac::OTG1_HS_PWRCLK;

        cfg_if!{
        // Note that on STM32H743 and related MCUs, OTG2 is known as "USB-FS", which
        // can be a bit confusing. This refers to the USB periphral on pins PA11 and PA12
        // for those MCUs. On newer ones like H723, use OTG1, which in that case, still
        // maps to PA11 and PA12.
            if #[cfg(not(any(feature = "h735", feature = "h7b3")))] {
                type Usb2RegGlobalType = pac::OTG2_HS_GLOBAL;
                type Usb2RegDeviceType = pac::OTG2_HS_DEVICE;
                type Usb2RegPwrclkType = pac::OTG2_HS_PWRCLK;
            }
        }
    } else if #[cfg(feature = "usbotg_fs")] {
        // Eg F4 and L4x6.
        type Usb1GlobalRegType = pac::OTG_FS_GLOBAL;
        type Usb1DeviceRegType = pac::OTG_FS_DEVICE;
        type Usb1PwrclkRegType = pac::OTG_FS_PWRCLK;
    }
}

pub struct Usb1 {
    pub usb_global: Usb1GlobalRegType,
    pub usb_device: Usb1DeviceRegType,
    pub usb_pwrclk: Usb1PwrclkRegType,
    pub hclk: u32,
}

impl Usb1 {
    pub fn new(
        usb_global: Usb1GlobalRegType,
        usb_device: Usb1DeviceRegType,
        usb_pwrclk: Usb1PwrclkRegType,
        hclk: u32,
    ) -> Self {
        Self {
            usb_global,
            usb_device,
            usb_pwrclk,
            hclk,
        }
    }
}

cfg_if! {
    if #[cfg(all(feature = "h7", not(any(feature = "h735", feature = "h7b3"))))] {
        pub struct Usb2 {
            pub usb_global: Usb2RegGlobalType,
            pub usb_device: Usb2RegDeviceType,
            pub usb_pwrclk: Usb2RegPwrclkType,
            pub hclk: u32,
        }

        impl Usb2 {
            pub fn new(
                usb_global: Usb2RegGlobalType,
                usb_device: Usb2RegDeviceType,
                usb_pwrclk: Usb2RegPwrclkType,
                hclk: u32,
            ) -> Self {
                Self {
                    usb_global,
                    usb_device,
                    usb_pwrclk,
                    hclk,
                }
            }
        }
    }
}

macro_rules! usb_peripheral {
    ($USB:ident, $GLOBAL:ident, $clock_enable_reg:ident, $reset_reg:ident, $en:ident, $rst:ident) => {
        unsafe impl Sync for $USB {}

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

            #[cfg(feature = "usbotg_fs")]
            const HIGH_SPEED: bool = false;
            #[cfg(feature = "usbotg_hs")]
            const HIGH_SPEED: bool = true;

            const FIFO_DEPTH_WORDS: usize = 1024; // <-- do something here maybe?
            const ENDPOINT_COUNT: usize = 9; // <--

            fn enable() {
                let pwr = unsafe { &*PWR::ptr() };
                let rcc = unsafe { &*RCC::ptr() };

                // USB Regulator in BYPASS mode
                #[cfg(feature = "h7")]
                // pwr.cr3.modify(|_, w| w.usbregen().set_bit()); // todo ?
                pwr.cr3.modify(|_, w| w.usb33den().set_bit());
                #[cfg(feature = "l4x6")] // this was present in the usb module
                pwr.cr2.modify(|_, w| w.usv().set_bit());
                // The f4 doesn't seem to have anything similar

                // Enable USB peripheral
                rcc.$clock_enable_reg.modify(|_, w| w.$en().set_bit());

                // Reset USB peripheral
                rcc.$reset_reg.modify(|_, w| w.$rst().set_bit());
                rcc.$reset_reg.modify(|_, w| w.$rst().clear_bit());
            }

            fn ahb_frequency_hz(&self) -> u32 {
                // For correct operation, the AHB frequency should be higher
                // than 30MHz. See RM0433 Rev 7. Section 57.4.4. This is checked
                // by the UsbBus implementation in synopsys-usb-otg.

                self.hclk
            }
        }
    };
}

cfg_if! {
    if #[cfg(any(feature = "f4", feature = "l4"))] {
        usb_peripheral! {
            Usb1, Usb1GlobalRegType, ahb2enr, ahb2rstr, otgfsen, otgfsrst
        }
    } else if #[cfg(feature = "h7")] {
        usb_peripheral! {
            Usb1, Usb1GlobalRegType, ahb1enr, ahb1rstr, usb1otgen, usb1otgrst
        }
    }
}

pub type Usb1BusType = UsbBus<Usb1>;

cfg_if! {
if #[cfg(all(feature = "h7", not(any(feature = "h735", feature = "h7b3"))))] {
    usb_peripheral! {
        Usb2, Usb2RegGlobalType, ahb1enr, ahb1rstr, usb2otgen, usb2otgrst
    }

    pub type Usb2BusType = UsbBus<Usb2>;
}
}

cfg_if! {
    if #[cfg(feature = "h7")] {
        pub struct Usb1Ulpi {
            pub usb_global: Usb1GlobalRegType,
            pub usb_device: Usb1DeviceRegType,
            pub usb_pwrclk: Usb1PwrclkRegType,
            pub prec: u32, // todo: What should this be? Maybe d2ccip2 / cdccip2 ?
            pub hclk: u32,
            pub ulpi_clk: Pin,
            pub ulpi_dir: Pin,
            pub ulpi_nxt: Pin,
            pub ulpi_stp: Pin,
            pub ulpi_d0: Pin,
            pub ulpi_d1: Pin,
            pub ulpi_d2: Pin,
            pub ulpi_d3: Pin,
            pub ulpi_d4: Pin,
            pub ulpi_d5: Pin,
            pub ulpi_d6: Pin,
            pub ulpi_d7: Pin,
        }

        unsafe impl Sync for Usb1Ulpi {}


        unsafe impl UsbPeripheral for Usb1Ulpi {
            const REGISTERS: *const () = Usb1GlobalRegType::ptr() as *const ();

            const HIGH_SPEED: bool = true;
            const FIFO_DEPTH_WORDS: usize = 1024;
            const ENDPOINT_COUNT: usize = 9;

            fn enable() {
                let rcc = unsafe { &*pac::RCC::ptr() };

                cortex_m::interrupt::free(|_| {
                    // Enable USB peripheral
                    rcc.ahb1enr.modify(|_, w| w.usb1otgen().enabled());

                    // Enable ULPI Clock
                    rcc.ahb1enr.modify(|_, w| w.usb1ulpien().enabled());

                    // Reset USB peripheral
                    rcc.ahb1rstr.modify(|_, w| w.usb1otgrst().set_bit());
                    rcc.ahb1rstr.modify(|_, w| w.usb1otgrst().clear_bit());
                });
            }

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

            fn phy_type(&self) -> synopsys_usb_otg::PhyType {
                synopsys_usb_otg::PhyType::ExternalHighSpeed
            }
        }
        pub type Usb1UlpiBusType = UsbBus<Usb1Ulpi>;
    }
}