Skip to main content

ch32_hal/flash/
mod.rs

1//! Flash memory (FLASH)
2//! CH32 FLASH is relatively close to STM32. This module is heavily inspired from embassy-stm32
3use embedded_storage::nor_flash::{NorFlashError, NorFlashErrorKind};
4
5#[cfg(not(any(flash_v4x7, flash_h4)))]
6mod common;
7#[cfg(not(any(flash_v4x7, flash_h4)))]
8pub use common::*;
9
10#[cfg(flash_v4x7)]
11mod v4x7;
12#[cfg(flash_v4x7)]
13pub use v4x7::*;
14
15#[cfg(flash_h4)]
16mod h4;
17#[cfg(flash_h4)]
18pub use h4::*;
19
20// Re-declare the legacy geometry instead of re-exporting the PAC constants:
21// the current metapac marks them deprecated in favor of `MEMORY_LAYOUT`, and
22// re-exporting would push that warning into every legacy build (including
23// downstream crates that deny warnings). The values are unchanged.
24#[cfg(not(any(flash_v4x7, flash_h4)))]
25#[allow(deprecated)]
26pub const FLASH_SIZE: usize = crate::pac::FLASH_SIZE;
27#[cfg(not(any(flash_v4x7, flash_h4)))]
28#[allow(deprecated)]
29pub const WRITE_SIZE: usize = crate::pac::WRITE_SIZE;
30
31#[cfg(any(flash_v4x7, flash_h4))]
32pub const FLASH_SIZE: usize = {
33    let regions = crate::pac::MEMORY_LAYOUT.regions;
34    let mut capacity = 0;
35    let mut index = 0;
36    while index < regions.len() {
37        if matches!(regions[index].role, crate::pac::mem_layout::MemoryRole::Application) {
38            core::assert!(regions[index].address as usize == 0x0800_0000 + capacity);
39            capacity += regions[index].size as usize;
40        }
41        index += 1;
42    }
43    capacity
44};
45
46#[cfg(any(flash_v4x7, flash_h4))]
47pub const WRITE_SIZE: usize =
48    match crate::pac::MEMORY_LAYOUT.find_by_role(crate::pac::mem_layout::MemoryRole::Application) {
49        Some(region) => match region.page_program_size() {
50            Some(size) => size as usize,
51            None => core::panic!("V4/H4 flash requires page programming geometry"),
52        },
53        None => core::panic!("V4/H4 flash requires application memory"),
54    };
55
56/// Read size (always 1)
57pub const READ_SIZE: usize = 1;
58
59/// Blocking flash mode typestate.
60pub enum Blocking {}
61/// Async flash mode typestate.
62pub enum Async {}
63
64/// Flash sector.
65#[derive(Debug, PartialEq)]
66#[cfg_attr(feature = "defmt", derive(defmt::Format))]
67pub struct FlashSector {
68    /// Absolute start address.
69    pub start: u32,
70    /// Size in bytes.
71    pub size: u32,
72}
73
74#[cfg_attr(flash_v3, path = "v3.rs")]
75#[cfg_attr(flash_l1, path = "l1.rs")]
76#[cfg_attr(flash_x0, path = "x0.rs")]
77#[cfg_attr(not(any(flash_v3, flash_l1, flash_x0)), path = "other.rs")]
78#[cfg(not(any(flash_v4x7, flash_h4)))]
79mod family;
80
81#[allow(unused_imports)]
82#[cfg(not(any(flash_v4x7, flash_h4)))]
83pub use family::*;
84
85/// Flash error
86///
87/// See CH32 Reference Manual for your chip for details.
88#[allow(missing_docs)]
89#[derive(Debug, Copy, Clone, PartialEq, Eq)]
90#[cfg_attr(feature = "defmt", derive(defmt::Format))]
91pub enum Error {
92    Prog,
93    Size,
94    Miss,
95    Seq,
96    Protected,
97    Unaligned,
98    Parallelism,
99    #[cfg(any(flash_v4x7, flash_h4))]
100    Busy,
101    #[cfg(any(flash_v4x7, flash_h4))]
102    Timeout,
103    #[cfg(any(flash_v4x7, flash_h4))]
104    Verify,
105    #[cfg(any(flash_v4x7, flash_h4))]
106    NotErased,
107    #[cfg(flash_v4x7)]
108    InvalidExecutionRegion,
109}
110
111impl NorFlashError for Error {
112    fn kind(&self) -> NorFlashErrorKind {
113        match self {
114            Self::Size => NorFlashErrorKind::OutOfBounds,
115            Self::Unaligned => NorFlashErrorKind::NotAligned,
116            _ => NorFlashErrorKind::Other,
117        }
118    }
119}