libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
qspi.h
Go to the documentation of this file.
1#ifndef DSY_QSPI
2#define DSY_QSPI
4#ifndef UNIT_TEST // for unit tests, a dummy implementation
5
6#include <cstdint>
7#include "daisy_core.h"
8#include "util/hal_map.h"
9
10#define DSY_QSPI_TEXT \
11 __attribute__((section( \
12 ".qspiflash_text")))
13#define DSY_QSPI_DATA \
14 __attribute__((section( \
15 ".qspiflash_data")))
16#define DSY_QSPI_BSS \
17 __attribute__((section( \
18 ".qspiflash_bss")))
20namespace daisy
21{
32{
33 public:
34 enum Result
35 {
36 OK = 0,
37 ERR
38 };
39
56
58 struct Config
59 {
67
81
82 //SCK, CE# (active low)
83 struct
84 {
92
95 };
96
103
105 const Config& GetConfig() const;
106
107 // Couldn't this just be called before anything else in init? That
108 // would make manually calling it unnecessary.
115
125
134
144
152
157
166
167 QSPIHandle() : pimpl_(nullptr) {}
168 QSPIHandle(const QSPIHandle& other) = default;
170
171 class Impl;
173 private:
174 Impl* pimpl_;
175};
176
179} // namespace daisy
180
181#else
182
183#include <cstdint>
184#include "../tests/TestIsolator.h"
185
186namespace daisy
187{
194class QSPIHandle
195{
196 public:
197 enum Result
198 {
199 OK = 0,
200 ERR
201 };
202
207 static Result ResetAndClear()
208 {
209 testIsolator_.GetStateForCurrentTest()->memory_.clear();
210 return Result::OK;
211 }
212
213
214 static Result Write(uint32_t address, uint32_t size, uint8_t* buffer)
215 {
216 // 256-byte aligned, normalized address value
217 uint32_t adjusted_addr = (address) & (uint32_t)(~0xff);
218 // Make sure memory is of approriate size
219 uint32_t total_bytes = adjusted_addr + size;
220 AdaptToSize(total_bytes);
221 // Copy data into vector
222 uint8_t* dest = testIsolator_.GetStateForCurrentTest()->memory_.data();
223 std::copy(&buffer[adjusted_addr],
224 &buffer[adjusted_addr + size],
225 &dest[adjusted_addr]);
226 return Result::OK;
227 }
228
229 static Result Erase(uint32_t start_addr, uint32_t end_addr)
230 {
231 uint32_t adjusted_start_addr = (start_addr) & (uint32_t)(~0xff);
232 uint32_t adjusted_end_addr = (end_addr) & (uint32_t)(~0xff);
233
234 // guard addresses
235 assert(adjusted_start_addr < kMaxAdjustedAddr);
236 assert(adjusted_end_addr < kMaxAdjustedAddr);
237
238 // Make sure vector is of appropriate size
239 // size should be at least (adjusted_end_addr)
240 AdaptToSize(adjusted_end_addr);
241 uint8_t* buff = testIsolator_.GetStateForCurrentTest()->memory_.data();
242 // Erases memory by setting all bits to 1
243 std::fill(&buff[adjusted_start_addr], &buff[adjusted_end_addr], 0xff);
244 return Result::OK;
245 }
246
249 static void* GetData(uint32_t offset = 0)
250 {
251 assert(offset < kMaxAdjustedAddr);
252 AdaptToSize(offset + 1);
253 return (void*)(testIsolator_.GetStateForCurrentTest()->memory_.data()
254 + offset);
255 }
256
261 static size_t GetCurrentSize()
262 {
263 return testIsolator_.GetStateForCurrentTest()->memory_.size();
264 }
265
266 private:
268 static void AdaptToSize(uint32_t required_bytes)
269 {
270 if(testIsolator_.GetStateForCurrentTest()->memory_.size()
271 < required_bytes)
272 testIsolator_.GetStateForCurrentTest()->memory_.resize(
273 required_bytes, 0x00);
274 }
275 static constexpr uint32_t kMaxAdjustedAddr = 0x800000;
276 struct QSPIState
277 {
278 // Emulate the byte-memory of the QSPI flash
279 std::vector<uint8_t> memory_;
280 };
281 static TestIsolator<QSPIState> testIsolator_;
282};
283
284
285} // namespace daisy
286
287
288#endif // ifndef UNIT_TEST
289
290#endif // ifndef DSY_QSPI
Definition leddriver.h:33
Definition qspi.h:32
const Config & GetConfig() const
QSPIHandle(const QSPIHandle &other)=default
Status
Definition qspi.h:50
@ GOOD
Definition qspi.h:51
@ E_INVALID_MODE
Definition qspi.h:54
@ E_HAL_ERROR
Definition qspi.h:52
@ E_SWITCHING_MODES
Definition qspi.h:53
QSPIHandle & operator=(const QSPIHandle &other)=default
Result EraseSector(uint32_t address)
Result Erase(uint32_t start_addr, uint32_t end_addr)
Result Init(const Config &config)
QSPIHandle()
Definition qspi.h:167
void * GetData(uint32_t offset=0)
Result
Definition qspi.h:35
@ OK
Definition qspi.h:36
@ ERR
Definition qspi.h:37
Result Write(uint32_t address, uint32_t size, uint8_t *buffer)
Result WritePage(uint32_t address, uint32_t size, uint8_t *buffer)
Hardware defines and helpers for daisy field platform.
Definition index.h:2
representation of hardware port/pin combination
Definition daisy_core.h:193
Definition qspi.h:59
Device
Definition qspi.h:62
@ IS25LP064A
Definition qspi.h:64
@ DEVICE_LAST
Definition qspi.h:65
@ IS25LP080D
Definition qspi.h:63
Pin io0
Definition qspi.h:85
Pin ncs
Definition qspi.h:90
Pin io2
Definition qspi.h:87
Pin clk
Definition qspi.h:89
Pin io3
Definition qspi.h:88
Device device
Definition qspi.h:93
struct daisy::QSPIHandle::Config::@16 pin_config
Mode
Definition qspi.h:76
@ MEMORY_MAPPED
Definition qspi.h:77
@ INDIRECT_POLLING
Definition qspi.h:78
@ MODE_LAST
Definition qspi.h:79
Pin io1
Definition qspi.h:86
Mode mode
Definition qspi.h:94