libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
linux_flash.hpp
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6#include <cstring>
7#include <fstream>
8
9#include "flash.hpp"
10#include "libxr.hpp"
11#include "libxr_def.hpp"
12#include "libxr_type.hpp"
13
14namespace LibXR
15{
21template <size_t FLASH_SIZE>
23{
24 public:
34 LinuxBinaryFileFlash(const std::string& file_path,
35 size_t min_erase_size = FLASH_SIZE / 2,
36 size_t min_write_size = sizeof(uint8_t),
37 bool write_order_check = false, bool write_as_one_check = false)
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 }
50
58 ErrorCode Erase(size_t offset, size_t size) override
59 {
60 ASSERT(offset % MinEraseSize() == 0);
61 ASSERT(size % MinEraseSize() == 0);
62
63 if ((offset + size) > flash_area_.size())
64 {
66 }
67
68 Memory::FastSet(flash_area_.data() + offset, 0xFF, size);
69
70 return SyncToFile();
71 }
72
80 ErrorCode Write(size_t offset, ConstRawData data) override
81 {
82 if ((offset + data.size_) > flash_area_.size())
83 {
85 }
86
87 if (offset % MinWriteSize() != 0 || data.size_ % MinWriteSize() != 0)
88 {
89 return ErrorCode::FAILED;
90 }
91
92 if (write_order_check_)
93 {
94 ASSERT(offset % MinEraseSize() == 0);
95 }
96
97 uint8_t* dst = flash_area_.data() + offset;
98 const uint8_t* src = static_cast<const uint8_t*>(data.addr_);
99
100 if (write_as_one_check_)
101 {
102 for (size_t i = 0; i < data.size_; ++i)
103 {
104 if ((~dst[i] & src[i]))
105 {
106 return ErrorCode::FAILED;
107 }
108 }
109 }
110
111 Memory::FastCopy(dst, src, data.size_);
112 return SyncToFile();
113 }
114
115 private:
116 std::string file_path_;
117 std::array<uint8_t, FLASH_SIZE> flash_area_ = {};
118 bool write_order_check_;
119 bool write_as_one_check_;
120
121 ErrorCode SyncToFile()
122 {
123 std::ofstream file(file_path_, std::ios::binary | std::ios::trunc);
124 if (!file)
125 {
126 return ErrorCode::FAILED;
127 }
128 file.write(reinterpret_cast<const char*>(flash_area_.data()),
129 static_cast<std::streamsize>(flash_area_.size()));
130 return ErrorCode::OK;
131 }
132};
133} // namespace LibXR
只读原始数据视图 / Immutable raw data view
size_t size_
数据字节数 / Data size in bytes
const void * addr_
数据起始地址 / Data start address
抽象基类,表示闪存接口。 Abstract base class representing a flash memory interface.
Definition flash.hpp:32
size_t MinWriteSize() const
获取最小可写块大小(字节) / Returns the minimum writable block size in bytes.
Definition flash.hpp:83
size_t MinEraseSize() const
获取最小可擦除块大小(字节)。 Returns the minimum erasable block size in bytes.
Definition flash.hpp:76
Linux 二进制文件闪存实现 / Linux binary-file flash implementation.
ErrorCode Write(size_t offset, ConstRawData data) override
写入闪存数据 / Write flash data
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
static void FastSet(void *dst, uint8_t value, size_t size)
快速内存填充 / Fast memory fill
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
可写原始数据视图 / Mutable raw data view
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes
@ OUT_OF_RANGE
超出范围 / Out of range
@ FAILED
操作失败 / Operation failed
@ OK
操作成功 / Operation successful