Skip to content

Commit 1330097

Browse files
authored
Merge pull request #59 from braun-embedded/usb
Improve USB module
2 parents a0ce654 + a621db3 commit 1330097

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

examples/usb_serial.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
extern crate panic_semihosting;
66

77
use cortex_m_rt::entry;
8-
use stm32l0xx_hal::usb::{Peripheral, UsbBus};
8+
use stm32l0xx_hal::usb::{USB, UsbBus};
99
use stm32l0xx_hal::{pac, prelude::*, rcc, syscfg::SYSCFG};
1010
use usb_device::prelude::*;
1111
use usbd_serial::{SerialPort, USB_CLASS_CDC};
@@ -16,15 +16,11 @@ fn main() -> ! {
1616

1717
let mut rcc = dp.RCC.freeze(rcc::Config::hsi16());
1818
let mut syscfg = SYSCFG::new(dp.SYSCFG, &mut rcc);
19-
rcc.enable_hsi48(&mut syscfg, dp.CRS);
19+
let hsi48 = rcc.enable_hsi48(&mut syscfg, dp.CRS);
2020

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

23-
let usb = Peripheral {
24-
usb: dp.USB,
25-
pin_dm: gpioa.pa11,
26-
pin_dp: gpioa.pa12,
27-
};
23+
let usb = USB::new(dp.USB, gpioa.pa11, gpioa.pa12, hsi48);
2824
let usb_bus = UsbBus::new(usb);
2925

3026
let mut serial = SerialPort::new(&usb_bus);

src/usb.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,36 @@
1212
//! Please check out the USB examples in the `examples/` directory to see how it
1313
//! fits together.
1414
15-
use crate::stm32::{RCC, USB};
15+
use crate::{
16+
pac::{self, RCC},
17+
rcc::HSI48,
18+
};
1619
use stm32_usbd::UsbPeripheral;
1720

1821
use crate::gpio::gpioa::{PA11, PA12};
1922
use crate::gpio::{Floating, Input};
2023
pub use stm32_usbd::UsbBus;
2124

22-
pub struct Peripheral {
23-
pub usb: USB,
24-
pub pin_dm: PA11<Input<Floating>>,
25-
pub pin_dp: PA12<Input<Floating>>,
25+
26+
pub struct USB(());
27+
28+
impl USB {
29+
pub fn new(
30+
_: pac::USB,
31+
_dm: PA11<Input<Floating>>,
32+
_dp: PA12<Input<Floating>>,
33+
_: HSI48,
34+
)
35+
-> Self
36+
{
37+
Self(())
38+
}
2639
}
2740

28-
unsafe impl Sync for Peripheral {}
41+
unsafe impl Sync for USB {}
2942

30-
unsafe impl UsbPeripheral for Peripheral {
31-
const REGISTERS: *const () = USB::ptr() as *const ();
43+
unsafe impl UsbPeripheral for USB {
44+
const REGISTERS: *const () = pac::USB::ptr() as *const ();
3245
const DP_PULL_UP_FEATURE: bool = true;
3346
const EP_MEMORY: *const () = 0x4000_6000 as _;
3447
const EP_MEMORY_SIZE: usize = 1024;
@@ -53,4 +66,4 @@ unsafe impl UsbPeripheral for Peripheral {
5366
}
5467
}
5568

56-
pub type UsbBusType = UsbBus<Peripheral>;
69+
pub type UsbBusType = UsbBus<USB>;

0 commit comments

Comments
 (0)