diff options
| author | chemicstry <[email protected]> | 2022-02-08 00:32:49 +0200 |
|---|---|---|
| committer | chemicstry <[email protected]> | 2022-02-08 01:46:32 +0200 |
| commit | db0d798b4896e97bf40e42cb54344e7a3f77a5c8 (patch) | |
| tree | 1af218e26c9351fa483130d1a46f2b29a3094443 /embassy-stm32/src | |
| parent | a4b4a7bcf995c9dcc32a2944887be853df604a1c (diff) | |
Add stm32 USB OTG peripherals
Diffstat (limited to 'embassy-stm32/src')
| -rw-r--r-- | embassy-stm32/src/lib.rs | 4 | ||||
| -rw-r--r-- | embassy-stm32/src/usb_otg_fs.rs | 159 | ||||
| -rw-r--r-- | embassy-stm32/src/usb_otg_hs.rs | 147 |
3 files changed, 310 insertions, 0 deletions
diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index dfc027733..3b9a61d45 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs | |||
| @@ -50,6 +50,10 @@ pub mod sdmmc; | |||
| 50 | pub mod spi; | 50 | pub mod spi; |
| 51 | #[cfg(usart)] | 51 | #[cfg(usart)] |
| 52 | pub mod usart; | 52 | pub mod usart; |
| 53 | #[cfg(feature = "usb-otg-fs")] | ||
| 54 | pub mod usb_otg_fs; | ||
| 55 | #[cfg(feature = "usb-otg-hs")] | ||
| 56 | pub mod usb_otg_hs; | ||
| 53 | 57 | ||
| 54 | #[cfg(feature = "subghz")] | 58 | #[cfg(feature = "subghz")] |
| 55 | pub mod subghz; | 59 | pub mod subghz; |
diff --git a/embassy-stm32/src/usb_otg_fs.rs b/embassy-stm32/src/usb_otg_fs.rs new file mode 100644 index 000000000..3e0cadb11 --- /dev/null +++ b/embassy-stm32/src/usb_otg_fs.rs | |||
| @@ -0,0 +1,159 @@ | |||
| 1 | use crate::gpio::sealed::{AFType, Pin}; | ||
| 2 | use crate::{peripherals, rcc::RccPeripheral}; | ||
| 3 | use core::marker::PhantomData; | ||
| 4 | use embassy::util::Unborrow; | ||
| 5 | use embassy_hal_common::unborrow; | ||
| 6 | pub use embassy_hal_common::usb::*; | ||
| 7 | pub use synopsys_usb_otg::UsbBus; | ||
| 8 | use synopsys_usb_otg::UsbPeripheral; | ||
| 9 | |||
| 10 | pub struct UsbOtgFs<'d, T: Instance> { | ||
| 11 | phantom: PhantomData<&'d mut T>, | ||
| 12 | } | ||
| 13 | |||
| 14 | impl<'d, T: Instance> UsbOtgFs<'d, T> { | ||
| 15 | pub fn new( | ||
| 16 | _peri: impl Unborrow<Target = T> + 'd, | ||
| 17 | dp: impl Unborrow<Target = impl DpPin<T>> + 'd, | ||
| 18 | dm: impl Unborrow<Target = impl DmPin<T>> + 'd, | ||
| 19 | ) -> Self { | ||
| 20 | unborrow!(dp, dm); | ||
| 21 | |||
| 22 | unsafe { | ||
| 23 | dp.set_as_af(dp.af_num(), AFType::OutputPushPull); | ||
| 24 | dm.set_as_af(dm.af_num(), AFType::OutputPushPull); | ||
| 25 | } | ||
| 26 | |||
| 27 | Self { | ||
| 28 | phantom: PhantomData, | ||
| 29 | } | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | impl<'d, T: Instance> Drop for UsbOtgFs<'d, T> { | ||
| 34 | fn drop(&mut self) { | ||
| 35 | T::reset(); | ||
| 36 | T::disable(); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | unsafe impl<'d, T: Instance> Send for UsbOtgFs<'d, T> {} | ||
| 41 | unsafe impl<'d, T: Instance> Sync for UsbOtgFs<'d, T> {} | ||
| 42 | |||
| 43 | unsafe impl<'d, T: Instance> UsbPeripheral for UsbOtgFs<'d, T> { | ||
| 44 | const REGISTERS: *const () = T::REGISTERS; | ||
| 45 | |||
| 46 | const HIGH_SPEED: bool = false; | ||
| 47 | |||
| 48 | cfg_if::cfg_if! { | ||
| 49 | if #[cfg(stm32f1)] { | ||
| 50 | const FIFO_DEPTH_WORDS: usize = 128; | ||
| 51 | const ENDPOINT_COUNT: usize = 8; | ||
| 52 | } else if #[cfg(any( | ||
| 53 | stm32f2, | ||
| 54 | stm32f401, | ||
| 55 | stm32f405, | ||
| 56 | stm32f407, | ||
| 57 | stm32f411, | ||
| 58 | stm32f415, | ||
| 59 | stm32f417, | ||
| 60 | stm32f427, | ||
| 61 | stm32f429, | ||
| 62 | stm32f437, | ||
| 63 | stm32f439, | ||
| 64 | ))] { | ||
| 65 | const FIFO_DEPTH_WORDS: usize = 320; | ||
| 66 | const ENDPOINT_COUNT: usize = 4; | ||
| 67 | } else if #[cfg(any( | ||
| 68 | stm32f412, | ||
| 69 | stm32f413, | ||
| 70 | stm32f423, | ||
| 71 | stm32f446, | ||
| 72 | stm32f469, | ||
| 73 | stm32f479, | ||
| 74 | stm32f7, | ||
| 75 | stm32l4, | ||
| 76 | stm32u5, | ||
| 77 | ))] { | ||
| 78 | const FIFO_DEPTH_WORDS: usize = 320; | ||
| 79 | const ENDPOINT_COUNT: usize = 6; | ||
| 80 | } else if #[cfg(stm32g0x1)] { | ||
| 81 | const FIFO_DEPTH_WORDS: usize = 512; | ||
| 82 | const ENDPOINT_COUNT: usize = 8; | ||
| 83 | } else { | ||
| 84 | compile_error!("USB_OTG_FS peripheral is not supported by this chip. Disable \"usb-otg-fs\" feature or select a different chip."); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | fn enable() { | ||
| 89 | <T as crate::rcc::sealed::RccPeripheral>::enable(); | ||
| 90 | <T as crate::rcc::sealed::RccPeripheral>::reset(); | ||
| 91 | } | ||
| 92 | |||
| 93 | fn ahb_frequency_hz(&self) -> u32 { | ||
| 94 | <T as crate::rcc::sealed::RccPeripheral>::frequency().0 | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | pub(crate) mod sealed { | ||
| 99 | use super::*; | ||
| 100 | |||
| 101 | pub trait Instance { | ||
| 102 | const REGISTERS: *const (); | ||
| 103 | } | ||
| 104 | |||
| 105 | pub trait DpPin<T: Instance>: Pin { | ||
| 106 | fn af_num(&self) -> u8; | ||
| 107 | } | ||
| 108 | |||
| 109 | pub trait DmPin<T: Instance>: Pin { | ||
| 110 | fn af_num(&self) -> u8; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | pub trait Instance: sealed::Instance + RccPeripheral {} | ||
| 115 | pub trait DpPin<T: Instance>: sealed::DpPin<T> {} | ||
| 116 | pub trait DmPin<T: Instance>: sealed::DmPin<T> {} | ||
| 117 | |||
| 118 | crate::pac::peripherals!( | ||
| 119 | (otgfs, $inst:ident) => { | ||
| 120 | impl sealed::Instance for crate::peripherals::$inst { | ||
| 121 | const REGISTERS: *const () = crate::pac::$inst.0 as *const (); | ||
| 122 | } | ||
| 123 | |||
| 124 | impl Instance for peripherals::$inst {} | ||
| 125 | }; | ||
| 126 | ); | ||
| 127 | |||
| 128 | crate::pac::interrupts!( | ||
| 129 | ($inst:ident, otgfs, $block:ident, GLOBAL, $irq:ident) => { | ||
| 130 | unsafe impl USBInterrupt for crate::interrupt::$irq {} | ||
| 131 | }; | ||
| 132 | ); | ||
| 133 | |||
| 134 | macro_rules! impl_pin { | ||
| 135 | ($inst:ident, $pin:ident, $signal:ident, $af:expr) => { | ||
| 136 | impl $signal<peripherals::$inst> for peripherals::$pin {} | ||
| 137 | |||
| 138 | impl sealed::$signal<peripherals::$inst> for peripherals::$pin { | ||
| 139 | fn af_num(&self) -> u8 { | ||
| 140 | $af | ||
| 141 | } | ||
| 142 | } | ||
| 143 | }; | ||
| 144 | } | ||
| 145 | |||
| 146 | crate::pac::peripheral_pins!( | ||
| 147 | ($inst:ident, otgfs, OTG_FS, $pin:ident, DP, $af:expr) => { | ||
| 148 | impl_pin!($inst, $pin, DpPin, $af); | ||
| 149 | }; | ||
| 150 | ($inst:ident, otgfs, OTG_FS, $pin:ident, DM, $af:expr) => { | ||
| 151 | impl_pin!($inst, $pin, DmPin, $af); | ||
| 152 | }; | ||
| 153 | ($inst:ident, otgfs, OTG_FS, $pin:ident, DP) => { | ||
| 154 | impl_pin!($inst, $pin, DpPin, 0); | ||
| 155 | }; | ||
| 156 | ($inst:ident, otgfs, OTG_FS, $pin:ident, DM) => { | ||
| 157 | impl_pin!($inst, $pin, DmPin, 0); | ||
| 158 | }; | ||
| 159 | ); | ||
diff --git a/embassy-stm32/src/usb_otg_hs.rs b/embassy-stm32/src/usb_otg_hs.rs new file mode 100644 index 000000000..8de4601e1 --- /dev/null +++ b/embassy-stm32/src/usb_otg_hs.rs | |||
| @@ -0,0 +1,147 @@ | |||
| 1 | use crate::gpio::sealed::{AFType, Pin}; | ||
| 2 | use crate::{peripherals, rcc::RccPeripheral}; | ||
| 3 | use core::marker::PhantomData; | ||
| 4 | use embassy::util::Unborrow; | ||
| 5 | use embassy_hal_common::unborrow; | ||
| 6 | pub use embassy_hal_common::usb::*; | ||
| 7 | pub use synopsys_usb_otg::UsbBus; | ||
| 8 | use synopsys_usb_otg::UsbPeripheral; | ||
| 9 | |||
| 10 | pub struct UsbOtgHs<'d, T: Instance> { | ||
| 11 | phantom: PhantomData<&'d mut T>, | ||
| 12 | } | ||
| 13 | |||
| 14 | impl<'d, T: Instance> UsbOtgHs<'d, T> { | ||
| 15 | pub fn new( | ||
| 16 | _peri: impl Unborrow<Target = T> + 'd, | ||
| 17 | dp: impl Unborrow<Target = impl DpPin<T>> + 'd, | ||
| 18 | dm: impl Unborrow<Target = impl DmPin<T>> + 'd, | ||
| 19 | ) -> Self { | ||
| 20 | unborrow!(dp, dm); | ||
| 21 | |||
| 22 | unsafe { | ||
| 23 | dp.set_as_af(dp.af_num(), AFType::OutputPushPull); | ||
| 24 | dm.set_as_af(dm.af_num(), AFType::OutputPushPull); | ||
| 25 | } | ||
| 26 | |||
| 27 | Self { | ||
| 28 | phantom: PhantomData, | ||
| 29 | } | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | impl<'d, T: Instance> Drop for UsbOtgHs<'d, T> { | ||
| 34 | fn drop(&mut self) { | ||
| 35 | T::reset(); | ||
| 36 | T::disable(); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | unsafe impl<'d, T: Instance> Send for UsbOtgHs<'d, T> {} | ||
| 41 | unsafe impl<'d, T: Instance> Sync for UsbOtgHs<'d, T> {} | ||
| 42 | |||
| 43 | unsafe impl<'d, T: Instance> UsbPeripheral for UsbOtgHs<'d, T> { | ||
| 44 | const REGISTERS: *const () = T::REGISTERS; | ||
| 45 | |||
| 46 | const HIGH_SPEED: bool = true; | ||
| 47 | |||
| 48 | cfg_if::cfg_if! { | ||
| 49 | if #[cfg(any( | ||
| 50 | stm32f2, | ||
| 51 | stm32f405, | ||
| 52 | stm32f407, | ||
| 53 | stm32f415, | ||
| 54 | stm32f417, | ||
| 55 | stm32f427, | ||
| 56 | stm32f429, | ||
| 57 | stm32f437, | ||
| 58 | stm32f439, | ||
| 59 | ))] { | ||
| 60 | const FIFO_DEPTH_WORDS: usize = 1024; | ||
| 61 | const ENDPOINT_COUNT: usize = 6; | ||
| 62 | } else if #[cfg(any( | ||
| 63 | stm32f446, | ||
| 64 | stm32f469, | ||
| 65 | stm32f479, | ||
| 66 | stm32f7, | ||
| 67 | stm32h7, | ||
| 68 | ))] { | ||
| 69 | const FIFO_DEPTH_WORDS: usize = 1024; | ||
| 70 | const ENDPOINT_COUNT: usize = 9; | ||
| 71 | } else { | ||
| 72 | compile_error!("USB_OTG_HS peripheral is not supported by this chip. Disable \"usb-otg-hs\" feature or select a different chip."); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | fn enable() { | ||
| 77 | <T as crate::rcc::sealed::RccPeripheral>::enable(); | ||
| 78 | <T as crate::rcc::sealed::RccPeripheral>::reset(); | ||
| 79 | } | ||
| 80 | |||
| 81 | fn ahb_frequency_hz(&self) -> u32 { | ||
| 82 | <T as crate::rcc::sealed::RccPeripheral>::frequency().0 | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | pub(crate) mod sealed { | ||
| 87 | use super::*; | ||
| 88 | |||
| 89 | pub trait Instance { | ||
| 90 | const REGISTERS: *const (); | ||
| 91 | } | ||
| 92 | |||
| 93 | pub trait DpPin<T: Instance>: Pin { | ||
| 94 | fn af_num(&self) -> u8; | ||
| 95 | } | ||
| 96 | |||
| 97 | pub trait DmPin<T: Instance>: Pin { | ||
| 98 | fn af_num(&self) -> u8; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | pub trait Instance: sealed::Instance + RccPeripheral {} | ||
| 103 | pub trait DpPin<T: Instance>: sealed::DpPin<T> {} | ||
| 104 | pub trait DmPin<T: Instance>: sealed::DmPin<T> {} | ||
| 105 | |||
| 106 | crate::pac::peripherals!( | ||
| 107 | (otghs, $inst:ident) => { | ||
| 108 | impl sealed::Instance for crate::peripherals::$inst { | ||
| 109 | const REGISTERS: *const () = crate::pac::$inst.0 as *const (); | ||
| 110 | } | ||
| 111 | |||
| 112 | impl Instance for peripherals::$inst {} | ||
| 113 | }; | ||
| 114 | ); | ||
| 115 | |||
| 116 | crate::pac::interrupts!( | ||
| 117 | ($inst:ident, otghs, $block:ident, GLOBAL, $irq:ident) => { | ||
| 118 | unsafe impl USBInterrupt for crate::interrupt::$irq {} | ||
| 119 | }; | ||
| 120 | ); | ||
| 121 | |||
| 122 | macro_rules! impl_pin { | ||
| 123 | ($inst:ident, $pin:ident, $signal:ident, $af:expr) => { | ||
| 124 | impl $signal<peripherals::$inst> for peripherals::$pin {} | ||
| 125 | |||
| 126 | impl sealed::$signal<peripherals::$inst> for peripherals::$pin { | ||
| 127 | fn af_num(&self) -> u8 { | ||
| 128 | $af | ||
| 129 | } | ||
| 130 | } | ||
| 131 | }; | ||
| 132 | } | ||
| 133 | |||
| 134 | crate::pac::peripheral_pins!( | ||
| 135 | ($inst:ident, otghs, OTG_HS, $pin:ident, DP, $af:expr) => { | ||
| 136 | impl_pin!($inst, $pin, DpPin, $af); | ||
| 137 | }; | ||
| 138 | ($inst:ident, otghs, OTG_HS, $pin:ident, DM, $af:expr) => { | ||
| 139 | impl_pin!($inst, $pin, DmPin, $af); | ||
| 140 | }; | ||
| 141 | ($inst:ident, otghs, OTG_HS, $pin:ident, DP) => { | ||
| 142 | impl_pin!($inst, $pin, DpPin, 0); | ||
| 143 | }; | ||
| 144 | ($inst:ident, otghs, OTG_HS, $pin:ident, DM) => { | ||
| 145 | impl_pin!($inst, $pin, DmPin, 0); | ||
| 146 | }; | ||
| 147 | ); | ||
