A personal rust firmware for the Badger 2040 W
at main 1.8 kB view raw
1use crate::FLASH_SIZE; 2use embassy_rp::flash::{Async, ERASE_SIZE}; 3use embassy_rp::peripherals::FLASH; 4use heapless::{String, Vec}; 5use postcard::{from_bytes, to_slice}; 6use serde::{Deserialize, Serialize}; 7use {defmt_rtt as _, panic_probe as _}; 8 9const BSSID_LEN: usize = 1_000; 10 11pub fn save_postcard_to_flash( 12 base_offset: u32, 13 flash: &mut embassy_rp::flash::Flash<'_, FLASH, Async, FLASH_SIZE>, 14 offset: u32, 15 data: &Save, 16) -> Result<(), &'static str> { 17 let mut buf = [0u8; ERASE_SIZE]; 18 19 let mut write_buf = [0u8; ERASE_SIZE]; 20 let written = to_slice(data, &mut write_buf).map_err(|_| "Serialization error")?; 21 22 if written.len() > ERASE_SIZE { 23 return Err("Data too large for flash sector"); 24 } 25 26 flash 27 .blocking_erase( 28 base_offset + offset, 29 base_offset + offset + ERASE_SIZE as u32, 30 ) 31 .map_err(|_| "Erase error")?; 32 33 buf[..written.len()].copy_from_slice(&written); 34 35 flash 36 .blocking_write(base_offset + offset, &buf) 37 .map_err(|_| "Write error")?; 38 39 Ok(()) 40} 41 42pub fn read_postcard_from_flash( 43 base_offset: u32, 44 flash: &mut embassy_rp::flash::Flash<'_, FLASH, Async, FLASH_SIZE>, 45 offset: u32, 46) -> Result<Save, &'static str> { 47 let mut buf = [0u8; ERASE_SIZE]; 48 49 flash 50 .blocking_read(base_offset + offset, &mut buf) 51 .map_err(|_| "Read error")?; 52 53 let data = from_bytes::<Save>(&buf).map_err(|_| "Deserialization error")?; 54 55 Ok(data) 56} 57 58#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)] 59pub struct Save { 60 pub wifi_counted: u32, 61 pub bssid: Vec<String<17>, BSSID_LEN>, 62} 63 64impl Save { 65 pub fn new() -> Self { 66 Self { 67 wifi_counted: 0, 68 bssid: Vec::new(), 69 } 70 } 71}