From eacb979c615ac1eb173fc831103b08dac82a1e34 Mon Sep 17 00:00:00 2001 From: Naxdy Date: Mon, 11 Mar 2024 19:41:34 +0100 Subject: [PATCH] get to a working state with multicore --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/gcc_hid.rs | 41 ++++++++++++++++++++++++++++++++--------- src/input.rs | 6 ++++-- src/main.rs | 39 +++++++++++++++++++++++---------------- 5 files changed, 67 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4a3b932..6671b51 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -167,6 +167,12 @@ dependencies = [ "void", ] +[[package]] +name = "format_no_std" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d8ca877c98f19024674b6959301d81d3708c417823fa050692b45b54d74fe7d" + [[package]] name = "frunk" version = "0.4.2" @@ -264,6 +270,7 @@ dependencies = [ "defmt", "defmt-rtt", "embedded-hal", + "format_no_std", "fugit", "packed_struct", "panic-halt", diff --git a/Cargo.toml b/Cargo.toml index b9a0b9e..21acbd5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ rp2040-boot2 = "0.3.0" fugit = "0.3.7" packed_struct = { version = "0.10.1", default_features = false } panic-halt = "0.2.0" +format_no_std = "1.0.2" # cargo build/run [profile.dev] diff --git a/src/gcc_hid.rs b/src/gcc_hid.rs index 4162072..8293845 100644 --- a/src/gcc_hid.rs +++ b/src/gcc_hid.rs @@ -1,9 +1,10 @@ use core::default::Default; -use defmt::{error, info, unwrap, Debug2Format}; +use defmt::{error, info, unwrap, Debug2Format, Format}; use embedded_hal::timer::CountDown as _; use fugit::ExtU32; use packed_struct::{derive::PackedStruct, PackedStruct}; +use rp2040_flash::flash::flash_unique_id; use rp2040_hal::timer::CountDown; use usb_device::{ bus::{UsbBus, UsbBusAllocator}, @@ -20,7 +21,7 @@ use usbd_human_interface_device::{ UsbHidError, }; -use crate::input::GCC_STATE; +use crate::{input::GCC_STATE, CORE_LOCK, LOCKED}; #[rustfmt::skip] pub const GCC_REPORT_DESCRIPTOR: &[u8] = &[ @@ -79,7 +80,7 @@ pub const GCC_REPORT_DESCRIPTOR: &[u8] = &[ 0xC0, // End Collection ]; -#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, PackedStruct)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, PackedStruct, Format)] #[packed_struct(bit_numbering = "lsb0", size_bytes = "1")] pub struct Buttons1 { #[packed_field(bits = "0")] @@ -100,7 +101,7 @@ pub struct Buttons1 { pub dpad_up: bool, } -#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, PackedStruct)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, PackedStruct, Format)] #[packed_struct(bit_numbering = "lsb0", size_bytes = "1")] pub struct Buttons2 { #[packed_field(bits = "0")] @@ -115,7 +116,7 @@ pub struct Buttons2 { pub blank1: u8, } -#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, PackedStruct)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, PackedStruct, Format)] #[packed_struct(bit_numbering = "msb0", size_bytes = "8")] pub struct GcReport { #[packed_field(bits = "0..=7")] @@ -248,17 +249,35 @@ pub fn usb_transfer_loop<'a, T: UsbBus>( usb_bus: UsbBusAllocator, mut poll_timer: CountDown<'a>, ) -> ! { - info!("Got to this point"); + // let mut serial_buffer = [0u8; 64]; + + // let serial = unsafe { + // let mut id = [0u8; 8]; + + // flash_unique_id(&mut id, true); + + // let s = format_no_std::show( + // &mut serial_buffer, + // format_args!( + // "{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}", + // id[0], id[1], id[2], id[3], id[4], id[5], id[6], id[7] + // ), + // ) + // .unwrap(); + + // info!("Detected flash with unique serial number {}", s); + + // s + // }; + let mut gcc = UsbHidClassBuilder::new() .add_device(GcConfig::default()) .build(&usb_bus); - info!("Got the gc"); - let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x057e, 0x0337)) .manufacturer("Naxdy") .product("NaxGCC") - .serial_number("fleeb") // TODO: Get this from the flash unique id + .serial_number("flarn") .device_class(0) .device_protocol(0) .device_sub_class(0) @@ -272,6 +291,10 @@ pub fn usb_transfer_loop<'a, T: UsbBus>( info!("Got here"); loop { + if unsafe { LOCKED } { + continue; + } + if poll_timer.wait().is_ok() { match gcc.device().write_report(&(unsafe { GCC_STATE })) { Err(UsbHidError::WouldBlock) => {} diff --git a/src/input.rs b/src/input.rs index 6f4488d..3c02b40 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,5 +1,5 @@ -use embedded_hal::digital::{v2::InputPin, v2::IoPin}; -use rp2040_hal::gpio::{FunctionSpi, Pin, PinId, PullDown}; +use defmt::info; +use embedded_hal::digital::v2::InputPin; use crate::gcc_hid::{Buttons1, Buttons2, GcReport}; @@ -83,6 +83,8 @@ pub fn input_loop< >( basic_inputs: BasicInputs, ) -> ! { + info!("Input loop started."); + let update_gcc_state = || unsafe { // simple booleans assign_pins!(GCC_STATE, basic_inputs, { diff --git a/src/main.rs b/src/main.rs index 4f5b29f..3ada658 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,16 +12,17 @@ mod flash_mem; mod gcc_hid; mod input; +use cortex_m::interrupt::Mutex; use defmt::{error, info}; -use gcc_hid::GcConfig; use fugit::{ExtU32, RateExtU32}; // Ensure we halt the program on panic (if we don't mention this crate it won't // be linked) use defmt_rtt as _; -use panic_halt as _; +use panic_probe as _; +use rp2040_flash::flash::flash_unique_id; // Alias for our HAL crate use rp2040_hal as hal; @@ -29,23 +30,19 @@ use rp2040_hal as hal; // register access use hal::{ gpio::FunctionSpi, - multicore::{Multicore, Stack}, + multicore::{self, Multicore, Stack}, pac, Spi, }; // Some traits we need use embedded_hal::{ - blocking::spi::Transfer, + blocking::{delay::DelayMs, spi::Transfer}, digital::v2::OutputPin, - spi::{FullDuplex, MODE_0}, + spi::MODE_0, timer::CountDown, }; -use usb_device::{ - bus::UsbBusAllocator, - device::{UsbDeviceBuilder, UsbVidPid}, -}; -use usbd_human_interface_device::usb_class::UsbHidClassBuilder; +use usb_device::bus::UsbBusAllocator; use crate::{ flash_mem::{read_from_flash, write_to_flash}, @@ -55,6 +52,10 @@ use crate::{ static mut CORE1_STACK: Stack<4096> = Stack::new(); +pub static mut LOCKED: bool = false; + +pub static CORE_LOCK: Mutex<()> = Mutex::new(()); + /// The linker will place this boot block at the start of our program image. We /// need this to help the ROM bootloader get our code up and running. /// Note: This boot block is not necessary when using a rp-hal based BSP @@ -95,7 +96,7 @@ fn main() -> ! { .ok() .unwrap(); - let timer = rp2040_hal::Timer::new(pac.TIMER, &mut pac.RESETS, &clocks); + let mut timer = rp2040_hal::Timer::new(pac.TIMER, &mut pac.RESETS, &clocks); // The single-cycle I/O block controls our GPIO pins let mut sio = hal::Sio::new(pac.SIO); @@ -118,9 +119,6 @@ fn main() -> ! { )); unsafe { - let some_byte: u8 = 0xAB; - info!("Byte to be written is {:02X}", some_byte); - write_to_flash(some_byte); let r = read_from_flash(); info!("Byte read from flash is {:02X}", r); } @@ -143,8 +141,8 @@ fn main() -> ! { let mut ccs = pins.gpio23.into_push_pull_output(); let mut acs = pins.gpio24.into_push_pull_output(); - ccs.set_low(); - acs.set_low(); + ccs.set_high().unwrap(); + acs.set_high().unwrap(); let spi_device = pac.SPI0; @@ -176,6 +174,15 @@ fn main() -> ! { } } + unsafe { + LOCKED = true; + timer.delay_ms(100); + let some_byte: u8 = 0xAB; + info!("Byte to be written is {:02X}", some_byte); + write_to_flash(some_byte); + LOCKED = false; + } + input_loop(BasicInputs { button_a: pins.gpio17.into_pull_up_input(), button_b: pins.gpio16.into_pull_up_input(),