libxr 1.0
Want to be the best embedded framework
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{
22template <size_t FLASH_SIZE>
24{
25 public:
36 LinuxBinaryFileFlash(const std::string& file_path,
37 size_t min_erase_size = FLASH_SIZE / 2,
38 size_t min_write_size = sizeof(uint8_t),
39 bool write_order_check = false, bool write_as_one_check = false)
40 : Flash(min_erase_size, min_write_size, RawData(&flash_area_, sizeof(flash_area_))),
41 file_path_(file_path),
42 write_order_check_(write_order_check),
43 write_as_one_check_(write_as_one_check)
44 {
45 std::ifstream file(file_path_, std::ios::binary);
46 if (file)
47 {
48 file.read(reinterpret_cast<char*>(flash_area_.data()),
49 static_cast<std::streamsize>(flash_area_.size()));
50 }
51 }
52
60 ErrorCode Erase(size_t offset, size_t size) override
61 {
62 ASSERT(offset % min_erase_size_ == 0);
63 ASSERT(size % min_erase_size_ == 0);
64
65 if ((offset + size) > flash_area_.size())
66 {
67 return ErrorCode::OUT_OF_RANGE;
68 }
69
70 std::memset(flash_area_.data() + offset, 0xFF, size);
71
72 return SyncToFile();
73 }
74
82 ErrorCode Write(size_t offset, ConstRawData data) override
83 {
84 if ((offset + data.size_) > flash_area_.size())
85 {
86 return ErrorCode::OUT_OF_RANGE;
87 }
88
89 if (offset % min_write_size_ != 0 || data.size_ % min_write_size_ != 0)
90 {
91 ASSERT(false);
92 return ErrorCode::FAILED;
93 }
94
95 if (write_order_check_)
96 {
97 ASSERT(offset % min_erase_size_ == 0);
98 }
99
100 uint8_t* dst = flash_area_.data() + offset;
101 const uint8_t* src = static_cast<const uint8_t*>(data.addr_);
102
103 if (write_as_one_check_)
104 {
105 for (size_t i = 0; i < data.size_; ++i)
106 {
107 if ((~dst[i] & src[i]))
108 {
109 ASSERT(false);
110 return ErrorCode::FAILED;
111 }
112 }
113 }
114
115 std::memcpy(dst, src, data.size_);
116 return SyncToFile();
117 }
118
119 private:
120 std::string file_path_;
121 std::array<uint8_t, FLASH_SIZE> flash_area_ = {};
122 bool write_order_check_;
123 bool write_as_one_check_;
124
125 ErrorCode SyncToFile()
126 {
127 std::ofstream file(file_path_, std::ios::binary | std::ios::trunc);
128 if (!file)
129 {
130 return ErrorCode::FAILED;
131 }
132 file.write(reinterpret_cast<const char*>(flash_area_.data()),
133 static_cast<std::streamsize>(flash_area_.size()));
134 return ErrorCode::OK;
135 }
136};
137} // namespace LibXR
常量原始数据封装类。 A class for encapsulating constant raw data.
size_t size_
数据大小(字节)。 The size of the data (in bytes).
const void * addr_
数据存储地址(常量)。 The storage address of the data (constant).
Abstract base class representing a flash memory interface. 抽象基类,表示闪存接口。
Definition flash.hpp:16
size_t min_write_size_
Minimum writable block size in bytes. 最小可写块大小(字节)。
Definition flash.hpp:36
size_t min_erase_size_
Minimum erasable block size in bytes. 最小可擦除块大小(字节)。
Definition flash.hpp:34
LinuxBinaryFileFlash 类,用于从二进制文件加载闪存数据 LinuxBinaryFileFlash class for loading flash data from a binary...
ErrorCode Write(size_t offset, ConstRawData data) override
写入数据 Write 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)
LinuxBinaryFileFlash 构造函数 LinuxBinaryFileFlash constructor.
ErrorCode Erase(size_t offset, size_t size) override
擦除闪存区域
原始数据封装类。 A class for encapsulating raw data.
LibXR Color Control Library / LibXR终端颜色控制库