Skip to content

Commit 20cdc35

Browse files
russellmccSh3Rm4n
andauthored
Support USB on the stm32f303xd/xe devices (#255)
* Support USB on the stm32f303xd/xe devices The 303xd and 303xe series devices do not have AF14 for pins PA11 and PA12, so the existing implementation will not work for those devices. According to an ST document [1], stm32f303xd and xe do not require any particular mode for the PA11 and PA12 pin, so I've parameterized the `usb::Peripheral` type using traits so that any mode will be supported. I've tried to follow a familiar pattern reminescent of the gpio alternate function configurations. [1]: https://www.st.com/resource/en/application_note/dm00260340-migrating-between-stm32f303-and-stm32f302-line-products-stmicroelectronics.pdf Co-authored-by: Fabian <[email protected]>
1 parent 86fb3c3 commit 20cdc35

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2020
### Breaking Changes
2121

2222
- Refactor CAN to use the [`bxCan`](https://github.com/stm32-rs/bxcan) crate. ([#207])
23-
- Add support for configuring parity and stop bits in addition to baud rate for `Serial` with
24-
`serial::config::Config`. ([#239])
23+
- Add support for configuring parity and stop bits in addition to baud rate for
24+
`Serial` with `serial::config::Config`. ([#239])
2525
- Implement `Serial::join` which allows to re-create the serial peripheral,
2626
when `Serial::split` was previously called. ([#252])
27+
- Parameterized `usb::Peripheral` and `usb::UsbType` on the pin configuration
28+
used ([#255])
2729

2830
## [v0.7.0] - 2021-06-18
2931

@@ -349,6 +351,7 @@ let clocks = rcc
349351
[defmt]: https://github.com/knurling-rs/defmt
350352
[filter]: https://defmt.ferrous-systems.com/filtering.html
351353

354+
[#255]: https://github.com/stm32-rs/stm32f3xx-hal/pull/255
352355
[#252]: https://github.com/stm32-rs/stm32f3xx-hal/pull/252
353356
[#247]: https://github.com/stm32-rs/stm32f3xx-hal/pull/247
354357
[#246]: https://github.com/stm32-rs/stm32f3xx-hal/pull/246

src/usb.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,44 @@
1111
use crate::pac::{RCC, USB};
1212
use stm32_usbd::UsbPeripheral;
1313

14+
use crate::gpio;
1415
use crate::gpio::gpioa::{PA11, PA12};
15-
use crate::gpio::{PushPull, AF14};
1616
pub use stm32_usbd::UsbBus;
1717

18+
/// Trait implemented by all pins that can be the "D-" pin for the USB peripheral
19+
pub trait DmPin: crate::private::Sealed {}
20+
21+
/// Trait implemented by all pins that can be the "D+" pin for the USB peripheral
22+
pub trait DpPin: crate::private::Sealed {}
23+
24+
#[cfg(any(feature = "stm32f303xb", feature = "stm32f303xc"))]
25+
impl DmPin for PA11<gpio::AF14<gpio::PushPull>> {}
26+
27+
#[cfg(any(feature = "stm32f303xb", feature = "stm32f303xc"))]
28+
impl DpPin for PA12<gpio::AF14<gpio::PushPull>> {}
29+
30+
#[cfg(any(feature = "stm32f303xd", feature = "stm32f303xe"))]
31+
impl<Mode> DmPin for PA11<Mode> {}
32+
33+
#[cfg(any(feature = "stm32f303xd", feature = "stm32f303xe"))]
34+
impl<Mode> DpPin for PA12<Mode> {}
35+
1836
/// USB Peripheral
1937
///
2038
/// Constructs the peripheral, which
2139
/// than gets passed to the [`UsbBus`].
22-
pub struct Peripheral {
40+
pub struct Peripheral<Dm: DmPin, Dp: DpPin> {
2341
/// USB Register Block
2442
pub usb: USB,
2543
/// Data Negativ Pin
26-
pub pin_dm: PA11<AF14<PushPull>>,
44+
pub pin_dm: Dm,
2745
/// Data Positiv Pin
28-
pub pin_dp: PA12<AF14<PushPull>>,
46+
pub pin_dp: Dp,
2947
}
3048

31-
unsafe impl Sync for Peripheral {}
49+
unsafe impl<Dm: DmPin, Dp: DpPin> Sync for Peripheral<Dm, Dp> {}
3250

33-
unsafe impl UsbPeripheral for Peripheral {
51+
unsafe impl<Dm: DmPin + Send, Dp: DpPin + Send> UsbPeripheral for Peripheral<Dm, Dp> {
3452
const REGISTERS: *const () = USB::ptr() as *const ();
3553
const DP_PULL_UP_FEATURE: bool = false;
3654
const EP_MEMORY: *const () = 0x4000_6000 as _;
@@ -65,7 +83,10 @@ unsafe impl UsbPeripheral for Peripheral {
6583
}
6684

6785
/// Type of the UsbBus
68-
///
69-
/// As this MCU family has only USB peripheral,
70-
/// this is the only possible concrete type construction.
71-
pub type UsbBusType = UsbBus<Peripheral>;
86+
#[cfg(any(feature = "stm32f303xb", feature = "stm32f303xc"))]
87+
pub type UsbBusType<Dm = PA11<gpio::AF14<gpio::PushPull>>, Dp = PA12<gpio::AF14<gpio::PushPull>>> =
88+
UsbBus<Peripheral<Dm, Dp>>;
89+
90+
/// Type of the UsbBus
91+
#[cfg(any(feature = "stm32f303xd", feature = "stm32f303xe"))]
92+
pub type UsbBusType<Dm = PA11<gpio::Input>, Dp = PA12<gpio::Input>> = UsbBus<Peripheral<Dm, Dp>>;

0 commit comments

Comments
 (0)