libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
flash_io.hpp
1
18void ReadFlashOrExit(size_t offset, RawData data)
19{
20 [[maybe_unused]] const auto flash_io_result = flash_.Read(offset, data);
21 REQUIRE(flash_io_result == ErrorCode::OK);
22}
23
31template <typename Data>
32void ReadFlashOrExit(size_t offset, Data& data)
33{
34 ReadFlashOrExit(offset, RawData(data));
35}
36
43void WriteFlashOrExit(size_t offset, ConstRawData data)
44{
45 [[maybe_unused]] const auto flash_io_result = Write(offset, data);
46 REQUIRE(flash_io_result == ErrorCode::OK);
47}
48
56template <typename Data>
57void WriteFlashOrExit(size_t offset, const Data& data)
58{
59 WriteFlashOrExit(offset, ConstRawData(data));
60}
61
68void EraseFlashOrExit(size_t offset, size_t size)
69{
70 [[maybe_unused]] const auto flash_io_result = flash_.Erase(offset, size);
71 REQUIRE(flash_io_result == ErrorCode::OK);
72}
73
81void CopyFlashData(size_t dst_offset, size_t src_offset, size_t size)
82{
83 for (size_t i = 0; i < size; i += MinWriteSize)
84 {
85 ReadFlashOrExit(src_offset + i, {write_buffer_, MinWriteSize});
86 WriteFlashOrExit(dst_offset + i, {write_buffer_, MinWriteSize});
87 }
88}
89
96size_t AlignSize(size_t size)
97{
98 return static_cast<size_t>((size + MinWriteSize - 1) / MinWriteSize) * MinWriteSize;
99}
100
111ErrorCode Write(size_t offset, ConstRawData data)
112{
113 if (data.size_ == 0)
114 {
115 return ErrorCode::OK;
116 }
117
118 if (data.size_ % MinWriteSize == 0)
119 {
120 return flash_.Write(offset, data);
121 }
122 else
123 {
124 auto final_block_index = data.size_ - data.size_ % MinWriteSize;
125 if (final_block_index != 0)
126 {
127 auto ec = flash_.Write(offset, {data.addr_, final_block_index});
128 if (ec != ErrorCode::OK)
129 {
130 return ec;
131 }
132 }
133 Memory::FastSet(write_buffer_, 0xff, MinWriteSize);
135 write_buffer_, reinterpret_cast<const uint8_t*>(data.addr_) + final_block_index,
136 data.size_ % MinWriteSize);
137 return flash_.Write(offset + final_block_index, {write_buffer_, MinWriteSize});
138 }
139}
static void FastCopy(void *dst, const void *src, size_t size)
快速内存拷贝 / Fast memory copy
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes