libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::STM32Flash Class Reference

STM32Flash 通用类,构造时传入扇区列表,自动判断编程粒度。 More...

#include <stm32_flash.hpp>

Inheritance diagram for LibXR::STM32Flash:
[legend]
Collaboration diagram for LibXR::STM32Flash:
[legend]

Public Member Functions

 STM32Flash (const FlashSector *sectors, size_t sector_count, size_t start_sector)
 STM32Flash 类,构造时传入扇区列表,自动判断编程粒度。
 
 STM32Flash (const FlashSector *sectors, size_t sector_count)
 STM32Flash 类,自动取最后两个扇区
 
ErrorCode Erase (size_t offset, size_t size) override
 Erases a section of the flash memory. 擦除闪存的指定区域。
 
ErrorCode Write (size_t offset, ConstRawData data) override
 Writes data to the flash memory. 向闪存写入数据。
 
- Public Member Functions inherited from LibXR::Flash
 Flash (size_t min_erase_size, size_t min_write_size, RawData flash_area)
 Constructs a Flash object with specified properties. 构造函数,初始化闪存属性。
 
virtual ErrorCode Read (size_t offset, RawData data)
 Reads data from the flash memory. 从闪存中读取数据。
 
size_t MinEraseSize () const
 Returns the minimum erasable block size in bytes. 获取最小可擦除块大小(字节)。
 
size_t MinWriteSize () const
 Returns the minimum writable block size in bytes. 获取最小可写块大小(字节)。
 
size_t Size () const
 Returns the size of the flash memory area. 获取闪存存储区域的大小。
 

Private Member Functions

bool IsInRange (uint32_t addr, size_t size) const
 

Static Private Member Functions

static constexpr uint32_t DetermineProgramType ()
 
static constexpr size_t DetermineMinWriteSize ()
 

Private Attributes

const FlashSectorsectors_
 
uint32_t base_address_
 
uint32_t program_type_
 
size_t sector_count_
 

Detailed Description

STM32Flash 通用类,构造时传入扇区列表,自动判断编程粒度。

Definition at line 101 of file stm32_flash.hpp.

Constructor & Destructor Documentation

◆ STM32Flash() [1/2]

STM32Flash::STM32Flash ( const FlashSector * sectors,
size_t sector_count,
size_t start_sector )

STM32Flash 类,构造时传入扇区列表,自动判断编程粒度。

Parameters
sectors扇区列表
sector_count扇区数量
start_sector起始扇区

Definition at line 5 of file stm32_flash.cpp.

7 : Flash(sectors[start_sector - 1].size, DetermineMinWriteSize(),
8 {reinterpret_cast<void*>(sectors[start_sector - 1].address),
9 sectors[sector_count - 1].address - sectors[start_sector - 1].address +
10 sectors[sector_count - 1].size}),
11 sectors_(sectors),
12 base_address_(sectors[start_sector - 1].address),
13 program_type_(DetermineProgramType()),
14 sector_count_(sector_count)
15{
16}
Flash(size_t min_erase_size, size_t min_write_size, RawData flash_area)
Constructs a Flash object with specified properties. 构造函数,初始化闪存属性。
Definition flash.cpp:5

◆ STM32Flash() [2/2]

LibXR::STM32Flash::STM32Flash ( const FlashSector * sectors,
size_t sector_count )
inline

STM32Flash 类,自动取最后两个扇区

Parameters
sectors扇区列表
sector_count扇区数量

Definition at line 119 of file stm32_flash.hpp.

120 : STM32Flash(sectors, sector_count, sector_count - 1)
121 {
122 }
STM32Flash(const FlashSector *sectors, size_t sector_count, size_t start_sector)
STM32Flash 类,构造时传入扇区列表,自动判断编程粒度。

Member Function Documentation

◆ DetermineMinWriteSize()

static constexpr size_t LibXR::STM32Flash::DetermineMinWriteSize ( )
inlinestaticconstexprprivate

Definition at line 151 of file stm32_flash.hpp.

152 {
153#ifdef FLASH_TYPEPROGRAM_BYTE
154 return 1;
155#elif defined(FLASH_TYPEPROGRAM_HALFWORD)
156 return 2;
157#elif defined(FLASH_TYPEPROGRAM_WORD)
158 return 4;
159#elif defined(FLASH_TYPEPROGRAM_DOUBLEWORD)
160 return 8;
161#elif defined(FLASH_TYPEPROGRAM_FLASHWORD)
162 return FLASH_NB_32BITWORD_IN_FLASHWORD * 4;
163#else
164#error "No supported FLASH_TYPEPROGRAM_xxx defined"
165#endif
166 }

◆ DetermineProgramType()

static constexpr uint32_t LibXR::STM32Flash::DetermineProgramType ( )
inlinestaticconstexprprivate

Definition at line 134 of file stm32_flash.hpp.

135 {
136#ifdef FLASH_TYPEPROGRAM_BYTE
137 return FLASH_TYPEPROGRAM_BYTE;
138#elif defined(FLASH_TYPEPROGRAM_HALFWORD)
139 return FLASH_TYPEPROGRAM_HALFWORD;
140#elif defined(FLASH_TYPEPROGRAM_WORD)
141 return FLASH_TYPEPROGRAM_WORD;
142#elif defined(FLASH_TYPEPROGRAM_DOUBLEWORD)
143 return FLASH_TYPEPROGRAM_DOUBLEWORD;
144#elif defined(FLASH_TYPEPROGRAM_FLASHWORD)
145 return FLASH_TYPEPROGRAM_FLASHWORD;
146#else
147#error "No supported FLASH_TYPEPROGRAM_xxx defined"
148#endif
149 }

◆ Erase()

ErrorCode STM32Flash::Erase ( size_t offset,
size_t size )
overridevirtual

Erases a section of the flash memory. 擦除闪存的指定区域。

Parameters
offsetThe starting offset of the section to erase. 要擦除的起始偏移地址。
sizeThe size of the section to erase. 要擦除的区域大小。
Returns
ErrorCode indicating success or failure. 返回操作结果的错误码。

Implements LibXR::Flash.

Definition at line 18 of file stm32_flash.cpp.

19{
20 if (size == 0)
21 {
22 return ErrorCode::ARG_ERR;
23 }
24
25 uint32_t start_addr = base_address_ + offset;
26 uint32_t end_addr = start_addr + size;
27
28#if defined(__ICACHE_PRESENT) && (__ICACHE_PRESENT == 1U)
29 bool i_cache_enabled = ((SCB->CCR & SCB_CCR_IC_Msk) != 0U);
30 if (i_cache_enabled)
31 {
32 SCB_DisableICache();
33 }
34#endif
35
36#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
37 bool d_cache_enabled = ((SCB->CCR & SCB_CCR_DC_Msk) != 0U);
38 if (d_cache_enabled)
39 {
40 SCB_DisableDCache();
41 }
42#endif
43
44 HAL_FLASH_Unlock();
45
46 for (size_t i = 0; i < sector_count_; ++i)
47 {
48 const auto& sector = sectors_[i];
49 if (sector.address + sector.size <= start_addr)
50 {
51 continue;
52 }
53 if (sector.address >= end_addr)
54 {
55 break;
56 }
57 FLASH_EraseInitTypeDef erase_init = {};
58
59#if defined(FLASH_TYPEERASE_PAGES) && defined(FLASH_PAGE_SIZE) // STM32F1/G4... series
60 erase_init.TypeErase = FLASH_TYPEERASE_PAGES;
61 SetNbPages(erase_init, sector.address, i);
62 erase_init.NbPages = 1;
63 SetBanks(erase_init, sector.address);
64#elif defined(FLASH_TYPEERASE_SECTORS) // STM32F4/F7/H7... series
65 erase_init.TypeErase = FLASH_TYPEERASE_SECTORS;
66 erase_init.Sector = static_cast<uint32_t>(i) % FLASH_SECTOR_TOTAL;
67 erase_init.NbSectors = 1;
68#if defined(FLASH_BANK_1)
69 erase_init.Banks = STM32FlashBankOf(sector.address);
70#endif
71#if defined(FLASH_CR_PSIZE)
72 erase_init.VoltageRange = FLASH_VOLTAGE_RANGE_1;
73#endif
74#else
75 return ErrorCode::NOT_SUPPORT;
76#endif
77
78 uint32_t error = 0;
79 HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&erase_init, &error);
80 if (status != HAL_OK || error != 0xFFFFFFFFU)
81 {
82 HAL_FLASH_Lock();
83 return ErrorCode::FAILED;
84 }
85 }
86
87 HAL_FLASH_Lock();
88
89#if defined(__ICACHE_PRESENT) && (__ICACHE_PRESENT == 1U)
90 if (i_cache_enabled)
91 {
92 SCB_EnableICache();
93 }
94#endif
95
96#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
97
98 if (d_cache_enabled)
99 {
100 SCB_EnableDCache();
101 }
102#endif
103
104 return ErrorCode::OK;
105}

◆ IsInRange()

bool STM32Flash::IsInRange ( uint32_t addr,
size_t size ) const
private

Definition at line 213 of file stm32_flash.cpp.

214{
215 const uint32_t BEGIN = base_address_;
216 const uint32_t LIMIT =
217 sectors_[sector_count_ - 1].address + sectors_[sector_count_ - 1].size;
218 const uint32_t END = addr + size;
219 return (addr >= BEGIN) && (END <= LIMIT) && (END >= addr); // 最后一项防溢出
220}

◆ Write()

ErrorCode STM32Flash::Write ( size_t offset,
ConstRawData data )
overridevirtual

Writes data to the flash memory. 向闪存写入数据。

Parameters
offsetThe starting offset to write data. 数据写入的起始偏移地址。
dataThe data to be written. 需要写入的数据。
Returns
ErrorCode indicating success or failure. 返回操作结果的错误码。

Implements LibXR::Flash.

Definition at line 107 of file stm32_flash.cpp.

108{
109 if (!data.addr_ || data.size_ == 0)
110 {
111 return ErrorCode::ARG_ERR;
112 }
113
114 uint32_t addr = base_address_ + offset;
115 if (!IsInRange(addr, data.size_))
116 {
117 return ErrorCode::OUT_OF_RANGE;
118 }
119
120#if defined(__ICACHE_PRESENT) && (__ICACHE_PRESENT == 1U)
121 bool i_cache_enabled = ((SCB->CCR & SCB_CCR_IC_Msk) != 0U);
122 if (i_cache_enabled)
123 {
124 SCB_DisableICache();
125 }
126#endif
127
128#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
129 bool d_cache_enabled = ((SCB->CCR & SCB_CCR_DC_Msk) != 0U);
130 if (d_cache_enabled)
131 {
132 SCB_DisableDCache();
133 }
134#endif
135
136 HAL_FLASH_Unlock();
137
138 const uint8_t* src = reinterpret_cast<const uint8_t*>(data.addr_);
139 size_t written = 0;
140
141#if defined(STM32H7) || defined(STM32H5)
142 alignas(32) uint32_t flash_word_buffer[8] = {0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu,
143 0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu,
144 0xFFFFFFFFu, 0xFFFFFFFFu};
145 while (written < data.size_)
146 {
147 size_t chunk_size = LibXR::min<size_t>(MinWriteSize(), data.size_ - written);
148
149 std::memset(flash_word_buffer, 0xFF, sizeof(flash_word_buffer));
150 std::memcpy(flash_word_buffer, src + written, chunk_size);
151
152 if (memcmp(reinterpret_cast<const uint8_t*>(addr + written), src + written,
153 chunk_size) == 0)
154 {
155 written += chunk_size;
156 continue;
157 }
158
159 if (HAL_FLASH_Program(program_type_, addr + written,
160 reinterpret_cast<uint32_t>(flash_word_buffer)) != HAL_OK)
161 {
162 HAL_FLASH_Lock();
163 return ErrorCode::FAILED;
164 }
165
166 written += chunk_size;
167 }
168
169#else
170 while (written < data.size_)
171 {
172 size_t chunk_size = LibXR::min<size_t>(MinWriteSize(), data.size_ - written);
173
174 if (memcmp(reinterpret_cast<const uint8_t*>(addr + written), src + written,
175 chunk_size) == 0)
176 {
177 written += chunk_size;
178 continue;
179 }
180
181 uint64_t word = 0xFFFFFFFFFFFFFFFF;
182 std::memcpy(&word, src + written, chunk_size);
183
184 if (HAL_FLASH_Program(program_type_, addr + written, word) != HAL_OK)
185 {
186 HAL_FLASH_Lock();
187 return ErrorCode::FAILED;
188 }
189
190 written += chunk_size;
191 }
192#endif
193
194 HAL_FLASH_Lock();
195
196#if defined(__ICACHE_PRESENT) && (__ICACHE_PRESENT == 1U)
197 if (i_cache_enabled)
198 {
199 SCB_EnableICache();
200 }
201#endif
202
203#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
204 if (d_cache_enabled)
205 {
206 SCB_EnableDCache();
207 }
208#endif
209
210 return ErrorCode::OK;
211}
size_t size_
数据大小(字节)。 The size of the data (in bytes).
const void * addr_
数据存储地址(常量)。 The storage address of the data (constant).
size_t MinWriteSize() const
Returns the minimum writable block size in bytes. 获取最小可写块大小(字节)。
Definition flash.hpp:73
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

Field Documentation

◆ base_address_

uint32_t LibXR::STM32Flash::base_address_
private

Definition at line 130 of file stm32_flash.hpp.

◆ program_type_

uint32_t LibXR::STM32Flash::program_type_
private

Definition at line 131 of file stm32_flash.hpp.

◆ sector_count_

size_t LibXR::STM32Flash::sector_count_
private

Definition at line 132 of file stm32_flash.hpp.

◆ sectors_

const FlashSector* LibXR::STM32Flash::sectors_
private

Definition at line 129 of file stm32_flash.hpp.


The documentation for this class was generated from the following files: