-
Notifications
You must be signed in to change notification settings - Fork 0
/
stm32h573i_discovery_ospi.c
1461 lines (1324 loc) · 45.9 KB
/
stm32h573i_discovery_ospi.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 stm32h573i_discovery_ospi.c
* @author MCD Application Team
* @brief This file includes a standard driver for the MX25LM51245G
* OSPI memory mounted on the STM32H573I-DK board.
******************************************************************************
* @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.
*
******************************************************************************
@verbatim
==============================================================================
##### How to use this driver #####
==============================================================================
[..]
(#) This driver is used to drive the MX25LM51245G Octal NOR external memory
mounted on STM32H573I_DK board.
(#) This driver need specific component driver (MX25LM51245G) to be included with.
(#) MX25LM51245G Initialization steps:
(++) Initialize the OSPI external memory using the BSP_OSPI_NOR_Init() function. This
function includes the MSP layer hardware resources initialization and the
OSPI interface with the external memory.
(#) MX25LM51245G Octal NOR memory operations
(++) OSPI 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_OSPI_NOR_Read()/BSP_OSPI_NOR_Write().
(++) The function BSP_OSPI_NOR_GetInfo() returns the configuration of the OSPI memory.
(see the OSPI memory data sheet)
(++) Perform erase block operation using the function BSP_OSPI_NOR_Erase_Block() and by
specifying the block address. You can perform an erase operation of the whole
chip by calling the function BSP_OSPI_NOR_Erase_Chip().
(++) The function BSP_OSPI_NOR_GetStatus() returns the current status of the OSPI memory.
(see the OSPI memory data sheet)
(++) The memory access can be configured in memory-mapped mode with the call of
function BSP_OSPI_NOR_EnableMemoryMapped(). To go back in indirect mode, the
function BSP_OSPI_NOR_DisableMemoryMapped() should be used.
(++) The erase operation can be suspend and resume with using functions
BSP_OSPI_NOR_SuspendErase() and BSP_OSPI_NOR_ResumeErase()
(++) It is possible to put the memory in deep power-down mode to reduce its consumption.
For this, the function BSP_OSPI_NOR_EnterDeepPowerDown() should be called. To leave
the deep power-down mode, the function BSP_OSPI_NOR_LeaveDeepPowerDown() should be called.
(++) The function BSP_OSPI_NOR_ReadID() returns the identifier of the memory
(see the OSPI memory data sheet)
(++) The configuration of the interface between peripheral and memory is done by
the function BSP_OSPI_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
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32h573i_discovery_ospi.h"
/** @addtogroup BSP
* @{
*/
/** @addtogroup STM32H573I_DK
* @{
*/
/** @defgroup STM32H573I_DK_OSPI OSPI
* @{
*/
/* Exported variables --------------------------------------------------------*/
/** @addtogroup STM32H573I_DK_OSPI_NOR_Exported_Variables
* @{
*/
XSPI_HandleTypeDef hospi_nor[OSPI_NOR_INSTANCES_NUMBER] = {0};
OSPI_NOR_Ctx_t Ospi_Nor_Ctx[OSPI_NOR_INSTANCES_NUMBER] = {
{
OSPI_ACCESS_NONE,
MX25LM51245G_SPI_MODE,
MX25LM51245G_STR_TRANSFER
}
};
/**
* @}
*/
/* Private constants --------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/** @defgroup STM32H573I_DK_OSPI_NOR_Private_Variables OSPI_NOR Private Variables
* @{
*/
#if (USE_HAL_XSPI_REGISTER_CALLBACKS == 1)
static uint32_t OspiNor_IsMspCbValid[OSPI_NOR_INSTANCES_NUMBER] = {0};
#endif /* USE_HAL_XSPI_REGISTER_CALLBACKS */
/**
* @}
*/
/* Private functions ---------------------------------------------------------*/
/** @defgroup STM32H573I_DK_OSPI_NOR_Private_Functions OSPI_NOR Private Functions
* @{
*/
static void OSPI_NOR_MspInit(XSPI_HandleTypeDef *hospi);
static void OSPI_NOR_MspDeInit(XSPI_HandleTypeDef *hospi);
static int32_t OSPI_NOR_ResetMemory(uint32_t Instance);
static int32_t OSPI_NOR_EnterDOPIMode(uint32_t Instance);
static int32_t OSPI_NOR_EnterSOPIMode(uint32_t Instance);
static int32_t OSPI_NOR_ExitOPIMode(uint32_t Instance);
static void OSPI1_DLYB_Enable(uint32_t Instance);
/**
* @}
*/
/* Exported functions ---------------------------------------------------------*/
/** @addtogroup STM32H573I_DK_OSPI_NOR_Exported_Functions
* @{
*/
/**
* @brief Initializes the OSPI interface.
* @param Instance OSPI Instance
* @param Init OSPI Init structure
* @retval BSP status
*/
int32_t BSP_OSPI_NOR_Init(uint32_t Instance, BSP_OSPI_NOR_Init_t *Init)
{
int32_t ret;
BSP_OSPI_NOR_Info_t pInfo;
MX_OSPI_InitTypeDef ospi_init;
/* Check if the instance is supported */
if (Instance >= OSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Check if the instance is already initialized */
if (Ospi_Nor_Ctx[Instance].IsInitialized == OSPI_ACCESS_NONE)
{
#if (USE_HAL_XSPI_REGISTER_CALLBACKS == 0)
/* Msp OSPI initialization */
OSPI_NOR_MspInit(&hospi_nor[Instance]);
#else
/* Register the OSPI MSP Callbacks */
if (OspiNor_IsMspCbValid[Instance] == 0UL)
{
if (BSP_OSPI_NOR_RegisterDefaultMspCallbacks(Instance) != BSP_ERROR_NONE)
{
return BSP_ERROR_PERIPH_FAILURE;
}
}
#endif /* USE_HAL_XSPI_REGISTER_CALLBACKS */
/* Get Flash information of one memory */
(void)MX25LM51245G_GetFlashInfo(&pInfo);
/* Fill config structure */
ospi_init.ClockPrescaler = 1;
ospi_init.MemorySize = (uint32_t)POSITION_VAL((uint32_t)pInfo.FlashSize);
ospi_init.SampleShifting = HAL_XSPI_SAMPLE_SHIFT_NONE;
ospi_init.TransferRate = (uint32_t)Init->TransferRate;
/* STM32 OSPI interface initialization */
if (MX_OSPI_NOR_Init(&hospi_nor[Instance], &ospi_init) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
/* OSPI Delay Block enable */
OSPI1_DLYB_Enable(Instance);
/* OSPI memory reset */
if (OSPI_NOR_ResetMemory(Instance) != BSP_ERROR_NONE)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Check if memory is ready */
else if (MX25LM51245G_AutoPollingMemReady(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
Ospi_Nor_Ctx[Instance].TransferRate) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Configure the memory */
else if (BSP_OSPI_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;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief De-Initializes the OSPI interface.
* @param Instance OSPI Instance
* @retval BSP status
*/
int32_t BSP_OSPI_NOR_DeInit(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if (Instance >= OSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Check if the instance is already initialized */
if (Ospi_Nor_Ctx[Instance].IsInitialized != OSPI_ACCESS_NONE)
{
/* Disable Memory mapped mode */
if (Ospi_Nor_Ctx[Instance].IsInitialized == OSPI_ACCESS_MMP)
{
if (BSP_OSPI_NOR_DisableMemoryMappedMode(Instance) != BSP_ERROR_NONE)
{
return BSP_ERROR_COMPONENT_FAILURE;
}
}
/* Set default Ospi_Nor_Ctx values */
Ospi_Nor_Ctx[Instance].IsInitialized = OSPI_ACCESS_NONE;
Ospi_Nor_Ctx[Instance].InterfaceMode = BSP_OSPI_NOR_SPI_MODE;
Ospi_Nor_Ctx[Instance].TransferRate = BSP_OSPI_NOR_STR_TRANSFER;
#if (USE_HAL_XSPI_REGISTER_CALLBACKS == 0)
OSPI_NOR_MspDeInit(&hospi_nor[Instance]);
#endif /* (USE_HAL_XSPI_REGISTER_CALLBACKS == 0) */
/* Call the DeInit function to reset the driver */
if (HAL_XSPI_DeInit(&hospi_nor[Instance]) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Initializes the OSPI interface.
* @param hospi OSPI handle
* @param Init OSPI config structure
* @retval BSP status
*/
__weak HAL_StatusTypeDef MX_OSPI_NOR_Init(XSPI_HandleTypeDef *hospi, MX_OSPI_InitTypeDef *Init)
{
/* OctoSPI initialization */
hospi->Instance = OCTOSPI1;
hospi->Init.FifoThresholdByte = 1;
hospi->Init.MemoryMode = HAL_XSPI_SINGLE_MEM;
hospi->Init.MemorySize = Init->MemorySize; /* 512 MBits */
hospi->Init.ChipSelectHighTimeCycle = 2;
hospi->Init.FreeRunningClock = HAL_XSPI_FREERUNCLK_DISABLE;
hospi->Init.ClockMode = HAL_XSPI_CLOCK_MODE_0;
hospi->Init.WrapSize = HAL_XSPI_WRAP_NOT_SUPPORTED;
hospi->Init.ClockPrescaler = Init->ClockPrescaler;
hospi->Init.SampleShifting = Init->SampleShifting;
hospi->Init.ChipSelectBoundary = 0;
if (Init->TransferRate == (uint32_t) BSP_OSPI_NOR_DTR_TRANSFER)
{
hospi->Init.MemoryType = HAL_XSPI_MEMTYPE_MACRONIX;
hospi->Init.DelayHoldQuarterCycle = HAL_XSPI_DHQC_ENABLE;
}
else
{
hospi->Init.MemoryType = HAL_XSPI_MEMTYPE_MICRON;
hospi->Init.DelayHoldQuarterCycle = HAL_XSPI_DHQC_DISABLE;
}
return HAL_XSPI_Init(hospi);
}
#if (USE_HAL_XSPI_REGISTER_CALLBACKS == 1)
/**
* @brief Default BSP OSPI Msp Callbacks
* @param Instance OSPI Instance
* @retval BSP status
*/
int32_t BSP_OSPI_NOR_RegisterDefaultMspCallbacks(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if (Instance >= OSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Register MspInit/MspDeInit Callbacks */
if (HAL_XSPI_RegisterCallback(&hospi_nor[Instance], HAL_XSPI_MSP_INIT_CB_ID, OSPI_NOR_MspInit) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else if (HAL_XSPI_RegisterCallback(&hospi_nor[Instance], HAL_XSPI_MSP_DEINIT_CB_ID, OSPI_NOR_MspDeInit) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
OspiNor_IsMspCbValid[Instance] = 1U;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief BSP OSPI Msp Callback registering
* @param Instance OSPI Instance
* @param CallBacks pointer to MspInit/MspDeInit callbacks functions
* @retval BSP status
*/
int32_t BSP_OSPI_NOR_RegisterMspCallbacks(uint32_t Instance, BSP_OSPI_Cb_t *CallBacks)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if (Instance >= OSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Register MspInit/MspDeInit Callbacks */
if (HAL_XSPI_RegisterCallback(&hospi_nor[Instance], HAL_XSPI_MSP_INIT_CB_ID, CallBacks->pMspInitCb) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else if (HAL_XSPI_RegisterCallback(&hospi_nor[Instance],
HAL_XSPI_MSP_DEINIT_CB_ID, CallBacks->pMspDeInitCb) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else
{
OspiNor_IsMspCbValid[Instance] = 1U;
}
}
/* Return BSP status */
return ret;
}
#endif /* (USE_HAL_XSPI_REGISTER_CALLBACKS == 1) */
/**
* @brief Reads an amount of data from the OSPI memory.
* @param Instance OSPI 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_OSPI_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 >= OSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
if (Ospi_Nor_Ctx[Instance].TransferRate == BSP_OSPI_NOR_STR_TRANSFER)
{
if (MX25LM51245G_ReadSTR(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
MX25LM51245G_4BYTES_SIZE, pData, ReadAddr, Size) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
}
else
{
if (MX25LM51245G_ReadDTR(&hospi_nor[Instance], pData, ReadAddr, Size) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Writes an amount of data to the OSPI memory.
* @param Instance OSPI 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_OSPI_NOR_Write(uint32_t Instance, const 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 >= OSPI_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 = MX25LM51245G_PAGE_SIZE - (WriteAddr % MX25LM51245G_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 (MX25LM51245G_AutoPollingMemReady(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
Ospi_Nor_Ctx[Instance].TransferRate) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Enable write operations */
else if (MX25LM51245G_WriteEnable(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
Ospi_Nor_Ctx[Instance].TransferRate) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
if (Ospi_Nor_Ctx[Instance].TransferRate == BSP_OSPI_NOR_STR_TRANSFER)
{
/* Issue page program command */
if (MX25LM51245G_PageProgram(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
MX25LM51245G_4BYTES_SIZE, (uint8_t *)data_addr, current_addr,
current_size) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
else
{
/* Issue page program command */
if (MX25LM51245G_PageProgramDTR(&hospi_nor[Instance], (uint8_t *)data_addr, current_addr,
current_size) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
}
if (ret == BSP_ERROR_NONE)
{
/* Configure automatic polling mode to wait for end of program */
if (MX25LM51245G_AutoPollingMemReady(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
Ospi_Nor_Ctx[Instance].TransferRate) != MX25LM51245G_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 + MX25LM51245G_PAGE_SIZE) > end_addr)
? (end_addr - current_addr)
: MX25LM51245G_PAGE_SIZE;
}
}
}
} while ((current_addr < end_addr) && (ret == BSP_ERROR_NONE));
}
/* Return BSP status */
return ret;
}
/**
* @brief Erases the specified block of the OSPI memory.
* @param Instance OSPI instance
* @param BlockAddress Block address to erase
* @param BlockSize Erase Block size: MX25LM51245G_ERASE_4K or MX25LM51245G_ERASE_64K
* @retval BSP status
*/
int32_t BSP_OSPI_NOR_Erase_Block(uint32_t Instance, uint32_t BlockAddress, BSP_OSPI_NOR_Erase_t BlockSize)
{
int32_t ret;
/* Check if the instance is supported */
if (Instance >= OSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Check Flash busy ? */
if (MX25LM51245G_AutoPollingMemReady(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
Ospi_Nor_Ctx[Instance].TransferRate) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Enable write operations */
else if (MX25LM51245G_WriteEnable(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
Ospi_Nor_Ctx[Instance].TransferRate) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Issue Block Erase command */
else if (MX25LM51245G_BlockErase(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
Ospi_Nor_Ctx[Instance].TransferRate, MX25LM51245G_4BYTES_SIZE,
BlockAddress, BlockSize) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Erases the entire OSPI memory.
* @param Instance OSPI instance
* @retval BSP status
*/
int32_t BSP_OSPI_NOR_Erase_Chip(uint32_t Instance)
{
int32_t ret;
/* Check if the instance is supported */
if (Instance >= OSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Check Flash busy ? */
if (MX25LM51245G_AutoPollingMemReady(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
Ospi_Nor_Ctx[Instance].TransferRate) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Enable write operations */
else if (MX25LM51245G_WriteEnable(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
Ospi_Nor_Ctx[Instance].TransferRate) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Issue Chip erase command */
else if (MX25LM51245G_ChipErase(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
Ospi_Nor_Ctx[Instance].TransferRate) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Reads current status of the OSPI memory.
* @param Instance OSPI instance
* @retval OSPI memory status: whether busy or not
*/
int32_t BSP_OSPI_NOR_GetStatus(uint32_t Instance)
{
static uint8_t reg[2];
int32_t ret;
/* Check if the instance is supported */
if (Instance >= OSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
if (MX25LM51245G_ReadSecurityRegister(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
Ospi_Nor_Ctx[Instance].TransferRate, reg) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Check the value of the register */
else if ((reg[0] & (MX25LM51245G_SECR_P_FAIL | MX25LM51245G_SECR_E_FAIL)) != 0U)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else if ((reg[0] & (MX25LM51245G_SECR_PSB | MX25LM51245G_SECR_ESB)) != 0U)
{
ret = BSP_ERROR_OSPI_SUSPENDED;
}
else if (MX25LM51245G_ReadStatusRegister(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
Ospi_Nor_Ctx[Instance].TransferRate, reg) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}/* Check the value of the register */
else if ((reg[0] & MX25LM51245G_SR_WIP) != 0U)
{
ret = BSP_ERROR_BUSY;
}
else
{
ret = BSP_ERROR_NONE;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Return the configuration of the OSPI memory.
* @param Instance OSPI instance
* @param pInfo pointer on the configuration structure
* @retval BSP status
*/
int32_t BSP_OSPI_NOR_GetInfo(uint32_t Instance, BSP_OSPI_NOR_Info_t *pInfo)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if (Instance >= OSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
(void)MX25LM51245G_GetFlashInfo(pInfo);
}
/* Return BSP status */
return ret;
}
/**
* @brief Configure the OSPI in memory-mapped mode
* @param Instance OSPI instance
* @retval BSP status
*/
int32_t BSP_OSPI_NOR_EnableMemoryMappedMode(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if (Instance >= OSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
if (Ospi_Nor_Ctx[Instance].TransferRate == BSP_OSPI_NOR_STR_TRANSFER)
{
if (MX25LM51245G_EnableMemoryMappedModeSTR(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
MX25LM51245G_4BYTES_SIZE) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else /* Update OSPI context if all operations are well done */
{
Ospi_Nor_Ctx[Instance].IsInitialized = OSPI_ACCESS_MMP;
}
}
else
{
if (MX25LM51245G_EnableMemoryMappedModeDTR(&hospi_nor[Instance],
Ospi_Nor_Ctx[Instance].InterfaceMode) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else /* Update OSPI context if all operations are well done */
{
Ospi_Nor_Ctx[Instance].IsInitialized = OSPI_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 OSPI instance
* @retval BSP status
*/
int32_t BSP_OSPI_NOR_DisableMemoryMappedMode(uint32_t Instance)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if (Instance >= OSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
if (Ospi_Nor_Ctx[Instance].IsInitialized != OSPI_ACCESS_MMP)
{
ret = BSP_ERROR_OSPI_MMP_UNLOCK_FAILURE;
}/* Abort MMP back to indirect mode */
else if (HAL_XSPI_Abort(&hospi_nor[Instance]) != HAL_OK)
{
ret = BSP_ERROR_PERIPH_FAILURE;
}
else /* Update OSPI NOR context if all operations are well done */
{
Ospi_Nor_Ctx[Instance].IsInitialized = OSPI_ACCESS_INDIRECT;
}
}
/* Return BSP status */
return ret;
}
/**
* @brief Get flash ID 3 Bytes:
* Manufacturer ID, Memory type, Memory density
* @param Instance OSPI instance
* @param Id Pointer to flash ID bytes
* @retval BSP status
*/
int32_t BSP_OSPI_NOR_ReadID(uint32_t Instance, uint8_t *Id)
{
int32_t ret;
/* Check if the instance is supported */
if (Instance >= OSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else if (MX25LM51245G_ReadID(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
Ospi_Nor_Ctx[Instance].TransferRate, Id) != MX25LM51245G_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 OSPI instance
* @param Mode OSPI mode
* @param Rate OSPI transfer rate
* @retval BSP status
*/
int32_t BSP_OSPI_NOR_ConfigFlash(uint32_t Instance, BSP_OSPI_NOR_Interface_t Mode, BSP_OSPI_NOR_Transfer_t Rate)
{
int32_t ret = BSP_ERROR_NONE;
/* Check if the instance is supported */
if (Instance >= OSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else
{
/* Check if MMP mode locked ************************************************/
if (Ospi_Nor_Ctx[Instance].IsInitialized == OSPI_ACCESS_MMP)
{
ret = BSP_ERROR_OSPI_MMP_LOCK_FAILURE;
}
else
{
/* Setup Flash interface ***************************************************/
switch (Ospi_Nor_Ctx[Instance].InterfaceMode)
{
case BSP_OSPI_NOR_OPI_MODE : /* 8-8-8 commands */
if ((Mode != BSP_OSPI_NOR_OPI_MODE) || (Rate != Ospi_Nor_Ctx[Instance].TransferRate))
{
/* Exit OPI mode */
ret = OSPI_NOR_ExitOPIMode(Instance);
if ((ret == BSP_ERROR_NONE) && (Mode == BSP_OSPI_NOR_OPI_MODE))
{
if (Ospi_Nor_Ctx[Instance].TransferRate == BSP_OSPI_NOR_STR_TRANSFER)
{
/* Enter DTR OPI mode */
ret = OSPI_NOR_EnterDOPIMode(Instance);
}
else
{
/* Enter STR OPI mode */
ret = OSPI_NOR_EnterSOPIMode(Instance);
}
}
}
break;
case BSP_OSPI_NOR_SPI_MODE : /* 1-1-1 commands, Power on H/W default setting */
default :
if (Mode == BSP_OSPI_NOR_OPI_MODE)
{
if (Rate == BSP_OSPI_NOR_STR_TRANSFER)
{
/* Enter STR OPI mode */
ret = OSPI_NOR_EnterSOPIMode(Instance);
}
else
{
/* Enter DTR OPI mode */
ret = OSPI_NOR_EnterDOPIMode(Instance);
}
}
break;
}
/* Update OSPI context if all operations are well done */
if (ret == BSP_ERROR_NONE)
{
/* Update current status parameter *****************************************/
Ospi_Nor_Ctx[Instance].IsInitialized = OSPI_ACCESS_INDIRECT;
Ospi_Nor_Ctx[Instance].InterfaceMode = Mode;
Ospi_Nor_Ctx[Instance].TransferRate = Rate;
}
}
}
/* Return BSP status */
return ret;
}
/**
* @brief This function suspends an ongoing erase command.
* @param Instance OSPI instance
* @retval BSP status
*/
int32_t BSP_OSPI_NOR_SuspendErase(uint32_t Instance)
{
int32_t ret;
/* Check if the instance is supported */
if (Instance >= OSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
/* Check whether the device is busy (erase operation is in progress). */
else if (BSP_OSPI_NOR_GetStatus(Instance) != BSP_ERROR_BUSY)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else if (MX25LM51245G_Suspend(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
Ospi_Nor_Ctx[Instance].TransferRate) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else if (BSP_OSPI_NOR_GetStatus(Instance) != BSP_ERROR_OSPI_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 OSPI instance
* @retval BSP status
*/
int32_t BSP_OSPI_NOR_ResumeErase(uint32_t Instance)
{
int32_t ret;
/* Check if the instance is supported */
if (Instance >= OSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
/* Check whether the device is busy (erase operation is in progress). */
else if (BSP_OSPI_NOR_GetStatus(Instance) != BSP_ERROR_OSPI_SUSPENDED)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else if (MX25LM51245G_Resume(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
Ospi_Nor_Ctx[Instance].TransferRate) != MX25LM51245G_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_OSPI_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 OSPI memory in deep power down mode.
* @param Instance OSPI instance
* @retval BSP status
*/
int32_t BSP_OSPI_NOR_EnterDeepPowerDown(uint32_t Instance)
{
int32_t ret;
/* Check if the instance is supported */
if (Instance >= OSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;
}
else if (MX25LM51245G_EnterPowerDown(&hospi_nor[Instance], Ospi_Nor_Ctx[Instance].InterfaceMode,
Ospi_Nor_Ctx[Instance].TransferRate) != MX25LM51245G_OK)
{
ret = BSP_ERROR_COMPONENT_FAILURE;
}
else
{
ret = BSP_ERROR_NONE;
}
/* --- Memory takes 10us max to enter deep power down --- */
/* Return BSP status */
return ret;
}
/**
* @brief This function leave the OSPI memory from deep power down mode.
* @param Instance OSPI instance
* @retval BSP status
*/
int32_t BSP_OSPI_NOR_LeaveDeepPowerDown(uint32_t Instance)
{
int32_t ret;
/* Check if the instance is supported */
if (Instance >= OSPI_NOR_INSTANCES_NUMBER)
{
ret = BSP_ERROR_WRONG_PARAM;