Arduino-Pedal-Effects/rust/pico/src/main.rs

172 lines
4.6 KiB
Rust
Raw Normal View History

2022-07-28 11:43:49 +00:00
#![no_std]
#![no_main]
mod serial;
2022-10-28 15:33:44 +00:00
2022-07-28 11:43:49 +00:00
use panic_probe as _;
2023-01-02 09:26:40 +00:00
#[rtic::app(device = rp_pico::hal::pac, peripherals = true)]
mod app {
// 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;
2022-07-28 11:43:49 +00:00
2023-01-02 09:26:40 +00:00
use bsp::hal::{
clocks::{init_clocks_and_plls, Clock},
gpio::{bank0::Gpio26, Floating, Input, Pin},
rom_data::reset_to_usb_boot,
sio::Sio,
timer::{Alarm, Alarm0},
usb::UsbBus,
watchdog::Watchdog,
Adc, Timer,
};
2022-07-28 11:43:49 +00:00
2023-01-02 09:26:40 +00:00
use fugit::MicrosDurationU32;
2022-10-28 15:33:44 +00:00
2023-01-02 09:26:40 +00:00
use embedded_hal::{adc::OneShot, digital::v2::OutputPin};
use usb_device::{class_prelude::*, prelude::*};
2023-01-02 09:26:40 +00:00
use crate::serial::{Commands, Serial};
2023-01-02 09:26:40 +00:00
const SCAN_TIME_US: MicrosDurationU32 = MicrosDurationU32::secs(1);
2022-07-28 11:43:49 +00:00
2023-01-02 09:26:40 +00:00
struct Data {
i: usize,
usb_bus: UsbBusAllocator<UsbBus>,
usb_dev: UsbDevice<'static, UsbBus>,
2022-07-28 11:43:49 +00:00
2023-01-02 09:26:40 +00:00
serial: Serial<'static>,
2022-10-23 12:44:11 +00:00
2023-01-02 09:26:40 +00:00
adc: Adc,
adc_pin_0: Pin<Gpio26, Input<Floating>>,
}
2022-10-28 15:33:44 +00:00
2023-01-02 09:26:40 +00:00
#[shared]
struct Shared {
timer: Timer,
alarm: Alarm0,
2022-10-28 15:33:44 +00:00
2023-01-02 09:26:40 +00:00
data: Data,
}
2022-10-27 06:08:19 +00:00
2023-01-02 09:26:40 +00:00
#[local]
struct Local {}
2023-01-02 09:26:40 +00:00
#[init]
fn init(c: init::Context) -> (Shared, Local, init::Monotonics) {
// Soft-reset does not release the hardware spinlocks
// Release them now to avoid a deadlock after debug or watchdog reset
unsafe {
bsp::hal::sio::spinlock_reset();
}
2022-10-27 06:08:19 +00:00
2023-01-02 09:26:40 +00:00
let mut resets = c.device.RESETS;
let mut watchdog = Watchdog::new(c.device.WATCHDOG);
let external_xtal_freq_hz = 12_000_000u32;
let clocks = init_clocks_and_plls(
external_xtal_freq_hz,
c.device.XOSC,
c.device.CLOCKS,
c.device.PLL_SYS,
c.device.PLL_USB,
&mut resets,
&mut watchdog,
)
.ok()
.unwrap();
let sio = Sio::new(c.device.SIO);
let pins = rp_pico::Pins::new(
c.device.IO_BANK0,
c.device.PADS_BANK0,
sio.gpio_bank0,
&mut resets,
);
let mut timer = Timer::new(c.device.TIMER, &mut resets);
let mut alarm = timer.alarm_0().unwrap();
let _ = alarm.schedule(SCAN_TIME_US);
alarm.enable_interrupt();
let mut led_pin = pins.led.into_push_pull_output();
led_pin.set_low().unwrap();
// Set up the USB driver
let usb_bus = UsbBusAllocator::new(UsbBus::new(
c.device.USBCTRL_REGS,
c.device.USBCTRL_DPRAM,
clocks.usb_clock,
true,
&mut resets,
));
let serial = Serial::new(&usb_bus);
// Create a USB device with a fake VID and PID
let 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 adc = Adc::new(c.device.ADC, &mut resets);
let 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 data = Data {
i: 0,
usb_bus,
usb_dev,
serial,
adc,
adc_pin_0,
};
(Shared { timer, alarm, data }, Local {}, init::Monotonics())
}
2022-10-27 06:08:19 +00:00
2023-01-02 09:26:40 +00:00
#[task(
binds = TIMER_IRQ_0,
priority = 1,
shared = [timer, alarm, data],
local = [tog: bool = true],
)]
fn timer_irq(mut c: timer_irq::Context) {
c.shared.data.lock(|data| {
data.i += 1;
2022-10-28 15:33:44 +00:00
2023-01-02 09:26:40 +00:00
if data.i == 100 {
data.i = 0;
2022-10-28 15:33:44 +00:00
2023-01-02 09:26:40 +00:00
let adc_0_value: u16 = data.adc.read(&mut data.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-10-27 06:08:19 +00:00
2023-01-02 09:26:40 +00:00
data.serial.send(adc_0_value);
}
2022-10-27 06:08:19 +00:00
2023-01-02 09:26:40 +00:00
if data.usb_dev.poll(&mut [&mut data.serial]) {
if let Some(command) = data.serial.read() {
match command {
Commands::Reset => reset_to_usb_boot(0, 0),
}
2022-11-18 14:33:19 +00:00
}
}
2023-01-02 09:26:40 +00:00
});
let mut alarm = c.shared.alarm;
(alarm).lock(|a| {
a.clear_interrupt();
let _ = a.schedule(SCAN_TIME_US);
});
}
}