-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstm32n6xx_nucleo_xspi.c
1471 lines (1327 loc) · 44.8 KB
/
stm32n6xx_nucleo_xspi.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 stm32n6xx_nucleo.c
* @author MCD Application Team
* @brief This file includes a standard driver for the XSPI
* memories mounted on STM32N6XX_NUCLEO board.
@verbatim
==============================================================================
##### How to use this driver #####
==============================================================================
[..]
(#) This driver is used to drive the MX25UM51245G Octal NOR
external memories mounted on STM32N6XX_NUCLEO board.
(#) This driver need specific component driver (MX25UM51245G) to be included with.
(#) MX25UM51245G Initialization steps:
(++) Initialize the OPSI external memory using the BSP_XSPI_NOR_Init() function. This
function includes the MSP layer hardware resources initialization and the
XSPI interface with the external memory.
(#) MX25UM51245G Octal NOR memory operations
(++) XSPI 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_XSPI_NOR_Read()/BSP_XSPI_NOR_Write().
(++) The function BSP_XSPI_NOR_GetInfo() returns the configuration of the XSPI memory.
(see the XSPI memory data sheet)
(++) Perform erase block operation using the function BSP_XSPI_NOR_Erase_Block() and by
specifying the block address. You can perform an erase operation of the whole
chip by calling the function BSP_XSPI_NOR_Erase_Chip().
(++) The function BSP_XSPI_NOR_GetStatus() returns the current status of the XSPI memory.
(see the XSPI memory data sheet)
(++) The memory access can be configured in memory-mapped mode with the call of
function BSP_XSPI_NOR_EnableMemoryMapped(). To go back in indirect mode, the
function BSP_XSPI_NOR_DisableMemoryMapped() should be used.
(++) The erase operation can be suspend and resume with using functions
BSP_XSPI_NOR_SuspendErase() and BSP_XSPI_NOR_ResumeErase()
(++) It is possible to put the memory in deep power-down mode to reduce its consumption.
For this, the function BSP_XSPI_NOR_EnterDeepPowerDown() should be called. To leave
the deep power-down mode, the function BSP_XSPI_NOR_LeaveDeepPowerDown() should be called.
(++) The function BSP_XSPI_NOR_ReadID() returns the identifier of the memory
(see the XSPI memory data sheet)
(++) The configuration of the interface between peripheral and memory is done by
the function BSP_XSPI_NOR_ConfigFlash(), three modes are possible :
- SPI : instruction, address and data on one line
- STR OPI : instruction, address and data on eight lines with sampling on one edge of clock
- DTR OPI : instruction, address and data on eight lines with sampling on both edgaes of clock
@endverbatim
******************************************************************************
* @attention
*
* Copyright (c) 2023 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 "stm32n6xx_nucleo_xspi.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup STM32N6XX_NUCLEO
* @{
*/
/** @defgroup STM32N6XX_NUCLEO_XSPI XSPI
* @{
*/
/* Exported variables --------------------------------------------------------*/
/** @addtogroup STM32N6XX_NUCLEO_XSPI_NOR_Exported_Variables
* @{
*/
XSPI_HandleTypeDef hxspi_nor[XSPI_NOR_INSTANCES_NUMBER] = {0};
XSPI_NOR_Ctx_t Xspi_Nor_Ctx[XSPI_NOR_INSTANCES_NUMBER] = {{
XSPI_ACCESS_NONE,
MX25UM51245G_SPI_MODE,
MX25UM51245G_STR_TRANSFER
}
};
/**
* @}
*/
/* Private constants --------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/** @defgroup STM32N6XX_NUCLEO_XSPI_NOR_Private_Variables XSPI_NOR Private Variables
* @{
*/
#if (USE_HAL_XSPI_REGISTER_CALLBACKS == 1)
static uint32_t XspiNor_IsMspCbValid[XSPI_NOR_INSTANCES_NUMBER] = {0};
#endif /* USE_HAL_XSPI_REGISTER_CALLBACKS */
/**
* @}
*/
/* Private functions ---------------------------------------------------------*/
/** @defgroup STM32N6XX_NUCLEO_XSPI_NOR_Private_Functions XSPI_NOR Private Functions
* @{
*/
static void XSPI_NOR_MspInit(XSPI_HandleTypeDef *hxspi);
static void XSPI_NOR_MspDeInit(XSPI_HandleTypeDef *hxspi);
static int32_t XSPI_NOR_ResetMemory(uint32_t Instance);
static int32_t XSPI_NOR_EnterDOPIMode(uint32_t Instance);
static int32_t XSPI_NOR_EnterSOPIMode(uint32_t Instance);
static int32_t XSPI_NOR_ExitOPIMode(uint32_t Instance);
/**
* @}
*/
/* Exported functions ---------------------------------------------------------*/
/** @addtogroup STM32N6XX_NUCLEO_XSPI_NOR_Exported_Functions
* @{
*/
/**
* @brief Initializes the XSPI interface.
* @param Instance XSPI Instance
* @param Init XSPI Init structure
* @retval BSP status
*/
int32_t BSP_XSPI_NOR_Init(uint32_t Instance, BSP_XSPI_NOR_Init_t *Init)
{
int32_t ret;
BSP_XSPI_NOR_Info_t pInfo;
MX_XSPI_InitTypeDef xspi_init;
/* Check if the instance is supported */
if (Instance >= XSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Check if the instance is already initialized */
if (Xspi_Nor_Ctx[Instance].IsInitialized == XSPI_ACCESS_NONE)
{
#if (USE_HAL_XSPI_REGISTER_CALLBACKS == 0)
/* Msp XSPI initialization */
XSPI_NOR_MspInit(&hxspi_nor[Instance]);
#else
/* Register the XSPI MSP Callbacks */
if (XspiNor_IsMspCbValid[Instance] == 0UL)
{
if (BSP_XSPI_NOR_RegisterDefaultMspCallbacks(Instance) != BSP_ERROR_NONE)
{
return BSP_ERROR_PERIPH_FAILURE;
}
}
#endif /* USE_HAL_XSPI_REGISTER_CALLBACKS */
/* Get Flash information of one memory */
(void)MX25UM51245G_GetFlashInfo(&pInfo);
/* Fill config structure */
xspi_init.ClockPrescaler = 3;
xspi_init.MemorySize = (uint32_t)POSITION_VAL((uint32_t)pInfo.FlashSize);
xspi_init.SampleShifting = HAL_XSPI_SAMPLE_SHIFT_NONE;
xspi_init.TransferRate = (uint32_t) Init->TransferRate;
/* STM32 XSPI interface initialization */
if (MX_XSPI_NOR_Init(&hxspi_nor[Instance], &xspi_init) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
/* XSPI memory reset */
else if (XSPI_NOR_ResetMemory(Instance) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
/* Check if memory is ready */
else if (MX25UM51245G_AutoPollingMemReady(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
Xspi_Nor_Ctx[Instance].TransferRate) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
/* Configure the memory */
else if (BSP_XSPI_NOR_ConfigFlash(Instance, Init->InterfaceMode, Init->TransferRate) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
}
else
{
ret = BSP_ERROR_NONE;
}
}
HAL_XSPI_SetClockPrescaler(&hxspi_nor[Instance], 0);
/* Return BSP status */
return ret;
}
/**
* @brief De-Initializes the XSPI interface.
* @param Instance XSPI Instance
* @retval BSP status
*/
int32_t BSP_XSPI_NOR_DeInit(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if (Instance >= XSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Check if the instance is already initialized */
if (Xspi_Nor_Ctx[Instance].IsInitialized != XSPI_ACCESS_NONE)
{
/* Disable Memory mapped mode */
if (Xspi_Nor_Ctx[Instance].IsInitialized == XSPI_ACCESS_MMP)
{
if (BSP_XSPI_NOR_DisableMemoryMappedMode(Instance) != BSP_ERROR_NONE)
{
return BSP_ERROR_COMPONENT_FAILURE;
}
}
/* Set default Xspi_Nor_Ctx values */
Xspi_Nor_Ctx[Instance].IsInitialized = XSPI_ACCESS_NONE;
Xspi_Nor_Ctx[Instance].InterfaceMode = BSP_XSPI_NOR_SPI_MODE;
Xspi_Nor_Ctx[Instance].TransferRate = BSP_XSPI_NOR_STR_TRANSFER;
/* Call the DeInit function to reset the driver */
if (HAL_XSPI_DeInit(&hxspi_nor[Instance]) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
#if (USE_HAL_XSPI_REGISTER_CALLBACKS == 0)
XSPI_NOR_MspDeInit(&hxspi_nor[Instance]);
#endif /* (USE_HAL_XSPI_REGISTER_CALLBACKS == 0) */
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Initializes the XSPI interface.
* @param hxspi XSPI handle
* @param Init XSPI config structure
* @retval BSP status
*/
__weak HAL_StatusTypeDef MX_XSPI_NOR_Init(XSPI_HandleTypeDef *hxspi, MX_XSPI_InitTypeDef *Init)
{
/* XSPI initialization */
hxspi->Instance = XSPI2;
hxspi->Init.FifoThresholdByte = 4;
hxspi->Init.MemoryMode = HAL_XSPI_SINGLE_MEM;
hxspi->Init.MemorySize = Init->MemorySize; /* 512 MBits */
hxspi->Init.ChipSelectHighTimeCycle = 2;
hxspi->Init.FreeRunningClock = HAL_XSPI_FREERUNCLK_DISABLE;
hxspi->Init.ClockMode = HAL_XSPI_CLOCK_MODE_0;
hxspi->Init.WrapSize = HAL_XSPI_WRAP_NOT_SUPPORTED;
hxspi->Init.ClockPrescaler = Init->ClockPrescaler;
hxspi->Init.SampleShifting = Init->SampleShifting;
hxspi->Init.ChipSelectBoundary = 0;
#if defined (OCTOSPI_DCR1_DLYBYP)
hxspi->Init.DelayBlockBypass = HAL_XSPI_DELAY_BLOCK_BYPASS;
#endif /* OCTOSPI_DCR1_DLYBYP */
if (Init->TransferRate == (uint32_t) BSP_XSPI_NOR_DTR_TRANSFER)
{
hxspi->Init.MemoryType = HAL_XSPI_MEMTYPE_MACRONIX;
hxspi->Init.DelayHoldQuarterCycle = HAL_XSPI_DHQC_ENABLE;
}
else
{
hxspi->Init.MemoryType = HAL_XSPI_MEMTYPE_MICRON;
hxspi->Init.DelayHoldQuarterCycle = HAL_XSPI_DHQC_DISABLE;
}
return HAL_XSPI_Init(hxspi);
}
#if (USE_HAL_XSPI_REGISTER_CALLBACKS == 1)
/**
* @brief Default BSP XSPI Msp Callbacks
* @param Instance XSPI Instance
* @retval BSP status
*/
int32_t BSP_XSPI_NOR_RegisterDefaultMspCallbacks(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if (Instance >= XSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Register MspInit/MspDeInit Callbacks */
if (HAL_XSPI_RegisterCallback(&hxspi_nor[Instance], HAL_XSPI_MSP_INIT_CB_ID, XSPI_NOR_MspInit) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else if (HAL_XSPI_RegisterCallback(&hxspi_nor[Instance], HAL_XSPI_MSP_DEINIT_CB_ID, XSPI_NOR_MspDeInit) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
XspiNor_IsMspCbValid[Instance] = 1U;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief BSP XSPI Msp Callback registering
* @param Instance XSPI Instance
* @param CallBacks pointer to MspInit/MspDeInit callbacks functions
* @retval BSP status
*/
int32_t BSP_XSPI_NOR_RegisterMspCallbacks(uint32_t Instance, BSP_XSPI_Cb_t *CallBacks)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if (Instance >= XSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Register MspInit/MspDeInit Callbacks */
if (HAL_XSPI_RegisterCallback(&hxspi_nor[Instance], HAL_XSPI_MSP_INIT_CB_ID, CallBacks->pMspInitCb) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else if (HAL_XSPI_RegisterCallback(&hxspi_nor[Instance], HAL_XSPI_MSP_DEINIT_CB_ID,
CallBacks->pMspDeInitCb) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
XspiNor_IsMspCbValid[Instance] = 1U;
}
}
/* Return BSP status */
return ret;
}
#endif /* (USE_HAL_XSPI_REGISTER_CALLBACKS == 1) */
/**
* @brief Reads an amount of data from the XSPI memory.
* @param Instance XSPI 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_XSPI_NOR_Read(uint32_t Instance, uint8_t *pData, uint32_t ReadAddr, uint32_t Size)
{
int32_t ret;
/* Check if the instance is supported */
if (Instance >= XSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
if (Xspi_Nor_Ctx[Instance].TransferRate == BSP_XSPI_NOR_STR_TRANSFER)
{
if (MX25UM51245G_ReadSTR(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
MX25UM51245G_4BYTES_SIZE, pData, ReadAddr, Size) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
}
else
{
if (MX25UM51245G_ReadDTR(&hxspi_nor[Instance], pData, ReadAddr, Size) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Writes an amount of data to the XSPI memory.
* @param Instance XSPI 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_XSPI_NOR_Write(uint32_t Instance, uint8_t *pData, uint32_t WriteAddr, uint32_t Size)
{
int32_t ret = BSP_ERROR_NONE;
uint32_t end_addr;
uint32_t current_size;
uint32_t current_addr;
uint32_t data_addr;
/* Check if the instance is supported */
if (Instance >= XSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Calculation of the size between the write address and the end of the page */
current_size = MX25UM51245G_PAGE_SIZE - (WriteAddr % MX25UM51245G_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;
data_addr = (uint32_t)pData;
/* Perform the write page by page */
do
{
/* Check if Flash busy ? */
if (MX25UM51245G_AutoPollingMemReady(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
Xspi_Nor_Ctx[Instance].TransferRate) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Enable write operations */
else if (MX25UM51245G_WriteEnable(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
Xspi_Nor_Ctx[Instance].TransferRate) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
if (Xspi_Nor_Ctx[Instance].TransferRate == BSP_XSPI_NOR_STR_TRANSFER)
{
/* Issue page program command */
if (MX25UM51245G_PageProgram(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
MX25UM51245G_4BYTES_SIZE, (uint8_t *)data_addr, current_addr,
current_size) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
else
{
/* Issue page program command */
if (MX25UM51245G_PageProgramDTR(&hxspi_nor[Instance], (uint8_t *)data_addr, current_addr,
current_size) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
if (ret == BSP_ERROR_NONE)
{
/* Configure automatic polling mode to wait for end of program */
if (MX25UM51245G_AutoPollingMemReady(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
Xspi_Nor_Ctx[Instance].TransferRate) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
/* Update the address and size variables for next page programming */
current_addr += current_size;
data_addr += current_size;
current_size = ((current_addr + MX25UM51245G_PAGE_SIZE) > end_addr) ? (end_addr - current_addr) : \
MX25UM51245G_PAGE_SIZE;
}
}
}
} while ((current_addr < end_addr) && (ret == BSP_ERROR_NONE));
}
/* Return BSP status */
return ret;
}
/**
* @brief Erases the specified block of the XSPI memory.
* @param Instance XSPI instance
* @param BlockAddress Block address to erase
* @param BlockSize Erase Block size
* @retval BSP status
*/
int32_t BSP_XSPI_NOR_Erase_Block(uint32_t Instance, uint32_t BlockAddress, BSP_XSPI_NOR_Erase_t BlockSize)
{
int32_t ret;
/* Check if the instance is supported */
if (Instance >= XSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Check Flash busy ? */
if (MX25UM51245G_AutoPollingMemReady(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
Xspi_Nor_Ctx[Instance].TransferRate) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Enable write operations */
else if (MX25UM51245G_WriteEnable(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
Xspi_Nor_Ctx[Instance].TransferRate) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Issue Block Erase command */
else if (MX25UM51245G_BlockErase(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
Xspi_Nor_Ctx[Instance].TransferRate, MX25UM51245G_4BYTES_SIZE,
BlockAddress, BlockSize) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Erases the entire XSPI memory.
* @param Instance XSPI instance
* @retval BSP status
*/
int32_t BSP_XSPI_NOR_Erase_Chip(uint32_t Instance)
{
int32_t ret;
/* Check if the instance is supported */
if (Instance >= XSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Check Flash busy ? */
if (MX25UM51245G_AutoPollingMemReady(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
Xspi_Nor_Ctx[Instance].TransferRate) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Enable write operations */
else if (MX25UM51245G_WriteEnable(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
Xspi_Nor_Ctx[Instance].TransferRate) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Issue Chip erase command */
else if (MX25UM51245G_ChipErase(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
Xspi_Nor_Ctx[Instance].TransferRate) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Reads current status of the XSPI memory.
* @param Instance QSPI instance
* @retval XSPI memory status: whether busy or not
*/
int32_t BSP_XSPI_NOR_GetStatus(uint32_t Instance)
{
static uint8_t reg[2];
int32_t ret;
/* Check if the instance is supported */
if (Instance >= XSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
if (MX25UM51245G_ReadSecurityRegister(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
Xspi_Nor_Ctx[Instance].TransferRate, reg) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Check the value of the register */
else if ((reg[0] & (MX25UM51245G_SECR_P_FAIL | MX25UM51245G_SECR_E_FAIL)) != 0U)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else if ((reg[0] & (MX25UM51245G_SECR_PSB | MX25UM51245G_SECR_ESB)) != 0U)
{
ret = BSP_ERROR_XSPI_SUSPENDED;
}
else if (MX25UM51245G_ReadStatusRegister(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
Xspi_Nor_Ctx[Instance].TransferRate, reg) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Check the value of the register */
else if ((reg[0] & MX25UM51245G_SR_WIP) != 0U)
{
ret = BSP_ERROR_BUSY;
}
else
{
ret = BSP_ERROR_NONE;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Return the configuration of the XSPI memory.
* @param Instance XSPI instance
* @param pInfo pointer on the configuration structure
* @retval BSP status
*/
int32_t BSP_XSPI_NOR_GetInfo(uint32_t Instance, BSP_XSPI_NOR_Info_t *pInfo)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if (Instance >= XSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
(void)MX25UM51245G_GetFlashInfo(pInfo);
}
/* Return BSP status */
return ret;
}
/**
* @brief Configure the XSPI in memory-mapped mode
* @param Instance XSPI instance
* @retval BSP status
*/
int32_t BSP_XSPI_NOR_EnableMemoryMappedMode(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if (Instance >= XSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
if (Xspi_Nor_Ctx[Instance].TransferRate == BSP_XSPI_NOR_STR_TRANSFER)
{
if (MX25UM51245G_EnableMemoryMappedModeSTR(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
MX25UM51245G_4BYTES_SIZE) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else /* Update XSPI context if all operations are well done */
{
Xspi_Nor_Ctx[Instance].IsInitialized = XSPI_ACCESS_MMP;
}
}
else
{
if (MX25UM51245G_EnableMemoryMappedModeDTR(&hxspi_nor[Instance],
Xspi_Nor_Ctx[Instance].InterfaceMode) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else /* Update XSPI context if all operations are well done */
{
Xspi_Nor_Ctx[Instance].IsInitialized = XSPI_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 XSPI instance
* @retval BSP status
*/
int32_t BSP_XSPI_NOR_DisableMemoryMappedMode(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
uint32_t tickstart = HAL_GetTick();
/* Check if the instance is supported */
if (Instance >= XSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
if (Xspi_Nor_Ctx[Instance].IsInitialized != XSPI_ACCESS_MMP)
{
ret = BSP_ERROR_XSPI_MMP_UNLOCK_FAILURE;
}/* Abort MMP back to indirect mode */
else if (HAL_XSPI_Abort(&hxspi_nor[Instance]) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
/* Wait until flag is in expected state */
while ((HAL_XSPI_GET_FLAG(&hxspi_nor[Instance], HAL_XSPI_FLAG_BUSY)) != RESET)
{
/* Check for the Timeout */
if (((HAL_GetTick() - tickstart) > hxspi_nor[Instance].Timeout) || (hxspi_nor[Instance].Timeout == 0U))
{
ret = BSP_ERROR_PERIPH_FAILURE;
break;
}
}
if (ret == BSP_ERROR_NONE)
{
/* Configure CR register with functional mode as indirect mode*/
MODIFY_REG(hxspi_nor[Instance].Instance->CR, (XSPI_CR_FMODE), 0U);
Xspi_Nor_Ctx[Instance].IsInitialized = XSPI_ACCESS_INDIRECT;
}
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Get flash ID 3 Bytes:
* Manufacturer ID, Memory type, Memory density
* @param Instance XSPI instance
* @param Id Pointer to flash ID bytes
* @retval BSP status
*/
int32_t BSP_XSPI_NOR_ReadID(uint32_t Instance, uint8_t *Id)
{
int32_t ret;
/* Check if the instance is supported */
if (Instance >= XSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else if (MX25UM51245G_ReadID(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
Xspi_Nor_Ctx[Instance].TransferRate, Id) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
/* 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 doesn't work.
* Indirect -> Indirect
* @param Instance XSPI instance
* @param Mode XSPI mode
* @param Rate XSPI transfer rate
* @retval BSP status
*/
int32_t BSP_XSPI_NOR_ConfigFlash(uint32_t Instance, BSP_XSPI_NOR_Interface_t Mode, BSP_XSPI_NOR_Transfer_t Rate)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if (Instance >= XSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Check if MMP mode locked ************************************************/
if (Xspi_Nor_Ctx[Instance].IsInitialized == XSPI_ACCESS_MMP)
{
ret = BSP_ERROR_XSPI_MMP_LOCK_FAILURE;
}
else
{
/* Setup Flash interface ***************************************************/
switch (Xspi_Nor_Ctx[Instance].InterfaceMode)
{
case BSP_XSPI_NOR_OPI_MODE : /* 8-8-8 commands */
if ((Mode != BSP_XSPI_NOR_OPI_MODE) || (Rate != Xspi_Nor_Ctx[Instance].TransferRate))
{
/* Exit OPI mode */
ret = XSPI_NOR_ExitOPIMode(Instance);
if ((ret == BSP_ERROR_NONE) && (Mode == BSP_XSPI_NOR_OPI_MODE))
{
if (Xspi_Nor_Ctx[Instance].TransferRate == BSP_XSPI_NOR_STR_TRANSFER)
{
/* Enter DTR OPI mode */
ret = XSPI_NOR_EnterDOPIMode(Instance);
}
else
{
/* Enter STR OPI mode */
ret = XSPI_NOR_EnterSOPIMode(Instance);
}
}
}
break;
case BSP_XSPI_NOR_SPI_MODE : /* 1-1-1 commands, Power on H/W default setting */
default :
if (Mode == BSP_XSPI_NOR_OPI_MODE)
{
if (Rate == BSP_XSPI_NOR_STR_TRANSFER)
{
/* Enter STR OPI mode */
ret = XSPI_NOR_EnterSOPIMode(Instance);
}
else
{
/* Enter DTR OPI mode */
ret = XSPI_NOR_EnterDOPIMode(Instance);
}
}
break;
}
/* Update XSPI context if all operations are well done */
if (ret == BSP_ERROR_NONE)
{
/* Update current status parameter *****************************************/
Xspi_Nor_Ctx[Instance].IsInitialized = XSPI_ACCESS_INDIRECT;
Xspi_Nor_Ctx[Instance].InterfaceMode = Mode;
Xspi_Nor_Ctx[Instance].TransferRate = Rate;
}
}
}
/* Return BSP status */
return ret;
}
/**
* @brief This function suspends an ongoing erase command.
* @param Instance QSPI instance
* @retval BSP status
*/
int32_t BSP_XSPI_NOR_SuspendErase(uint32_t Instance)
{
int32_t ret;
/* Check if the instance is supported */
if (Instance >= XSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
/* Check whether the device is busy (erase operation is in progress). */
else if (BSP_XSPI_NOR_GetStatus(Instance) != BSP_ERROR_BUSY)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else if (MX25UM51245G_Suspend(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
Xspi_Nor_Ctx[Instance].TransferRate) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else if (BSP_XSPI_NOR_GetStatus(Instance) != BSP_ERROR_XSPI_SUSPENDED)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
/* Return BSP status */
return ret;
}
/**
* @brief This function resumes a paused erase command.
* @param Instance QSPI instance
* @retval BSP status
*/
int32_t BSP_XSPI_NOR_ResumeErase(uint32_t Instance)
{
int32_t ret;
/* Check if the instance is supported */
if (Instance >= XSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
/* Check whether the device is busy (erase operation is in progress). */
else if (BSP_XSPI_NOR_GetStatus(Instance) != BSP_ERROR_XSPI_SUSPENDED)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else if (MX25UM51245G_Resume(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
Xspi_Nor_Ctx[Instance].TransferRate) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
/*
When this command is executed, the status register write in progress bit is set to 1, and
the flag status register program erase controller bit is set to 0. This command is ignored
if the device is not in a suspended state.
*/
else if (BSP_XSPI_NOR_GetStatus(Instance) != BSP_ERROR_BUSY)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
/* Return BSP status */
return ret;
}
/**
* @brief This function enter the XSPI memory in deep power down mode.
* @param Instance QSPI instance
* @retval BSP status
*/
int32_t BSP_XSPI_NOR_EnterDeepPowerDown(uint32_t Instance)
{
int32_t ret;
/* Check if the instance is supported */
if (Instance >= XSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else if (MX25UM51245G_EnterPowerDown(&hxspi_nor[Instance], Xspi_Nor_Ctx[Instance].InterfaceMode,
Xspi_Nor_Ctx[Instance].TransferRate) != MX25UM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
/* --- Memory takes 10us max to enter deep power down --- */