8 use hal::clock::GenericClockController;
9 use hal::pac::{interrupt, CorePeripherals, Peripherals};
12 use usb_device::bus::UsbBusAllocator;
14 use usb_device::prelude::*;
15 use usbd_serial::{SerialPort, USB_CLASS_CDC};
17 use cortex_m::asm::delay as cycle_delay;
18 use cortex_m::peripheral::NVIC;
22 let mut peripherals = Peripherals::take().unwrap();
23 let mut core = CorePeripherals::take().unwrap();
24 let mut clocks = GenericClockController::with_internal_32kosc(
27 &mut peripherals.SYSCTRL,
28 &mut peripherals.NVMCTRL,
30 let mut pins = hal::Pins::new(peripherals.PORT);
31 let mut red_led = pins.d13.into_open_drain_output(&mut pins.port);
33 let bus_allocator = unsafe {
34 USB_ALLOCATOR = Some(hal::usb_allocator(
42 USB_ALLOCATOR.as_ref().unwrap()
46 USB_SERIAL = Some(SerialPort::new(&bus_allocator));
48 UsbDeviceBuilder::new(&bus_allocator, UsbVidPid(0x16c0, 0x27dd))
49 .manufacturer("Fake company")
50 .product("Serial port")
51 .serial_number("TEST")
52 .device_class(USB_CLASS_CDC)
58 core.NVIC.set_priority(interrupt::USB, 1);
59 NVIC::unmask(interrupt::USB);
62 // Flash the LED in a spin loop to demonstrate that USB is
63 // entirely interrupt driven.
65 cycle_delay(15 * 1024 * 1024);
70 static mut USB_ALLOCATOR: Option<UsbBusAllocator<UsbBus>> = None;
71 static mut USB_BUS: Option<UsbDevice<UsbBus>> = None;
72 static mut USB_SERIAL: Option<SerialPort<UsbBus>> = None;
76 USB_BUS.as_mut().map(|usb_dev| {
77 USB_SERIAL.as_mut().map(|serial| {
78 usb_dev.poll(&mut [serial]);
79 let mut buf = [0u8; 64];
81 if let Ok(count) = serial.read(&mut buf) {
82 for (i, c) in buf.iter().enumerate() {
86 serial.write(&[c.clone()]);