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{
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 {
65 return ErrorCode::OUT_OF_RANGE;
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 {
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 }
116
117 private:
118 std::string file_path_;
119 std::array<uint8_t, FLASH_SIZE> flash_area_ = {};
120 bool write_order_check_;
121 bool write_as_one_check_;
122
123 ErrorCode SyncToFile()
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 }
134};
135} // 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:19
size_t MinWriteSize() const
Returns the minimum writable block size in bytes. 获取最小可写块大小(字节)。
Definition flash.hpp:73
size_t MinEraseSize() const
Returns the minimum erasable block size in bytes. 获取最小可擦除块大小(字节)。
Definition flash.hpp:64
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
Definition libxr_mem.cpp:3
原始数据封装类。 A class for encapsulating raw data.
LibXR 命名空间
Definition ch32_can.hpp:14