forked from nanoframework/nf-interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target_littlefs.c
596 lines (488 loc) · 17.2 KB
/
target_littlefs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
#include <ch.h>
#include <hal.h>
#include <target_littlefs.h>
#include <hal_littlefs.h>
#include <hal_stm32_qspi.h>
#define HAL_QPSI_TIMEOUT_CONFIG_COMMAND ((uint32_t)10)
uint8_t lfs_inputBuffer[W25Q512_PAGE_SIZE];
uint8_t lfs_outputBuffer[W25Q512_PAGE_SIZE];
int32_t lfs_inputBufferSize = W25Q512_PAGE_SIZE;
int32_t lfs_outputBufferSize = W25Q512_PAGE_SIZE;
#ifdef DEBUG
uint8_t tempBuffer[W25Q512_PAGE_SIZE];
#endif
///////////////
// declarations
static uint8_t QSPI_ResetMemory(QSPI_HandleTypeDef *hqspi);
static uint8_t QSPI_EnterMemory_QPI(QSPI_HandleTypeDef *hqspi);
static uint8_t QSPI_WriteEnable(QSPI_HandleTypeDef *hqspi);
static uint8_t QSPI_AutoPollingMemReady(QSPI_HandleTypeDef *hqspi, uint32_t Timeout);
static uint8_t QSPI_ReadChipID(QSPI_HandleTypeDef *hqspi, uint8_t *buffer);
static uint8_t QSPI_Read(uint8_t *pData, uint32_t readAddr, uint32_t size);
static uint8_t QSPI_Write(uint8_t *pData, uint32_t writeAddr, uint32_t size);
static uint8_t QSPI_Erase_Block(uint32_t blockAddress);
static bool QSPI_Erase_Chip();
// target specific implementation of hal_lfs_sync
int32_t hal_lfs_sync_(const struct lfs_config *c)
{
(void)c;
__DSB();
return 0;
}
// target specific implementation of hal_lfs_erase
int32_t hal_lfs_erase_0(const struct lfs_config *c, lfs_block_t block)
{
uint32_t ret;
uint32_t addr = block * c->block_size;
ret = QSPI_Erase_Block(addr);
if (ret != QSPI_OK)
{
__NOP();
return LFS_ERR_IO;
}
return LFS_ERR_OK;
}
// target specific implementation of hal_lfs_read
int32_t hal_lfs_read_0(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
{
uint32_t ret;
uint32_t addr = block * c->block_size + off;
ret = QSPI_Read((uint8_t *)buffer, addr, size);
if (ret != QSPI_OK)
{
__NOP();
return LFS_ERR_IO;
}
return LFS_ERR_OK;
}
// target specific implementation of hal_lfs_prog
int32_t hal_lfs_prog_0(
const struct lfs_config *c,
lfs_block_t block,
lfs_off_t off,
const void *buffer,
lfs_size_t size)
{
uint32_t ret;
uint32_t addr = block * c->block_size + off;
ret = QSPI_Write((uint8_t *)buffer, addr, size);
if (ret != QSPI_OK)
{
__NOP();
return LFS_ERR_IO;
}
#ifdef DEBUG
memset(tempBuffer, 0xBB, size);
// read back and compare
QSPI_Read(tempBuffer, (block * c->block_size + off), size);
for (lfs_size_t i = 0; i < size; i++)
{
ASSERT(((const uint8_t *)buffer)[i] == tempBuffer[i]);
}
#endif
return LFS_ERR_OK;
}
// target specific implementation of chip erase
bool hal_lfs_erase_chip_0()
{
return QSPI_Erase_Chip() == TRUE;
}
static bool QSPI_Erase_Chip()
{
// need to do this one one block at a time to avoid watchdog reset
for (uint32_t i = 0; i < W25Q512_FLASH_SIZE / W25Q512_SECTOR_SIZE; i++)
{
if (QSPI_Erase_Block(i * W25Q512_SECTOR_SIZE) != QSPI_OK)
{
return FALSE;
}
// reset watchdog
Watchdog_Reset();
}
return TRUE;
}
static uint8_t QSPI_ResetMemory(QSPI_HandleTypeDef *hqspi)
{
QSPI_CommandTypeDef s_command;
// Initialize the Mode Bit Reset command
s_command.InstructionMode = QSPI_INSTRUCTION_1_LINE;
s_command.Instruction = RESET_ENABLE_CMD;
s_command.AddressMode = QSPI_ADDRESS_NONE;
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
s_command.DataMode = QSPI_DATA_NONE;
s_command.DummyCycles = 0;
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
// Send the command
if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
{
return QSPI_ERROR;
}
// Send the SW reset command
s_command.Instruction = RESET_MEMORY_CMD;
if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
{
return QSPI_ERROR;
}
// Configure automatic polling mode to wait the memory is ready
if (QSPI_AutoPollingMemReady(hqspi, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != QSPI_OK)
{
return QSPI_ERROR;
}
return QSPI_OK;
}
static uint8_t QSPI_EnterMemory_QPI(QSPI_HandleTypeDef *hqspi)
{
QSPI_CommandTypeDef s_command;
uint8_t reg[] = {0};
// Initialize the read volatile configuration register command
s_command.InstructionMode = QSPI_INSTRUCTION_1_LINE;
s_command.Instruction = READ_STATUS_REG2_CMD;
s_command.AddressMode = QSPI_ADDRESS_NONE;
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
s_command.DataMode = QSPI_DATA_1_LINE;
s_command.DummyCycles = 0;
s_command.NbData = 1;
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
// Configure the command
if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
{
return QSPI_ERROR;
}
// Reception of the data
if (HAL_QSPI_Receive(hqspi, ®[0], HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
{
return QSPI_ERROR;
}
// Enable write operations
if (QSPI_WriteEnable(hqspi) != QSPI_OK)
{
return QSPI_ERROR;
}
// Update status register 2 (with quad enable bit)
s_command.Instruction = WRITE_STATUS_REG2_CMD;
MODIFY_REG(reg[0], 0, W25Q512_SR2_QE);
// write status register 2
if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
{
return QSPI_ERROR;
}
// Transmission of the data
if (HAL_QSPI_Transmit(hqspi, ®[0], HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
{
return QSPI_ERROR;
}
// read status register for confirmation
s_command.Instruction = READ_STATUS_REG2_CMD;
// Configure the command
if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
{
return QSPI_ERROR;
}
// Reception of the data
if (HAL_QSPI_Receive(hqspi, ®[0], HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
{
return QSPI_ERROR;
}
if (reg[0] & W25Q512_SR2_QE)
{
return QSPI_OK;
}
else
{
return QSPI_ERROR;
}
}
static uint8_t QSPI_WriteEnable(QSPI_HandleTypeDef *hqspi)
{
QSPI_CommandTypeDef s_command;
QSPI_AutoPollingTypeDef s_config;
// Enable write operations
s_command.InstructionMode = QSPI_INSTRUCTION_1_LINE;
s_command.Instruction = WRITE_ENABLE_CMD;
s_command.AddressMode = QSPI_ADDRESS_NONE;
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
s_command.DataMode = QSPI_DATA_NONE;
s_command.DummyCycles = 0;
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
{
return QSPI_ERROR;
}
// Configure automatic polling mode to wait for write enabling
s_config.Match = W25Q512_SR_WREN;
s_config.Mask = W25Q512_SR_WREN;
s_config.MatchMode = QSPI_MATCH_MODE_AND;
s_config.StatusBytesSize = 1;
s_config.Interval = 0x10;
s_config.AutomaticStop = QSPI_AUTOMATIC_STOP_ENABLE;
s_command.Instruction = READ_STATUS_REG1_CMD;
s_command.DataMode = QSPI_DATA_1_LINE;
if (HAL_QSPI_AutoPolling(hqspi, &s_command, &s_config, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
{
return QSPI_ERROR;
}
return QSPI_OK;
}
static uint8_t QSPI_AutoPollingMemReady(QSPI_HandleTypeDef *hqspi, uint32_t Timeout)
{
QSPI_CommandTypeDef s_command;
QSPI_AutoPollingTypeDef sConfig;
// Configure automatic polling mode to wait for memory ready
s_command.InstructionMode = QSPI_INSTRUCTION_1_LINE;
s_command.Instruction = READ_STATUS_REG1_CMD;
s_command.AddressMode = QSPI_ADDRESS_NONE;
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
s_command.DataMode = QSPI_DATA_1_LINE;
s_command.DummyCycles = 0;
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
sConfig.Match = 0;
sConfig.Mask = W25Q512_SR_WIP;
sConfig.MatchMode = QSPI_MATCH_MODE_AND;
sConfig.StatusBytesSize = 1;
sConfig.Interval = 0x10;
sConfig.AutomaticStop = QSPI_AUTOMATIC_STOP_ENABLE;
if (HAL_QSPI_AutoPolling(hqspi, &s_command, &sConfig, Timeout) != HAL_OK)
{
return QSPI_ERROR;
}
return QSPI_OK;
}
static uint8_t QSPI_ReadChipID(QSPI_HandleTypeDef *hqspi, uint8_t *buffer)
{
QSPI_CommandTypeDef s_command;
// Configure automatic polling mode to wait for memory ready
s_command.InstructionMode = QSPI_INSTRUCTION_1_LINE;
s_command.Instruction = READ_ID_CMD2;
s_command.AddressMode = QSPI_ADDRESS_NONE;
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
s_command.DataMode = QSPI_DATA_1_LINE;
s_command.NbData = 6;
s_command.DummyCycles = 0;
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
if (HAL_QSPI_Command(hqspi, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
{
return QSPI_ERROR;
}
// Reception of the data
if (HAL_QSPI_Receive(hqspi, buffer, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
{
return QSPI_ERROR;
}
return QSPI_OK;
}
uint8_t QSPI_Read(uint8_t *pData, uint32_t readAddr, uint32_t size)
{
QSPI_CommandTypeDef s_command;
HAL_StatusTypeDef status;
uint8_t retryCounter = 3;
// Initialize the read command
s_command.InstructionMode = QSPI_INSTRUCTION_1_LINE;
s_command.Instruction = QUAD_OUT_FAST_READ_CMD;
s_command.AddressMode = QSPI_ADDRESS_1_LINE;
s_command.AddressSize = QSPI_ADDRESS_24_BITS;
s_command.Address = readAddr;
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
s_command.DataMode = QSPI_DATA_4_LINES;
s_command.DummyCycles = W25Q512_DUMMY_CYCLES_READ_QUAD;
s_command.NbData = size;
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
config_command:
// Configure the command
status = HAL_QSPI_Command(&QSPID1, &s_command, HAL_QPSI_TIMEOUT_CONFIG_COMMAND);
if (status != HAL_OK)
{
// try to clear busy bit, if retry counter not exceeded
if (retryCounter-- == 0)
{
__NOP();
return QSPI_ERROR;
}
else
{
// clear QSPI Busy bit
// https://community.st.com/t5/stm32-mcus-products/qspi-flag-qspi-flag-busy-sometimes-stays-set/td-p/365865
QSPID1.State = HAL_QSPI_STATE_BUSY;
status = HAL_QSPI_Abort(&QSPID1);
goto config_command;
}
}
// Set S# timing for Read command
MODIFY_REG(QSPID1.Instance->DCR, QUADSPI_DCR_CSHT, QSPI_CS_HIGH_TIME_2_CYCLE);
// Reception of the data
status = HAL_QSPI_Receive(&QSPID1, pData, HAL_QPSI_TIMEOUT_DEFAULT_VALUE);
if (status != HAL_OK)
{
__NOP();
}
// Restore S# timing for nonRead commands
MODIFY_REG(QSPID1.Instance->DCR, QUADSPI_DCR_CSHT, QSPI_CS_HIGH_TIME_5_CYCLE);
return status == HAL_OK ? QSPI_OK : QSPI_ERROR;
}
uint8_t QSPI_Write(uint8_t *pData, uint32_t writeAddr, uint32_t size)
{
QSPI_CommandTypeDef s_command;
uint32_t end_addr, current_size, current_addr;
// Calculation of the size between the write address and the end of the page
current_size = W25Q512_PAGE_SIZE - (writeAddr % (W25Q512_PAGE_SIZE));
// Check if the size of the data is less than the remaining place in the page
if (current_size > size)
{
current_size = size;
}
// Initialize the adress variables
current_addr = writeAddr;
end_addr = writeAddr + size;
// Initialize the program command
s_command.InstructionMode = QSPI_INSTRUCTION_1_LINE;
s_command.Instruction = PAGE_PROG_CMD;
s_command.AddressMode = QSPI_ADDRESS_1_LINE;
s_command.AddressSize = QSPI_ADDRESS_24_BITS;
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
s_command.DataMode = QSPI_DATA_1_LINE;
s_command.DummyCycles = 0;
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
// Perform the write page by page
do
{
s_command.Address = current_addr;
s_command.NbData = current_size;
// Enable write operations
if (QSPI_WriteEnable(&QSPID1) != QSPI_OK)
{
return QSPI_ERROR;
}
// Configure the command
if (HAL_QSPI_Command(&QSPID1, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
{
return QSPI_ERROR;
}
// Transmission of the data
if (HAL_QSPI_Transmit(&QSPID1, pData, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
{
return QSPI_ERROR;
}
// Configure automatic polling mode to wait for end of program
if (QSPI_AutoPollingMemReady(&QSPID1, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != QSPI_OK)
{
return QSPI_ERROR;
}
// Update the address and size variables for next page programming
current_addr += current_size;
pData += current_size;
current_size = ((current_addr + W25Q512_PAGE_SIZE) > end_addr) ? (end_addr - current_addr) : W25Q512_PAGE_SIZE;
} while (current_addr < end_addr);
return QSPI_OK;
}
uint8_t QSPI_Erase_Block(uint32_t blockAddress)
{
QSPI_CommandTypeDef s_command;
// Initialize the erase command
s_command.InstructionMode = QSPI_INSTRUCTION_1_LINE;
s_command.Instruction = SECTOR_ERASE_CMD;
s_command.AddressMode = QSPI_ADDRESS_1_LINE;
s_command.AddressSize = QSPI_ADDRESS_24_BITS;
s_command.Address = blockAddress;
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
s_command.DataMode = QSPI_DATA_NONE;
s_command.DummyCycles = 0;
s_command.DdrMode = QSPI_DDR_MODE_DISABLE;
s_command.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
s_command.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
// Enable write operations
if (QSPI_WriteEnable(&QSPID1) != QSPI_OK)
{
return QSPI_ERROR;
}
// Send the command
if (HAL_QSPI_Command(&QSPID1, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
{
return QSPI_ERROR;
}
// Configure automatic polling mode to wait for end of erase
if (QSPI_AutoPollingMemReady(&QSPID1, W25Q512_SECTOR_ERASE_MAX_TIME) != QSPI_OK)
{
return QSPI_ERROR;
}
return QSPI_OK;
}
int8_t target_lfs_init()
{
uint8_t dataBuffer[3];
// need to adjust MPU in order to use QSPI memory, despite using indirect access
// this is a know issue with STM32F7
// https://community.st.com/t5/stm32-mcus-products/qspi-flag-qspi-flag-busy-sometimes-stays-set/td-p/365865
// need to disable MPU in order to change regions
HAL_MPU_Disable();
MPU_Region_InitTypeDef MPU_InitStruct;
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER1;
MPU_InitStruct.BaseAddress = 0x90000000;
MPU_InitStruct.Size = MPU_REGION_SIZE_64MB;
MPU_InitStruct.SubRegionDisable = 0x0;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
MPU_InitStruct.AccessPermission = MPU_REGION_NO_ACCESS;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
// Enable the MPU
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
// QSPI initialization
QSPID1.Init.ClockPrescaler = 1;
QSPID1.Init.FifoThreshold = 4;
QSPID1.Init.SampleShifting = QSPI_SAMPLE_SHIFTING_HALFCYCLE;
// OK to use the W25Q512_FLASH_SIZE for this instance
QSPID1.Init.FlashSize = POSITION_VAL(W25Q512_FLASH_SIZE) - 1;
QSPID1.Init.ChipSelectHighTime = QSPI_CS_HIGH_TIME_2_CYCLE;
QSPID1.Init.ClockMode = QSPI_CLOCK_MODE_0;
QSPID1.Init.FlashID = QSPI_FLASH_ID_1;
QSPID1.Init.DualFlash = QSPI_DUALFLASH_DISABLE;
// init driver
qspiStart(&QSPID1);
if (HAL_QSPI_Init(&QSPID1) != HAL_OK)
{
return QSPI_ERROR;
}
// QSPI memory reset
if (QSPI_ResetMemory(&QSPID1) != QSPI_OK)
{
return QSPI_NOT_SUPPORTED;
}
// Put QSPI memory in QPI mode
if (QSPI_EnterMemory_QPI(&QSPID1) != QSPI_OK)
{
return QSPI_NOT_SUPPORTED;
}
// sanity check: read device ID and unique ID
// this instruction requires a buffer with 6 positions
if (QSPI_ReadChipID(&QSPID1, dataBuffer) != QSPI_OK)
{
return QSPI_ERROR;
}
// constants from ID Definitions table in W25Q512 datasheet
ASSERT(dataBuffer[0] == W25Q512_MANUFACTURER_ID);
ASSERT(dataBuffer[1] == W25Q512_DEVICE_ID1);
ASSERT(dataBuffer[2] == W25Q512_DEVICE_ID2);
// from W25Q512 datasheet: Time Delay Before Write Instruction is >5ms
chThdSleepMilliseconds(10);
return LFS_ERR_OK;
}