libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::CH32Flash Class Reference

CH32 闪存驱动实现 / CH32 flash driver implementation. More...

#include <ch32_flash.hpp>

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

Public Member Functions

 CH32Flash (const FlashRegion *regions, size_t region_count, uint32_t start_address)
 构造闪存对象 / Construct flash object
 
 CH32Flash (const FlashRegion *regions, size_t region_count)
 用末尾两个扇区构造,供 DatabaseRaw 的主块和备份块使用 / Construct with the last two sectors, for the main and backup blocks of DatabaseRaw
 
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.
 

Static Public Member Functions

static constexpr size_t MinWriteSize ()
 最小写入粒度(半字) / Minimum write size (half-word)
 
static constexpr uint32_t PageSize ()
 快速擦除页大小 / Page erase size in fast erase mode
 

Static Public Attributes

static constexpr size_t MIN_WRITE_SIZE = 2
 最小写入单位(字节,半字编程);可直接作为 DatabaseRaw 的模板参数 / Minimum write unit in bytes (half-word programming); usable directly as the DatabaseRaw template argument
 

Detailed Description

CH32 闪存驱动实现 / CH32 flash driver implementation.

Note
扇区表须从 Flash 起始地址开始,按地址用若干段列出整片 Flash 的扇区。 The sector table must list the sectors of the whole Flash in address order as runs, starting at the Flash base.

Definition at line 20 of file ch32_flash.hpp.

Constructor & Destructor Documentation

◆ CH32Flash() [1/2]

CH32Flash::CH32Flash ( const FlashRegion * regions,
size_t region_count,
uint32_t start_address )

构造闪存对象 / Construct flash object

Parameters
regions扇区表 / Sector table
region_count扇区表的段数 / Number of runs in the sector table
start_address存储区的起始地址,须正好是某个扇区的起点;存储区一直到 Flash 末尾 / Start address of the storage area; must be exactly the start of a sector; the area extends to the end of the Flash

Definition at line 159 of file ch32_flash.cpp.

161 : Flash(SectorSizeAt(regions, region_count, start_address), MinWriteSize(),
162 {reinterpret_cast<void*>(start_address),
163 TableEnd(regions, region_count) - start_address}),
164 regions_(regions),
165 base_address_(start_address),
166 region_count_(region_count)
167{
168 // `Flash` 基类看到的是从 `start_address` 到 Flash 末尾的一整段连续窗口,
169 // `regions_` 仍保留整片 Flash 的扇区表,用于范围检查。
170 // The `Flash` base class sees one contiguous window from `start_address` to the end of
171 // the Flash, while `regions_` still holds the whole Flash's sector table for bounds
172 // checks.
173 InitHotPathsOnce();
174}
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.
Definition flash.cpp:7

◆ CH32Flash() [2/2]

CH32Flash::CH32Flash ( const FlashRegion * regions,
size_t region_count )

用末尾两个扇区构造,供 DatabaseRaw 的主块和备份块使用 / Construct with the last two sectors, for the main and backup blocks of DatabaseRaw

Parameters
regions扇区表 / Sector table
region_count扇区表的段数 / Number of runs in the sector table

Definition at line 176 of file ch32_flash.cpp.

177 : CH32Flash(regions, region_count, SecondToLastSector(regions, region_count))
178{
179}
CH32Flash(const FlashRegion *regions, size_t region_count, uint32_t start_address)
构造闪存对象 / Construct flash object

Member Function Documentation

◆ 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 181 of file ch32_flash.cpp.

182{
183 if (size == 0)
184 {
185 return ErrorCode::ARG_ERR;
186 }
187
188 ASSERT(SystemCoreClock <= 120000000);
189
190 const uint32_t START_ADDR = base_address_ + static_cast<uint32_t>(offset);
191 if (!IsInRange(START_ADDR, size))
192 {
194 }
195 const uint32_t END_ADDR = START_ADDR + static_cast<uint32_t>(size);
196 const auto erase_hot_path = g_ch32_flash_hot_paths.erase;
197 DEV_ASSERT(erase_hot_path != nullptr);
198 FlashAccessSession session;
199
200 // 当前已验证的 CH32V2/V3 快速擦除路径都按 256B 页对齐工作,
201 // 因此子页范围的擦除请求进入 SRAM 热路径前必须先扩到整页边界。
202 // The currently validated CH32V2/V3 fast erase path works on 256-byte pages,
203 // so a sub-page erase request must be widened to page boundaries before
204 // entering the SRAM hot path.
205 const uint32_t fast_size = 256u;
206 const uint32_t ERASE_BEGIN = START_ADDR & ~(fast_size - 1u);
207 const uint32_t ERASE_END = (END_ADDR + fast_size - 1u) & ~(fast_size - 1u);
208 if (ERASE_END <= ERASE_BEGIN)
209 {
210 return ErrorCode::OK;
211 }
212
213 return erase_hot_path(ERASE_BEGIN, ERASE_END);
214}
@ OUT_OF_RANGE
超出范围 / Out of range
@ OK
操作成功 / Operation successful
@ ARG_ERR
参数错误 / Argument error

◆ MinWriteSize()

static constexpr size_t LibXR::CH32Flash::MinWriteSize ( )
inlinestaticconstexpr

最小写入粒度(半字) / Minimum write size (half-word)

Definition at line 51 of file ch32_flash.hpp.

◆ PageSize()

static constexpr uint32_t LibXR::CH32Flash::PageSize ( )
inlinestaticconstexpr

快速擦除页大小 / Page erase size in fast erase mode

Definition at line 56 of file ch32_flash.hpp.

◆ Write()

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

向闪存写入数据 / 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 216 of file ch32_flash.cpp.

217{
218 ASSERT(SystemCoreClock <= 120000000);
219
220 if (!data.addr_ || data.size_ == 0)
221 {
222 return ErrorCode::ARG_ERR;
223 }
224
225 const uint32_t START_ADDR = base_address_ + static_cast<uint32_t>(offset);
226 if (!IsInRange(START_ADDR, data.size_))
227 {
229 }
230
231 const uint8_t* src = reinterpret_cast<const uint8_t*>(data.addr_);
232 const uint32_t END_ADDR = START_ADDR + static_cast<uint32_t>(data.size_);
233 return CH32FlashWriteHotPath(START_ADDR, END_ADDR, src);
234}
size_t size_
数据字节数 / Data size in bytes
const void * addr_
数据起始地址 / Data start address

Member Data Documentation

◆ MIN_WRITE_SIZE

size_t LibXR::CH32Flash::MIN_WRITE_SIZE = 2
staticconstexpr

最小写入单位(字节,半字编程);可直接作为 DatabaseRaw 的模板参数 / Minimum write unit in bytes (half-word programming); usable directly as the DatabaseRaw template argument

Definition at line 28 of file ch32_flash.hpp.


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