stm32_rust_template/driver/flash/
mod.rs

1#![allow(dead_code)]
2
3use bitflags::bitflags;
4use core::ops::FnMut;
5
6/// Describes a single sector of the flash memory.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct SectorInfo {
9    /// Sector start address.
10    pub start: u32,
11    /// Sector end address (start + size - 1).
12    pub end: u32,
13}
14
15/// Provides information about the flash memory device.
16/// It is assumed that this information is static for a given device.
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub struct FlashInfo {
19    /// A slice containing the sector layout information.
20    /// If `None`, the sectors are of uniform size.
21    pub sector_info: Option<&'static [SectorInfo]>,
22    /// The total number of sectors.
23    pub sector_count: u32,
24    /// The size of a single sector in bytes if the sectors are uniform.
25    /// If `sector_info` is used, this field is typically 0.
26    pub sector_size: u32,
27    /// The optimal programming page size in bytes.
28    pub page_size: u32,
29    /// The smallest programmable unit in bytes.
30    pub program_unit: u32,
31    /// The value of memory after it has been erased (usually 0xFF).
32    pub erased_value: u8,
33}
34
35/// Represents the status of the flash memory device.
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub struct Status {
38    /// `true` if a flash operation is in progress.
39    pub busy: bool,
40    /// `true` if a read, program, or erase operation has failed.
41    pub error: bool,
42}
43
44bitflags! {
45    /// Represents events that can occur during flash operations.
46    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
47    pub struct Event: u32 {
48        /// The flash memory is ready for a new operation.
49        const READY = (1 << 0);
50        /// An error occurred during a read, program, or erase operation.
51        const ERROR = (1 << 1);
52    }
53}
54
55/// A generic error type for the flash driver, using i32 for error codes.
56pub type Error = i32;
57
58/// A specialized Result type for flash operations.
59pub type Result<T> = core::result::Result<T, Error>;
60
61/// A trait that defines a standard interface for a flash memory driver.
62pub trait Flash<'a> {
63    /// Initializes the flash memory peripheral.
64    ///
65    /// The provided callback will be invoked to signal flash events.
66    fn initialize(&mut self, callback: impl FnMut(Event) + 'a) -> Result<()>;
67
68    /// De-initializes the flash memory peripheral.
69    fn uninitialize(&mut self) -> Result<()>;
70
71    /// Reads data from the flash memory.
72    ///
73    /// # Arguments
74    ///
75    /// * `addr` - The starting address to read from.
76    /// * `data` - A mutable buffer to store the read data.
77    fn read_data(&mut self, addr: u32, data: &mut [u8]) -> Result<()>;
78
79    /// Programs data into the flash memory.
80    ///
81    /// # Arguments
82    ///
83    /// * `addr` - The starting address to write to.
84    /// * `data` - The data to be programmed.
85    fn program_data(&mut self, addr: u32, data: &[u8]) -> Result<()>;
86
87    /// Erases a single sector of the flash memory.
88    ///
89    /// # Arguments
90    ///
91    /// * `addr` - The address of the sector to be erased. Any address within the sector is usually sufficient.
92    fn erase_sector(&mut self, addr: u32) -> Result<()>;
93
94    /// Erases the entire flash memory chip.
95    ///
96    /// This is a potentially destructive operation and may not be supported by all devices.
97    fn erase_chip(&mut self) -> Result<()>;
98
99    /// Gets the current status of the flash memory device.
100    fn get_status(&self) -> Status;
101
102    /// Gets information about the flash memory device.
103    fn get_info(&self) -> &FlashInfo;
104}