fix(flash): access after a delay
All checks were successful
Publish nightly release / build (push) Successful in 1m22s

apparently accessing the flash immediately after boot poses some issues
This commit is contained in:
Naxdy 2024-04-03 01:46:14 +02:00
parent ba50594425
commit d6bb5d5266
Signed by: Naxdy
GPG key ID: CC15075846BCE91B
4 changed files with 37 additions and 22 deletions

View file

@ -10,7 +10,7 @@ use core::{
use defmt::{debug, error, info, warn, Format}; use defmt::{debug, error, info, warn, Format};
use embassy_futures::yield_now; use embassy_futures::yield_now;
use embassy_rp::{ use embassy_rp::{
flash::{Async, Flash, ERASE_SIZE}, flash::{Async, Blocking, Flash, ERASE_SIZE},
peripherals::FLASH, peripherals::FLASH,
}; };
use packed_struct::{derive::PackedStruct, PackedStruct}; use packed_struct::{derive::PackedStruct, PackedStruct};
@ -476,7 +476,7 @@ enum NotchAdjustmentType {
/// This needs to be incremented for ANY change to ControllerConfig /// This needs to be incremented for ANY change to ControllerConfig
/// else we risk loading uninitialized memory. /// else we risk loading uninitialized memory.
pub const CONTROLLER_CONFIG_REVISION: u8 = 1; pub const CONTROLLER_CONFIG_REVISION: u8 = 21;
#[derive(Debug, Clone, Format, PackedStruct)] #[derive(Debug, Clone, Format, PackedStruct)]
#[packed_struct(endian = "msb")] #[packed_struct(endian = "msb")]
@ -563,7 +563,15 @@ impl ControllerConfig {
mut flash: &mut Flash<'static, FLASH, Async, FLASH_SIZE>, mut flash: &mut Flash<'static, FLASH, Async, FLASH_SIZE>,
) -> Result<Self, embassy_rp::flash::Error> { ) -> Result<Self, embassy_rp::flash::Error> {
let mut controller_config_packed: <ControllerConfig as packed_struct::PackedStruct>::ByteArray = [0u8; 659]; // ControllerConfig byte size let mut controller_config_packed: <ControllerConfig as packed_struct::PackedStruct>::ByteArray = [0u8; 659]; // ControllerConfig byte size
flash.blocking_read(ADDR_OFFSET, &mut controller_config_packed)?;
let r = flash.blocking_read(ADDR_OFFSET, &mut controller_config_packed);
if let Err(_) = r {
warn!("Controller config not found in flash, using default.");
controller_config_packed = [0u8; 659];
} else {
r.unwrap();
}
match ControllerConfig::unpack(&controller_config_packed).unwrap() { match ControllerConfig::unpack(&controller_config_packed).unwrap() {
a if a.config_revision == CONTROLLER_CONFIG_REVISION => { a if a.config_revision == CONTROLLER_CONFIG_REVISION => {
@ -1588,7 +1596,7 @@ async fn configuration_main_loop<
a.buttons_1.button_x = true; a.buttons_1.button_x = true;
a.buttons_1.button_a = true; a.buttons_1.button_a = true;
a.stick_x = 127; a.stick_x = 127;
a.stick_y = (127 a.stick_y = (127 as i8
+ match final_config.input_consistency_mode { + match final_config.input_consistency_mode {
true => 69, true => 69,
false => -69, false => -69,
@ -1626,6 +1634,13 @@ pub async fn config_task(
info!("Config task is running."); info!("Config task is running.");
Timer::after_millis(1000).await;
let new_config = ControllerConfig::from_flash_memory(&mut flash).unwrap();
SIGNAL_CHANGE_RUMBLE_STRENGTH.signal(new_config.rumble_strength);
SIGNAL_CONFIG_CHANGE.signal(new_config);
loop { loop {
let desired_config_state = SIGNAL_CONFIG_MODE_STATUS_CHANGE.wait().await; let desired_config_state = SIGNAL_CONFIG_MODE_STATUS_CHANGE.wait().await;

View file

@ -261,11 +261,9 @@ impl Handler for MyDeviceHandler {
} }
#[embassy_executor::task] #[embassy_executor::task]
pub async fn usb_transfer_task( pub async fn usb_transfer_task(driver: Driver<'static, USB>, input_consistency_mode: bool) {
driver: Driver<'static, USB>, let raw_serial = [0u8; 8];
raw_serial: [u8; 8],
input_consistency_mode: bool,
) {
let mut serial_buffer = [0u8; 64]; let mut serial_buffer = [0u8; 64];
let serial = format_no_std::show( let serial = format_no_std::show(

View file

@ -148,7 +148,7 @@ async fn update_stick_states(
let mut cx_sum = 0u32; let mut cx_sum = 0u32;
let mut cy_sum = 0u32; let mut cy_sum = 0u32;
let end_time = Instant::now() + Duration::from_micros(250); // this seems kinda magic, and it is, but let end_time = Instant::now() + Duration::from_micros(200); // this seems kinda magic, and it is, but
let mut spi_unlocked = SPI_SHARED.lock().await; let mut spi_unlocked = SPI_SHARED.lock().await;
let mut spi_acs_unlocked = SPI_ACS_SHARED.lock().await; let mut spi_acs_unlocked = SPI_ACS_SHARED.lock().await;
@ -377,6 +377,14 @@ fn update_button_states<
gcc_state.buttons_1.dpad_right = btn_dright.is_low(); gcc_state.buttons_1.dpad_right = btn_dright.is_low();
gcc_state.buttons_1.dpad_up = btn_dup.is_low(); gcc_state.buttons_1.dpad_up = btn_dup.is_low();
gcc_state.buttons_1.dpad_down = btn_ddown.is_low(); gcc_state.buttons_1.dpad_down = btn_ddown.is_low();
gcc_state.trigger_l = match gcc_state.buttons_2.button_l {
true => 255,
false => 0,
};
gcc_state.trigger_r = match gcc_state.buttons_2.button_r {
true => 255,
false => 0,
};
} }
#[embassy_executor::task] #[embassy_executor::task]
@ -386,8 +394,6 @@ pub async fn input_integrity_benchmark() {
report: { report: {
let mut report = GcReport::default(); let mut report = GcReport::default();
report.buttons_1.dpad_up = true; report.buttons_1.dpad_up = true;
report.cstick_x = 0;
report.cstick_y = 0;
report report
}, },
duration_ms: 100, duration_ms: 100,
@ -501,7 +507,7 @@ pub async fn update_stick_states_task(
spi_ccs: Output<'static, AnyPin>, spi_ccs: Output<'static, AnyPin>,
mut controller_config: ControllerConfig, mut controller_config: ControllerConfig,
) { ) {
Timer::after_secs(1).await; Timer::after_secs(5).await;
*SPI_SHARED.lock().await = Some(spi); *SPI_SHARED.lock().await = Some(spi);
*SPI_ACS_SHARED.lock().await = Some(spi_acs); *SPI_ACS_SHARED.lock().await = Some(spi_acs);
*SPI_CCS_SHARED.lock().await = Some(spi_ccs); *SPI_CCS_SHARED.lock().await = Some(spi_ccs);
@ -559,7 +565,7 @@ pub async fn update_stick_states_task(
match Instant::now() { match Instant::now() {
n => { n => {
match (n - last_loop_time).as_micros() { match (n - last_loop_time).as_micros() {
a if a > 1999 => debug!("Loop took {} us", a), a if a > 1666 => debug!("Loop took {} us", a),
_ => {} _ => {}
}; };
last_loop_time = n; last_loop_time = n;

View file

@ -17,6 +17,7 @@ use defmt::{debug, info};
use embassy_executor::Executor; use embassy_executor::Executor;
use embassy_executor::InterruptExecutor; use embassy_executor::InterruptExecutor;
use embassy_futures::join::join; use embassy_futures::join::join;
use embassy_rp::flash::Blocking;
use embassy_rp::interrupt; use embassy_rp::interrupt;
use embassy_rp::interrupt::InterruptExt; use embassy_rp::interrupt::InterruptExt;
use embassy_rp::{ use embassy_rp::{
@ -29,6 +30,7 @@ use embassy_rp::{
spi::{self, Spi}, spi::{self, Spi},
usb::{Driver, InterruptHandler}, usb::{Driver, InterruptHandler},
}; };
use embassy_time::Instant;
use gcc_hid::usb_transfer_task; use gcc_hid::usb_transfer_task;
use gpio::{Level, Output}; use gpio::{Level, Output};
@ -62,14 +64,9 @@ fn main() -> ! {
// reading and writing from flash has to be done on the main thread, else funny things happen. // reading and writing from flash has to be done on the main thread, else funny things happen.
let mut flash = Flash::<_, Async, FLASH_SIZE>::new(p.FLASH, p.DMA_CH0); let flash = Flash::<_, Async, FLASH_SIZE>::new(p.FLASH, p.DMA_CH0);
let mut uid = [0u8; 8]; let controller_config = ControllerConfig::default();
flash.blocking_unique_id(&mut uid).unwrap();
let controller_config = ControllerConfig::from_flash_memory(&mut flash).unwrap();
debug!("Read unique id: {:02X}", uid);
let mosi = p.PIN_7; let mosi = p.PIN_7;
let miso = p.PIN_4; let miso = p.PIN_4;
@ -107,7 +104,6 @@ fn main() -> ! {
spawner spawner
.spawn(usb_transfer_task( .spawn(usb_transfer_task(
driver, driver,
uid,
controller_config.input_consistency_mode, controller_config.input_consistency_mode,
)) ))
.unwrap(); .unwrap();