embassy_stm32/flash/
mod.rs

1//! Flash memory (FLASH)
2use embedded_storage::nor_flash::{NorFlashError, NorFlashErrorKind};
3
4#[cfg(flash_f4)]
5mod asynch;
6#[cfg(flash)]
7mod common;
8#[cfg(eeprom)]
9mod eeprom;
10
11#[cfg(flash_f4)]
12pub use asynch::InterruptHandler;
13#[cfg(flash)]
14pub use common::*;
15#[cfg(eeprom)]
16#[allow(unused_imports)]
17pub use eeprom::*;
18
19pub use crate::_generated::flash_regions::*;
20#[cfg(eeprom)]
21pub use crate::_generated::{EEPROM_BASE, EEPROM_SIZE};
22pub use crate::_generated::{FLASH_BASE, FLASH_SIZE, MAX_ERASE_SIZE, WRITE_SIZE};
23
24/// Get all flash regions.
25pub fn get_flash_regions() -> &'static [&'static FlashRegion] {
26    &FLASH_REGIONS
27}
28
29/// Read size (always 1)
30pub const READ_SIZE: usize = 1;
31
32/// Blocking flash mode typestate.
33pub enum Blocking {}
34/// Async flash mode typestate.
35pub enum Async {}
36
37/// Flash memory region
38#[derive(Debug)]
39#[cfg_attr(feature = "defmt", derive(defmt::Format))]
40pub struct FlashRegion {
41    /// Bank number.
42    pub bank: FlashBank,
43    /// Absolute base address.
44    pub base: u32,
45    /// Size in bytes.
46    pub size: u32,
47    /// Erase size (sector size).
48    pub erase_size: u32,
49    /// Minimum write size.
50    pub write_size: u32,
51    /// Erase value (usually `0xFF`, but is `0x00` in some chips)
52    pub erase_value: u8,
53    pub(crate) _ensure_internal: (),
54}
55
56impl FlashRegion {
57    /// Absolute end address.
58    pub const fn end(&self) -> u32 {
59        self.base + self.size
60    }
61
62    /// Number of sectors in the region.
63    pub const fn sectors(&self) -> u8 {
64        (self.size / self.erase_size) as u8
65    }
66}
67
68/// Flash sector.
69#[derive(Debug, PartialEq)]
70#[cfg_attr(feature = "defmt", derive(defmt::Format))]
71pub struct FlashSector {
72    /// Bank number.
73    pub bank: FlashBank,
74    /// Sector number within the bank.
75    pub index_in_bank: u8,
76    /// Absolute start address.
77    pub start: u32,
78    /// Size in bytes.
79    pub size: u32,
80}
81
82/// Flash bank.
83#[derive(Clone, Copy, Debug, PartialEq)]
84#[cfg_attr(feature = "defmt", derive(defmt::Format))]
85pub enum FlashBank {
86    /// Bank 1
87    Bank1 = 0,
88    /// Bank 2
89    Bank2 = 1,
90    /// OTP region,
91    Otp,
92}
93#[cfg(all(eeprom, not(any(flash_l0, flash_l1))))]
94compile_error!("The 'eeprom' cfg is enabled for a non-L0/L1 chip family. This is an unsupported configuration.");
95#[cfg_attr(any(flash_l0, flash_l1, flash_l4, flash_l5, flash_wl, flash_wb), path = "l.rs")]
96#[cfg_attr(flash_f0, path = "f0.rs")]
97#[cfg_attr(any(flash_f1, flash_f3), path = "f1f3.rs")]
98#[cfg_attr(flash_f2, path = "f2.rs")]
99#[cfg_attr(flash_f4, path = "f4.rs")]
100#[cfg_attr(flash_f7, path = "f7.rs")]
101#[cfg_attr(any(flash_g0x0, flash_g0x1, flash_g4c2, flash_g4c3, flash_g4c4), path = "g.rs")]
102#[cfg_attr(flash_h7, path = "h7.rs")]
103#[cfg_attr(flash_h7ab, path = "h7.rs")]
104#[cfg_attr(any(flash_u5, flash_wba), path = "u5.rs")]
105#[cfg_attr(flash_h5, path = "h5.rs")]
106#[cfg_attr(flash_h50, path = "h50.rs")]
107#[cfg_attr(flash_u0, path = "u0.rs")]
108#[cfg_attr(
109    not(any(
110        flash_l0, flash_l1, flash_l4, flash_l5, flash_wl, flash_wb, flash_f0, flash_f1, flash_f2, flash_f3, flash_f4,
111        flash_f7, flash_g0x0, flash_g0x1, flash_g4c2, flash_g4c3, flash_g4c4, flash_h7, flash_h7ab, flash_u5,
112        flash_wba, flash_h50, flash_u0, flash_h5,
113    )),
114    path = "other.rs"
115)]
116mod family;
117
118#[allow(unused_imports)]
119pub use family::*;
120
121/// Flash error
122///
123/// See STM32 Reference Manual for your chip for details.
124#[allow(missing_docs)]
125#[derive(Debug, Copy, Clone, PartialEq, Eq)]
126#[cfg_attr(feature = "defmt", derive(defmt::Format))]
127pub enum Error {
128    Prog,
129    Size,
130    Miss,
131    Seq,
132    Protected,
133    Unaligned,
134    Parallelism,
135}
136
137impl NorFlashError for Error {
138    fn kind(&self) -> NorFlashErrorKind {
139        match self {
140            Self::Size => NorFlashErrorKind::OutOfBounds,
141            Self::Unaligned => NorFlashErrorKind::NotAligned,
142            _ => NorFlashErrorKind::Other,
143        }
144    }
145}