forked from stm32-rs/stm32h7xx-hal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
usb_serial.rs
107 lines (88 loc) · 2.71 KB
/
usb_serial.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//! CDC-ACM serial port example using polling in a busy loop
//!
//! Note: This example must be built in release mode to work reliably
#![no_std]
#![no_main]
#[macro_use]
#[allow(unused)]
mod utilities;
use cortex_m_rt::entry;
use stm32h7xx_hal::rcc::rec::UsbClkSel;
use stm32h7xx_hal::usb_hs::{UsbBus, USB1};
use stm32h7xx_hal::{prelude::*, stm32};
use usb_device::prelude::*;
static mut EP_MEMORY: [u32; 1024] = [0; 1024];
#[entry]
fn main() -> ! {
let dp = stm32::Peripherals::take().unwrap();
// Power
let pwr = dp.PWR.constrain();
let vos = example_power!(pwr).freeze();
// RCC
let rcc = dp.RCC.constrain();
let mut ccdr = rcc.sys_ck(80.mhz()).freeze(vos, &dp.SYSCFG);
// 48MHz CLOCK
let _ = ccdr.clocks.hsi48_ck().expect("HSI48 must run");
ccdr.peripheral.kernel_usb_clk_mux(UsbClkSel::HSI48);
// IO
#[cfg(not(feature = "rm0455"))]
let (pin_dm, pin_dp) = {
let gpiob = dp.GPIOB.split(ccdr.peripheral.GPIOB);
(
gpiob.pb14.into_alternate_af12(),
gpiob.pb15.into_alternate_af12(),
)
};
#[cfg(feature = "rm0455")]
let (pin_dm, pin_dp) = {
let gpioa = dp.GPIOA.split(ccdr.peripheral.GPIOA);
(
gpioa.pa11.into_alternate_af10(),
gpioa.pa12.into_alternate_af10(),
)
};
let usb = USB1::new(
dp.OTG1_HS_GLOBAL,
dp.OTG1_HS_DEVICE,
dp.OTG1_HS_PWRCLK,
pin_dm,
pin_dp,
ccdr.peripheral.USB1OTG,
&ccdr.clocks,
);
let usb_bus = UsbBus::new(usb, unsafe { &mut EP_MEMORY });
let mut serial = usbd_serial::SerialPort::new(&usb_bus);
let mut usb_dev =
UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
.manufacturer("Fake company")
.product("Serial port")
.serial_number("TEST")
.device_class(usbd_serial::USB_CLASS_CDC)
.build();
loop {
if !usb_dev.poll(&mut [&mut serial]) {
continue;
}
let mut buf = [0u8; 64];
match serial.read(&mut buf) {
Ok(count) if count > 0 => {
// Echo back in upper case
for c in buf[0..count].iter_mut() {
if 0x61 <= *c && *c <= 0x7a {
*c &= !0x20;
}
}
let mut write_offset = 0;
while write_offset < count {
match serial.write(&buf[write_offset..count]) {
Ok(len) if len > 0 => {
write_offset += len;
}
_ => {}
}
}
}
_ => {}
}
}
}