-
Notifications
You must be signed in to change notification settings - Fork 4
/
stm32h747i_discovery_qspi.c
1060 lines (954 loc) · 32.1 KB
/
stm32h747i_discovery_qspi.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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
******************************************************************************
* @file stm32h747i_discovery_qspi.c
* @author MCD Application Team
* @brief This file includes a standard driver for the MT25TL01G QSPI
* memory mounted on STM32H747I-DISCO board.
@verbatim
How To use this driver:
-----------------------
- This driver is used to drive the MT25TL01G QSPI external
memory mounted on STM32H747I-DISCO board.
- This driver need a specific component driver (MT25TL01G) to be included with.
Driver description:
-------------------
- Initialization steps:
+ Initialize the QPSI external memory using the BSP_QSPI_Init() function. This
function includes the MSP layer hardware resources initialization and the
QSPI interface with the external memory.
STR and DTR transfer rates are supported.
SPI, SPI 2-IO, SPI-4IO and QPI modes are supported
- QSPI memory operations
+ QSPI memory can be accessed with read/write operations once it is
initialized.
Read/write operation can be performed with AHB access using the functions
BSP_QSPI_Read()/BSP_QSPI_Write().
+ The function BSP_QSPI_GetInfo() returns the configuration of the QSPI memory.
(see the QSPI memory data sheet)
+ Perform erase block operation using the function BSP_QSPI_EraseBlock() and by
specifying the block address. You can perform an erase operation of the whole
chip by calling the function BSP_QSPI_EraseChip().
+ The function BSP_QSPI_GetStatus() returns the current status of the QSPI memory.
(see the QSPI memory data sheet)
+ The function BSP_QSPI_EnableMemoryMappedMode enables the QSPI memory mapped mode
+ The function BSP_QSPI_DisableMemoryMappedMode disables the QSPI memory mapped mode
+ The function BSP_QSPI_ConfigFlash() allow to configure the QSPI mode and transfer rate
Note:
--------
Regarding the "Instance" parameter, needed for all functions, it is used to select
an QSPI instance. On the STM32H747I_DISCO board, there's one instance. Then, this
parameter should be 0.
@endverbatim
******************************************************************************
* @attention
*
* Copyright (c) 2018 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32h747i_discovery_qspi.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup STM32H747I_DISCO
* @{
*/
/** @defgroup STM32H747I_DISCO_QSPI QSPI
* @{
*/
/* Private variables ---------------------------------------------------------*/
/** @defgroup STM32H747I_DISCO_QSPI_Exported_Variables Exported Variables
* @{
*/
QSPI_HandleTypeDef hqspi;
BSP_QSPI_Ctx_t QSPI_Ctx[QSPI_INSTANCES_NUMBER];
/**
* @}
*/
/* Private functions ---------------------------------------------------------*/
/** @defgroup STM32H747I_DISCO_QSPI_Private_Functions Private Functions
* @{
*/
static void QSPI_MspInit(QSPI_HandleTypeDef *hQspi);
static void QSPI_MspDeInit(QSPI_HandleTypeDef *hSspi);
static int32_t QSPI_ResetMemory(uint32_t Instance);
static int32_t QSPI_DummyCyclesCfg(uint32_t Instance);
/**
* @}
*/
/** @defgroup STM32H747I_DISCO_QSPI_Exported_Functions Exported Functions
* @{
*/
/**
* @brief Initializes the QSPI interface.
* Instance management :
* Instance can running at Indirect or MMP mode.
* Every indirect instance can switch between.
* Once MMP instance active then Instance switch function disabled.
* Only 1 MMP instance can run.
* Instance switch function enabled after MMP instance exit to Indirect mode.
*
* MT25TL01G action :
* QE(Quad Enable, Non-volatile) bit of Status Register
* QE = 0; WP# & RESET# pin active
* Accept 1-1-1, 1-1-2, 1-2-2 commands
* QE = 1; WP# become SIO2 pin, RESET# become SIO3 pin
* Accept 1-1-1, 1-1-2, 1-2-2, 1-1-4, 1-4-4 commands
* Enter QPI mode by issue EQIO(0x35) command from 1-1-1 mode
* Accept 4-4-4 commands
* Exit QPI mode by issue RSTQIO(0xF5) command from 4-4-4 mode
* Accept commands, dependent QE bit status
* Memory Read commands support STR(Single Transfer Rate) & DTR(Double Transfer Rate) mode
*
* Force QE = 1, Enter 4-Byte address mode
* Configure Dummy cycle & ODS(Out Driver Strength)
* STR/DTR effect read commands only
* @param Instance QSPI Instance
* @param Init QSPI Init structure
* @retval BSP status
*/
int32_t BSP_QSPI_Init(uint32_t Instance, BSP_QSPI_Init_t *Init)
{
int32_t ret = BSP_ERROR_NONE;
BSP_QSPI_Info_t pInfo;
MX_QSPI_Init_t qspi_init;
/* Table to handle clock prescalers:
1: For STR mode to reach max 108Mhz
3: For DTR mode to reach max 54Mhz
*/
static const uint32_t PrescalerTab[2] = {1, 3};
/* Check if the instance is supported */
if(Instance >= QSPI_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Check if instance is already initialized */
if(QSPI_Ctx[Instance].IsInitialized == QSPI_ACCESS_NONE)
{
#if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
/* Register the QSPI MSP Callbacks */
if(QSPI_Ctx[Instance].IsMspCallbacksValid == 0UL)
{
if(BSP_QSPI_RegisterDefaultMspCallbacks(Instance) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
}
#else
/* Msp QSPI initialization */
QSPI_MspInit(&hqspi);
#endif /* USE_HAL_QSPI_REGISTER_CALLBACKS */
if(ret == BSP_ERROR_NONE)
{
/* STM32 QSPI interface initialization */
(void)MT25TL01G_GetFlashInfo(&pInfo);
qspi_init.ClockPrescaler = PrescalerTab[Init->TransferRate];
qspi_init.DualFlashMode = QSPI_DUALFLASH_ENABLE;
qspi_init.FlashSize = (uint32_t)POSITION_VAL((uint32_t)pInfo.FlashSize) - 1U;
qspi_init.SampleShifting = (Init->TransferRate == BSP_QSPI_STR_TRANSFER) ? QSPI_SAMPLE_SHIFTING_HALFCYCLE : QSPI_SAMPLE_SHIFTING_NONE;
if(MX_QSPI_Init(&hqspi, &qspi_init) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}/* QSPI memory reset */
else if(QSPI_ResetMemory(Instance) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Force Flash enter 4 Byte address mode */
else if(MT25TL01G_AutoPollingMemReady(&hqspi, QSPI_Ctx[Instance].InterfaceMode) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else if(MT25TL01G_Enter4BytesAddressMode(&hqspi, QSPI_Ctx[Instance].InterfaceMode) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Configuration of the dummy cycles on QSPI memory side */
else if(QSPI_DummyCyclesCfg(Instance) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Configure Flash to desired mode */
if(BSP_QSPI_ConfigFlash(Instance, Init->InterfaceMode, Init->TransferRate) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
}
}
}
/* Return BSP status */
return ret;
}
/**
* @brief De-Initializes the QSPI interface.
* @param Instance QSPI Instance
* @retval BSP status
*/
int32_t BSP_QSPI_DeInit(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if(Instance >= QSPI_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
if(QSPI_Ctx[Instance].IsInitialized == QSPI_ACCESS_MMP)
{
if(BSP_QSPI_DisableMemoryMappedMode(Instance) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
if(ret == BSP_ERROR_NONE)
{
/* Set default QSPI_Ctx values */
QSPI_Ctx[Instance].IsInitialized = QSPI_ACCESS_NONE;
QSPI_Ctx[Instance].InterfaceMode = BSP_QSPI_SPI_MODE;
QSPI_Ctx[Instance].TransferRate = BSP_QSPI_STR_TRANSFER;
QSPI_Ctx[Instance].DualFlashMode = QSPI_DUALFLASH_ENABLE;
#if (USE_HAL_QSPI_REGISTER_CALLBACKS == 0)
QSPI_MspDeInit(&hqspi);
#endif /* (USE_HAL_QSPI_REGISTER_CALLBACKS == 0) */
/* Call the DeInit function to reset the driver */
if (HAL_QSPI_DeInit(&hqspi) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Initializes the QSPI interface.
* @param hQspi QSPI handle
* @param Config QSPI configuration structure
* @retval BSP status
*/
__weak HAL_StatusTypeDef MX_QSPI_Init(QSPI_HandleTypeDef *hQspi, MX_QSPI_Init_t *Config)
{
/* QSPI initialization */
/* QSPI freq = SYSCLK /(1 + ClockPrescaler) Mhz */
hQspi->Instance = QUADSPI;
hQspi->Init.ClockPrescaler = Config->ClockPrescaler;
hQspi->Init.FifoThreshold = 1;
hQspi->Init.SampleShifting = Config->SampleShifting;
hQspi->Init.FlashSize = Config->FlashSize;
hQspi->Init.ChipSelectHighTime = QSPI_CS_HIGH_TIME_4_CYCLE; /* Min 50ns for nonRead */
hQspi->Init.ClockMode = QSPI_CLOCK_MODE_0;
hQspi->Init.FlashID = QSPI_FLASH_ID_1;
hQspi->Init.DualFlash = Config->DualFlashMode;
return HAL_QSPI_Init(hQspi);
}
#if (USE_HAL_QSPI_REGISTER_CALLBACKS == 1)
/**
* @brief Default BSP QSPI Msp Callbacks
* @param Instance QSPI Instance
* @retval BSP status
*/
int32_t BSP_QSPI_RegisterDefaultMspCallbacks (uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if(Instance >= QSPI_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Register MspInit/MspDeInit Callbacks */
if(HAL_QSPI_RegisterCallback(&hqspi, HAL_QSPI_MSPINIT_CB_ID, QSPI_MspInit) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else if(HAL_QSPI_RegisterCallback(&hqspi, HAL_QSPI_MSPDEINIT_CB_ID, QSPI_MspDeInit) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
QSPI_Ctx[Instance].IsMspCallbacksValid = 1U;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief BSP QSPI Msp Callback registering
* @param Instance QSPI Instance
* @param CallBacks pointer to MspInit/MspDeInit callbacks functions
* @retval BSP status
*/
int32_t BSP_QSPI_RegisterMspCallbacks (uint32_t Instance, BSP_QSPI_Cb_t *CallBacks)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if(Instance >= QSPI_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Register MspInit/MspDeInit Callbacks */
if(HAL_QSPI_RegisterCallback(&hqspi, HAL_QSPI_MSPINIT_CB_ID, CallBacks->pMspInitCb) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else if(HAL_QSPI_RegisterCallback(&hqspi, HAL_QSPI_MSPDEINIT_CB_ID, CallBacks->pMspDeInitCb) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
QSPI_Ctx[Instance].IsMspCallbacksValid = 1U;
}
}
/* Return BSP status */
return ret;
}
#endif /* (USE_HAL_QSPI_REGISTER_CALLBACKS == 1) */
/**
* @brief Reads an amount of data from the QSPI memory.
* @param Instance QSPI instance
* @param pData Pointer to data to be read
* @param ReadAddr Read start address
* @param Size Size of data to read
* @retval BSP status
*/
int32_t BSP_QSPI_Read(uint32_t Instance, uint8_t *pData, uint32_t ReadAddr, uint32_t Size)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if(Instance >= QSPI_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
if(QSPI_Ctx[Instance].TransferRate == BSP_QSPI_STR_TRANSFER)
{
if(MT25TL01G_ReadSTR(&hqspi, QSPI_Ctx[Instance].InterfaceMode, pData, ReadAddr, Size) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
else
{
if(MT25TL01G_ReadDTR(&hqspi, QSPI_Ctx[Instance].InterfaceMode, pData, ReadAddr, Size) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Writes an amount of data to the QSPI memory.
* @param Instance QSPI instance
* @param pData Pointer to data to be written
* @param WriteAddr Write start address
* @param Size Size of data to write
* @retval BSP status
*/
int32_t BSP_QSPI_Write(uint32_t Instance, uint8_t *pData, uint32_t WriteAddr, uint32_t Size)
{
int32_t ret = BSP_ERROR_NONE;
uint32_t end_addr, current_size, current_addr;
uint8_t *write_data;
/* Check if the instance is supported */
if(Instance >= QSPI_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Calculation of the size between the write address and the end of the page */
current_size = MT25TL01G_PAGE_SIZE - (WriteAddr % MT25TL01G_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 address variables */
current_addr = WriteAddr;
end_addr = WriteAddr + Size;
write_data = pData;
/* Perform the write page by page */
do
{
/* Check if Flash busy ? */
if(MT25TL01G_AutoPollingMemReady(&hqspi, QSPI_Ctx[Instance].InterfaceMode) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Enable write operations */
else if(MT25TL01G_WriteEnable(&hqspi, QSPI_Ctx[Instance].InterfaceMode) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Issue page program command */
else if(MT25TL01G_PageProgram(&hqspi, QSPI_Ctx[Instance].InterfaceMode, write_data, current_addr, current_size) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Configure automatic polling mode to wait for end of program */
else if (MT25TL01G_AutoPollingMemReady(&hqspi, QSPI_Ctx[Instance].InterfaceMode) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Update the address and size variables for next page programming */
current_addr += current_size;
write_data += current_size;
current_size = ((current_addr + MT25TL01G_PAGE_SIZE) > end_addr) ? (end_addr - current_addr) : MT25TL01G_PAGE_SIZE;
}
} while ((current_addr < end_addr) && (ret == BSP_ERROR_NONE));
}
/* Return BSP status */
return ret;
}
/**
* @brief Erases the specified block of the QSPI memory.
* MT25TL01G support 4K, 32K, 64K size block erase commands for each Die.
* i.e 8K, 64K, 128K at BSP level (see BSP_QSPI_Erase_t type definition)
* @param Instance QSPI instance
* @param BlockAddress Block address to erase
* @param BlockSize Erase Block size
* @retval BSP status
*/
int32_t BSP_QSPI_EraseBlock(uint32_t Instance, uint32_t BlockAddress, BSP_QSPI_Erase_t BlockSize)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if(Instance >= QSPI_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Check Flash busy ? */
if(MT25TL01G_AutoPollingMemReady(&hqspi, QSPI_Ctx[Instance].InterfaceMode) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Enable write operations */
else if(MT25TL01G_WriteEnable(&hqspi, QSPI_Ctx[Instance].InterfaceMode) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Issue Block Erase command */
if(MT25TL01G_BlockErase(&hqspi, QSPI_Ctx[Instance].InterfaceMode, BlockAddress, (MT25TL01G_Erase_t)BlockSize) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Erases the entire QSPI memory.
* @param Instance QSPI instance
* @retval BSP status
*/
int32_t BSP_QSPI_EraseChip(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if(Instance >= QSPI_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Check Flash busy ? */
if(MT25TL01G_AutoPollingMemReady(&hqspi, QSPI_Ctx[Instance].InterfaceMode) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Enable write operations */
else if (MT25TL01G_WriteEnable(&hqspi, QSPI_Ctx[Instance].InterfaceMode) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Issue Chip erase command */
if(MT25TL01G_ChipErase(&hqspi, QSPI_Ctx[Instance].InterfaceMode) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Reads current status of the QSPI memory.
* If WIP != 0 then return busy.
* @param Instance QSPI instance
* @retval QSPI memory status: whether busy or not
*/
int32_t BSP_QSPI_GetStatus(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
uint8_t reg;
/* Check if the instance is supported */
if(Instance >= QSPI_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
if(MT25TL01G_ReadStatusRegister(&hqspi, QSPI_Ctx[Instance].InterfaceMode, ®) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Check the value of the register */
if ((reg & MT25TL01G_SR_WIP) != 0U)
{
ret = BSP_ERROR_BUSY;
}
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Return the configuration of the QSPI memory.
* @param Instance QSPI instance
* @param pInfo pointer on the configuration structure
* @retval BSP status
*/
int32_t BSP_QSPI_GetInfo(uint32_t Instance, BSP_QSPI_Info_t *pInfo)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if(Instance >= QSPI_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
(void)MT25TL01G_GetFlashInfo(pInfo);
}
/* Return BSP status */
return ret;
}
/**
* @brief Configure the QSPI in memory-mapped mode
* Only 1 Instance can running MMP mode. And it will lock system at this mode.
* @param Instance QSPI instance
* @retval BSP status
*/
int32_t BSP_QSPI_EnableMemoryMappedMode(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if(Instance >= QSPI_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
if(QSPI_Ctx[Instance].TransferRate == BSP_QSPI_STR_TRANSFER)
{
if(MT25TL01G_EnableMemoryMappedModeSTR(&hqspi, QSPI_Ctx[Instance].InterfaceMode) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else /* Update QSPI context if all operations are well done */
{
QSPI_Ctx[Instance].IsInitialized = QSPI_ACCESS_MMP;
}
}
else
{
if(MT25TL01G_EnableMemoryMappedModeDTR(&hqspi, QSPI_Ctx[Instance].InterfaceMode) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else /* Update QSPI context if all operations are well done */
{
QSPI_Ctx[Instance].IsInitialized = QSPI_ACCESS_MMP;
}
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Exit form memory-mapped mode
* Only 1 Instance can running MMP mode. And it will lock system at this mode.
* @param Instance QSPI instance
* @retval BSP status
*/
int32_t BSP_QSPI_DisableMemoryMappedMode(uint32_t Instance)
{
uint8_t Dummy;
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if(Instance >= QSPI_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
if(QSPI_Ctx[Instance].IsInitialized != QSPI_ACCESS_MMP)
{
ret = BSP_ERROR_QSPI_MMP_UNLOCK_FAILURE;
}/* Abort MMP back to indirect mode */
else if(HAL_QSPI_Abort(&hqspi) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
/* Force QSPI interface Sampling Shift to half cycle */
hqspi.Init.SampleShifting = QSPI_SAMPLE_SHIFTING_HALFCYCLE;
if(HAL_QSPI_Init(&hqspi)!= HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
/* Dummy read for exit from Performance Enhance mode */
else if(MT25TL01G_ReadSTR(&hqspi, QSPI_Ctx[Instance].InterfaceMode, &Dummy, 0, 1) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else /* Update QSPI context if all operations are well done */
{
QSPI_Ctx[Instance].IsInitialized = QSPI_ACCESS_INDIRECT;
}
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Get flash ID, 3 Byte
* Manufacturer ID, Memory type, Memory density
* @param Instance QSPI instance
* @param Id QSPI Identifier
* @retval BSP status
*/
int32_t BSP_QSPI_ReadID(uint32_t Instance, uint8_t *Id)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if(Instance >= QSPI_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
if(MT25TL01G_ReadID(&hqspi, QSPI_Ctx[Instance].InterfaceMode, Id) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Set Flash to desired Interface mode. And this instance becomes current instance.
* If current instance running at MMP mode then this function isn't work.
* Indirect -> Indirect
* @param Instance QSPI instance
* @param Mode QSPI mode
* @param Rate QSPI transfer rate
* @retval BSP status
*/
int32_t BSP_QSPI_ConfigFlash(uint32_t Instance, BSP_QSPI_Interface_t Mode, BSP_QSPI_Transfer_t Rate)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if(Instance >= QSPI_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Check if MMP mode locked ************************************************/
if(QSPI_Ctx[Instance].IsInitialized == QSPI_ACCESS_MMP)
{
ret = BSP_ERROR_QSPI_MMP_LOCK_FAILURE;
}
else
{
/* Setup MCU transfer rate setting ***************************************************/
hqspi.Init.SampleShifting = (Rate == BSP_QSPI_STR_TRANSFER) ? QSPI_SAMPLE_SHIFTING_HALFCYCLE : QSPI_SAMPLE_SHIFTING_NONE;
if(HAL_QSPI_Init(&hqspi)!= HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
/* Setup Flash interface ***************************************************/
switch(QSPI_Ctx[Instance].InterfaceMode)
{
case MT25TL01G_QPI_MODE : /* 4-4-4 commands */
if(Mode != MT25TL01G_QPI_MODE)
{
if(MT25TL01G_ExitQPIMode(&hqspi) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
break;
case BSP_QSPI_SPI_MODE : /* 1-1-1 commands, Power on H/W default setting */
case BSP_QSPI_SPI_2IO_MODE : /* 1-2-2 read commands */
case BSP_QSPI_SPI_4IO_MODE : /* 1-4-4 read commands */
default :
if(Mode == MT25TL01G_QPI_MODE)
{
if(MT25TL01G_EnterQPIMode(&hqspi) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
break;
}
/* Update QSPI context if all operations are well done */
if(ret == BSP_ERROR_NONE)
{
/* Update current status parameter *****************************************/
QSPI_Ctx[Instance].IsInitialized = QSPI_ACCESS_INDIRECT;
QSPI_Ctx[Instance].InterfaceMode = Mode;
QSPI_Ctx[Instance].TransferRate = Rate;
}
}
}
}
/* Return BSP status */
return ret;
}
/**
* @}
*/
/** @defgroup STM32H747I_DISCO_QSPI_Private_Functions Private Functions
* @{
*/
/**
* @brief QSPI MSP Initialization
* @param hQspi : QSPI handle
* This function configures the hardware resources used in this example:
* - Peripheral's clock enable
* - Peripheral's GPIO Configuration
* - NVIC configuration for QSPI interrupt
* @retval None
*/
static void QSPI_MspInit(QSPI_HandleTypeDef *hQspi)
{
GPIO_InitTypeDef gpio_init_structure;
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* Enable the QuadSPI memory interface clock */
QSPI_CLK_ENABLE();
/* Reset the QuadSPI memory interface */
QSPI_FORCE_RESET();
QSPI_RELEASE_RESET();
/* Enable GPIO clocks */
QSPI_CLK_GPIO_CLK_ENABLE();
QSPI_BK1_CS_GPIO_CLK_ENABLE();
QSPI_BK1_D0_GPIO_CLK_ENABLE();
QSPI_BK1_D1_GPIO_CLK_ENABLE();
QSPI_BK1_D2_GPIO_CLK_ENABLE();
QSPI_BK1_D3_GPIO_CLK_ENABLE();
QSPI_BK2_CS_GPIO_CLK_ENABLE();
QSPI_BK2_D0_GPIO_CLK_ENABLE();
QSPI_BK2_D1_GPIO_CLK_ENABLE();
QSPI_BK2_D2_GPIO_CLK_ENABLE();
QSPI_BK2_D3_GPIO_CLK_ENABLE();
/*##-2- Configure peripheral GPIO ##########################################*/
/* QSPI CLK GPIO pin configuration */
gpio_init_structure.Pin = QSPI_CLK_PIN;
gpio_init_structure.Mode = GPIO_MODE_AF_PP;
gpio_init_structure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
gpio_init_structure.Pull = GPIO_NOPULL;
gpio_init_structure.Alternate = GPIO_AF9_QUADSPI;
HAL_GPIO_Init(QSPI_CLK_GPIO_PORT, &gpio_init_structure);
/* QSPI CS GPIO pin configuration */
gpio_init_structure.Pin = QSPI_BK1_CS_PIN;
gpio_init_structure.Pull = GPIO_PULLUP;
gpio_init_structure.Alternate = GPIO_AF10_QUADSPI;
HAL_GPIO_Init(QSPI_BK1_CS_GPIO_PORT, &gpio_init_structure);
/* QSPI D0 GPIO pin configuration */
gpio_init_structure.Pin = QSPI_BK1_D0_PIN;
gpio_init_structure.Pull = GPIO_NOPULL;
gpio_init_structure.Alternate = GPIO_AF9_QUADSPI;
HAL_GPIO_Init(QSPI_BK1_D0_GPIO_PORT, &gpio_init_structure);
gpio_init_structure.Pin = QSPI_BK2_D0_PIN;
gpio_init_structure.Alternate = GPIO_AF9_QUADSPI;
HAL_GPIO_Init(QSPI_BK2_D0_GPIO_PORT, &gpio_init_structure);
/* QSPI D1 GPIO pin configuration */
gpio_init_structure.Pin = QSPI_BK1_D1_PIN;
gpio_init_structure.Alternate = GPIO_AF10_QUADSPI;
HAL_GPIO_Init(QSPI_BK1_D1_GPIO_PORT, &gpio_init_structure);
gpio_init_structure.Pin = QSPI_BK2_D1_PIN;
gpio_init_structure.Alternate = GPIO_AF9_QUADSPI;
HAL_GPIO_Init(QSPI_BK2_D1_GPIO_PORT, &gpio_init_structure);
/* QSPI D2 GPIO pin configuration */
gpio_init_structure.Pin = QSPI_BK1_D2_PIN;
gpio_init_structure.Alternate = GPIO_AF9_QUADSPI;
HAL_GPIO_Init(QSPI_BK1_D2_GPIO_PORT, &gpio_init_structure);
gpio_init_structure.Pin = QSPI_BK2_D2_PIN;
HAL_GPIO_Init(QSPI_BK2_D2_GPIO_PORT, &gpio_init_structure);
/* QSPI D3 GPIO pin configuration */
gpio_init_structure.Pin = QSPI_BK1_D3_PIN;
HAL_GPIO_Init(QSPI_BK1_D3_GPIO_PORT, &gpio_init_structure);
gpio_init_structure.Pin = QSPI_BK2_D3_PIN;
HAL_GPIO_Init(QSPI_BK2_D3_GPIO_PORT, &gpio_init_structure);
/*##-3- Configure the NVIC for QSPI #########################################*/
/* NVIC configuration for QSPI interrupt */
HAL_NVIC_SetPriority(QUADSPI_IRQn, 0x0F, 0);
HAL_NVIC_EnableIRQ(QUADSPI_IRQn);
}
/**
* @brief QSPI MSP De-Initialization
* @param hQspi QSPI handle
* This function frees the hardware resources used in this example:
* - Disable the Peripheral's clock
* - Revert GPIO and NVIC configuration to their default state
* @retval None
*/
static void QSPI_MspDeInit(QSPI_HandleTypeDef *hQspi)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(hQspi);
/*##-2- Disable peripherals and GPIO Clocks ################################*/
/* De-Configure QSPI pins */
HAL_GPIO_DeInit(QSPI_CLK_GPIO_PORT, QSPI_CLK_PIN);
HAL_GPIO_DeInit(QSPI_BK1_CS_GPIO_PORT, QSPI_BK1_CS_PIN);
HAL_GPIO_DeInit(QSPI_BK1_D0_GPIO_PORT, QSPI_BK1_D0_PIN);
HAL_GPIO_DeInit(QSPI_BK1_D1_GPIO_PORT, QSPI_BK1_D1_PIN);
HAL_GPIO_DeInit(QSPI_BK1_D2_GPIO_PORT, QSPI_BK1_D2_PIN);
HAL_GPIO_DeInit(QSPI_BK1_D3_GPIO_PORT, QSPI_BK1_D3_PIN);
HAL_GPIO_DeInit(QSPI_BK2_CS_GPIO_PORT, QSPI_BK2_CS_PIN);
HAL_GPIO_DeInit(QSPI_BK2_D0_GPIO_PORT, QSPI_BK2_D0_PIN);
HAL_GPIO_DeInit(QSPI_BK2_D1_GPIO_PORT, QSPI_BK2_D1_PIN);
HAL_GPIO_DeInit(QSPI_BK2_D2_GPIO_PORT, QSPI_BK2_D2_PIN);
HAL_GPIO_DeInit(QSPI_BK2_D3_GPIO_PORT, QSPI_BK2_D3_PIN);
/*##-3- Reset peripherals ##################################################*/
/* Reset the QuadSPI memory interface */
QSPI_FORCE_RESET();
QSPI_RELEASE_RESET();
/* Disable the QuadSPI memory interface clock */
QSPI_CLK_DISABLE();
}
/**
* @brief This function reset the QSPI Flash memory.
* Fore QPI+SPI reset to avoid system come from unknown status.
* Flash accept 1-1-1, 1-1-2, 1-2-2 commands after reset.
* @param Instance QSPI instance
* @retval BSP status
*/
static int32_t QSPI_ResetMemory(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
/* Send RESET ENABLE command in QPI mode (QUAD I/Os, 4-4-4) */
if(MT25TL01G_ResetEnable(&hqspi, MT25TL01G_QPI_MODE) != MT25TL01G_OK)
{
ret =BSP_ERROR_COMPONENT_FAILURE;
}/* Send RESET memory command in QPI mode (QUAD I/Os, 4-4-4) */
else if(MT25TL01G_ResetMemory(&hqspi, MT25TL01G_QPI_MODE) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Wait Flash ready */
else if(MT25TL01G_AutoPollingMemReady(&hqspi, QSPI_Ctx[Instance].InterfaceMode) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Send RESET ENABLE command in SPI mode (1-1-1) */
else if(MT25TL01G_ResetEnable(&hqspi, BSP_QSPI_SPI_MODE) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Send RESET memory command in SPI mode (1-1-1) */
else if(MT25TL01G_ResetMemory(&hqspi, BSP_QSPI_SPI_MODE) != MT25TL01G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
QSPI_Ctx[Instance].IsInitialized = QSPI_ACCESS_INDIRECT; /* After reset S/W setting to indirect access */
QSPI_Ctx[Instance].InterfaceMode = BSP_QSPI_SPI_MODE; /* After reset H/W back to SPI mode by default */
QSPI_Ctx[Instance].TransferRate = BSP_QSPI_STR_TRANSFER; /* After reset S/W setting to STR mode */
}
/* Return BSP status */
return ret;
}
/**
* @brief This function configure the dummy cycles on memory side.
* Dummy cycle bit locate in Configuration Register[7:6]
* @param Instance QSPI instance
* @retval BSP status
*/
static int32_t QSPI_DummyCyclesCfg(uint32_t Instance)
{
int32_t ret= BSP_ERROR_NONE;
QSPI_CommandTypeDef s_command;
uint16_t reg=0;
/* Initialize the read volatile configuration register command */
s_command.InstructionMode = QSPI_INSTRUCTION_4_LINES;
s_command.Instruction = MT25TL01G_READ_VOL_CFG_REG_CMD;
s_command.AddressMode = QSPI_ADDRESS_NONE;
s_command.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;