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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#![no_main]
#![no_std]
extern crate panic_semihosting;
// extern crate panic_halt;
use cortex_m_rt::entry;
// use cortex_m_semihosting::{dbg, heprintln};
#[allow(unused_imports)]
use hal::prelude::*;
#[allow(unused_imports)]
use lpc55_hal as hal;
use hal::drivers::{pins, Timer, UsbBus};
use usb_device::device::{UsbDeviceBuilder, UsbVidPid};
use usbd_serial::CdcAcmClass;
#[entry]
fn main() -> ! {
let hal = hal::new();
let mut anactrl = hal.anactrl;
let mut pmc = hal.pmc;
let mut syscon = hal.syscon;
let mut gpio = hal.gpio.enabled(&mut syscon);
let mut iocon = hal.iocon.enabled(&mut syscon);
let mut red_led = pins::Pio1_6::take()
.unwrap()
.into_gpio_pin(&mut iocon, &mut gpio)
.into_output(hal::drivers::pins::Level::High); // start turned off
let usb0_vbus_pin = pins::Pio0_22::take()
.unwrap()
.into_usb0_vbus_pin(&mut iocon);
iocon.disabled(&mut syscon).release(); // save the environment :)
let clocks = hal::ClockRequirements::default()
// .system_frequency(24.mhz())
// .system_frequency(72.mhz())
.system_frequency(96.MHz())
.configure(&mut anactrl, &mut pmc, &mut syscon)
.unwrap();
let mut _delay_timer = Timer::new(
hal.ctimer
.0
.enabled(&mut syscon, clocks.support_1mhz_fro_token().unwrap()),
);
// Can use compile to use either the "HighSpeed" or "FullSpeed" USB peripheral.
// Default is full speed.
#[cfg(feature = "highspeed-usb-example")]
let usb_peripheral = hal.usbhs.enabled_as_device(
&mut anactrl,
&mut pmc,
&mut syscon,
&mut _delay_timer,
clocks.support_usbhs_token().unwrap(),
);
#[cfg(not(feature = "highspeed-usb-example"))]
let usb_peripheral = hal.usbfs.enabled_as_device(
&mut anactrl,
&mut pmc,
&mut syscon,
clocks.support_usbfs_token().unwrap(),
);
let usb_bus = UsbBus::new(usb_peripheral, usb0_vbus_pin);
let mut cdc_acm = CdcAcmClass::new(&usb_bus, 8);
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x1209, 0xcc1d))
.manufacturer("nickray")
.product("Demo Demo Demo")
.serial_number("2019-10-10")
.device_release(0x0123)
// Must be 64 bytes for HighSpeed
.max_packet_size_0(64)
// .device_class(USB_CLASS_CDC)
.build();
// dbg!("main loop");
let mut need_zlp = false;
let mut buf = [0u8; 8];
let mut size = 0;
let mut buf_in_use = false;
loop {
// if !usb_dev.poll(&mut []) {
// if !usb_dev.poll(&mut [&mut serial]) {
if !usb_dev.poll(&mut [&mut cdc_acm]) {
continue;
}
if !(buf_in_use || need_zlp) {
if let Ok(count) = cdc_acm.read_packet(&mut buf) {
size = count;
buf_in_use = true;
// dbg!(&buf[..count]);
// if count > 1 {
// dbg!(count);
// }
}
}
if buf_in_use {
red_led.set_low().ok(); // Turn on
if let Ok(count) = cdc_acm.write_packet(&buf[..size]) {
assert!(count == size);
buf_in_use = false;
need_zlp = size == 8;
}
red_led.set_high().ok(); // Turn off
}
if need_zlp {
if let Ok(count) = cdc_acm.write_packet(&[]) {
assert!(count == 0);
need_zlp = false;
}
}
// let mut buf = [0u8; 512];
// match serial.read(&mut buf) {
// Ok(count) if count > 0 => {
// assert!(count == 1);
// // hprintln!("received some data on the serial port: {:?}", &buf[..count]).ok();
// // cortex_m_semihosting::hprintln!("received:\n{}", core::str::from_utf8(&buf[..count]).unwrap()).ok();
// red_led.set_low().ok(); // Turn on
// // cortex_m_semihosting::hprintln!("read {:?}", &buf[..count]).ok();
// cortex_m_semihosting::hprintln!("read {:?}", count).ok();
// // Echo back in upper case
// for c in buf[0..count].iter_mut() {
// if (0x61 <= *c && *c <= 0x7a) || (0x41 <= *c && *c <= 0x5a) {
// *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;
// cortex_m_semihosting::hprintln!("wrote {:?}", len).ok();
// },
// _ => {},
// }
// }
// // hprintln!("wrote it back").ok();
// }
// _ => {}
// }
// red_led.set_high().ok(); // Turn off
}
}