get to a working state with multicore
This commit is contained in:
parent
6ba86985d5
commit
eacb979c61
5 changed files with 67 additions and 27 deletions
7
Cargo.lock
generated
7
Cargo.lock
generated
|
@ -167,6 +167,12 @@ dependencies = [
|
||||||
"void",
|
"void",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "format_no_std"
|
||||||
|
version = "1.0.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2d8ca877c98f19024674b6959301d81d3708c417823fa050692b45b54d74fe7d"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "frunk"
|
name = "frunk"
|
||||||
version = "0.4.2"
|
version = "0.4.2"
|
||||||
|
@ -264,6 +270,7 @@ dependencies = [
|
||||||
"defmt",
|
"defmt",
|
||||||
"defmt-rtt",
|
"defmt-rtt",
|
||||||
"embedded-hal",
|
"embedded-hal",
|
||||||
|
"format_no_std",
|
||||||
"fugit",
|
"fugit",
|
||||||
"packed_struct",
|
"packed_struct",
|
||||||
"panic-halt",
|
"panic-halt",
|
||||||
|
|
|
@ -20,6 +20,7 @@ rp2040-boot2 = "0.3.0"
|
||||||
fugit = "0.3.7"
|
fugit = "0.3.7"
|
||||||
packed_struct = { version = "0.10.1", default_features = false }
|
packed_struct = { version = "0.10.1", default_features = false }
|
||||||
panic-halt = "0.2.0"
|
panic-halt = "0.2.0"
|
||||||
|
format_no_std = "1.0.2"
|
||||||
|
|
||||||
# cargo build/run
|
# cargo build/run
|
||||||
[profile.dev]
|
[profile.dev]
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
use core::default::Default;
|
use core::default::Default;
|
||||||
|
|
||||||
use defmt::{error, info, unwrap, Debug2Format};
|
use defmt::{error, info, unwrap, Debug2Format, Format};
|
||||||
use embedded_hal::timer::CountDown as _;
|
use embedded_hal::timer::CountDown as _;
|
||||||
use fugit::ExtU32;
|
use fugit::ExtU32;
|
||||||
use packed_struct::{derive::PackedStruct, PackedStruct};
|
use packed_struct::{derive::PackedStruct, PackedStruct};
|
||||||
|
use rp2040_flash::flash::flash_unique_id;
|
||||||
use rp2040_hal::timer::CountDown;
|
use rp2040_hal::timer::CountDown;
|
||||||
use usb_device::{
|
use usb_device::{
|
||||||
bus::{UsbBus, UsbBusAllocator},
|
bus::{UsbBus, UsbBusAllocator},
|
||||||
|
@ -20,7 +21,7 @@ use usbd_human_interface_device::{
|
||||||
UsbHidError,
|
UsbHidError,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::input::GCC_STATE;
|
use crate::{input::GCC_STATE, CORE_LOCK, LOCKED};
|
||||||
|
|
||||||
#[rustfmt::skip]
|
#[rustfmt::skip]
|
||||||
pub const GCC_REPORT_DESCRIPTOR: &[u8] = &[
|
pub const GCC_REPORT_DESCRIPTOR: &[u8] = &[
|
||||||
|
@ -79,7 +80,7 @@ pub const GCC_REPORT_DESCRIPTOR: &[u8] = &[
|
||||||
0xC0, // End Collection
|
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")]
|
#[packed_struct(bit_numbering = "lsb0", size_bytes = "1")]
|
||||||
pub struct Buttons1 {
|
pub struct Buttons1 {
|
||||||
#[packed_field(bits = "0")]
|
#[packed_field(bits = "0")]
|
||||||
|
@ -100,7 +101,7 @@ pub struct Buttons1 {
|
||||||
pub dpad_up: bool,
|
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")]
|
#[packed_struct(bit_numbering = "lsb0", size_bytes = "1")]
|
||||||
pub struct Buttons2 {
|
pub struct Buttons2 {
|
||||||
#[packed_field(bits = "0")]
|
#[packed_field(bits = "0")]
|
||||||
|
@ -115,7 +116,7 @@ pub struct Buttons2 {
|
||||||
pub blank1: u8,
|
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")]
|
#[packed_struct(bit_numbering = "msb0", size_bytes = "8")]
|
||||||
pub struct GcReport {
|
pub struct GcReport {
|
||||||
#[packed_field(bits = "0..=7")]
|
#[packed_field(bits = "0..=7")]
|
||||||
|
@ -248,17 +249,35 @@ pub fn usb_transfer_loop<'a, T: UsbBus>(
|
||||||
usb_bus: UsbBusAllocator<T>,
|
usb_bus: UsbBusAllocator<T>,
|
||||||
mut poll_timer: CountDown<'a>,
|
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()
|
let mut gcc = UsbHidClassBuilder::new()
|
||||||
.add_device(GcConfig::default())
|
.add_device(GcConfig::default())
|
||||||
.build(&usb_bus);
|
.build(&usb_bus);
|
||||||
|
|
||||||
info!("Got the gc");
|
|
||||||
|
|
||||||
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x057e, 0x0337))
|
let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x057e, 0x0337))
|
||||||
.manufacturer("Naxdy")
|
.manufacturer("Naxdy")
|
||||||
.product("NaxGCC")
|
.product("NaxGCC")
|
||||||
.serial_number("fleeb") // TODO: Get this from the flash unique id
|
.serial_number("flarn")
|
||||||
.device_class(0)
|
.device_class(0)
|
||||||
.device_protocol(0)
|
.device_protocol(0)
|
||||||
.device_sub_class(0)
|
.device_sub_class(0)
|
||||||
|
@ -272,6 +291,10 @@ pub fn usb_transfer_loop<'a, T: UsbBus>(
|
||||||
info!("Got here");
|
info!("Got here");
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
if unsafe { LOCKED } {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if poll_timer.wait().is_ok() {
|
if poll_timer.wait().is_ok() {
|
||||||
match gcc.device().write_report(&(unsafe { GCC_STATE })) {
|
match gcc.device().write_report(&(unsafe { GCC_STATE })) {
|
||||||
Err(UsbHidError::WouldBlock) => {}
|
Err(UsbHidError::WouldBlock) => {}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use embedded_hal::digital::{v2::InputPin, v2::IoPin};
|
use defmt::info;
|
||||||
use rp2040_hal::gpio::{FunctionSpi, Pin, PinId, PullDown};
|
use embedded_hal::digital::v2::InputPin;
|
||||||
|
|
||||||
use crate::gcc_hid::{Buttons1, Buttons2, GcReport};
|
use crate::gcc_hid::{Buttons1, Buttons2, GcReport};
|
||||||
|
|
||||||
|
@ -83,6 +83,8 @@ pub fn input_loop<
|
||||||
>(
|
>(
|
||||||
basic_inputs: BasicInputs<A, B, X, Y, Dl, Dr, Dd, Du, S, Z, R, L>,
|
basic_inputs: BasicInputs<A, B, X, Y, Dl, Dr, Dd, Du, S, Z, R, L>,
|
||||||
) -> ! {
|
) -> ! {
|
||||||
|
info!("Input loop started.");
|
||||||
|
|
||||||
let update_gcc_state = || unsafe {
|
let update_gcc_state = || unsafe {
|
||||||
// simple booleans
|
// simple booleans
|
||||||
assign_pins!(GCC_STATE, basic_inputs, {
|
assign_pins!(GCC_STATE, basic_inputs, {
|
||||||
|
|
39
src/main.rs
39
src/main.rs
|
@ -12,16 +12,17 @@ mod flash_mem;
|
||||||
mod gcc_hid;
|
mod gcc_hid;
|
||||||
mod input;
|
mod input;
|
||||||
|
|
||||||
|
use cortex_m::interrupt::Mutex;
|
||||||
use defmt::{error, info};
|
use defmt::{error, info};
|
||||||
use gcc_hid::GcConfig;
|
|
||||||
|
|
||||||
use fugit::{ExtU32, RateExtU32};
|
use fugit::{ExtU32, RateExtU32};
|
||||||
|
|
||||||
// Ensure we halt the program on panic (if we don't mention this crate it won't
|
// Ensure we halt the program on panic (if we don't mention this crate it won't
|
||||||
// be linked)
|
// be linked)
|
||||||
use defmt_rtt as _;
|
use defmt_rtt as _;
|
||||||
use panic_halt as _;
|
use panic_probe as _;
|
||||||
|
|
||||||
|
use rp2040_flash::flash::flash_unique_id;
|
||||||
// Alias for our HAL crate
|
// Alias for our HAL crate
|
||||||
use rp2040_hal as hal;
|
use rp2040_hal as hal;
|
||||||
|
|
||||||
|
@ -29,23 +30,19 @@ use rp2040_hal as hal;
|
||||||
// register access
|
// register access
|
||||||
use hal::{
|
use hal::{
|
||||||
gpio::FunctionSpi,
|
gpio::FunctionSpi,
|
||||||
multicore::{Multicore, Stack},
|
multicore::{self, Multicore, Stack},
|
||||||
pac, Spi,
|
pac, Spi,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Some traits we need
|
// Some traits we need
|
||||||
use embedded_hal::{
|
use embedded_hal::{
|
||||||
blocking::spi::Transfer,
|
blocking::{delay::DelayMs, spi::Transfer},
|
||||||
digital::v2::OutputPin,
|
digital::v2::OutputPin,
|
||||||
spi::{FullDuplex, MODE_0},
|
spi::MODE_0,
|
||||||
timer::CountDown,
|
timer::CountDown,
|
||||||
};
|
};
|
||||||
|
|
||||||
use usb_device::{
|
use usb_device::bus::UsbBusAllocator;
|
||||||
bus::UsbBusAllocator,
|
|
||||||
device::{UsbDeviceBuilder, UsbVidPid},
|
|
||||||
};
|
|
||||||
use usbd_human_interface_device::usb_class::UsbHidClassBuilder;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
flash_mem::{read_from_flash, write_to_flash},
|
flash_mem::{read_from_flash, write_to_flash},
|
||||||
|
@ -55,6 +52,10 @@ use crate::{
|
||||||
|
|
||||||
static mut CORE1_STACK: Stack<4096> = Stack::new();
|
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
|
/// 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.
|
/// 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
|
/// Note: This boot block is not necessary when using a rp-hal based BSP
|
||||||
|
@ -95,7 +96,7 @@ fn main() -> ! {
|
||||||
.ok()
|
.ok()
|
||||||
.unwrap();
|
.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
|
// The single-cycle I/O block controls our GPIO pins
|
||||||
let mut sio = hal::Sio::new(pac.SIO);
|
let mut sio = hal::Sio::new(pac.SIO);
|
||||||
|
@ -118,9 +119,6 @@ fn main() -> ! {
|
||||||
));
|
));
|
||||||
|
|
||||||
unsafe {
|
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();
|
let r = read_from_flash();
|
||||||
info!("Byte read from flash is {:02X}", r);
|
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 ccs = pins.gpio23.into_push_pull_output();
|
||||||
let mut acs = pins.gpio24.into_push_pull_output();
|
let mut acs = pins.gpio24.into_push_pull_output();
|
||||||
|
|
||||||
ccs.set_low();
|
ccs.set_high().unwrap();
|
||||||
acs.set_low();
|
acs.set_high().unwrap();
|
||||||
|
|
||||||
let spi_device = pac.SPI0;
|
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 {
|
input_loop(BasicInputs {
|
||||||
button_a: pins.gpio17.into_pull_up_input(),
|
button_a: pins.gpio17.into_pull_up_input(),
|
||||||
button_b: pins.gpio16.into_pull_up_input(),
|
button_b: pins.gpio16.into_pull_up_input(),
|
||||||
|
|
Loading…
Reference in a new issue