//! Blinks the LED on a Pico board //! //! This will blink an LED attached to GP25, which is the pin the Pico uses for the on-board LED. #![no_std] #![no_main] extern crate alloc; use alloc::string::ToString; use bsp::entry; use defmt::*; use defmt_rtt as _; use embedded_hal::digital::v2::OutputPin; use panic_probe as _; // Provide an alias for our BSP so we can switch targets quickly. // Uncomment the BSP you included in Cargo.toml, the rest of the code does not need to change. use rp_pico as bsp; // use sparkfun_pro_micro_rp2040 as bsp; use bsp::hal::{ clocks::{init_clocks_and_plls, Clock}, pac, sio::Sio, usb::UsbBus, watchdog::Watchdog, Adc, }; use embedded_hal::adc::OneShot; use usb_device::{class_prelude::*, prelude::*}; use usbd_serial::SerialPort; #[entry] fn main() -> ! { info!("Program start"); let mut pac = pac::Peripherals::take().unwrap(); let core = pac::CorePeripherals::take().unwrap(); let mut watchdog = Watchdog::new(pac.WATCHDOG); // External high-speed crystal on the pico board is 12Mhz let external_xtal_freq_hz = 12_000_000u32; let clocks = init_clocks_and_plls( external_xtal_freq_hz, pac.XOSC, pac.CLOCKS, pac.PLL_SYS, pac.PLL_USB, &mut pac.RESETS, &mut watchdog, ) .ok() .unwrap(); let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz()); let sio = Sio::new(pac.SIO); let pins = bsp::Pins::new( pac.IO_BANK0, pac.PADS_BANK0, sio.gpio_bank0, &mut pac.RESETS, ); let mut led_pin = pins.led.into_push_pull_output(); led_pin.set_low(); // Set up the USB driver let usb_bus = UsbBusAllocator::new(UsbBus::new( pac.USBCTRL_REGS, pac.USBCTRL_DPRAM, clocks.usb_clock, true, &mut pac.RESETS, )); // Set up the USB Communications Class Device driver let mut serial = SerialPort::new(&usb_bus); // Create a USB device with a fake VID and PID let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd)) .manufacturer("Fake company") .product("Serial port") .serial_number("TEST") .device_class(2) // from: https://www.usb.org/defined-class-codes .build(); let mut adc = Adc::new(pac.ADC, &mut pac.RESETS); let mut adc_pin_0 = pins.gpio26.into_floating_input(); // let mut adc_pin_1 = pins.gpio27.into_floating_input(); // let mut adc_pin_2 = pins.gpio28.into_floating_input(); let mut i = 0; loop { info!("start!"); // serial.write("test".as_bytes()); // serial_send(&mut serial, "test"); // delay.delay_ms(1000); i += 1; if i == 100 { i = 0; let adc_0_value: u16 = adc.read(&mut adc_pin_0).unwrap(); // let adc_1_value: u16 = adc.read(&mut adc_pin_1).unwrap(); // let adc_2_value: u16 = adc.read(&mut adc_pin_2).unwrap(); serial_send(&mut serial, &adc_0_value.to_string()); } delay.delay_ms(8); if usb_dev.poll(&mut [&mut serial]) { // let mut buf = [0u8; 64]; // match serial.read(&mut buf) { // Err(_e) => { // // Do nothing // } // Ok(0) => { // // Do nothing // } // Ok(count) => { // // Convert to upper case // buf.iter_mut().take(count).for_each(|b| { // b.make_ascii_uppercase(); // }); // // Send back to the host // let mut wr_ptr = &buf[..count]; // while !wr_ptr.is_empty() { // match serial.write(wr_ptr) { // Ok(len) => wr_ptr = &wr_ptr[len..], // // On error, just drop unwritten data. // // One possible error is Err(WouldBlock), meaning the USB // // write buffer is full. // Err(_) => break, // }; // } // } // } } } } fn serial_send(serial: &mut SerialPort, msg: &str) { // Send back to the host let mut wr_ptr = msg.as_bytes(); while !wr_ptr.is_empty() { match serial.write(wr_ptr) { Ok(len) => wr_ptr = &wr_ptr[len..], // On error, just drop unwritten data. // One possible error is Err(WouldBlock), meaning the USB // write buffer is full. Err(_) => break, }; } } // End of file