Add stm32 USB OTG peripherals

This commit is contained in:
chemicstry
2022-02-08 00:32:49 +02:00
parent a4b4a7bcf9
commit db0d798b48
7 changed files with 427 additions and 1 deletions

View File

@ -23,6 +23,7 @@ futures = { version = "0.3.17", default-features = false, features = ["async-awa
rand_core = "0.6.3"
sdio-host = "0.5.0"
embedded-sdmmc = { git = "https://github.com/thalesfragoso/embedded-sdmmc-rs", branch = "async", optional = true }
synopsys-usb-otg = { version = "0.3", features = ["cortex-m"], optional = true }
critical-section = "0.2.5"
bare-metal = "1.0.0"
atomic-polyfill = "0.1.5"
@ -43,6 +44,11 @@ memory-x = ["stm32-metapac/memory-x"]
subghz = []
exti = []
# These features are exclusive
# synopsys-usb-otg does not support simultaneous FS and HS
usb-otg-fs = ["synopsys-usb-otg", "synopsys-usb-otg/fs"]
usb-otg-hs = ["synopsys-usb-otg", "synopsys-usb-otg/hs"]
# Features starting with `_` are for internal use only. They're not intended
# to be enabled by other crates, and are not covered by semver guarantees.
_time-driver = ["embassy/time-tick-32768hz"]

View File

@ -185,6 +185,14 @@ fn main() {
println!("cargo:rustc-cfg={}x{}", &chip_name[..9], &chip_name[10..11]);
}
// =======
// Features for targeting groups of chips
println!("cargo:rustc-cfg={}", &chip_name[..7]); // stm32f4
println!("cargo:rustc-cfg={}", &chip_name[..9]); // stm32f429
println!("cargo:rustc-cfg={}x", &chip_name[..8]); // stm32f42x
println!("cargo:rustc-cfg={}x{}", &chip_name[..7], &chip_name[8..9]); // stm32f4x9
// ========
// Handle time-driver-XXXX features.

View File

@ -50,6 +50,10 @@ pub mod sdmmc;
pub mod spi;
#[cfg(usart)]
pub mod usart;
#[cfg(feature = "usb-otg-fs")]
pub mod usb_otg_fs;
#[cfg(feature = "usb-otg-hs")]
pub mod usb_otg_hs;
#[cfg(feature = "subghz")]
pub mod subghz;

View File

@ -0,0 +1,159 @@
use crate::gpio::sealed::{AFType, Pin};
use crate::{peripherals, rcc::RccPeripheral};
use core::marker::PhantomData;
use embassy::util::Unborrow;
use embassy_hal_common::unborrow;
pub use embassy_hal_common::usb::*;
pub use synopsys_usb_otg::UsbBus;
use synopsys_usb_otg::UsbPeripheral;
pub struct UsbOtgFs<'d, T: Instance> {
phantom: PhantomData<&'d mut T>,
}
impl<'d, T: Instance> UsbOtgFs<'d, T> {
pub fn new(
_peri: impl Unborrow<Target = T> + 'd,
dp: impl Unborrow<Target = impl DpPin<T>> + 'd,
dm: impl Unborrow<Target = impl DmPin<T>> + 'd,
) -> Self {
unborrow!(dp, dm);
unsafe {
dp.set_as_af(dp.af_num(), AFType::OutputPushPull);
dm.set_as_af(dm.af_num(), AFType::OutputPushPull);
}
Self {
phantom: PhantomData,
}
}
}
impl<'d, T: Instance> Drop for UsbOtgFs<'d, T> {
fn drop(&mut self) {
T::reset();
T::disable();
}
}
unsafe impl<'d, T: Instance> Send for UsbOtgFs<'d, T> {}
unsafe impl<'d, T: Instance> Sync for UsbOtgFs<'d, T> {}
unsafe impl<'d, T: Instance> UsbPeripheral for UsbOtgFs<'d, T> {
const REGISTERS: *const () = T::REGISTERS;
const HIGH_SPEED: bool = false;
cfg_if::cfg_if! {
if #[cfg(stm32f1)] {
const FIFO_DEPTH_WORDS: usize = 128;
const ENDPOINT_COUNT: usize = 8;
} else if #[cfg(any(
stm32f2,
stm32f401,
stm32f405,
stm32f407,
stm32f411,
stm32f415,
stm32f417,
stm32f427,
stm32f429,
stm32f437,
stm32f439,
))] {
const FIFO_DEPTH_WORDS: usize = 320;
const ENDPOINT_COUNT: usize = 4;
} else if #[cfg(any(
stm32f412,
stm32f413,
stm32f423,
stm32f446,
stm32f469,
stm32f479,
stm32f7,
stm32l4,
stm32u5,
))] {
const FIFO_DEPTH_WORDS: usize = 320;
const ENDPOINT_COUNT: usize = 6;
} else if #[cfg(stm32g0x1)] {
const FIFO_DEPTH_WORDS: usize = 512;
const ENDPOINT_COUNT: usize = 8;
} else {
compile_error!("USB_OTG_FS peripheral is not supported by this chip. Disable \"usb-otg-fs\" feature or select a different chip.");
}
}
fn enable() {
<T as crate::rcc::sealed::RccPeripheral>::enable();
<T as crate::rcc::sealed::RccPeripheral>::reset();
}
fn ahb_frequency_hz(&self) -> u32 {
<T as crate::rcc::sealed::RccPeripheral>::frequency().0
}
}
pub(crate) mod sealed {
use super::*;
pub trait Instance {
const REGISTERS: *const ();
}
pub trait DpPin<T: Instance>: Pin {
fn af_num(&self) -> u8;
}
pub trait DmPin<T: Instance>: Pin {
fn af_num(&self) -> u8;
}
}
pub trait Instance: sealed::Instance + RccPeripheral {}
pub trait DpPin<T: Instance>: sealed::DpPin<T> {}
pub trait DmPin<T: Instance>: sealed::DmPin<T> {}
crate::pac::peripherals!(
(otgfs, $inst:ident) => {
impl sealed::Instance for crate::peripherals::$inst {
const REGISTERS: *const () = crate::pac::$inst.0 as *const ();
}
impl Instance for peripherals::$inst {}
};
);
crate::pac::interrupts!(
($inst:ident, otgfs, $block:ident, GLOBAL, $irq:ident) => {
unsafe impl USBInterrupt for crate::interrupt::$irq {}
};
);
macro_rules! impl_pin {
($inst:ident, $pin:ident, $signal:ident, $af:expr) => {
impl $signal<peripherals::$inst> for peripherals::$pin {}
impl sealed::$signal<peripherals::$inst> for peripherals::$pin {
fn af_num(&self) -> u8 {
$af
}
}
};
}
crate::pac::peripheral_pins!(
($inst:ident, otgfs, OTG_FS, $pin:ident, DP, $af:expr) => {
impl_pin!($inst, $pin, DpPin, $af);
};
($inst:ident, otgfs, OTG_FS, $pin:ident, DM, $af:expr) => {
impl_pin!($inst, $pin, DmPin, $af);
};
($inst:ident, otgfs, OTG_FS, $pin:ident, DP) => {
impl_pin!($inst, $pin, DpPin, 0);
};
($inst:ident, otgfs, OTG_FS, $pin:ident, DM) => {
impl_pin!($inst, $pin, DmPin, 0);
};
);

View File

@ -0,0 +1,147 @@
use crate::gpio::sealed::{AFType, Pin};
use crate::{peripherals, rcc::RccPeripheral};
use core::marker::PhantomData;
use embassy::util::Unborrow;
use embassy_hal_common::unborrow;
pub use embassy_hal_common::usb::*;
pub use synopsys_usb_otg::UsbBus;
use synopsys_usb_otg::UsbPeripheral;
pub struct UsbOtgHs<'d, T: Instance> {
phantom: PhantomData<&'d mut T>,
}
impl<'d, T: Instance> UsbOtgHs<'d, T> {
pub fn new(
_peri: impl Unborrow<Target = T> + 'd,
dp: impl Unborrow<Target = impl DpPin<T>> + 'd,
dm: impl Unborrow<Target = impl DmPin<T>> + 'd,
) -> Self {
unborrow!(dp, dm);
unsafe {
dp.set_as_af(dp.af_num(), AFType::OutputPushPull);
dm.set_as_af(dm.af_num(), AFType::OutputPushPull);
}
Self {
phantom: PhantomData,
}
}
}
impl<'d, T: Instance> Drop for UsbOtgHs<'d, T> {
fn drop(&mut self) {
T::reset();
T::disable();
}
}
unsafe impl<'d, T: Instance> Send for UsbOtgHs<'d, T> {}
unsafe impl<'d, T: Instance> Sync for UsbOtgHs<'d, T> {}
unsafe impl<'d, T: Instance> UsbPeripheral for UsbOtgHs<'d, T> {
const REGISTERS: *const () = T::REGISTERS;
const HIGH_SPEED: bool = true;
cfg_if::cfg_if! {
if #[cfg(any(
stm32f2,
stm32f405,
stm32f407,
stm32f415,
stm32f417,
stm32f427,
stm32f429,
stm32f437,
stm32f439,
))] {
const FIFO_DEPTH_WORDS: usize = 1024;
const ENDPOINT_COUNT: usize = 6;
} else if #[cfg(any(
stm32f446,
stm32f469,
stm32f479,
stm32f7,
stm32h7,
))] {
const FIFO_DEPTH_WORDS: usize = 1024;
const ENDPOINT_COUNT: usize = 9;
} else {
compile_error!("USB_OTG_HS peripheral is not supported by this chip. Disable \"usb-otg-hs\" feature or select a different chip.");
}
}
fn enable() {
<T as crate::rcc::sealed::RccPeripheral>::enable();
<T as crate::rcc::sealed::RccPeripheral>::reset();
}
fn ahb_frequency_hz(&self) -> u32 {
<T as crate::rcc::sealed::RccPeripheral>::frequency().0
}
}
pub(crate) mod sealed {
use super::*;
pub trait Instance {
const REGISTERS: *const ();
}
pub trait DpPin<T: Instance>: Pin {
fn af_num(&self) -> u8;
}
pub trait DmPin<T: Instance>: Pin {
fn af_num(&self) -> u8;
}
}
pub trait Instance: sealed::Instance + RccPeripheral {}
pub trait DpPin<T: Instance>: sealed::DpPin<T> {}
pub trait DmPin<T: Instance>: sealed::DmPin<T> {}
crate::pac::peripherals!(
(otghs, $inst:ident) => {
impl sealed::Instance for crate::peripherals::$inst {
const REGISTERS: *const () = crate::pac::$inst.0 as *const ();
}
impl Instance for peripherals::$inst {}
};
);
crate::pac::interrupts!(
($inst:ident, otghs, $block:ident, GLOBAL, $irq:ident) => {
unsafe impl USBInterrupt for crate::interrupt::$irq {}
};
);
macro_rules! impl_pin {
($inst:ident, $pin:ident, $signal:ident, $af:expr) => {
impl $signal<peripherals::$inst> for peripherals::$pin {}
impl sealed::$signal<peripherals::$inst> for peripherals::$pin {
fn af_num(&self) -> u8 {
$af
}
}
};
}
crate::pac::peripheral_pins!(
($inst:ident, otghs, OTG_HS, $pin:ident, DP, $af:expr) => {
impl_pin!($inst, $pin, DpPin, $af);
};
($inst:ident, otghs, OTG_HS, $pin:ident, DM, $af:expr) => {
impl_pin!($inst, $pin, DmPin, $af);
};
($inst:ident, otghs, OTG_HS, $pin:ident, DP) => {
impl_pin!($inst, $pin, DpPin, 0);
};
($inst:ident, otghs, OTG_HS, $pin:ident, DM) => {
impl_pin!($inst, $pin, DmPin, 0);
};
);