rmk_q6_he_ansi/flash_wrapper_async.rs
1// Based on the implementation from https://github.com/anpage/rmk-keychron-q6-max/blob/main/src/flash16k.rs
2// Copyright (C) 2025 Alex Page
3// Modified by Kurt Fuchs to support async flash operations for RMK
4// Copyright (C) 2026
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <http://www.gnu.org/licenses/>.
18//
19
20use embedded_storage_async::nor_flash::{ErrorType, NorFlash, ReadNorFlash};
21
22/// Wrapper to report 16KB erase size for STM32F4 sectors 1-2.
23pub struct Flash16K<F>(pub F);
24
25impl<F: ErrorType> ErrorType for Flash16K<F> {
26 /// Error type propagated from the wrapped flash implementation.
27 type Error = F::Error;
28}
29
30impl<F: ReadNorFlash> ReadNorFlash for Flash16K<F> {
31 /// Read granularity for the underlying flash.
32 const READ_SIZE: usize = F::READ_SIZE;
33
34 /// Read bytes from the flash at the given offset.
35 async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> {
36 self.0.read(offset, bytes).await
37 }
38
39 /// Return the total capacity of the flash device.
40 fn capacity(&self) -> usize { self.0.capacity() }
41}
42
43#[rustfmt::skip]
44impl<F: NorFlash> NorFlash for Flash16K<F> {
45 /// Write granularity for the underlying flash.
46 const WRITE_SIZE: usize = F::WRITE_SIZE;
47 /// Erase size reported as 16 KiB - matches STM32F4 sectors 1 and 2.
48 const ERASE_SIZE: usize = 16_usize.saturating_mul(1024);
49
50 /// Erase the flash range between the given offsets.
51 async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { self.0.erase(from, to).await }
52
53 /// Write bytes to the flash at the given offset.
54 async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> {
55 self.0.write(offset, bytes).await
56 }
57}