libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::LinuxBinaryFileFlash< FLASH_SIZE > Class Template Reference

Linux 二进制文件闪存实现 / Linux binary-file flash implementation. More...

#include <linux_flash.hpp>

Inheritance diagram for LibXR::LinuxBinaryFileFlash< FLASH_SIZE >:
[legend]
Collaboration diagram for LibXR::LinuxBinaryFileFlash< FLASH_SIZE >:
[legend]

Public Member Functions

 LinuxBinaryFileFlash (const std::string &file_path, size_t min_erase_size=FLASH_SIZE/2, size_t min_write_size=sizeof(uint8_t), bool write_order_check=false, bool write_as_one_check=false)
 构造 Linux 文件闪存对象 / Construct Linux file-backed flash
 
ErrorCode Erase (size_t offset, size_t size) override
 擦除闪存区域 / Erase flash area
 
ErrorCode Write (size_t offset, ConstRawData data) override
 写入闪存数据 / Write flash data
 
- 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

ErrorCode SyncToFile ()
 

Private Attributes

std::string file_path_
 
std::array< uint8_t, FLASH_SIZE > flash_area_ = {}
 
bool write_order_check_
 
bool write_as_one_check_
 

Detailed Description

template<size_t FLASH_SIZE>
class LibXR::LinuxBinaryFileFlash< FLASH_SIZE >

Linux 二进制文件闪存实现 / Linux binary-file flash implementation.

Template Parameters
FLASH_SIZE闪存容量(字节) / Flash size in bytes

Definition at line 22 of file linux_flash.hpp.

Constructor & Destructor Documentation

◆ LinuxBinaryFileFlash()

template<size_t FLASH_SIZE>
LibXR::LinuxBinaryFileFlash< FLASH_SIZE >::LinuxBinaryFileFlash ( const std::string & file_path,
size_t min_erase_size = FLASH_SIZE / 2,
size_t min_write_size = sizeof(uint8_t),
bool write_order_check = false,
bool write_as_one_check = false )
inline

构造 Linux 文件闪存对象 / Construct Linux file-backed flash

Parameters
file_path二进制文件路径 / Binary file path
min_erase_size最小擦除块大小 / Minimum erase block size
min_write_size最小写入块大小 / Minimum write block size
write_order_check写入顺序检查开关 / Enable write order check
write_as_one_check写入一致性检查开关 / Enable write consistency check

Definition at line 34 of file linux_flash.hpp.

38 : Flash(min_erase_size, min_write_size, RawData(&flash_area_, sizeof(flash_area_))),
39 file_path_(file_path),
40 write_order_check_(write_order_check),
41 write_as_one_check_(write_as_one_check)
42 {
43 std::ifstream file(file_path_, std::ios::binary);
44 if (file)
45 {
46 file.read(reinterpret_cast<char*>(flash_area_.data()),
47 static_cast<std::streamsize>(flash_area_.size()));
48 }
49 }
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

Member Function Documentation

◆ Erase()

template<size_t FLASH_SIZE>
ErrorCode LibXR::LinuxBinaryFileFlash< FLASH_SIZE >::Erase ( size_t offset,
size_t size )
inlineoverridevirtual

擦除闪存区域 / Erase flash area

Parameters
offset相对闪存起始地址的偏移 / Offset from flash base
size擦除长度 / Erase size
Returns
ErrorCode 错误码 / Error code

Implements LibXR::Flash.

Definition at line 58 of file linux_flash.hpp.

59 {
60 ASSERT(offset % MinEraseSize() == 0);
61 ASSERT(size % MinEraseSize() == 0);
62
63 if ((offset + size) > flash_area_.size())
64 {
65 return ErrorCode::OUT_OF_RANGE;
66 }
67
68 Memory::FastSet(flash_area_.data() + offset, 0xFF, size);
69
70 return SyncToFile();
71 }
size_t MinEraseSize() const
Returns the minimum erasable block size in bytes. 获取最小可擦除块大小(字节)。
Definition flash.hpp:64
static void FastSet(void *dst, uint8_t value, size_t size)
快速内存填充 / Fast memory fill

◆ SyncToFile()

template<size_t FLASH_SIZE>
ErrorCode LibXR::LinuxBinaryFileFlash< FLASH_SIZE >::SyncToFile ( )
inlineprivate

Definition at line 123 of file linux_flash.hpp.

124 {
125 std::ofstream file(file_path_, std::ios::binary | std::ios::trunc);
126 if (!file)
127 {
128 return ErrorCode::FAILED;
129 }
130 file.write(reinterpret_cast<const char*>(flash_area_.data()),
131 static_cast<std::streamsize>(flash_area_.size()));
132 return ErrorCode::OK;
133 }

◆ Write()

template<size_t FLASH_SIZE>
ErrorCode LibXR::LinuxBinaryFileFlash< FLASH_SIZE >::Write ( size_t offset,
ConstRawData data )
inlineoverridevirtual

写入闪存数据 / Write flash data

Parameters
offset相对闪存起始地址的偏移 / Offset from flash base
data写入数据 / Data to write
Returns
ErrorCode 错误码 / Error code

Implements LibXR::Flash.

Definition at line 80 of file linux_flash.hpp.

81 {
82 if ((offset + data.size_) > flash_area_.size())
83 {
84 return ErrorCode::OUT_OF_RANGE;
85 }
86
87 if (offset % MinWriteSize() != 0 || data.size_ % MinWriteSize() != 0)
88 {
89 ASSERT(false);
90 return ErrorCode::FAILED;
91 }
92
93 if (write_order_check_)
94 {
95 ASSERT(offset % MinEraseSize() == 0);
96 }
97
98 uint8_t* dst = flash_area_.data() + offset;
99 const uint8_t* src = static_cast<const uint8_t*>(data.addr_);
100
101 if (write_as_one_check_)
102 {
103 for (size_t i = 0; i < data.size_; ++i)
104 {
105 if ((~dst[i] & src[i]))
106 {
107 ASSERT(false);
108 return ErrorCode::FAILED;
109 }
110 }
111 }
112
113 Memory::FastCopy(dst, src, data.size_);
114 return SyncToFile();
115 }
size_t MinWriteSize() const
Returns the minimum writable block size in bytes. 获取最小可写块大小(字节)。
Definition flash.hpp:73
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
Definition libxr_mem.cpp:3

Field Documentation

◆ file_path_

template<size_t FLASH_SIZE>
std::string LibXR::LinuxBinaryFileFlash< FLASH_SIZE >::file_path_
private

Definition at line 118 of file linux_flash.hpp.

◆ flash_area_

template<size_t FLASH_SIZE>
std::array<uint8_t, FLASH_SIZE> LibXR::LinuxBinaryFileFlash< FLASH_SIZE >::flash_area_ = {}
private

Definition at line 119 of file linux_flash.hpp.

119{};

◆ write_as_one_check_

template<size_t FLASH_SIZE>
bool LibXR::LinuxBinaryFileFlash< FLASH_SIZE >::write_as_one_check_
private

Definition at line 121 of file linux_flash.hpp.

◆ write_order_check_

template<size_t FLASH_SIZE>
bool LibXR::LinuxBinaryFileFlash< FLASH_SIZE >::write_order_check_
private

Definition at line 120 of file linux_flash.hpp.


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