#![no_std] #![no_main] //! Makes the grand_central_m4 appear as a USB serial port. The color of the //! neopixel LED can be changed by sending bytes to the serial port. //! //! Sending the characters R, G, and O set the LED red, green, and off //! respectively. For example: //! $> sudo stty -F /dev/ttyACM0 115200 raw -echo //! $> sudo bash -c "echo 'R' > /dev/ttyACM0" //! $> sudo bash -c "echo 'G' > /dev/ttyACM0" //! $> sudo bash -c "echo 'O' > /dev/ttyACM0" //! //! Note leds may appear white during debug. Either build for release or add //! opt-level = 2 to profile.dev in Cargo.toml use bsp::hal; use grand_central_m4 as bsp; #[cfg(not(feature = "use_semihosting"))] use panic_halt as _; #[cfg(feature = "use_semihosting")] use panic_semihosting as _; use bsp::entry; use cortex_m::interrupt::free as disable_interrupts; use cortex_m::peripheral::NVIC; use hal::clock::GenericClockController; use hal::pac::{interrupt, CorePeripherals, Peripherals}; use hal::prelude::*; use hal::time::Hertz; use hal::timer::TimerCounter; use hal::timer_traits::InterruptDrivenTimer; use hal::usb::UsbBus; use smart_leds::{colors, hsv::RGB8, SmartLedsWrite}; use usb_device::bus::UsbBusAllocator; use usb_device::prelude::*; use usbd_serial::{SerialPort, USB_CLASS_CDC}; use ws2812_timer_delay as ws2812; #[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.mclk, &mut peripherals.osc32kctrl, &mut peripherals.oscctrl, &mut peripherals.nvmctrl, ); let gclk0 = clocks.gclk0(); let tc2_3 = clocks.tc2_tc3(&gclk0).unwrap(); let mut timer = TimerCounter::tc3_(&tc2_3, peripherals.tc3, &mut peripherals.mclk); InterruptDrivenTimer::start(&mut timer, Hertz::MHz(3).into_duration()); let pins = bsp::Pins::new(peripherals.port); let neopixel_pin = pins.neopixel.into_push_pull_output(); let mut neopixel = ws2812::Ws2812::new(timer, neopixel_pin); let _ = neopixel.write((0..5).map(|_| RGB8::default())); let bus_allocator = unsafe { USB_ALLOCATOR = Some(bsp::usb_allocator( peripherals.usb, &mut clocks, &mut peripherals.mclk, pins.usb_dm, pins.usb_dp, )); USB_ALLOCATOR.as_ref().unwrap() }; unsafe { USB_SERIAL = Some(SerialPort::new(&bus_allocator)); USB_BUS = Some( UsbDeviceBuilder::new(&bus_allocator, UsbVidPid(0x16c0, 0x27dd)) .strings(&[StringDescriptors::new(LangID::EN_US) .manufacturer("Fake company") .product("Serial port") .serial_number("TEST")]) .expect("Failed to set strings") .device_class(USB_CLASS_CDC) .build(), ); } unsafe { core.NVIC.set_priority(interrupt::USB_OTHER, 1); core.NVIC.set_priority(interrupt::USB_TRCPT0, 1); core.NVIC.set_priority(interrupt::USB_TRCPT1, 1); NVIC::unmask(interrupt::USB_OTHER); NVIC::unmask(interrupt::USB_TRCPT0); NVIC::unmask(interrupt::USB_TRCPT1); } loop { let pending = disable_interrupts(|_| unsafe { let pending = PENDING_COLOR; PENDING_COLOR = None; pending }); if let Some(color) = pending { let _ = neopixel.write((0..5).map(|_| color)); } } } static mut USB_ALLOCATOR: Option> = None; static mut USB_BUS: Option> = None; static mut USB_SERIAL: Option> = None; static mut PENDING_COLOR: 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) { let last = buf[count - 1] as char; let color = match last { 'R' => colors::RED, 'G' => colors::GREEN, 'O' => colors::ORANGE, _ => RGB8::default(), }; PENDING_COLOR = Some(color); }; }); }); }; } #[interrupt] fn USB_OTHER() { poll_usb(); } #[interrupt] fn USB_TRCPT0() { poll_usb(); } #[interrupt] fn USB_TRCPT1() { poll_usb(); }