libxr  1.0
Want to be the best embedded framework
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 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. 向闪存写入数据。
 
- 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 / 快速擦除页大小
 

Private Member Functions

bool IsInRange (uint32_t addr, size_t size) const
 

Static Private Member Functions

static void ClearFlashFlagsOnce ()
 

Private Attributes

const FlashSectorsectors_
 
uint32_t base_address_
 
size_t sector_count_
 

Detailed Description

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

Definition at line 23 of file ch32_flash.hpp.

Constructor & Destructor Documentation

◆ 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.

123 : Flash(sectors[start_sector - 1].size, MinWriteSize(),
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 // `Flash` 基类看到的是从 `start_sector` 开始的一整段连续逻辑窗口,
132 // `sectors_` 仍保留原始物理扇区表,用于范围检查和地址换算。
133 // The `Flash` base class sees one contiguous logical window starting at
134 // `start_sector`, while `sectors_` still preserves the physical sector table
135 // for bounds checks and address translation.
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. 构造函数,初始化闪存属性。
Definition flash.cpp:7
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

Member Function Documentation

◆ ClearFlashFlagsOnce()

void CH32Flash::ClearFlashFlagsOnce ( )
inlinestaticprivate

Definition at line 120 of file ch32_flash.cpp.

120{ flash_clear_flags_once(); }

◆ Erase()

ErrorCode CH32Flash::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 139 of file ch32_flash.cpp.

140{
141 if (size == 0)
142 {
143 return ErrorCode::ARG_ERR;
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);
156 FlashAccessSession session;
157
158 // 当前已验证的 CH32V2/V3 快速擦除路径都按 256B 页对齐工作,
159 // 因此子页范围的擦除请求进入 SRAM 热路径前必须先扩到整页边界。
160 // The currently validated CH32V2/V3 fast erase path works on 256-byte pages,
161 // so a sub-page erase request must be widened to page boundaries before
162 // entering the SRAM hot path.
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 {
168 return ErrorCode::OK;
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()

ErrorCode CH32Flash::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 174 of file ch32_flash.cpp.

175{
176 ASSERT(SystemCoreClock <= 120000000);
177
178 if (!data.addr_ || data.size_ == 0)
179 {
180 ASSERT(false);
181 return ErrorCode::ARG_ERR;
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

Field Documentation

◆ base_address_

uint32_t LibXR::CH32Flash::base_address_
private

Definition at line 53 of file ch32_flash.hpp.

◆ sector_count_

size_t LibXR::CH32Flash::sector_count_
private

Definition at line 54 of file ch32_flash.hpp.

◆ sectors_

const FlashSector* LibXR::CH32Flash::sectors_
private

Definition at line 52 of file ch32_flash.hpp.


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