stm32g0xx_hal/flash/traits.rs
1/// Flash page representation where each flash page represents a region of 2048 bytes. The flash
2/// controller can only erase on a page basis.
3#[derive(Copy, Clone, Debug)]
4pub struct FlashPage(pub usize);
5
6/// Flash operation error
7#[derive(Copy, Clone, Debug)]
8pub enum Error {
9 /// Flash controller is not done yet
10 Busy,
11 /// Error detected (by command execution, or because no command could be executed)
12 Illegal,
13 /// Set during read if ECC decoding logic detects correctable or uncorrectable error
14 EccError,
15 /// Page number is out of range
16 PageOutOfRange,
17 /// (Legal) command failed
18 Failure,
19}
20
21/// A type alias for the result of a Flash operation.
22pub type Result = core::result::Result<(), Error>;
23
24pub trait Read {
25 /// Native type of the flash for reading with the correct alignment of the memory and size
26 ///
27 /// Can be `u8`, `u16`, `u32`, ..., or any user defined type
28 type NativeType;
29
30 /// Read from the flash memory using the native interface
31 fn read_native(&self, address: usize, array: &mut [Self::NativeType]);
32
33 /// Read a buffer of bytes from memory
34 fn read(&self, address: usize, buf: &mut [u8]);
35}
36
37pub trait WriteErase {
38 /// Native type of the flash for writing with the correct alignment and size
39 ///
40 /// Can be `u8`, `u16`, `u32`, ..., or any user defined type
41 type NativeType;
42
43 /// check flash status
44 fn status(&self) -> Result;
45
46 /// Erase specified flash page.
47 fn erase_page(&mut self, page: FlashPage) -> Result;
48
49 /// The smallest possible write, depends on platform
50 fn write_native(&mut self, address: usize, array: &[Self::NativeType]) -> Result;
51
52 /// Read a buffer of bytes to memory, this uses the native writes internally and if it's not
53 /// the same length and a set of native writes the write will be padded to fill a native write.
54 fn write(&mut self, address: usize, data: &[u8]) -> Result;
55}