2022-07-28 11:43:49 +00:00
|
|
|
//! 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]
|
|
|
|
|
2022-11-01 08:51:00 +00:00
|
|
|
mod serial;
|
2022-10-28 15:33:44 +00:00
|
|
|
|
2022-07-28 11:43:49 +00:00
|
|
|
use bsp::entry;
|
|
|
|
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 bsp::hal::{
|
|
|
|
clocks::{init_clocks_and_plls, Clock},
|
|
|
|
pac,
|
2022-11-18 14:33:19 +00:00
|
|
|
rom_data::reset_to_usb_boot,
|
2022-07-28 11:43:49 +00:00
|
|
|
sio::Sio,
|
2022-10-26 05:28:59 +00:00
|
|
|
usb::UsbBus,
|
2022-07-28 11:43:49 +00:00
|
|
|
watchdog::Watchdog,
|
2022-10-28 15:33:44 +00:00
|
|
|
Adc,
|
2022-07-28 11:43:49 +00:00
|
|
|
};
|
|
|
|
|
2022-10-28 15:33:44 +00:00
|
|
|
use embedded_hal::adc::OneShot;
|
|
|
|
|
2022-10-26 05:28:59 +00:00
|
|
|
use usb_device::{class_prelude::*, prelude::*};
|
2022-11-01 08:51:00 +00:00
|
|
|
|
2022-11-18 14:33:19 +00:00
|
|
|
use crate::serial::{Commands, Serial};
|
2022-10-26 05:28:59 +00:00
|
|
|
|
2022-07-28 11:43:49 +00:00
|
|
|
#[entry]
|
|
|
|
fn main() -> ! {
|
|
|
|
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();
|
|
|
|
|
2022-10-23 12:44:11 +00:00
|
|
|
let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz());
|
|
|
|
|
|
|
|
let sio = Sio::new(pac.SIO);
|
2022-07-28 11:43:49 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2022-11-01 08:51:00 +00:00
|
|
|
led_pin.set_low().unwrap();
|
2022-10-26 05:28:59 +00:00
|
|
|
|
|
|
|
// 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,
|
|
|
|
));
|
|
|
|
|
2022-11-01 08:51:00 +00:00
|
|
|
let mut serial = Serial::new(&usb_bus);
|
2022-10-26 05:28:59 +00:00
|
|
|
|
|
|
|
// Create a USB device with a fake VID and PID
|
2022-10-27 06:08:19 +00:00
|
|
|
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
|
2022-10-26 05:28:59 +00:00
|
|
|
.manufacturer("Fake company")
|
|
|
|
.product("Serial port")
|
|
|
|
.serial_number("TEST")
|
|
|
|
.device_class(2) // from: https://www.usb.org/defined-class-codes
|
|
|
|
.build();
|
|
|
|
|
2022-10-28 15:33:44 +00:00
|
|
|
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();
|
|
|
|
|
2022-10-27 06:08:19 +00:00
|
|
|
let mut i = 0;
|
|
|
|
|
2022-07-28 11:43:49 +00:00
|
|
|
loop {
|
2022-10-26 05:28:59 +00:00
|
|
|
// serial.write("test".as_bytes());
|
|
|
|
|
|
|
|
// serial_send(&mut serial, "test");
|
|
|
|
// delay.delay_ms(1000);
|
2022-10-27 06:08:19 +00:00
|
|
|
|
|
|
|
i += 1;
|
|
|
|
|
|
|
|
if i == 100 {
|
|
|
|
i = 0;
|
2022-10-28 15:33:44 +00:00
|
|
|
|
|
|
|
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();
|
|
|
|
|
2022-11-01 08:51:00 +00:00
|
|
|
serial.send(adc_0_value);
|
2022-10-27 06:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delay.delay_ms(8);
|
|
|
|
|
|
|
|
if usb_dev.poll(&mut [&mut serial]) {
|
2022-11-18 14:33:19 +00:00
|
|
|
if let Some(command) = serial.read() {
|
|
|
|
match command {
|
|
|
|
Commands::Reset => reset_to_usb_boot(0, 0),
|
|
|
|
}
|
|
|
|
}
|
2022-10-27 06:08:19 +00:00
|
|
|
}
|
2022-10-26 05:28:59 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-23 12:44:11 +00:00
|
|
|
|
2022-07-28 11:43:49 +00:00
|
|
|
// End of file
|