Skip to content

Commit d3dac9b

Browse files
committed
Improve otg_fs::Usb API.
1 parent e64497b commit d3dac9b

File tree

3 files changed

+58
-45
lines changed

3 files changed

+58
-45
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ version = "0.6.0"
5454
optional = true
5555

5656
[dependencies.synopsys-usb-otg]
57-
version = "0.2.4"
57+
version = "0.3.0"
5858
features = ["cortex-m", "fs"]
5959
optional = true
6060

examples/otg_fs_serial.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extern crate panic_semihosting;
88

99
use cortex_m_rt::entry;
1010
use stm32l4xx_hal::gpio::Speed;
11-
use stm32l4xx_hal::otg_fs::{UsbBus, USB};
11+
use stm32l4xx_hal::otg_fs::{Usb, UsbBus};
1212
use stm32l4xx_hal::prelude::*;
1313
use stm32l4xx_hal::rcc::{
1414
ClockSecuritySystem, CrystalBypass, MsiFreq, PllConfig, PllDivider, PllSource,
@@ -28,17 +28,6 @@ fn enable_crs() {
2828
crs.cr.modify(|_, w| w.cen().set_bit());
2929
}
3030

31-
/// Enables VddUSB power supply
32-
fn enable_usb_pwr() {
33-
// Enable PWR peripheral
34-
let rcc = unsafe { &(*RCC::ptr()) };
35-
rcc.apb1enr1.modify(|_, w| w.pwren().set_bit());
36-
37-
// Enable VddUSB
38-
let pwr = unsafe { &*PWR::ptr() };
39-
pwr.cr2.modify(|_, w| w.usv().set_bit());
40-
}
41-
4231
static mut EP_MEMORY: [u32; 1024] = [0; 1024];
4332

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

8473
// Enable clock recovery system.
8574
enable_crs();
86-
// Enable USB power (and disable VddUSB power isolation).
87-
enable_usb_pwr();
8875

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

91-
let usb = USB {
92-
usb_global: dp.OTG_FS_GLOBAL,
93-
usb_device: dp.OTG_FS_DEVICE,
94-
usb_pwrclk: dp.OTG_FS_PWRCLK,
95-
hclk: clocks.hclk(),
96-
pin_dm: gpioa
78+
let usb = Usb::new(
79+
dp.OTG_FS_GLOBAL,
80+
dp.OTG_FS_DEVICE,
81+
dp.OTG_FS_PWRCLK,
82+
gpioa
9783
.pa11
9884
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh)
9985
.set_speed(Speed::VeryHigh),
100-
pin_dp: gpioa
86+
gpioa
10187
.pa12
10288
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh)
10389
.set_speed(Speed::VeryHigh),
104-
};
90+
&mut pwr,
91+
clocks.hclk(),
92+
);
10593

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

src/otg_fs.rs

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,57 @@
22
//!
33
//! The STM32L4 series only supports the full-speed peripheral.
44
5-
use crate::rcc::{Enable, Reset};
6-
use crate::stm32;
7-
8-
use crate::gpio::{
9-
gpioa::{PA11, PA12},
10-
Alternate, PushPull,
11-
};
12-
use crate::time::Hertz;
13-
145
pub use synopsys_usb_otg::UsbBus;
156
use synopsys_usb_otg::UsbPeripheral;
167

17-
pub struct USB {
18-
pub usb_global: stm32::OTG_FS_GLOBAL,
19-
pub usb_device: stm32::OTG_FS_DEVICE,
20-
pub usb_pwrclk: stm32::OTG_FS_PWRCLK,
8+
use crate::{
9+
gpio::{
10+
gpioa::{PA11, PA12},
11+
Alternate, PushPull,
12+
},
13+
pac,
14+
pwr::Pwr,
15+
rcc::{Clocks, Enable, Reset},
16+
time::Hertz,
17+
};
18+
19+
pub struct Usb {
20+
usb_global: pac::OTG_FS_GLOBAL,
21+
usb_device: pac::OTG_FS_DEVICE,
22+
usb_pwrclk: pac::OTG_FS_PWRCLK,
2123
// TODO: check type
22-
pub pin_dm: PA11<Alternate<PushPull, 10>>,
23-
pub pin_dp: PA12<Alternate<PushPull, 10>>,
24-
pub hclk: Hertz,
24+
pin_dm: PA11<Alternate<PushPull, 10>>,
25+
pin_dp: PA12<Alternate<PushPull, 10>>,
26+
hclk: Hertz,
27+
}
28+
29+
impl Usb {
30+
pub fn new(
31+
global: pac::OTG_FS_GLOBAL,
32+
device: pac::OTG_FS_DEVICE,
33+
pwrclk: pac::OTG_FS_PWRCLK,
34+
dm: PA11<Alternate<PushPull, 10>>,
35+
dp: PA12<Alternate<PushPull, 10>>,
36+
pwr: &mut Pwr,
37+
clocks: Clocks,
38+
) -> Self {
39+
pwr.cr2.reg().modify(|_, w| w.usv().set_bit());
40+
41+
Self {
42+
usb_global: global,
43+
usb_device: device,
44+
usb_pwrclk: pwrclk,
45+
pin_dm: dm,
46+
pin_dp: dp,
47+
hclk: clocks.hclk(),
48+
}
49+
}
2550
}
2651

27-
unsafe impl Sync for USB {}
52+
unsafe impl Sync for Usb {}
2853

29-
unsafe impl UsbPeripheral for USB {
30-
const REGISTERS: *const () = stm32::OTG_FS_GLOBAL::ptr() as *const ();
54+
unsafe impl UsbPeripheral for Usb {
55+
const REGISTERS: *const () = pac::OTG_FS_GLOBAL::ptr() as *const ();
3156

3257
const HIGH_SPEED: bool = false;
3358
const FIFO_DEPTH_WORDS: usize = 320;
@@ -37,10 +62,10 @@ unsafe impl UsbPeripheral for USB {
3762
fn enable() {
3863
cortex_m::interrupt::free(|_| unsafe {
3964
// Enable USB peripheral
40-
stm32::OTG_FS_GLOBAL::enable_unchecked();
65+
pac::OTG_FS_GLOBAL::enable_unchecked();
4166

4267
// Reset USB peripheral
43-
stm32::OTG_FS_GLOBAL::reset_unchecked();
68+
pac::OTG_FS_GLOBAL::reset_unchecked();
4469
});
4570
}
4671

@@ -49,4 +74,4 @@ unsafe impl UsbPeripheral for USB {
4974
}
5075
}
5176

52-
pub type UsbBusType = UsbBus<USB>;
77+
pub type UsbBusType = UsbBus<Usb>;

0 commit comments

Comments
 (0)