Add USB driver

This commit is contained in:
Vadim Kaushan 2019-11-13 14:28:43 +03:00
parent 9cf30e9961
commit 86c7f95c0e
No known key found for this signature in database
GPG Key ID: A501C5DF67C05C4E
3 changed files with 55 additions and 0 deletions

View File

@ -37,6 +37,7 @@ embedded-hal = { version = "0.2", features = ["unproven"] }
stm32f0 = "0.8"
nb = "0.1"
void = { version = "1.0", default-features = false }
stm32-usbd = { version = "0.5.0", features = ["ram_access_2x16"], optional = true }
[dev-dependencies]
panic-halt = "0.2"

View File

@ -69,5 +69,15 @@ pub mod timers;
feature = "stm32f098",
))]
pub mod tsc;
#[cfg(all(
feature = "stm32-usbd",
any(
feature = "stm32f042",
feature = "stm32f048",
feature = "stm32f072",
feature = "stm32f078",
)
))]
pub mod usb;
#[cfg(feature = "device-selected")]
pub mod watchdog;

44
src/usb.rs Normal file
View File

@ -0,0 +1,44 @@
//! USB peripheral
use crate::stm32::{RCC, USB};
use stm32_usbd::UsbPeripheral;
use crate::gpio::gpioa::{PA11, PA12};
use crate::gpio::{Floating, Input};
pub use stm32_usbd::UsbBus;
pub struct Peripheral {
pub usb: USB,
pub pin_dm: PA11<Input<Floating>>,
pub pin_dp: PA12<Input<Floating>>,
}
unsafe impl Sync for Peripheral {}
unsafe impl UsbPeripheral for Peripheral {
const REGISTERS: *const () = USB::ptr() as *const ();
const DP_PULL_UP_FEATURE: bool = true;
const EP_MEMORY: *const () = 0x4000_6000 as _;
const EP_MEMORY_SIZE: usize = 1024;
fn enable() {
let rcc = unsafe { (&*RCC::ptr()) };
cortex_m::interrupt::free(|_| {
// Enable USB peripheral
rcc.apb1enr.modify(|_, w| w.usben().set_bit());
// Reset USB peripheral
rcc.apb1rstr.modify(|_, w| w.usbrst().set_bit());
rcc.apb1rstr.modify(|_, w| w.usbrst().clear_bit());
});
}
fn startup_delay() {
// There is a chip specific startup delay. For STM32F103xx it's 1µs and this should wait for
// at least that long.
cortex_m::asm::delay(72);
}
}
pub type UsbBusType = UsbBus<Peripheral>;