Skip to content

Commit 0bbb50f

Browse files
authored
Move usb-remap internals for certain ICs to USB enable() (#119)
Move usb-remap internals for certain ICs to USB enable()
1 parent 86ecf61 commit 0bbb50f

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
- Simplify USB PA11/12 remapping for STM32F042x via `usb_bus.usb_remap()` function.
11+
1012
## [v0.17.0] - 2020-06-27
1113

1214
### Changed

examples/usb_serial.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ use usbd_serial::{SerialPort, USB_CLASS_CDC};
1515
fn main() -> ! {
1616
let mut dp = pac::Peripherals::take().unwrap();
1717

18-
/* Uncomment the following lines if you have a chip in TSSOP20 (STM32F042F)
19-
or UFQFPN28 (STM32F042G) package
20-
This code enables clock for SYSCFG and remaps USB pins to PA9 and PA10.
21-
*/
22-
//dp.RCC.apb2enr.modify(|_, w| w.syscfgen().set_bit());
23-
//dp.SYSCFG.cfgr1.modify(|_, w| w.pa11_pa12_rmp().remapped());
24-
2518
let mut rcc = dp
2619
.RCC
2720
.configure()
@@ -43,10 +36,20 @@ fn main() -> ! {
4336
pin_dm: gpioa.pa11,
4437
pin_dp: gpioa.pa12,
4538
};
39+
4640
let usb_bus = UsbBus::new(usb);
4741

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

44+
/*
45+
* IMPORTANT: if you have a chip in TSSOP20 (STM32F042F) or UFQFPN28 (STM32F042G) package,
46+
* and want to use USB, make sure you call `remap_pins(rcc, syscfg)`, otherwise the device will not enumerate.
47+
*
48+
* Uncomment the following function if the situation above applies to you.
49+
*/
50+
51+
//usb_bus.remap_pins();
52+
5053
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
5154
.manufacturer("Fake company")
5255
.product("Serial port")

src/usb.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! See <https://github.com/stm32-rs/stm32f0xx-hal/tree/master/examples>
66
//! for usage examples.
77
8-
use crate::pac::{RCC, USB};
8+
use crate::pac::{RCC, SYSCFG, USB};
99
use stm32_usbd::UsbPeripheral;
1010

1111
use crate::gpio::gpioa::{PA11, PA12};
@@ -27,7 +27,7 @@ unsafe impl UsbPeripheral for Peripheral {
2727
const EP_MEMORY_SIZE: usize = 1024;
2828

2929
fn enable() {
30-
let rcc = unsafe { (&*RCC::ptr()) };
30+
let rcc = unsafe { &*RCC::ptr() };
3131

3232
cortex_m::interrupt::free(|_| {
3333
// Enable USB peripheral
@@ -46,4 +46,13 @@ unsafe impl UsbPeripheral for Peripheral {
4646
}
4747
}
4848

49+
pub fn remap_pins(rcc: &mut RCC, syscfg: &mut SYSCFG) {
50+
cortex_m::interrupt::free(|_| {
51+
// Remap PA11/PA12 pins to PA09/PA10 for USB on
52+
// TSSOP20 (STM32F042F) or UFQFPN28 (STM32F042G) packages
53+
rcc.apb2enr.modify(|_, w| w.syscfgen().set_bit());
54+
syscfg.cfgr1.modify(|_, w| w.pa11_pa12_rmp().remapped());
55+
});
56+
}
57+
4958
pub type UsbBusType = UsbBus<Peripheral>;

0 commit comments

Comments
 (0)