embassy_utils/flash/
flash_lock.rs1use embassy_rp_plus::embassy_rp::flash;
2use embassy_rp_plus::embassy_rp::flash::{Async, Flash, Instance, Mode};
3use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
4use embassy_sync::mutex::Mutex;
5use crate::flash::flash_util::FlashUtil;
6
7pub struct FlashLock<'a, T: Instance, M: Mode, const FLASH_SIZE: usize> {
9 flash: Mutex<CriticalSectionRawMutex, Flash<'a, T, M, FLASH_SIZE>>,
11}
12
13impl<'a, T: Instance, M: Mode, const FLASH_SIZE: usize> FlashLock<'a, T, M, FLASH_SIZE> {
15 #[inline]
17 pub fn new(flash: Flash<'a, T, M, FLASH_SIZE>) -> Self {
18 Self { flash: Mutex::new(flash) }
19 }
20
21 #[inline]
23 pub fn build_flash_util(&'a self, offset: u32, erase_size: u32) -> FlashUtil<'a, T, M, FLASH_SIZE> {
24 FlashUtil::new(self, offset, erase_size)
25 }
26
27 #[inline]
29 pub fn build_flash_util_default(&'a self) -> FlashUtil<'a, T, M, FLASH_SIZE> {
30 FlashUtil::new_default(self)
31 }
32
33 #[inline]
35 pub async fn capacity(&self) -> usize {
36 self.flash.lock().await.capacity()
37 }
38
39 #[inline]
41 pub async fn blocking_jedec_id(&self) -> Result<u32, flash::Error> {
42 self.flash.lock().await.blocking_jedec_id()
43 }
44
45 #[inline]
47 pub async fn blocking_unique_id(&self, uid: &mut [u8]) -> Result<(), flash::Error> {
48 self.flash.lock().await.blocking_unique_id(uid)
49 }
50
51 #[inline]
53 pub async fn blocking_read(&self, offset: u32, bytes: &mut [u8]) -> Result<(), flash::Error> {
54 self.flash.lock().await.blocking_read(offset, bytes)
55 }
56
57 #[inline]
59 pub async fn blocking_erase(&self, from: u32, to: u32) -> Result<(), flash::Error> {
60 self.flash.lock().await.blocking_erase(from, to)
61 }
62
63 #[inline]
65 pub async fn blocking_write(&self, offset: u32, bytes: &[u8]) -> Result<(), flash::Error> {
66 self.flash.lock().await.blocking_write(offset, bytes)
67 }
68
69 pub async fn try_erase_write(&self, offset: u32, to: u32, buf: &[u8]) -> Result<(), flash::Error> {
73 let mut flash = self.flash.lock().await;
74 flash.blocking_erase(offset, to)?;
75 flash.blocking_write(offset, buf)
76 }
77}
78
79impl<'a, T: Instance, const FLASH_SIZE: usize> FlashLock<'a, T, Async, FLASH_SIZE> {
81 #[inline]
83 pub async fn read(&self, offset: u32, bytes: &mut [u8]) -> Result<(), flash::Error> {
84 self.flash.lock().await.read(offset, bytes).await
85 }
86}