CH32 闪存驱动实现 / CH32 flash driver implementation.
More...
#include <ch32_flash.hpp>
|
| | CH32Flash (const FlashSector *sectors, size_t sector_count, size_t start_sector) |
| | 构造闪存对象 / Construct flash object
|
| |
| | CH32Flash (const FlashSector *sectors, size_t sector_count) |
| | 构造并使用默认起始扇区 / Construct with default start sector
|
| |
| 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. 向闪存写入数据。
|
| |
| | 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. 获取闪存存储区域的大小。
|
| |
|
| static constexpr size_t | MinWriteSize () |
| | 最小写入粒度(半字) / Minimum write size (half-word)
|
| |
| static constexpr uint32_t | PageSize () |
| | Page erase size in fast erase mode / 快速擦除页大小
|
| |
|
| bool | IsInRange (uint32_t addr, size_t size) const |
| |
CH32 闪存驱动实现 / CH32 flash driver implementation.
Definition at line 23 of file ch32_flash.hpp.
◆ CH32Flash() [1/2]
| CH32Flash::CH32Flash |
( |
const FlashSector * | sectors, |
|
|
size_t | sector_count, |
|
|
size_t | start_sector ) |
构造闪存对象 / Construct flash object
Definition at line 122 of file ch32_flash.cpp.
124 {
reinterpret_cast<void*
>(sectors[start_sector - 1].
address),
125 sectors[sector_count - 1].address - sectors[start_sector - 1].address +
126 sectors[sector_count - 1].size}),
127 sectors_(sectors),
128 base_address_(sectors[start_sector - 1].address),
129 sector_count_(sector_count)
130{
131
132
133
134
135
136 InitHotPathsOnce();
137}
static constexpr size_t MinWriteSize()
最小写入粒度(半字) / Minimum write size (half-word)
Flash(size_t min_erase_size, size_t min_write_size, RawData flash_area)
Constructs a Flash object with specified properties. 构造函数,初始化闪存属性。
uint32_t address
扇区起始地址 / Sector base address
◆ CH32Flash() [2/2]
| LibXR::CH32Flash::CH32Flash |
( |
const FlashSector * | sectors, |
|
|
size_t | sector_count ) |
|
inline |
构造并使用默认起始扇区 / Construct with default start sector
Definition at line 33 of file ch32_flash.hpp.
34 :
CH32Flash(sectors, sector_count, sector_count - 1)
35 {
36 }
CH32Flash(const FlashSector *sectors, size_t sector_count, size_t start_sector)
构造闪存对象 / Construct flash object
◆ ClearFlashFlagsOnce()
| void CH32Flash::ClearFlashFlagsOnce |
( |
| ) |
|
|
inlinestaticprivate |
◆ Erase()
| ErrorCode CH32Flash::Erase |
( |
size_t | offset, |
|
|
size_t | size ) |
|
overridevirtual |
Erases a section of the flash memory. 擦除闪存的指定区域。
- Parameters
-
| offset | The starting offset of the section to erase. 要擦除的起始偏移地址。 |
| size | The size of the section to erase. 要擦除的区域大小。 |
- Returns
- ErrorCode indicating success or failure. 返回操作结果的错误码。
Implements LibXR::Flash.
Definition at line 139 of file ch32_flash.cpp.
140{
141 if (size == 0)
142 {
144 }
145
146 ASSERT(SystemCoreClock <= 120000000);
147
148 const uint32_t START_ADDR = base_address_ + static_cast<uint32_t>(offset);
149 if (!IsInRange(START_ADDR, size))
150 {
152 }
153 const uint32_t END_ADDR = START_ADDR + static_cast<uint32_t>(size);
154 const auto erase_hot_path = g_ch32_flash_hot_paths.erase;
155 ASSERT(erase_hot_path != nullptr);
157
158
159
160
161
162
163 const uint32_t fast_size = 256u;
164 const uint32_t ERASE_BEGIN = START_ADDR & ~(fast_size - 1u);
165 const uint32_t ERASE_END = (END_ADDR + fast_size - 1u) & ~(fast_size - 1u);
166 if (ERASE_END <= ERASE_BEGIN)
167 {
169 }
170
171 return erase_hot_path(ERASE_BEGIN, ERASE_END);
172}
@ OUT_OF_RANGE
超出范围 | Out of range
@ OK
操作成功 | Operation successful
@ ARG_ERR
参数错误 | Argument error
◆ IsInRange()
| bool CH32Flash::IsInRange |
( |
uint32_t | addr, |
|
|
size_t | size ) const |
|
private |
Definition at line 245 of file ch32_flash.cpp.
246{
247 const uint32_t BEGIN = base_address_;
248 const uint32_t LIMIT =
249 sectors_[sector_count_ - 1].
address + sectors_[sector_count_ - 1].
size;
250 const uint32_t END = addr + static_cast<uint32_t>(size);
251 return (addr >= BEGIN) && (END <= LIMIT) && (END >= addr);
252}
uint32_t size
扇区大小(字节) / Sector size in bytes
◆ MinWriteSize()
| static constexpr size_t LibXR::CH32Flash::MinWriteSize |
( |
| ) |
|
|
inlinestaticconstexpr |
最小写入粒度(半字) / Minimum write size (half-word)
Definition at line 41 of file ch32_flash.hpp.
◆ PageSize()
| static constexpr uint32_t LibXR::CH32Flash::PageSize |
( |
| ) |
|
|
inlinestaticconstexpr |
Page erase size in fast erase mode / 快速擦除页大小
Definition at line 46 of file ch32_flash.hpp.
◆ Write()
Writes data to the flash memory. 向闪存写入数据。
- Parameters
-
| offset | The starting offset to write data. 数据写入的起始偏移地址。 |
| data | The data to be written. 需要写入的数据。 |
- Returns
- ErrorCode indicating success or failure. 返回操作结果的错误码。
Implements LibXR::Flash.
Definition at line 174 of file ch32_flash.cpp.
175{
176 ASSERT(SystemCoreClock <= 120000000);
177
179 {
180 ASSERT(false);
182 }
183
184 const uint32_t START_ADDR = base_address_ + static_cast<uint32_t>(offset);
185 if (!IsInRange(START_ADDR, data.
size_))
186 {
187 ASSERT(false);
189 }
190
191 const uint8_t* src =
reinterpret_cast<const uint8_t*
>(data.
addr_);
192 const uint32_t END_ADDR = START_ADDR +
static_cast<uint32_t
>(data.
size_);
193 return CH32FlashWriteHotPath(START_ADDR, END_ADDR, src);
194}
size_t size_
数据字节数 / Data size in bytes
const void * addr_
数据起始地址 / Data start address
◆ base_address_
| uint32_t LibXR::CH32Flash::base_address_ |
|
private |
◆ sector_count_
| size_t LibXR::CH32Flash::sector_count_ |
|
private |
◆ sectors_
The documentation for this class was generated from the following files: