X-Git-Url: https://git.patrickod.com/tor.noisebridge.net/diff/src/components/MoreInformation.js?a=blobdiff_plain;f=rs-lorachat%2Fsrc%2Fmain.rs;h=89ad86887f19693e0dee1ae09d39d68f844fa337;hb=99a9806b9b2b042d168f32ae10dd4b89a3295021;hp=e7a11a969c037e00a796aafeff6258501ec15e9a;hpb=882e8be16da3fe125ad4d7947f8fb0d81276f2cb;p=tincan.git diff --git a/rs-lorachat/src/main.rs b/rs-lorachat/src/main.rs index e7a11a9..89ad868 100644 --- a/rs-lorachat/src/main.rs +++ b/rs-lorachat/src/main.rs @@ -1,3 +1,101 @@ -fn main() { - println!("Hello, world!"); +#![no_std] +#![no_main] + +extern crate cortex_m; +extern crate feather_m0 as hal; +extern crate panic_halt; +extern crate usb_device; +extern crate usbd_serial; + +use hal::clock::GenericClockController; +use hal::entry; +use hal::pac::{interrupt, CorePeripherals, Peripherals}; +use hal::prelude::*; + +use hal::usb::UsbBus; +use usb_device::bus::UsbBusAllocator; + +use usb_device::prelude::*; +use usbd_serial::{SerialPort, USB_CLASS_CDC}; + +use cortex_m::asm::delay as cycle_delay; +use cortex_m::peripheral::NVIC; + +#[entry] +fn main() -> ! { + let mut peripherals = Peripherals::take().unwrap(); + let mut core = CorePeripherals::take().unwrap(); + let mut clocks = GenericClockController::with_internal_32kosc( + peripherals.GCLK, + &mut peripherals.PM, + &mut peripherals.SYSCTRL, + &mut peripherals.NVMCTRL, + ); + let mut pins = hal::Pins::new(peripherals.PORT); + let mut red_led = pins.d13.into_open_drain_output(&mut pins.port); + + let bus_allocator = unsafe { + USB_ALLOCATOR = Some(hal::usb_allocator( + peripherals.USB, + &mut clocks, + &mut peripherals.PM, + pins.usb_dm, + pins.usb_dp, + &mut pins.port, + )); + USB_ALLOCATOR.as_ref().unwrap() + }; + + unsafe { + USB_SERIAL = Some(SerialPort::new(&bus_allocator)); + USB_BUS = Some( + UsbDeviceBuilder::new(&bus_allocator, UsbVidPid(0x16c0, 0x27dd)) + .manufacturer("Fake company") + .product("Serial port") + .serial_number("TEST") + .device_class(USB_CLASS_CDC) + .build(), + ); + } + + unsafe { + core.NVIC.set_priority(interrupt::USB, 1); + NVIC::unmask(interrupt::USB); + } + + // Flash the LED in a spin loop to demonstrate that USB is + // entirely interrupt driven. + loop { + cycle_delay(15 * 1024 * 1024); + red_led.toggle(); + } +} + +static mut USB_ALLOCATOR: Option> = None; +static mut USB_BUS: Option> = None; +static mut USB_SERIAL: Option> = None; + +fn poll_usb() { + unsafe { + USB_BUS.as_mut().map(|usb_dev| { + USB_SERIAL.as_mut().map(|serial| { + usb_dev.poll(&mut [serial]); + let mut buf = [0u8; 64]; + + if let Ok(count) = serial.read(&mut buf) { + for (i, c) in buf.iter().enumerate() { + if i >= count { + break; + } + serial.write(&[c.clone()]); + } + }; + }); + }); + }; +} + +#[interrupt] +fn USB() { + poll_usb(); }