Skip to content

Commit 1bb0773

Browse files
Pål-Kristian Engstadengstad
authored andcommitted
Experimental support for Synopsys USB library.
The STM32L4 series are available in different lines: - STM32L4x1: Access Line. - STM32L4x2: USB Device. - STM32L4x3: USB Device, LCD. - STM32L4x5: USB OTG 2.0. - STM32L4x6: USB OTG 2.0, LCD. USB OTG is implemented by the Synopsys USB library, which is also used by a number of products in their STM32F-series chips. This commit adds support for full-speed (FS) USB support for the 4x5 and 4x6 lines.
1 parent 62e15e8 commit 1bb0773

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ version = "0.5.0"
5050
features = ["ram_access_2x16"]
5151
optional = true
5252

53+
[dependencies.synopsys-usb-otg]
54+
version = "0.2.4"
55+
features = ["cortex-m", "fs"]
56+
optional = true
57+
5358
[package.metadata.docs.rs]
5459
features = ["rt", "stm32l4x2", "stm32-usbd"]
5560

@@ -61,6 +66,7 @@ stm32l4x3 = ["stm32l4/stm32l4x3"]
6166
stm32l4x5 = ["stm32l4/stm32l4x5"]
6267
stm32l4x6 = ["stm32l4/stm32l4x6"]
6368
unproven = ["embedded-hal/unproven"]
69+
otg_fs = ["synopsys-usb-otg"]
6470

6571
[dev-dependencies]
6672
panic-halt = "0.2.0"

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ pub mod gpio;
117117
feature = "stm32l4x6"
118118
))]
119119
pub mod i2c;
120+
#[cfg(all(feature = "otg_fs",
121+
any(feature = "stm32l4x5", feature = "stm32l4x6")
122+
))]
123+
pub mod otg_fs;
120124
#[cfg(any(
121125
feature = "stm32l4x1",
122126
feature = "stm32l4x2",

src/otg_fs.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//! USB OTG full-speed peripheral
2+
//!
3+
//! The STM32L4 series only supports the full-speed peripheral.
4+
5+
use crate::stm32;
6+
7+
use crate::gpio::{
8+
gpioa::{PA11, PA12},
9+
Alternate, Floating, Input, AF10,
10+
};
11+
use crate::time::Hertz;
12+
13+
pub use synopsys_usb_otg::UsbBus;
14+
use synopsys_usb_otg::UsbPeripheral;
15+
16+
pub struct USB {
17+
pub usb_global: stm32::OTG_FS_GLOBAL,
18+
pub usb_device: stm32::OTG_FS_DEVICE,
19+
pub usb_pwrclk: stm32::OTG_FS_PWRCLK,
20+
pub pin_dm: PA11<Alternate<AF10, Input<Floating>>>,
21+
pub pin_dp: PA12<Alternate<AF10, Input<Floating>>>,
22+
pub hclk: Hertz,
23+
}
24+
25+
unsafe impl Sync for USB {}
26+
27+
unsafe impl UsbPeripheral for USB {
28+
const REGISTERS: *const () = stm32::OTG_FS_GLOBAL::ptr() as *const ();
29+
30+
const HIGH_SPEED: bool = false;
31+
const FIFO_DEPTH_WORDS: usize = 320;
32+
33+
const ENDPOINT_COUNT: usize = 6;
34+
35+
fn enable() {
36+
let rcc = unsafe { &*stm32::RCC::ptr() };
37+
38+
cortex_m::interrupt::free(|_| {
39+
// Enable USB peripheral
40+
rcc.ahb2enr.modify(|_, w| w.otgfsen().set_bit());
41+
let _ = rcc.ahb2enr.read().otgfsen().bit_is_set();
42+
43+
// Reset USB peripheral
44+
rcc.ahb2rstr.modify(|_, w| w.otgfsrst().set_bit());
45+
rcc.ahb2rstr.modify(|_, w| w.otgfsrst().clear_bit());
46+
});
47+
}
48+
49+
fn ahb_frequency_hz(&self) -> u32 {
50+
self.hclk.0
51+
}
52+
}
53+
54+
pub type UsbBusType = UsbBus<USB>;

src/rcc.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,7 @@ impl CFGR {
778778

779779
// MSI always starts on reset
780780
if self.msi.is_none() {
781-
rcc.cr
782-
.modify(|_, w| w.msion().clear_bit().msipllen().clear_bit())
781+
rcc.cr.modify(|_, w| w.msion().clear_bit().msipllen().clear_bit())
783782
}
784783

785784
//

0 commit comments

Comments
 (0)