Skip to content

Commit b2636de

Browse files
bors[bot]Slamy
andauthored
Merge #525
525: Cloned usb example and added irq usage r=burrbull a=Slamy As discussed today. I tried to mimic the style of the original file. Feel free to edit it. Tested on stm32f407g-disc1 board using: `cargo embed --features="stm32f407 otg-fs usb_fs" --example=usb_serial_irq` There might be a better way to run the examples but this is lacking in the documentation of the project and I don't know how. Co-authored-by: Slamy <[email protected]>
2 parents fa67f41 + d763dea commit b2636de

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3636
- Implementation of From trait for PartiallyErasedPin-to-ErasedPin [#507]
3737
- `SysMonoTimerExt` helper trait, `Pwm::(get/set)_duty_time` [#497]
3838
- example of using i2s in out with rtic and interrupt.
39+
- example of using USB CDC with interrupts.
3940

4041
[#489]: https://github.com/stm32-rs/stm32f4xx-hal/pull/489
4142
[#490]: https://github.com/stm32-rs/stm32f4xx-hal/pull/490

Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,13 @@ name = "blinky-timer-irq"
372372
required-features = ["tim2"] # stm32f411
373373

374374
[[example]]
375-
name = "usb_serial"
375+
name = "usb_serial_poll"
376376
required-features = ["otg-fs", "usb_fs"] # stm32f401
377377

378+
[[example]]
379+
name = "usb_serial_irq"
380+
required-features = ["otg-fs", "usb_fs"] # stm32f407
381+
378382
[[example]]
379383
name = "sd"
380384
required-features = ["gpiod", "sdio", "sdio-host"] # stm32f405

examples/usb_serial_irq.rs

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//! CDC-ACM serial port example using interrupts.
2+
//! Target board: any STM32F4 with a OTG FS peripheral and a 8MHz HSE crystal
3+
#![no_std]
4+
#![no_main]
5+
6+
use panic_halt as _;
7+
8+
use core::cell::RefCell;
9+
use cortex_m::interrupt::Mutex;
10+
use cortex_m_rt::entry;
11+
use stm32f4xx_hal::otg_fs::{UsbBus, USB};
12+
use stm32f4xx_hal::pac::{interrupt, Interrupt};
13+
use stm32f4xx_hal::{pac, prelude::*};
14+
use usb_device::class_prelude::UsbBusAllocator;
15+
use usb_device::prelude::*;
16+
use usbd_serial::SerialPort;
17+
18+
// Make USB serial device globally available
19+
static G_USB_SERIAL: Mutex<RefCell<Option<SerialPort<UsbBus<USB>>>>> =
20+
Mutex::new(RefCell::new(None));
21+
22+
// Make USB device globally available
23+
static G_USB_DEVICE: Mutex<RefCell<Option<UsbDevice<UsbBus<USB>>>>> =
24+
Mutex::new(RefCell::new(None));
25+
26+
#[entry]
27+
fn main() -> ! {
28+
static mut EP_MEMORY: [u32; 1024] = [0; 1024];
29+
static mut USB_BUS: Option<UsbBusAllocator<stm32f4xx_hal::otg_fs::UsbBusType>> = None;
30+
31+
let dp = pac::Peripherals::take().unwrap();
32+
33+
let rcc = dp.RCC.constrain();
34+
35+
let clocks = rcc.cfgr.sysclk((168).MHz()).pclk1((8).MHz()).freeze();
36+
37+
let gpioa = dp.GPIOA.split();
38+
39+
let usb = USB {
40+
usb_global: dp.OTG_FS_GLOBAL,
41+
usb_device: dp.OTG_FS_DEVICE,
42+
usb_pwrclk: dp.OTG_FS_PWRCLK,
43+
pin_dm: gpioa.pa11.into_alternate(),
44+
pin_dp: gpioa.pa12.into_alternate(),
45+
hclk: clocks.hclk(),
46+
};
47+
48+
*USB_BUS = Some(stm32f4xx_hal::otg_fs::UsbBusType::new(usb, EP_MEMORY));
49+
let usb_bus = USB_BUS.as_ref().unwrap();
50+
51+
cortex_m::interrupt::free(|cs| {
52+
*G_USB_SERIAL.borrow(cs).borrow_mut() = Some(SerialPort::new(usb_bus));
53+
54+
*G_USB_DEVICE.borrow(cs).borrow_mut() = Some(
55+
UsbDeviceBuilder::new(usb_bus, UsbVidPid(0x16c0, 0x27dd))
56+
.manufacturer("Fake company")
57+
.product("Serial port")
58+
.serial_number("TEST")
59+
.device_class(usbd_serial::USB_CLASS_CDC)
60+
.build(),
61+
);
62+
});
63+
64+
unsafe {
65+
cortex_m::peripheral::NVIC::unmask(Interrupt::OTG_FS);
66+
}
67+
68+
loop {
69+
// Do nothing. Everything is done in the IRQ handler
70+
}
71+
}
72+
73+
#[interrupt]
74+
fn OTG_FS() {
75+
static mut USB_SERIAL: Option<SerialPort<UsbBus<USB>>> = None;
76+
static mut USB_DEVICE: Option<UsbDevice<UsbBus<USB>>> = None;
77+
78+
let usb_dev = USB_DEVICE.get_or_insert_with(|| {
79+
cortex_m::interrupt::free(|cs| {
80+
// Move USB device here, leaving a None in its place
81+
G_USB_DEVICE.borrow(cs).replace(None).unwrap()
82+
})
83+
});
84+
85+
let serial = USB_SERIAL.get_or_insert_with(|| {
86+
cortex_m::interrupt::free(|cs| {
87+
// Move USB serial device here, leaving a None in its place
88+
G_USB_SERIAL.borrow(cs).replace(None).unwrap()
89+
})
90+
});
91+
92+
if usb_dev.poll(&mut [serial]) {
93+
let mut buf = [0u8; 64];
94+
95+
match serial.read(&mut buf) {
96+
Ok(count) if count > 0 => {
97+
// Echo back in upper case
98+
for c in buf[0..count].iter_mut() {
99+
if 0x61 <= *c && *c <= 0x7a {
100+
*c &= !0x20;
101+
}
102+
}
103+
104+
let mut write_offset = 0;
105+
while write_offset < count {
106+
match serial.write(&buf[write_offset..count]) {
107+
Ok(len) if len > 0 => {
108+
write_offset += len;
109+
}
110+
_ => {}
111+
}
112+
}
113+
}
114+
_ => {}
115+
}
116+
}
117+
}
File renamed without changes.

0 commit comments

Comments
 (0)