Skip to content

Commit d77c1a9

Browse files
committed
USB: Add support for external ULPI PHYs
1 parent 5471426 commit d77c1a9

File tree

3 files changed

+109
-7
lines changed

3 files changed

+109
-7
lines changed

.github/workflows/clippy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ jobs:
1919
- uses: actions-rs/clippy-check@v1
2020
with:
2121
token: ${{ secrets.GITHUB_TOKEN }}
22-
args: --examples --target thumbv7em-none-eabihf --features=rt,stm32h743v
22+
args: --examples --target thumbv7em-none-eabihf --features=rt,stm32h743v -- -A clippy::empty_loop

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ paste = "1.0.1"
3535
bare-metal = "1.0.0"
3636
sdio-host = { version = "0.4", optional = true }
3737
stm32-fmc = { version = "0.2", optional = true }
38-
synopsys-usb-otg = { version = "^0.2.2", features = ["cortex-m"], optional = true }
38+
synopsys-usb-otg = { version = "^0.2.4", features = ["cortex-m"], optional = true }
3939

4040
[dependencies.smoltcp]
4141
version = "0.7.0"

src/usb_hs.rs

Lines changed: 107 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//! USB OTG peripherals
22
//!
33
//! Requires the `usb_hs` feature.
4-
//!
5-
//! Note that only full-speed mode is supported,
6-
//! external high-speed PHY is not supported.
74
85
use crate::rcc;
96
use crate::stm32;
@@ -12,9 +9,15 @@ use crate::gpio::{
129
gpioa::{PA11, PA12},
1310
Alternate, AF10,
1411
};
12+
1513
#[cfg(not(feature = "rm0455"))]
1614
use crate::gpio::{
17-
gpiob::{PB14, PB15},
15+
gpioa::{PA3, PA5},
16+
gpiob::{PB0, PB1, PB10, PB11, PB12, PB13, PB14, PB15, PB5},
17+
gpioc::PC0,
18+
gpioc::{PC2, PC3},
19+
gpioh::PH4,
20+
gpioi::PI11,
1821
AF12,
1922
};
2023

@@ -33,6 +36,7 @@ pub struct USB1 {
3336
pub prec: rcc::rec::Usb1Otg,
3437
pub hclk: Hertz,
3538
}
39+
3640
#[cfg(not(feature = "rm0455"))]
3741
pub struct USB2 {
3842
pub usb_global: stm32::OTG2_HS_GLOBAL,
@@ -64,7 +68,6 @@ macro_rules! usb_peripheral {
6468

6569
const HIGH_SPEED: bool = true;
6670
const FIFO_DEPTH_WORDS: usize = 1024;
67-
6871
const ENDPOINT_COUNT: usize = 9;
6972

7073
fn enable() {
@@ -106,3 +109,102 @@ usb_peripheral! {
106109
}
107110
#[cfg(not(feature = "rm0455"))]
108111
pub type Usb2BusType = UsbBus<USB2>;
112+
113+
#[cfg(not(feature = "rm0455"))]
114+
pub struct USB1_ULPI {
115+
pub usb_global: stm32::OTG1_HS_GLOBAL,
116+
pub usb_device: stm32::OTG1_HS_DEVICE,
117+
pub usb_pwrclk: stm32::OTG1_HS_PWRCLK,
118+
pub prec: rcc::rec::Usb1Otg,
119+
pub hclk: Hertz,
120+
pub ulpi_clk: PA5<Alternate<AF10>>,
121+
pub ulpi_dir: Usb1UlpiDirPin,
122+
pub ulpi_nxt: Usb1UlpiNxtPin,
123+
pub ulpi_stp: PC0<Alternate<AF10>>,
124+
pub ulpi_d0: PA3<Alternate<AF10>>,
125+
pub ulpi_d1: PB0<Alternate<AF10>>,
126+
pub ulpi_d2: PB1<Alternate<AF10>>,
127+
pub ulpi_d3: PB10<Alternate<AF10>>,
128+
pub ulpi_d4: PB11<Alternate<AF10>>,
129+
pub ulpi_d5: PB12<Alternate<AF10>>,
130+
pub ulpi_d6: PB13<Alternate<AF10>>,
131+
pub ulpi_d7: PB5<Alternate<AF10>>,
132+
}
133+
134+
#[cfg(not(feature = "rm0455"))]
135+
pub enum Usb1UlpiDirPin {
136+
PC2(PC2<Alternate<AF10>>),
137+
PI11(PI11<Alternate<AF10>>),
138+
}
139+
140+
#[cfg(not(feature = "rm0455"))]
141+
impl From<PI11<Alternate<AF10>>> for Usb1UlpiDirPin {
142+
fn from(v: PI11<Alternate<AF10>>) -> Self {
143+
Usb1UlpiDirPin::PI11(v)
144+
}
145+
}
146+
147+
#[cfg(not(feature = "rm0455"))]
148+
impl From<PC2<Alternate<AF10>>> for Usb1UlpiDirPin {
149+
fn from(v: PC2<Alternate<AF10>>) -> Self {
150+
Usb1UlpiDirPin::PC2(v)
151+
}
152+
}
153+
154+
#[cfg(not(feature = "rm0455"))]
155+
pub enum Usb1UlpiNxtPin {
156+
PC3(PC3<Alternate<AF10>>),
157+
PH4(PH4<Alternate<AF10>>),
158+
}
159+
160+
#[cfg(not(feature = "rm0455"))]
161+
impl From<PH4<Alternate<AF10>>> for Usb1UlpiNxtPin {
162+
fn from(v: PH4<Alternate<AF10>>) -> Self {
163+
Usb1UlpiNxtPin::PH4(v)
164+
}
165+
}
166+
167+
#[cfg(not(feature = "rm0455"))]
168+
impl From<PC3<Alternate<AF10>>> for Usb1UlpiNxtPin {
169+
fn from(v: PC3<Alternate<AF10>>) -> Self {
170+
Usb1UlpiNxtPin::PC3(v)
171+
}
172+
}
173+
174+
#[cfg(not(feature = "rm0455"))]
175+
unsafe impl Sync for USB1_ULPI {}
176+
177+
#[cfg(not(feature = "rm0455"))]
178+
unsafe impl UsbPeripheral for USB1_ULPI {
179+
const REGISTERS: *const () = stm32::OTG1_HS_GLOBAL::ptr() as *const ();
180+
181+
const HIGH_SPEED: bool = true;
182+
const FIFO_DEPTH_WORDS: usize = 1024;
183+
const ENDPOINT_COUNT: usize = 9;
184+
185+
fn enable() {
186+
let rcc = unsafe { &*stm32::RCC::ptr() };
187+
188+
cortex_m::interrupt::free(|_| {
189+
// Enable USB peripheral
190+
rcc.ahb1enr.modify(|_, w| w.usb1otgen().enabled());
191+
192+
// Enable ULPI Clock
193+
rcc.ahb1enr.modify(|_, w| w.usb1ulpien().enabled());
194+
195+
// Reset USB peripheral
196+
rcc.ahb1rstr.modify(|_, w| w.usb1otgrst().set_bit());
197+
rcc.ahb1rstr.modify(|_, w| w.usb1otgrst().clear_bit());
198+
});
199+
}
200+
201+
fn ahb_frequency_hz(&self) -> u32 {
202+
self.hclk.0
203+
}
204+
205+
fn phy_type(&self) -> synopsys_usb_otg::PhyType {
206+
synopsys_usb_otg::PhyType::ExternalHighSpeed
207+
}
208+
}
209+
#[cfg(not(feature = "rm0455"))]
210+
pub type Usb1UlpiBusType = UsbBus<USB1_ULPI>;

0 commit comments

Comments
 (0)