libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
flash_io.hpp
1
18void ReadFlashOrExit(size_t offset, RawData data)
19{
20 REQUIRE(flash_.Read(offset, data) == ErrorCode::OK);
21}
22
30template <typename Data>
31void ReadFlashOrExit(size_t offset, Data& data)
32{
33 ReadFlashOrExit(offset, RawData(data));
34}
35
42void WriteFlashOrExit(size_t offset, ConstRawData data)
43{
44 REQUIRE(Write(offset, data) == ErrorCode::OK);
45}
46
54template <typename Data>
55void WriteFlashOrExit(size_t offset, const Data& data)
56{
57 WriteFlashOrExit(offset, ConstRawData(data));
58}
59
66void EraseFlashOrExit(size_t offset, size_t size)
67{
68 REQUIRE(flash_.Erase(offset, size) == ErrorCode::OK);
69}
70
78void CopyFlashData(size_t dst_offset, size_t src_offset, size_t size)
79{
80 for (size_t i = 0; i < size; i += MinWriteSize)
81 {
82 ReadFlashOrExit(src_offset + i, {write_buffer_, MinWriteSize});
83 WriteFlashOrExit(dst_offset + i, {write_buffer_, MinWriteSize});
84 }
85}
86
93size_t AlignSize(size_t size)
94{
95 return static_cast<size_t>((size + MinWriteSize - 1) / MinWriteSize) * MinWriteSize;
96}
97
108ErrorCode Write(size_t offset, ConstRawData data)
109{
110 if (data.size_ == 0)
111 {
112 return ErrorCode::OK;
113 }
114
115 if (data.size_ % MinWriteSize == 0)
116 {
117 return flash_.Write(offset, data);
118 }
119 else
120 {
121 auto final_block_index = data.size_ - data.size_ % MinWriteSize;
122 if (final_block_index != 0)
123 {
124 auto ec = flash_.Write(offset, {data.addr_, final_block_index});
125 if (ec != ErrorCode::OK)
126 {
127 return ec;
128 }
129 }
130 Memory::FastSet(write_buffer_, 0xff, MinWriteSize);
132 write_buffer_, reinterpret_cast<const uint8_t*>(data.addr_) + final_block_index,
133 data.size_ % MinWriteSize);
134 return flash_.Write(offset + final_block_index, {write_buffer_, MinWriteSize});
135 }
136}
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
ErrorCode
定义错误码枚举