Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve otg_fs::Usb API. #322

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ version = "0.6.0"
optional = true

[dependencies.synopsys-usb-otg]
version = "0.2.4"
version = "0.3.0"
features = ["cortex-m", "fs"]
optional = true

Expand Down
34 changes: 11 additions & 23 deletions examples/otg_fs_serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ extern crate panic_semihosting;

use cortex_m_rt::entry;
use stm32l4xx_hal::gpio::Speed;
use stm32l4xx_hal::otg_fs::{UsbBus, USB};
use stm32l4xx_hal::otg_fs::{Usb, UsbBus};
use stm32l4xx_hal::prelude::*;
use stm32l4xx_hal::rcc::{
ClockSecuritySystem, CrystalBypass, MsiFreq, PllConfig, PllDivider, PllSource,
};
use stm32l4xx_hal::stm32::{Peripherals, CRS, PWR, RCC};
use stm32l4xx_hal::stm32::{Peripherals, CRS, RCC};
use usb_device::prelude::*;

/// Enable CRS (Clock Recovery System)
Expand All @@ -28,17 +28,6 @@ fn enable_crs() {
crs.cr.modify(|_, w| w.cen().set_bit());
}

/// Enables VddUSB power supply
fn enable_usb_pwr() {
// Enable PWR peripheral
let rcc = unsafe { &(*RCC::ptr()) };
rcc.apb1enr1.modify(|_, w| w.pwren().set_bit());

// Enable VddUSB
let pwr = unsafe { &*PWR::ptr() };
pwr.cr2.modify(|_, w| w.usv().set_bit());
}

static mut EP_MEMORY: [u32; 1024] = [0; 1024];

#[entry]
Expand Down Expand Up @@ -83,25 +72,24 @@ unsafe fn main() -> ! {

// Enable clock recovery system.
enable_crs();
// Enable USB power (and disable VddUSB power isolation).
enable_usb_pwr();

let mut gpioa = dp.GPIOA.split(&mut rcc.ahb2);

let usb = USB {
usb_global: dp.OTG_FS_GLOBAL,
usb_device: dp.OTG_FS_DEVICE,
usb_pwrclk: dp.OTG_FS_PWRCLK,
hclk: clocks.hclk(),
pin_dm: gpioa
let usb = Usb::new(
dp.OTG_FS_GLOBAL,
dp.OTG_FS_DEVICE,
dp.OTG_FS_PWRCLK,
gpioa
.pa11
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh)
.set_speed(Speed::VeryHigh),
pin_dp: gpioa
gpioa
.pa12
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh)
.set_speed(Speed::VeryHigh),
};
&mut pwr,
clocks,
);

let usb_bus = UsbBus::new(usb, &mut EP_MEMORY);

Expand Down
69 changes: 47 additions & 22 deletions src/otg_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,57 @@
//!
//! The STM32L4 series only supports the full-speed peripheral.

use crate::rcc::{Enable, Reset};
use crate::stm32;

use crate::gpio::{
gpioa::{PA11, PA12},
Alternate, PushPull,
};
use crate::time::Hertz;

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

pub struct USB {
pub usb_global: stm32::OTG_FS_GLOBAL,
pub usb_device: stm32::OTG_FS_DEVICE,
pub usb_pwrclk: stm32::OTG_FS_PWRCLK,
use crate::{
gpio::{
gpioa::{PA11, PA12},
Alternate, PushPull,
},
pac,
pwr::Pwr,
rcc::{Clocks, Enable, Reset},
time::Hertz,
};

pub struct Usb {
_global: pac::OTG_FS_GLOBAL,
_device: pac::OTG_FS_DEVICE,
_pwrclk: pac::OTG_FS_PWRCLK,
// TODO: check type
pub pin_dm: PA11<Alternate<PushPull, 10>>,
pub pin_dp: PA12<Alternate<PushPull, 10>>,
pub hclk: Hertz,
_dm: PA11<Alternate<PushPull, 10>>,
_dp: PA12<Alternate<PushPull, 10>>,
hclk: Hertz,
}

impl Usb {
pub fn new(
global: pac::OTG_FS_GLOBAL,
device: pac::OTG_FS_DEVICE,
pwrclk: pac::OTG_FS_PWRCLK,
dm: PA11<Alternate<PushPull, 10>>,
dp: PA12<Alternate<PushPull, 10>>,
pwr: &mut Pwr,
clocks: Clocks,
) -> Self {
pwr.cr2.reg().modify(|_, w| w.usv().set_bit());

Self {
_global: global,
_device: device,
_pwrclk: pwrclk,
_dm: dm,
_dp: dp,
hclk: clocks.hclk(),
}
}
}

unsafe impl Sync for USB {}
unsafe impl Sync for Usb {}

unsafe impl UsbPeripheral for USB {
const REGISTERS: *const () = stm32::OTG_FS_GLOBAL::ptr() as *const ();
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;
Expand All @@ -37,10 +62,10 @@ unsafe impl UsbPeripheral for USB {
fn enable() {
cortex_m::interrupt::free(|_| unsafe {
// Enable USB peripheral
stm32::OTG_FS_GLOBAL::enable_unchecked();
pac::OTG_FS_GLOBAL::enable_unchecked();

// Reset USB peripheral
stm32::OTG_FS_GLOBAL::reset_unchecked();
pac::OTG_FS_GLOBAL::reset_unchecked();
});
}

Expand All @@ -49,4 +74,4 @@ unsafe impl UsbPeripheral for USB {
}
}

pub type UsbBusType = UsbBus<USB>;
pub type UsbBusType = UsbBus<Usb>;