firmware for my Touchscreen E-Paper Input Module for Framework Laptop 16
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

fw16-epd-main: use once_cell for serial number

+15 -19
+1
Cargo.toml
··· 25 25 crc32fast = { version = "1.4", default-features = false } 26 26 embedded-graphics = { version = "0.8.1", features = ["defmt"] } 27 27 heapless = "0.8" 28 + once_cell = { version = "1.20", default-features = false, features = ["critical-section", "portable-atomic"] }
+1
fw16-epd-main/Cargo.toml
··· 23 23 crc32fast.workspace = true 24 24 embedded-graphics.workspace = true 25 25 heapless.workspace = true 26 + once_cell.workspace = true
+13 -19
fw16-epd-main/src/main.rs
··· 18 18 use mcp9808::reg_conf::{Configuration, ShutdownMode}; 19 19 use mcp9808::reg_res::ResolutionVal; 20 20 use mcp9808::reg_temp_generic::ReadableTempRegister; 21 + use once_cell::sync::OnceCell; 21 22 use portable_atomic::{AtomicBool, AtomicU128, AtomicU8}; 22 23 use portable_atomic::Ordering; 23 24 use usb_device::bus::UsbBusAllocator; ··· 59 60 static mut GLOBAL_USB_BUS: Option<UsbBusAllocator<hal::usb::UsbBus>> = None; 60 61 static mut GLOBAL_USB_SERIAL: Option<SerialPort<hal::usb::UsbBus>> = None; 61 62 62 - static mut SERIAL_NUMBER: [u8; 16] = [b'X'; 16]; 63 + static SERIAL_NUMBER: OnceCell<[u8; 16]> = OnceCell::new(); 63 64 64 65 static TOUCH_EVENT_BUFFER: Mutex<RefCell<TouchEventBuffer>> = Mutex::new(RefCell::new(TouchEventBuffer::new())); 65 66 static TOUCH_ENABLED: AtomicBool = AtomicBool::new(false); ··· 89 90 } 90 91 } 91 92 92 - // Safety: The only time a mutable reference to SERIAL_NUMBER is created is right 93 - // at the start of the program when the serial number is read from the flash. This 94 - // function must never be called before then. 95 - #[allow(static_mut_refs)] 96 93 extern "C" fn serial_number() -> &'static [u8; 16] { 97 - unsafe { &SERIAL_NUMBER } 94 + SERIAL_NUMBER.get().unwrap() 98 95 } 99 96 100 97 struct TouchEventBuffer<const SIZE: usize = 32> { ··· 177 174 let mut id = [0u8; 8]; 178 175 unsafe { rp2040_flash::flash::flash_unique_id(&mut id, true) }; 179 176 let mut id = u64::from_be_bytes(id); 180 - unsafe { 181 - // Safety: This is the only place we modify SERIAL_NUMBER, and this is before any 182 - // shared references are created 183 - #[allow(static_mut_refs)] 184 - for c in SERIAL_NUMBER.iter_mut().rev() { 185 - let nibble = (id & 0x0f) as u8; 186 - *c = match nibble { 187 - 0x0..=0x9 => b'0' + nibble, 188 - 0xa..=0xf => b'A' + nibble - 0xa, 189 - _ => unreachable!(), 190 - }; 191 - id >>= 4; 192 - } 177 + let mut serial = [0u8; 16]; 178 + for c in serial.iter_mut().rev() { 179 + let nibble = (id & 0x0f) as u8; 180 + *c = match nibble { 181 + 0x0..=0x9 => b'0' + nibble, 182 + 0xa..=0xf => b'A' + nibble - 0xa, 183 + _ => unreachable!(), 184 + }; 185 + id >>= 4; 193 186 } 187 + SERIAL_NUMBER.set(serial).unwrap(); 194 188 unsafe { cortex_m::interrupt::enable() }; 195 189 196 190 info!("Framework 16 EPD firmware version {}, serial no. {}", env!("CARGO_PKG_VERSION"), unsafe { core::str::from_utf8_unchecked(serial_number()) });