]> patrickod personal git archive - tincan.git/blob - rs-lorachat/src/main.rs
working main.rs build
[tincan.git] / rs-lorachat / src / main.rs
1 #![no_std]
2 #![no_main]
3
4 use panic_halt as _;
5 use feather_m0 as hal;
6 use hal::entry;
7
8 use hal::clock::GenericClockController;
9 use hal::pac::{interrupt, CorePeripherals, Peripherals};
10
11 use hal::usb::UsbBus;
12 use usb_device::bus::UsbBusAllocator;
13
14 use usb_device::prelude::*;
15 use usbd_serial::{SerialPort, USB_CLASS_CDC};
16
17 use cortex_m::asm::delay as cycle_delay;
18 use cortex_m::peripheral::NVIC;
19
20 #[entry]
21 fn main() -> ! {
22     let mut peripherals = Peripherals::take().unwrap();
23     let mut core = CorePeripherals::take().unwrap();
24     let mut clocks = GenericClockController::with_internal_32kosc(
25         peripherals.GCLK,
26         &mut peripherals.PM,
27         &mut peripherals.SYSCTRL,
28         &mut peripherals.NVMCTRL,
29     );
30     let mut pins = hal::Pins::new(peripherals.PORT);
31     let mut red_led = pins.d13.into_open_drain_output(&mut pins.port);
32
33     let bus_allocator = unsafe {
34         USB_ALLOCATOR = Some(hal::usb_allocator(
35             peripherals.USB,
36             &mut clocks,
37             &mut peripherals.PM,
38             pins.usb_dm,
39             pins.usb_dp,
40             &mut pins.port,
41         ));
42         USB_ALLOCATOR.as_ref().unwrap()
43     };
44
45     unsafe {
46         USB_SERIAL = Some(SerialPort::new(&bus_allocator));
47         USB_BUS = Some(
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)
53                 .build(),
54         );
55     }
56
57     unsafe {
58         core.NVIC.set_priority(interrupt::USB, 1);
59         NVIC::unmask(interrupt::USB);
60     }
61
62     // Flash the LED in a spin loop to demonstrate that USB is
63     // entirely interrupt driven.
64     loop {
65         cycle_delay(15 * 1024 * 1024);
66         red_led.toggle();
67     }
68 }
69
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;
73
74 fn poll_usb() {
75     unsafe {
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];
80
81                 if let Ok(count) = serial.read(&mut buf) {
82                     for (i, c) in buf.iter().enumerate() {
83                         if i >= count {
84                             break;
85                         }
86                         serial.write(&[c.clone()]);
87                     }
88                 };
89             });
90         });
91     };
92 }
93
94 #[interrupt]
95 fn USB() {
96     poll_usb();
97 }