forked from nanoframework/nf-interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target_spiffs.c
822 lines (660 loc) · 23.3 KB
/
target_spiffs.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
//
// Copyright (c) .NET Foundation and Contributors
// Portions Copyright (c) 2016 STMicroelectronics. All rights reserved.
// See LICENSE file in the project root for full license information.
//
#include <hal.h>
#include <hal_spiffs.h>
#include <cache.h>
#ifdef SPIFFS_SPI1
static const SPIConfig spiConfig = {
.circular = false,
.slave = false,
.data_cb = NULL,
.error_cb = NULL,
// CPHA=0, CPOL=0, MSb first
.cr1 = 0U, // SPI_CR1_CPOL | SPI_CR1_BR_0,
// transfer length to 8bit
.cr2 = SPI_CR2_DS_2 | SPI_CR2_DS_1 | SPI_CR2_DS_0};
#if defined(__GNUC__)
__attribute__((aligned(32)))
#endif
uint8_t writeBuffer[SPIFFS_LOGICAL_PAGE_SIZE];
#if defined(__GNUC__)
__attribute__((aligned(32)))
#endif
uint8_t readBuffer[SPIFFS_LOGICAL_PAGE_SIZE];
///////////////
// Definitions
#define CS_SELECT palClearPad(PAL_PORT(LINE_FLASH_SPI1_CS), PAL_PAD(LINE_FLASH_SPI1_CS))
#define CS_UNSELECT palSetPad(PAL_PORT(LINE_FLASH_SPI1_CS), PAL_PAD(LINE_FLASH_SPI1_CS))
///////////////
// declarations
bool SPI_Erase_Block(u32_t addr);
bool SPI_Read(uint8_t *pData, uint32_t readAddr, uint32_t size);
bool SPI_Write(uint8_t *pData, uint32_t writeAddr, uint32_t size);
extern uint32_t HAL_GetTick(void);
// target specific implementation of hal_spiffs_erase
s32_t hal_spiffs_erase_0(u32_t addr, u32_t size)
{
// how many sectors need to be erased?
uint32_t erase_count = (size + SPIFFS0_ERASE_BLOCK_SIZE - 1) / SPIFFS0_ERASE_BLOCK_SIZE;
for (uint32_t i = 0; i < erase_count; i++)
{
Watchdog_Reset();
if (!SPI_Erase_Block(addr))
{
return SPIFFS_ERROR;
}
// adjust sector address
addr += i * SPIFFS0_ERASE_BLOCK_SIZE;
}
return SPIFFS_SUCCESS;
}
// target specific implementation of hal_spiffs_read
s32_t hal_spiffs_read_0(u32_t addr, u32_t size, u8_t *dst)
{
if (!SPI_Read(dst, addr, size))
{
return SPIFFS_ERROR;
}
return SPIFFS_SUCCESS;
}
// target specific implementation of hal_spiffs_write
s32_t hal_spiffs_write_0(u32_t addr, u32_t size, u8_t *src)
{
if (!SPI_Write(src, addr, size))
{
return SPIFFS_ERROR;
}
return SPIFFS_SUCCESS;
}
bool SPI_WaitOnBusy()
{
uint32_t tickstart = HAL_GetTick();
writeBuffer[0] = READ_STATUS_REG1_CMD;
CS_SELECT;
// send read status register 1
spiSend(&SPID1, 1, writeBuffer);
while (true)
{
// read register value
spiReceive(&SPID1, 1, readBuffer);
if (!(readBuffer[0] & AT25SF641_SR_BUSY))
{
// BuSY bit is cleared
break;
}
if ((HAL_GetTick() - tickstart) > HAL_SPI_TIMEOUT_DEFAULT_VALUE)
{
// operation timeout
// unselect SPI
CS_UNSELECT;
return false;
}
}
CS_UNSELECT;
return true;
}
bool SPI_Erase_Block(u32_t addr)
{
// send write enable
writeBuffer[0] = WRITE_ENABLE_CMD;
CS_SELECT;
spiSend(&SPID1, 1, writeBuffer);
CS_UNSELECT;
// send block erase
writeBuffer[0] = SECTOR_ERASE_CMD;
writeBuffer[1] = (uint8_t)(addr >> 16);
writeBuffer[2] = (uint8_t)(addr >> 8);
writeBuffer[3] = (uint8_t)addr;
// flush DMA buffer to ensure cache coherency
// (only required for Cortex-M7)
cacheBufferFlush(writeBuffer, 4);
CS_SELECT;
spiSend(&SPID1, 4, writeBuffer);
CS_UNSELECT;
// wait for erase operation to complete
return SPI_WaitOnBusy();
}
bool SPI_Read(uint8_t *pData, uint32_t readAddr, uint32_t size)
{
// send read page command
writeBuffer[0] = READ_CMD;
writeBuffer[1] = (uint8_t)(readAddr >> 16);
writeBuffer[2] = (uint8_t)(readAddr >> 8);
writeBuffer[3] = (uint8_t)readAddr;
// flush DMA buffer to ensure cache coherency
// (only required for Cortex-M7)
cacheBufferFlush(writeBuffer, 4);
CS_SELECT;
spiSend(&SPID1, 4, writeBuffer);
// clear read buffer
memset(readBuffer, 0, SPIFFS_LOGICAL_PAGE_SIZE);
spiReceive(&SPID1, size, readBuffer);
CS_UNSELECT;
// invalidate cache
// (only required for Cortex-M7)
cacheBufferInvalidate(readBuffer, size);
// copy to pointer
memcpy(pData, readBuffer, size);
return true;
}
bool SPI_Write(uint8_t *pData, uint32_t writeAddr, uint32_t size)
{
// send write enable
writeBuffer[0] = WRITE_ENABLE_CMD;
CS_SELECT;
spiSend(&SPID1, 1, writeBuffer);
CS_UNSELECT;
// send write page
writeBuffer[0] = PAGE_PROG_CMD;
writeBuffer[1] = (uint8_t)(writeAddr >> 16);
writeBuffer[2] = (uint8_t)(writeAddr >> 8);
writeBuffer[3] = (uint8_t)writeAddr;
// flush DMA buffer to ensure cache coherency
// (only required for Cortex-M7)
cacheBufferFlush(writeBuffer, 4);
CS_SELECT;
spiSend(&SPID1, 4, writeBuffer);
// copy from buffer
memcpy(writeBuffer, pData, size);
// flush DMA buffer to ensure cache coherency
// (only required for Cortex-M7)
cacheBufferFlush(writeBuffer, size);
spiSend(&SPID1, size, writeBuffer);
CS_UNSELECT;
// wait for erase operation to complete
SPI_WaitOnBusy();
// send write disable
writeBuffer[0] = WRITE_DISABLE_CMD;
cacheBufferFlush(writeBuffer, 1);
CS_SELECT;
spiSend(&SPID1, 1, writeBuffer);
CS_UNSELECT;
return true;
}
#endif // SPIFFS_SPI1
#ifdef SPIFFS_QSPI
#include <hal_stm32_qspi.h>
///////////////
// declarations
static uint8_t QSPI_ResetMemory(QSPI_HandleTypeDef *hqspi);
// static uint8_t QSPI_EnterFourBytesAddress(QSPI_HandleTypeDef *hqspi);
static uint8_t QSPI_EnterMemory_QPI(QSPI_HandleTypeDef *hqspi);
// static uint8_t QSPI_ExitMemory_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);
// target specific implementation of hal_spiffs_erase
s32_t hal_spiffs_erase_0(u32_t addr, u32_t size)
{
// how many sectors need to be erased?
uint32_t erase_count = (size + SPIFFS0_ERASE_BLOCK_SIZE - 1) / SPIFFS0_ERASE_BLOCK_SIZE;
for (uint32_t i = 0; i < erase_count; i++)
{
if (QSPI_Erase_Block(addr) != QSPI_OK)
{
return SPIFFS_ERROR;
}
// adjust sector address
addr += i * SPIFFS0_ERASE_BLOCK_SIZE;
}
return SPIFFS_SUCCESS;
}
// target specific implementation of hal_spiffs_read
s32_t hal_spiffs_read_0(u32_t addr, u32_t size, u8_t *dst)
{
if (QSPI_Read(dst, addr, size) != QSPI_OK)
{
return SPIFFS_ERROR;
}
return SPIFFS_SUCCESS;
}
// target specific implementation of hal_spiffs_write
s32_t hal_spiffs_write_0(u32_t addr, u32_t size, u8_t *src)
{
if (QSPI_Write(src, addr, size) != QSPI_OK)
{
return SPIFFS_ERROR;
}
return SPIFFS_SUCCESS;
}
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_ExitMemory_QPI( QSPI_HandleTypeDef *hqspi)
// {
// QSPI_CommandTypeDef s_command;
// /* Initialize the QPI enable command */
// /* QSPI memory is supported to be in QPI mode, so CMD on 4 LINES */
// s_command.InstructionMode = QSPI_INSTRUCTION_4_LINES;
// s_command.Instruction = EXIT_QUAD_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;
// }
// return QSPI_OK;
// }
// static uint8_t QSPI_EnterFourBytesAddress(QSPI_HandleTypeDef *hqspi)
// {
// (void)hqspi;
// // QSPI_CommandTypeDef s_command;
// // uint8_t reg1;
// // /* Initialize the command */
// // s_command.InstructionMode = QSPI_INSTRUCTION_1_LINE;
// // s_command.Instruction = ENTER_4_BYTE_ADDR_MODE_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;
// // /* Enable write operations */
// // if (QSPI_WriteEnable(hqspi) != QSPI_OK)
// // {
// // return QSPI_ERROR;
// // }
// // /* Send the command */
// // 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_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; /* same value on both memory types */
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; /* same value on both memory types */
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;
/* Initialize the read command */
s_command.InstructionMode = QSPI_INSTRUCTION_1_LINE;
s_command.Instruction = QUAD_OUT_FAST_READ_CMD; /* same value on both memory types */
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;
/* Configure the command */
if (HAL_QSPI_Command(&QSPID1, &s_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
{
return QSPI_ERROR;
}
/* Set S# timing for Read command */
MODIFY_REG(QSPID1.Instance->DCR, QUADSPI_DCR_CSHT, QSPI_CS_HIGH_TIME_2_CYCLE);
/* Reception of the data */
if (HAL_QSPI_Receive(&QSPID1, pData, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK)
{
return QSPI_ERROR;
}
/* Restore S# timing for nonRead commands */
MODIFY_REG(QSPID1.Instance->DCR, QUADSPI_DCR_CSHT, QSPI_CS_HIGH_TIME_5_CYCLE);
return QSPI_OK;
}
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 = QUAD_IN_FAST_PROG_CMD; /* same value on both memory types */
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_4_LINES;
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;
}
#endif // SPIFFS_QSPI
// initialization of everything required for SPIFFS
// has to work for both drivers
uint8_t target_spiffs_init()
{
#ifdef SPIFFS_SPI1
// init driver
spiAcquireBus(&SPID1);
spiStart(&SPID1, &spiConfig);
////////////////////////////////////////////////////////////////////////
// no need to worry with cache issues at this early stage of the boot //
////////////////////////////////////////////////////////////////////////
// resume from deep power down
// have to send this to make sure device is functional after sleep
writeBuffer[0] = RESUME_DEEP_PD_CMD;
CS_SELECT;
spiSend(&SPID1, 1, writeBuffer);
CS_UNSELECT;
// sanity check: read device ID and unique ID
writeBuffer[0] = READ_ID_CMD2;
// flush DMA buffer to ensure cache coherency
cacheBufferFlush(writeBuffer, 1);
CS_SELECT;
spiExchange(&SPID1, 4, writeBuffer, readBuffer);
CS_UNSELECT;
// constants from ID Definitions table in AT25SF641 datasheet
ASSERT(readBuffer[1] == AT25SF641_MANUFACTURER_ID);
ASSERT(readBuffer[2] == AT25SF641_DEVICE_ID1);
ASSERT(readBuffer[3] == AT25SF641_DEVICE_ID2);
#endif // SPIFFS_SPI1
#ifdef SPIFFS_QSPI
uint8_t device_id[6];
memset(device_id, 0, sizeof(device_id));
/* QSPI initialization */
QSPID1.Init.ClockPrescaler = 1;
QSPID1.Init.FifoThreshold = 4;
QSPID1.Init.SampleShifting = QSPI_SAMPLE_SHIFTING_HALFCYCLE;
// OK to use the SPIFFS_TOTAL_SIZE for this instance
QSPID1.Init.FlashSize = POSITION_VAL(SPIFFS0_TOTAL_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;
}
// not used with this memory chip
// /* Set the QSPI memory in 4-bytes address mode */
// if(QSPI_EnterFourBytesAddress(&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, device_id) != QSPI_OK)
{
return QSPI_ERROR;
}
// constants from ID Definitions table in W25Q512 datasheet
ASSERT(device_id[0] == W25Q512_MANUFACTURER_ID);
ASSERT(device_id[1] == W25Q512_DEVICE_ID1);
ASSERT(device_id[2] == W25Q512_DEVICE_ID2);
#endif // SPIFFS_QSPI
return 1;
}
#ifdef SPIFFS_SPI1
#endif // SPIFFS_SPI1
#ifdef SPIFFS_QSPI
#endif // SPIFFS_QSPI