-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusb_serial.rs
79 lines (65 loc) · 2.04 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
//! USB serial example for the HT32F1755
#![no_std]
#![no_main]
use defmt_rtt as _;
use ht32f1yyy_hal as hal;
use panic_probe as _;
use hal::ckcu::CkcuExt;
use hal::gpio::GpioExt;
use hal::pac;
use hal::time::RateExtU32;
use hal::usb::{Peripheral, UsbBus};
use usb_device::prelude::*;
use usbd_serial::{SerialPort, USB_CLASS_CDC};
#[cortex_m_rt::entry]
fn main() -> ! {
defmt::info!("Example: USB serial");
let dp = pac::Peripherals::take().unwrap();
let ckcu = dp.CKCU.constrain(dp.RSTCU);
let _clocks = ckcu
.configuration
.use_hse(8.MHz())
.ck_sys(144u32.MHz())
.hclk(72u32.MHz())
.ck_usb(48u32.MHz())
.freeze();
let gpioa = dp.GPIOA.split();
let dppu = gpioa.pa4.into_output_push_pull();
let usb = Peripheral { usb: dp.USB, dppu };
let usb_bus = UsbBus::new(usb);
let mut serial = SerialPort::new(&usb_bus);
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
.device_class(USB_CLASS_CDC)
.strings(&[StringDescriptors::new(LangID::EN)
.manufacturer("BigCo Inc.")
.product("Serial port")
.serial_number("DEADBEEF")])
.expect("Cannot set string descriptors")
.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;
}
_ => {}
}
}
}
_ => {}
}
}
}