1 | pub use embedded_storage::nor_flash::{ErrorType, NorFlashError, NorFlashErrorKind}; |
2 | |
3 | /// Read only NOR flash trait. |
4 | pub trait ReadNorFlash: ErrorType { |
5 | /// The minumum number of bytes the storage peripheral can read |
6 | const READ_SIZE: usize; |
7 | |
8 | /// Read a slice of data from the storage peripheral, starting the read |
9 | /// operation at the given address offset, and reading `bytes.len()` bytes. |
10 | /// |
11 | /// # Errors |
12 | /// |
13 | /// Returns an error if the arguments are not aligned or out of bounds. The implementation |
14 | /// can use the [`check_read`] helper function. |
15 | async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error>; |
16 | |
17 | /// The capacity of the peripheral in bytes. |
18 | fn capacity(&self) -> usize; |
19 | } |
20 | |
21 | /// NOR flash trait. |
22 | pub trait NorFlash: ReadNorFlash { |
23 | /// The minumum number of bytes the storage peripheral can write |
24 | const WRITE_SIZE: usize; |
25 | |
26 | /// The minumum number of bytes the storage peripheral can erase |
27 | const ERASE_SIZE: usize; |
28 | |
29 | /// Erase the given storage range, clearing all data within `[from..to]`. |
30 | /// The given range will contain all 1s afterwards. |
31 | /// |
32 | /// If power is lost during erase, contents of the page are undefined. |
33 | /// |
34 | /// # Errors |
35 | /// |
36 | /// Returns an error if the arguments are not aligned or out of bounds (the case where `to > |
37 | /// from` is considered out of bounds). The implementation can use the [`check_erase`] |
38 | /// helper function. |
39 | async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error>; |
40 | |
41 | /// If power is lost during write, the contents of the written words are undefined, |
42 | /// but the rest of the page is guaranteed to be unchanged. |
43 | /// It is not allowed to write to the same word twice. |
44 | /// |
45 | /// # Errors |
46 | /// |
47 | /// Returns an error if the arguments are not aligned or out of bounds. The implementation |
48 | /// can use the [`check_write`] helper function. |
49 | async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error>; |
50 | } |
51 | |
52 | impl<T: ReadNorFlash> ReadNorFlash for &mut T { |
53 | const READ_SIZE: usize = T::READ_SIZE; |
54 | |
55 | async fn read(&mut self, offset: u32, bytes: &mut [u8]) -> Result<(), Self::Error> { |
56 | T::read(self, offset, bytes).await |
57 | } |
58 | |
59 | fn capacity(&self) -> usize { |
60 | T::capacity(self) |
61 | } |
62 | } |
63 | |
64 | impl<T: NorFlash> NorFlash for &mut T { |
65 | const WRITE_SIZE: usize = T::WRITE_SIZE; |
66 | const ERASE_SIZE: usize = T::ERASE_SIZE; |
67 | |
68 | async fn erase(&mut self, from: u32, to: u32) -> Result<(), Self::Error> { |
69 | T::erase(self, from, to).await |
70 | } |
71 | |
72 | async fn write(&mut self, offset: u32, bytes: &[u8]) -> Result<(), Self::Error> { |
73 | T::write(self, offset, bytes).await |
74 | } |
75 | } |
76 | |
77 | /// Marker trait for NorFlash relaxing the restrictions on `write`. |
78 | /// |
79 | /// Writes to the same word twice are now allowed. The result is the logical AND of the |
80 | /// previous data and the written data. That is, it is only possible to change 1 bits to 0 bits. |
81 | /// |
82 | /// If power is lost during write: |
83 | /// - Bits that were 1 on flash and are written to 1 are guaranteed to stay as 1 |
84 | /// - Bits that were 1 on flash and are written to 0 are undefined |
85 | /// - Bits that were 0 on flash are guaranteed to stay as 0 |
86 | /// - Rest of the bits in the page are guaranteed to be unchanged |
87 | pub trait MultiwriteNorFlash: NorFlash {} |
88 | |