forked from sparkfun/AmbiqSuiteSDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patham_devices_mspi_atxp032.c
1575 lines (1424 loc) · 55.4 KB
/
am_devices_mspi_atxp032.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 am_devices_mspi_atxp032.c
//!
//! @brief General Multibit SPI Flash driver.
//
//*****************************************************************************
//*****************************************************************************
//
// Copyright (c) 2020, Ambiq Micro
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// Third party software included in this distribution is subject to the
// additional license terms as defined in the /docs/licenses directory.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// This is part of revision 2.4.2 of the AmbiqSuite Development Package.
//
//*****************************************************************************
#include "am_mcu_apollo.h"
#include "am_devices_mspi_atxp032.h"
#include "am_util_stdio.h"
#include "am_bsp.h"
#include "am_util_delay.h"
//*****************************************************************************
//
// Global variables.
//
//*****************************************************************************
#define AM_DEVICES_MSPI_ATXP032_TIMEOUT 1000000
#define AM_DEVICES_MSPI_ATXP032_ERASE_TIMEOUT 1000000
typedef struct
{
uint32_t ui32Module;
void *pMspiHandle;
am_hal_mspi_dev_config_t stSetting;
bool bOccupied;
} am_devices_mspi_atxp032_t;
am_devices_mspi_atxp032_t gAmAtxp032[AM_DEVICES_MSPI_ATXP032_MAX_DEVICE_NUM];
am_hal_mspi_dev_config_t MSPI_ATXP032_Serial_CE0_MSPIConfig =
{
.eSpiMode = AM_HAL_MSPI_SPI_MODE_0,
.eClockFreq = AM_HAL_MSPI_CLK_24MHZ,
.ui8TurnAround = 8,
.eAddrCfg = AM_HAL_MSPI_ADDR_4_BYTE,
.eInstrCfg = AM_HAL_MSPI_INSTR_1_BYTE,
.eDeviceConfig = AM_HAL_MSPI_FLASH_SERIAL_CE0,
.bSeparateIO = true,
.bSendInstr = true,
.bSendAddr = true,
.bTurnaround = true,
.ui8ReadInstr = AM_DEVICES_MSPI_ATXP032_FAST_READ,
.ui8WriteInstr = AM_DEVICES_MSPI_ATXP032_PAGE_PROGRAM,
#if defined(AM_PART_APOLLO3P)
.ui8WriteLatency = 0,
.bEnWriteLatency = false,
.bEmulateDDR = false,
.ui16DMATimeLimit = 0,
.eDMABoundary = AM_HAL_MSPI_BOUNDARY_NONE,
#endif
.ui32TCBSize = 0,
.pTCB = NULL,
.scramblingStartAddr = 0,
.scramblingEndAddr = 0,
};
am_hal_mspi_dev_config_t MSPI_ATXP032_Serial_CE1_MSPIConfig =
{
.eSpiMode = AM_HAL_MSPI_SPI_MODE_0,
.eClockFreq = AM_HAL_MSPI_CLK_24MHZ,
.ui8TurnAround = 8,
.eAddrCfg = AM_HAL_MSPI_ADDR_4_BYTE,
.eInstrCfg = AM_HAL_MSPI_INSTR_1_BYTE,
.eDeviceConfig = AM_HAL_MSPI_FLASH_SERIAL_CE1,
.bSeparateIO = true,
.bSendInstr = true,
.bSendAddr = true,
.bTurnaround = true,
.ui8ReadInstr = AM_DEVICES_MSPI_ATXP032_FAST_READ,
.ui8WriteInstr = AM_DEVICES_MSPI_ATXP032_PAGE_PROGRAM,
#if defined(AM_PART_APOLLO3P)
.ui8WriteLatency = 0,
.bEnWriteLatency = false,
.bEmulateDDR = false,
.ui16DMATimeLimit = 0,
.eDMABoundary = AM_HAL_MSPI_BOUNDARY_NONE,
#endif
.ui32TCBSize = 0,
.pTCB = NULL,
.scramblingStartAddr = 0,
.scramblingEndAddr = 0,
};
am_hal_mspi_dev_config_t MSPI_ATXP032_Quad_CE0_MSPIConfig =
{
.eSpiMode = AM_HAL_MSPI_SPI_MODE_0,
.eClockFreq = AM_HAL_MSPI_CLK_24MHZ,
.ui8TurnAround = 8,
.eAddrCfg = AM_HAL_MSPI_ADDR_4_BYTE,
.eInstrCfg = AM_HAL_MSPI_INSTR_1_BYTE,
.eDeviceConfig = AM_HAL_MSPI_FLASH_QUAD_CE0,
.bSeparateIO = false,
.bSendInstr = true,
.bSendAddr = true,
.bTurnaround = true,
.ui8ReadInstr = AM_DEVICES_MSPI_ATXP032_FAST_READ,
.ui8WriteInstr = AM_DEVICES_MSPI_ATXP032_PAGE_PROGRAM,
#if defined(AM_PART_APOLLO3P)
.ui8WriteLatency = 0,
.bEnWriteLatency = false,
.bEmulateDDR = false,
.ui16DMATimeLimit = 0,
.eDMABoundary = AM_HAL_MSPI_BOUNDARY_NONE,
#endif
.ui32TCBSize = 0,
.pTCB = NULL,
.scramblingStartAddr = 0,
.scramblingEndAddr = 0,
};
am_hal_mspi_dev_config_t MSPI_ATXP032_Quad_CE1_MSPIConfig =
{
.eSpiMode = AM_HAL_MSPI_SPI_MODE_0,
.eClockFreq = AM_HAL_MSPI_CLK_24MHZ,
.ui8TurnAround = 8,
.eAddrCfg = AM_HAL_MSPI_ADDR_4_BYTE,
.eInstrCfg = AM_HAL_MSPI_INSTR_1_BYTE,
.eDeviceConfig = AM_HAL_MSPI_FLASH_QUAD_CE1,
.bSeparateIO = false,
.bSendInstr = true,
.bSendAddr = true,
.bTurnaround = true,
.ui8ReadInstr = AM_DEVICES_MSPI_ATXP032_FAST_READ,
.ui8WriteInstr = AM_DEVICES_MSPI_ATXP032_PAGE_PROGRAM,
#if defined(AM_PART_APOLLO3P)
.ui8WriteLatency = 0,
.bEnWriteLatency = false,
.bEmulateDDR = false,
.ui16DMATimeLimit = 0,
.eDMABoundary = AM_HAL_MSPI_BOUNDARY_NONE,
#endif
.ui32TCBSize = 0,
.pTCB = NULL,
.scramblingStartAddr = 0,
.scramblingEndAddr = 0,
};
am_hal_mspi_dev_config_t MSPI_ATXP032_Octal_CE0_MSPIConfig =
{
.eSpiMode = AM_HAL_MSPI_SPI_MODE_0,
.eClockFreq = AM_HAL_MSPI_CLK_24MHZ,
.ui8TurnAround = 8,
.eAddrCfg = AM_HAL_MSPI_ADDR_4_BYTE,
.eInstrCfg = AM_HAL_MSPI_INSTR_1_BYTE,
.eDeviceConfig = AM_HAL_MSPI_FLASH_OCTAL_CE0,
.bSeparateIO = false,
.bSendInstr = true,
.bSendAddr = true,
.bTurnaround = true,
.ui8ReadInstr = AM_DEVICES_MSPI_ATXP032_FAST_READ,
.ui8WriteInstr = AM_DEVICES_MSPI_ATXP032_PAGE_PROGRAM,
#if defined(AM_PART_APOLLO3P)
.ui8WriteLatency = 0,
.bEnWriteLatency = false,
.bEmulateDDR = false,
.ui16DMATimeLimit = 0,
.eDMABoundary = AM_HAL_MSPI_BOUNDARY_NONE,
#endif
.ui32TCBSize = 0,
.pTCB = NULL,
.scramblingStartAddr = 0,
.scramblingEndAddr = 0,
};
am_hal_mspi_dev_config_t MSPI_ATXP032_Octal_CE1_MSPIConfig =
{
.eSpiMode = AM_HAL_MSPI_SPI_MODE_0,
.eClockFreq = AM_HAL_MSPI_CLK_24MHZ,
.ui8TurnAround = 8,
.eAddrCfg = AM_HAL_MSPI_ADDR_4_BYTE,
.eInstrCfg = AM_HAL_MSPI_INSTR_1_BYTE,
.eDeviceConfig = AM_HAL_MSPI_FLASH_OCTAL_CE1,
.bSeparateIO = false,
.bSendInstr = true,
.bSendAddr = true,
.bTurnaround = true,
.ui8ReadInstr = AM_DEVICES_MSPI_ATXP032_FAST_READ,
.ui8WriteInstr = AM_DEVICES_MSPI_ATXP032_PAGE_PROGRAM,
#if defined(AM_PART_APOLLO3P)
.ui8WriteLatency = 0,
.bEnWriteLatency = false,
.bEmulateDDR = false,
.ui16DMATimeLimit = 0,
.eDMABoundary = AM_HAL_MSPI_BOUNDARY_NONE,
#endif
.ui32TCBSize = 0,
.pTCB = NULL,
.scramblingStartAddr = 0,
.scramblingEndAddr = 0,
};
struct
{
am_hal_mspi_device_e eHalDeviceEnum;
am_hal_mspi_dev_config_t *psDevConfig;
}g_ATXP032_DevConfig[] =
{
{AM_HAL_MSPI_FLASH_SERIAL_CE0, &MSPI_ATXP032_Serial_CE0_MSPIConfig},
{AM_HAL_MSPI_FLASH_SERIAL_CE1, &MSPI_ATXP032_Serial_CE1_MSPIConfig},
{AM_HAL_MSPI_FLASH_QUAD_CE0, &MSPI_ATXP032_Quad_CE0_MSPIConfig},
{AM_HAL_MSPI_FLASH_QUAD_CE1, &MSPI_ATXP032_Quad_CE1_MSPIConfig},
{AM_HAL_MSPI_FLASH_OCTAL_CE0, &MSPI_ATXP032_Octal_CE0_MSPIConfig},
{AM_HAL_MSPI_FLASH_OCTAL_CE1, &MSPI_ATXP032_Octal_CE1_MSPIConfig},
};
//
// Forward declarations.
//
static uint32_t am_devices_mspi_atxp032_command_write(void *pHandle,
uint8_t ui8Instr,
bool bSendAddr,
uint32_t ui32Addr,
uint32_t *pData,
uint32_t ui32NumBytes);
static uint32_t am_devices_mspi_atxp032_command_read(void *pHandle,
uint8_t ui8Instr,
bool bSendAddr,
uint32_t ui32Addr,
uint32_t *pData,
uint32_t ui32NumBytes);
//*****************************************************************************
//
// Adesto ATXP032 Support
//
//*****************************************************************************
//
// Device specific initialization function.
//
static uint32_t
am_device_init_flash(void *pHandle)
{
uint32_t ui32PIOBuffer[32] = {0};
am_devices_mspi_atxp032_t *pFlash = (am_devices_mspi_atxp032_t *)pHandle;
//
// Set the Dummy Cycles in Status/Control register 3 to 8.
//
am_devices_mspi_atxp032_command_write(pHandle, AM_DEVICES_MSPI_ATXP032_WRITE_ENABLE, false, 0, ui32PIOBuffer, 0);
ui32PIOBuffer[0] = 0x00000003;
am_devices_mspi_atxp032_command_write(pHandle, AM_DEVICES_ATXP032_WRITE_STATUS_CTRL, false, 0, ui32PIOBuffer, 2);
//
// Configure the ATXP032 mode based on the MSPI configuration.
//
am_devices_mspi_atxp032_command_write(pHandle, AM_DEVICES_MSPI_ATXP032_WRITE_ENABLE, false, 0, ui32PIOBuffer, 0);
switch ( pFlash->stSetting.eDeviceConfig )
{
case AM_HAL_MSPI_FLASH_SERIAL_CE0:
case AM_HAL_MSPI_FLASH_SERIAL_CE1:
am_devices_mspi_atxp032_command_write(pHandle, AM_DEVICES_ATXP032_RETURN_TO_SPI_MODE, false, 0, ui32PIOBuffer, 0);
break;
case AM_HAL_MSPI_FLASH_DUAL_CE0:
case AM_HAL_MSPI_FLASH_DUAL_CE1:
case AM_HAL_MSPI_FLASH_QUADPAIRED:
case AM_HAL_MSPI_FLASH_QUADPAIRED_SERIAL:
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
case AM_HAL_MSPI_FLASH_QUAD_CE0:
case AM_HAL_MSPI_FLASH_QUAD_CE1:
am_devices_mspi_atxp032_command_write(pHandle, AM_DEVICES_ATXP032_ENTER_QUAD_MODE, false, 0, ui32PIOBuffer, 0);
break;
case AM_HAL_MSPI_FLASH_OCTAL_CE0:
case AM_HAL_MSPI_FLASH_OCTAL_CE1:
am_devices_mspi_atxp032_command_write(pHandle, AM_DEVICES_ATXP032_ENTER_OCTAL_MODE, false, 0, ui32PIOBuffer, 0);
break;
}
return AM_DEVICES_MSPI_ATXP032_STATUS_SUCCESS;
}
//
// Device specific de-initialization function.
//
static uint32_t
am_device_deinit_flash(void *pHandle)
{
uint32_t ui32Status;
am_devices_mspi_atxp032_t *pFlash = (am_devices_mspi_atxp032_t *)pHandle;
uint32_t ui32PIOBuffer[32] = {0};
//
// Configure the Adesto ATXP032 Device mode.
//
switch (pFlash->stSetting.eDeviceConfig)
{
case AM_HAL_MSPI_FLASH_SERIAL_CE0:
case AM_HAL_MSPI_FLASH_SERIAL_CE1:
// Nothing to do. Device defaults to SPI mode.
break;
case AM_HAL_MSPI_FLASH_QUAD_CE0:
case AM_HAL_MSPI_FLASH_QUAD_CE1:
case AM_HAL_MSPI_FLASH_OCTAL_CE0:
case AM_HAL_MSPI_FLASH_OCTAL_CE1:
ui32Status = am_devices_mspi_atxp032_command_write(pHandle, AM_DEVICES_ATXP032_RETURN_TO_SPI_MODE, false, 0, ui32PIOBuffer, 0);
if (AM_HAL_STATUS_SUCCESS != ui32Status)
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
break;
case AM_HAL_MSPI_FLASH_DUAL_CE0:
case AM_HAL_MSPI_FLASH_DUAL_CE1:
case AM_HAL_MSPI_FLASH_QUADPAIRED:
case AM_HAL_MSPI_FLASH_QUADPAIRED_SERIAL:
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
//break;
}
return AM_DEVICES_MSPI_ATXP032_STATUS_SUCCESS;
}
//*****************************************************************************
//
// Generic Command Write function.
//
//*****************************************************************************
static uint32_t
am_devices_mspi_atxp032_command_write(void *pHandle, uint8_t ui8Instr, bool bSendAddr,
uint32_t ui32Addr, uint32_t *pData,
uint32_t ui32NumBytes)
{
uint32_t ui32Status;
am_devices_mspi_atxp032_t *pFlash = (am_devices_mspi_atxp032_t *)pHandle;
am_hal_mspi_pio_transfer_t stMSPIFlashPIOTransaction = {0};
// Create the individual write transaction.
stMSPIFlashPIOTransaction.ui32NumBytes = ui32NumBytes;
stMSPIFlashPIOTransaction.eDirection = AM_HAL_MSPI_TX;
stMSPIFlashPIOTransaction.bSendAddr = bSendAddr;
stMSPIFlashPIOTransaction.ui32DeviceAddr = ui32Addr;
stMSPIFlashPIOTransaction.bSendInstr = true;
stMSPIFlashPIOTransaction.ui16DeviceInstr = ui8Instr;
stMSPIFlashPIOTransaction.bTurnaround = false;
#if 0 // A3DS-25 Deprecate MSPI CONT
stMSPIFlashPIOTransaction.bContinue = false;
#endif // A3DS-25
if (AM_HAL_MSPI_FLASH_QUADPAIRED == pFlash->stSetting.eDeviceConfig)
{
stMSPIFlashPIOTransaction.bQuadCmd = true;
}
else
{
stMSPIFlashPIOTransaction.bQuadCmd = false;
}
stMSPIFlashPIOTransaction.pui32Buffer = pData;
// Execute the transction over MSPI.
ui32Status = am_hal_mspi_blocking_transfer(pFlash->pMspiHandle, &stMSPIFlashPIOTransaction,
AM_DEVICES_MSPI_ATXP032_TIMEOUT);
return ui32Status;
}
//*****************************************************************************
//
// Generic Command Read function.
//
//*****************************************************************************
static uint32_t
am_devices_mspi_atxp032_command_read(void *pHandle, uint8_t ui8Instr, bool bSendAddr,
uint32_t ui32Addr, uint32_t *pData,
uint32_t ui32NumBytes)
{
uint32_t ui32Status;
am_devices_mspi_atxp032_t *pFlash = (am_devices_mspi_atxp032_t *)pHandle;
am_hal_mspi_pio_transfer_t stMSPIFlashPIOTransaction = {0};
// Create the individual write transaction.
stMSPIFlashPIOTransaction.eDirection = AM_HAL_MSPI_RX;
stMSPIFlashPIOTransaction.bSendAddr = bSendAddr;
stMSPIFlashPIOTransaction.ui32DeviceAddr = ui32Addr;
stMSPIFlashPIOTransaction.bSendInstr = true;
stMSPIFlashPIOTransaction.ui16DeviceInstr = ui8Instr;
stMSPIFlashPIOTransaction.bTurnaround = false;
#if 0 // A3DS-25 Deprecate MSPI CONT
stMSPIFlashPIOTransaction.bContinue = false;
#endif // A3DS-25
if (AM_HAL_MSPI_FLASH_QUADPAIRED == pFlash->stSetting.eDeviceConfig)
{
stMSPIFlashPIOTransaction.ui32NumBytes = ui32NumBytes * 2;
stMSPIFlashPIOTransaction.bQuadCmd = true;
}
else
{
stMSPIFlashPIOTransaction.ui32NumBytes = ui32NumBytes;
stMSPIFlashPIOTransaction.bQuadCmd = false;
}
stMSPIFlashPIOTransaction.pui32Buffer = pData;
// Execute the transction over MSPI.
ui32Status = am_hal_mspi_blocking_transfer(pFlash->pMspiHandle, &stMSPIFlashPIOTransaction,
AM_DEVICES_MSPI_ATXP032_TIMEOUT);
return ui32Status;
}
static void
pfnMSPI_ATXP032_Callback(void *pCallbackCtxt, uint32_t status)
{
// Set the DMA complete flag.
*(volatile bool *)pCallbackCtxt = true;
}
//*****************************************************************************
//
//! @brief Initialize the mspi_flash driver.
//!
//! @param psIOMSettings - IOM device structure describing the target spiflash.
//! @param pfnWriteFunc - Function to use for spi writes.
//! @param pfnReadFunc - Function to use for spi reads.
//!
//! This function should be called before any other am_devices_spiflash
//! functions. It is used to set tell the other functions how to communicate
//! with the external spiflash hardware.
//!
//! The \e pfnWriteFunc and \e pfnReadFunc variables may be used to provide
//! alternate implementations of SPI write and read functions respectively. If
//! they are left set to 0, the default functions am_hal_iom_spi_write() and
//! am_hal_iom_spi_read() will be used.
//!
//! @return None.
//
//*****************************************************************************
uint32_t
am_devices_mspi_atxp032_init(uint32_t ui32Module, am_devices_mspi_atxp032_config_t *psMSPISettings, void **ppHandle, void **ppMspiHandle)
{
uint32_t ui32Status;
am_hal_mspi_dev_config_t *psConfig = g_ATXP032_DevConfig[0].psDevConfig;
void *pMspiHandle;
uint32_t ui32Index = 0;
if ((ui32Module > AM_REG_MSPI_NUM_MODULES) || (psMSPISettings == NULL))
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
// Allocate a vacant device handle
for ( ui32Index = 0; ui32Index < AM_DEVICES_MSPI_ATXP032_MAX_DEVICE_NUM; ui32Index++ )
{
if ( gAmAtxp032[ui32Index].bOccupied == false )
{
break;
}
}
if ( ui32Index == AM_DEVICES_MSPI_ATXP032_MAX_DEVICE_NUM )
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
for ( uint32_t i = 0; i < (sizeof(g_ATXP032_DevConfig) / sizeof(g_ATXP032_DevConfig[0])); i++ )
{
if ( psMSPISettings->eDeviceConfig == g_ATXP032_DevConfig[i].eHalDeviceEnum )
{
psConfig = g_ATXP032_DevConfig[i].psDevConfig;
psConfig->eClockFreq = psMSPISettings->eClockFreq;
psConfig->pTCB = psMSPISettings->pNBTxnBuf;
psConfig->ui32TCBSize = psMSPISettings->ui32NBTxnBufLength;
psConfig->scramblingStartAddr = psMSPISettings->ui32ScramblingStartAddr;
psConfig->scramblingEndAddr = psMSPISettings->ui32ScramblingEndAddr;
break;
}
}
//
// Enable fault detection.
//
#if AM_APOLLO3_MCUCTRL
am_hal_mcuctrl_control(AM_HAL_MCUCTRL_CONTROL_FAULT_CAPTURE_ENABLE, 0);
#else // AM_APOLLO3_MCUCTRL
am_hal_mcuctrl_fault_capture_enable();
#endif // AM_APOLLO3_MCUCTRL
//
// Configure the MSPI for Serial or Quad-Paired Serial operation during initialization.
//
switch (psConfig->eDeviceConfig)
{
case AM_HAL_MSPI_FLASH_SERIAL_CE0:
case AM_HAL_MSPI_FLASH_QUAD_CE0:
case AM_HAL_MSPI_FLASH_OCTAL_CE0:
gAmAtxp032[ui32Index].stSetting = MSPI_ATXP032_Serial_CE0_MSPIConfig;
if (AM_HAL_STATUS_SUCCESS != am_hal_mspi_initialize(ui32Module, &pMspiHandle))
{
am_util_stdio_printf("Error - Failed to initialize MSPI.\n");
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
if (AM_HAL_STATUS_SUCCESS != am_hal_mspi_power_control(pMspiHandle, AM_HAL_SYSCTRL_WAKE, false))
{
am_util_stdio_printf("Error - Failed to power on MSPI.\n");
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
if (AM_HAL_STATUS_SUCCESS != am_hal_mspi_device_configure(pMspiHandle, &MSPI_ATXP032_Serial_CE0_MSPIConfig))
{
am_util_stdio_printf("Error - Failed to configure MSPI.\n");
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
if (AM_HAL_STATUS_SUCCESS != am_hal_mspi_enable(pMspiHandle))
{
am_util_stdio_printf("Error - Failed to enable MSPI.\n");
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
am_bsp_mspi_pins_enable(ui32Module, MSPI_ATXP032_Serial_CE0_MSPIConfig.eDeviceConfig);
break;
case AM_HAL_MSPI_FLASH_SERIAL_CE1:
case AM_HAL_MSPI_FLASH_QUAD_CE1:
case AM_HAL_MSPI_FLASH_OCTAL_CE1:
gAmAtxp032[ui32Index].stSetting = MSPI_ATXP032_Serial_CE1_MSPIConfig;
if (AM_HAL_STATUS_SUCCESS != am_hal_mspi_initialize(ui32Module, &pMspiHandle))
{
am_util_stdio_printf("Error - Failed to initialize MSPI.\n");
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
if (AM_HAL_STATUS_SUCCESS != am_hal_mspi_power_control(pMspiHandle, AM_HAL_SYSCTRL_WAKE, false))
{
am_util_stdio_printf("Error - Failed to power on MSPI.\n");
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
if (AM_HAL_STATUS_SUCCESS != am_hal_mspi_device_configure(pMspiHandle, &MSPI_ATXP032_Serial_CE1_MSPIConfig))
{
am_util_stdio_printf("Error - Failed to configure MSPI.\n");
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
if (AM_HAL_STATUS_SUCCESS != am_hal_mspi_enable(pMspiHandle))
{
am_util_stdio_printf("Error - Failed to enable MSPI.\n");
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
am_bsp_mspi_pins_enable(ui32Module, MSPI_ATXP032_Serial_CE1_MSPIConfig.eDeviceConfig);
break;
case AM_HAL_MSPI_FLASH_DUAL_CE0:
case AM_HAL_MSPI_FLASH_DUAL_CE1:
case AM_HAL_MSPI_FLASH_QUADPAIRED:
case AM_HAL_MSPI_FLASH_QUADPAIRED_SERIAL:
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
//break;
}
gAmAtxp032[ui32Index].pMspiHandle = pMspiHandle;
gAmAtxp032[ui32Index].ui32Module = ui32Module;
if (AM_HAL_STATUS_SUCCESS != am_devices_mspi_atxp032_reset((void*)&gAmAtxp032[ui32Index]))
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
gAmAtxp032[ui32Index].stSetting = *psConfig;
//
// Device specific MSPI Flash initialization.
//
ui32Status = am_device_init_flash((void*)&gAmAtxp032[ui32Index]);
if (AM_HAL_STATUS_SUCCESS != ui32Status)
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
// Disable MSPI defore re-configuring it
ui32Status = am_hal_mspi_disable(pMspiHandle);
if (AM_HAL_STATUS_SUCCESS != ui32Status)
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
//
// Re-Configure the MSPI for the requested operation mode.
//
ui32Status = am_hal_mspi_device_configure(pMspiHandle, psConfig);
if (AM_HAL_STATUS_SUCCESS != ui32Status)
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
// Re-Enable MSPI
ui32Status = am_hal_mspi_enable(pMspiHandle);
if (AM_HAL_STATUS_SUCCESS != ui32Status)
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
//
// Configure the MSPI pins.
//
am_bsp_mspi_pins_enable(ui32Module, psConfig->eDeviceConfig);
//
// Enable MSPI interrupts.
//
ui32Status = am_hal_mspi_interrupt_clear(pMspiHandle, AM_HAL_MSPI_INT_CQUPD | AM_HAL_MSPI_INT_ERR );
if (AM_HAL_STATUS_SUCCESS != ui32Status)
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
ui32Status = am_hal_mspi_interrupt_enable(pMspiHandle, AM_HAL_MSPI_INT_CQUPD | AM_HAL_MSPI_INT_ERR );
if (AM_HAL_STATUS_SUCCESS != ui32Status)
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
//
// Return the handle.
//
gAmAtxp032[ui32Index].bOccupied = true;
*ppMspiHandle = pMspiHandle;
*ppHandle = (void *)&gAmAtxp032[ui32Index];
//
// Return the status.
//
return AM_DEVICES_MSPI_ATXP032_STATUS_SUCCESS;
}
//*****************************************************************************
//
//! @brief De-Initialization the mspi_flash driver.
//!
//! @param psIOMSettings - IOM device structure describing the target spiflash.
//! @param pfnWriteFunc - Function to use for spi writes.
//! @param pfnReadFunc - Function to use for spi reads.
//!
//! This function should be called before any other am_devices_spiflash
//! functions. It is used to set tell the other functions how to communicate
//! with the external spiflash hardware.
//!
//! The \e pfnWriteFunc and \e pfnReadFunc variables may be used to provide
//! alternate implementations of SPI write and read functions respectively. If
//! they are left set to 0, the default functions am_hal_iom_spi_write() and
//! am_hal_iom_spi_read() will be used.
//!
//! @return None.
//
//*****************************************************************************
uint32_t
am_devices_mspi_atxp032_deinit(void *pHandle)
{
uint32_t ui32Status;
am_devices_mspi_atxp032_t *pFlash = (am_devices_mspi_atxp032_t *)pHandle;
//
// Device specific MSPI Flash de-initialization.
//
ui32Status = am_device_deinit_flash(pHandle);
if (AM_HAL_STATUS_SUCCESS != ui32Status)
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
if (AM_HAL_STATUS_SUCCESS != am_devices_mspi_atxp032_reset(pHandle))
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
//
// Disable and clear the interrupts to start with.
//
ui32Status = am_hal_mspi_interrupt_disable(pFlash->pMspiHandle, 0xFFFFFFFF);
if (AM_HAL_STATUS_SUCCESS != ui32Status)
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
ui32Status = am_hal_mspi_interrupt_clear(pFlash->pMspiHandle, 0xFFFFFFFF);
if (AM_HAL_STATUS_SUCCESS != ui32Status)
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
//
// Disable the MSPI instance.
//
ui32Status = am_hal_mspi_disable(pFlash->pMspiHandle);
if (AM_HAL_STATUS_SUCCESS != ui32Status)
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
if (AM_HAL_STATUS_SUCCESS != am_hal_mspi_power_control(pFlash->pMspiHandle, AM_HAL_SYSCTRL_DEEPSLEEP, false))
{
am_util_stdio_printf("Error - Failed to power on MSPI.\n");
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
//
// Deinitialize the MSPI instance.
//
ui32Status = am_hal_mspi_deinitialize(pFlash->pMspiHandle);
if (AM_HAL_STATUS_SUCCESS != ui32Status)
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
// Free this device handle
pFlash->bOccupied = false;
//
// Clear the Flash Caching.
//
#if AM_CMSIS_REGS
CACHECTRL->CACHECFG = 0;
#else // AM_CMSIS_REGS
AM_REG(CACHECTRL, CACHECFG) = 0;
#endif // AM_CMSIS_REGS
//
// Return the status.
//
return AM_DEVICES_MSPI_ATXP032_STATUS_SUCCESS;
}
//*****************************************************************************
//
//! @brief Reads the current status of the external flash
//!
//! @param ui32DeviceNumber - Device number of the external flash
//!
//! This function reads the device ID register of the external flash, and returns
//! the result as an 32-bit unsigned integer value.
//!
//! @return 32-bit status
//
//*****************************************************************************
uint32_t
am_devices_mspi_atxp032_reset(void *pHandle)
{
uint32_t ui32PIOBuffer[32] = {0};
//
// Return the device to SPI mode.
//
if (AM_HAL_STATUS_SUCCESS != am_devices_mspi_atxp032_command_write(pHandle, AM_DEVICES_ATXP032_RETURN_TO_SPI_MODE, false, 0, ui32PIOBuffer, 0))
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
return AM_DEVICES_MSPI_ATXP032_STATUS_SUCCESS;
}
//*****************************************************************************
//
//! @brief Reads the ID of the external flash and returns the value.
//!
//! @param pDeviceID - Pointer to the return buffer for the Device ID.
//!
//! This function reads the device ID register of the external flash, and returns
//! the result as an 32-bit unsigned integer value.
//!
//! @return 32-bit status
//
//*****************************************************************************
uint32_t
am_devices_mspi_atxp032_id(void *pHandle)
{
uint32_t ui32Status;
uint32_t ui32DeviceID;
//
// Send the command sequence to read the Device ID and return status.
//
uint8_t ui8Response[11];
ui32Status = am_devices_mspi_atxp032_command_read(pHandle, AM_DEVICES_MSPI_ATXP032_READ_ID, false, 0, (uint32_t *)&ui8Response[0], 11);
ui32DeviceID = (ui8Response[7] << 16) | (ui8Response[8] << 8) | ui8Response[9];
if ( ((ui32DeviceID & AM_DEVICES_MSPI_ATXP032_ID_MASK) == AM_DEVICES_MSPI_ATXP032_ID) &&
(AM_HAL_STATUS_SUCCESS == ui32Status) )
{
return AM_DEVICES_MSPI_ATXP032_STATUS_SUCCESS;
}
else
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
}
//*****************************************************************************
//
//! @brief Reads the current status of the external flash
//!
//! This function reads the status register of the external flash, and returns
//! the result as an 8-bit unsigned integer value. The processor will block
//! during the data transfer process, but will return as soon as the status
//! register had been read.
//!
//! Macro definitions for interpreting the contents of the status register are
//! included in the header file.
//!
//! @return 8-bit status register contents
//
//*****************************************************************************
uint32_t
am_devices_mspi_atxp032_status(void *pHandle, uint32_t *pStatus)
{
uint32_t ui32Status;
//
// Send the command sequence to read the device status.
//
ui32Status = am_devices_mspi_atxp032_command_read(pHandle, AM_DEVICES_MSPI_ATXP032_READ_STATUS, false, 0, pStatus, 1);
if (AM_HAL_STATUS_SUCCESS != ui32Status)
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
return AM_DEVICES_MSPI_ATXP032_STATUS_SUCCESS;
}
uint32_t
am_devices_mspi_atxp032_read_adv(void *pHandle, uint8_t *pui8RxBuffer,
uint32_t ui32ReadAddress,
uint32_t ui32NumBytes,
uint32_t ui32PauseCondition,
uint32_t ui32StatusSetClr,
am_hal_mspi_callback_t pfnCallback,
void *pCallbackCtxt)
{
am_hal_mspi_dma_transfer_t Transaction;
uint32_t ui32Status;
am_devices_mspi_atxp032_t *pFlash = (am_devices_mspi_atxp032_t *)pHandle;
// Set the DMA priority
Transaction.ui8Priority = 1;
// Set the transfer direction to RX (Read)
Transaction.eDirection = AM_HAL_MSPI_RX;
// Set the transfer count in bytes.
Transaction.ui32TransferCount = ui32NumBytes;
// Set the address to read data from.
Transaction.ui32DeviceAddress = ui32ReadAddress;
// Set the target SRAM buffer address.
Transaction.ui32SRAMAddress = (uint32_t)pui8RxBuffer;
// Clear the CQ stimulus.
Transaction.ui32PauseCondition = ui32PauseCondition;
// Clear the post-processing
Transaction.ui32StatusSetClr = ui32StatusSetClr;
// Check the transaction status.
ui32Status = am_hal_mspi_nonblocking_transfer(pFlash->pMspiHandle, &Transaction,
AM_HAL_MSPI_TRANS_DMA, pfnCallback, pCallbackCtxt);
if (AM_HAL_STATUS_SUCCESS != ui32Status)
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
//
// Return the status.
//
return AM_DEVICES_MSPI_ATXP032_STATUS_SUCCESS;
}
//*****************************************************************************
//
//! @brief Reads the contents of the external flash into a buffer.
//!
//! @param pui8RxBuffer - Buffer to store the received data from the flash
//! @param ui32ReadAddress - Address of desired data in external flash
//! @param ui32NumBytes - Number of bytes to read from external flash
//!
//! This function reads the external flash at the provided address and stores
//! the received data into the provided buffer location. This function will
//! only store ui32NumBytes worth of data.
//
//! @return 32-bit status
//
//*****************************************************************************
uint32_t
am_devices_mspi_atxp032_read(void *pHandle, uint8_t *pui8RxBuffer,
uint32_t ui32ReadAddress,
uint32_t ui32NumBytes,
bool bWaitForCompletion)
{
am_hal_mspi_dma_transfer_t Transaction;
uint32_t ui32Status;
am_devices_mspi_atxp032_t *pFlash = (am_devices_mspi_atxp032_t *)pHandle;
// Set the DMA priority
Transaction.ui8Priority = 1;
// Set the transfer direction to RX (Read)
Transaction.eDirection = AM_HAL_MSPI_RX;
// Set the transfer count in bytes.
Transaction.ui32TransferCount = ui32NumBytes;
// Set the address to read data from.
Transaction.ui32DeviceAddress = ui32ReadAddress;
// Set the target SRAM buffer address.
Transaction.ui32SRAMAddress = (uint32_t)pui8RxBuffer;
// Clear the CQ stimulus.
Transaction.ui32PauseCondition = 0;
// Clear the post-processing
Transaction.ui32StatusSetClr = 0;
if (bWaitForCompletion)
{
// Start the transaction.
volatile bool bDMAComplete = false;
ui32Status = am_hal_mspi_nonblocking_transfer(pFlash->pMspiHandle, &Transaction, AM_HAL_MSPI_TRANS_DMA, pfnMSPI_ATXP032_Callback, (void *)&bDMAComplete);
// Check the transaction status.
if (AM_HAL_STATUS_SUCCESS != ui32Status)
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
// Wait for DMA Complete or Timeout
for (uint32_t i = 0; i < AM_DEVICES_MSPI_ATXP032_TIMEOUT; i++)
{
if (bDMAComplete)
{
break;
}
//
// Call the BOOTROM cycle function to delay for about 1 microsecond.
//
am_hal_flash_delay( FLASH_CYCLES_US(1) );
}
// Check the status.
if (!bDMAComplete)
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
}
else
{
// Check the transaction status.
ui32Status = am_hal_mspi_nonblocking_transfer(pFlash->pMspiHandle, &Transaction,
AM_HAL_MSPI_TRANS_DMA, NULL, NULL);
if (AM_HAL_STATUS_SUCCESS != ui32Status)
{
return AM_DEVICES_MSPI_ATXP032_STATUS_ERROR;
}
}
//
// Return the status.
//
return AM_DEVICES_MSPI_ATXP032_STATUS_SUCCESS;
}
//*****************************************************************************
//
//! @brief Reads the contents of the external flash into a buffer.
//!
//! @param pui8RxBuffer - Buffer to store the received data from the flash
//! @param ui32ReadAddress - Address of desired data in external flash
//! @param ui32NumBytes - Number of bytes to read from external flash
//!
//! This function reads the external flash at the provided address and stores
//! the received data into the provided buffer location. This function will
//! only store ui32NumBytes worth of data.
//
//! @return 32-bit status
//