-
Notifications
You must be signed in to change notification settings - Fork 0
/
spi_flash.c
2484 lines (2269 loc) · 85.7 KB
/
spi_flash.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
//*****************************************************************************
//
// spi_flash.c - Driver for a SPI flash that supports the "Intel" SPI flash
// command set, capable of utilizing Bi-SPI and Quad-SPI.
//
// Copyright (c) 2012-2020 Texas Instruments Incorporated. All rights reserved.
// Software License Agreement
//
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
//
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 2.2.0.295 of the Tiva Utility Library.
//
//*****************************************************************************
#include <stdint.h>
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_ssi.h"
#include "inc/hw_types.h"
#include "inc/hw_udma.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/ssi.h"
#include "driverlib/udma.h"
#include "utils/spi_flash.h"
//*****************************************************************************
//
//! \addtogroup spi_flash_api
//! @{
//
//*****************************************************************************
//*****************************************************************************
//
// The commands that can be sent to the SPI flash. This is the "generic"
// command set that is supported by a wide number of SPI flashes.
//
//*****************************************************************************
#define CMD_WRSR 0x01 // Write status register
#define CMD_PP 0x02 // Page program
#define CMD_READ 0x03 // Read data
#define CMD_WRDI 0x04 // Disable writes
#define CMD_RDSR 0x05 // Read status register
#define CMD_WREN 0x06 // Enable writes
#define CMD_FREAD 0x0b // Fast read data
#define CMD_SE 0x20 // Sector erase (4K)
#define CMD_DREAD 0x3b // 1 in 2 out read data
#define CMD_BE32 0x52 // Block erase (32K)
#define CMD_QREAD 0x6b // 1 in 4 out read data
#define CMD_RDID 0x9f // Read JEDEC ID
#define CMD_CE 0xc7 // Chip erase
#define CMD_BE64 0xd8 // Block erase (64K)
//*****************************************************************************
//
// The states for the SPI flash interrupt handler state machine.
//
//*****************************************************************************
#define STATE_IDLE 0
#define STATE_CMD 1
#define STATE_ADDR1 2
#define STATE_ADDR2 3
#define STATE_ADDR3 4
#define STATE_READ_DUMMY 5
#define STATE_READ_DATA_SETUP 6
#define STATE_READ_DATA 7
#define STATE_READ_DATA_DMA 8
#define STATE_READ_DATA_END 9
#define STATE_WRITE_DATA_SETUP 10
#define STATE_WRITE_DATA 11
#define STATE_WRITE_DATA_DMA 12
#define STATE_WRITE_DATA_END 13
//*****************************************************************************
//
//! Handles SSI module interrupts for the SPI flash driver.
//!
//! \param pState is a pointer to the SPI flash driver instance data.
//!
//! This function handles SSI module interrupts that are generated as a result
//! of SPI flash driver operations. This must be called by the application in
//! response to the SSI module interrupt when using the SPIFlashxxxNonBlocking
//! APIs.
//!
//! \return Returns \b SPI_FLASH_IDLE if there is no transfer in progress,
//! \b SPI_FLASH_WORKING is the requested transfer is still in progress, or
//! \b SPI_FLASH_DONE if the requested transfer has completed.
//
//*****************************************************************************
uint32_t
SPIFlashIntHandler(tSPIFlashState *pState)
{
uint32_t ui32Data, ui32Count;
//
// Set the write count to four. This is the maximum number of bytes that
// will be written into the SSI transmit FIFO in the interrupt handler.
// Writing more might be possible but makes the latency of handling future
// SSI interrupt critical to preventing receive FIFO overruns.
//
ui32Count = 4;
//
// Get the set of asserted and unmasked SSI module interrupts. Only some
// of these are directly handled; the others are implicitly handled via the
// operation of the state machine.
//
ui32Data = HWREG(pState->ui32Base + SSI_O_MIS);
//
// See if the uDMA transmit complete interrupt has asserted.
//
if(ui32Data & SSI_MIS_DMATXMIS)
{
//
// Determine the size of the uDMA transfer based on the number of bytes
// left to write.
//
if(pState->ui32WriteCount > 1024)
{
//
// There are more than 1024 bytes left to transfer, so the uDMA
// transfer that just completed was for a full 1024 bytes.
//
pState->ui32WriteCount -= 1024;
//
// If a page program is being performed, then the data buffer
// pointer needs to be incremented as well.
//
if(pState->ui16Cmd == CMD_PP)
{
//
// Increment the data buffer pointer.
//
pState->pui8Buffer += 1024;
//
// See if there is more than one byte left to transfer.
//
if(pState->ui32WriteCount > 1)
{
//
// Configure the uDMA to transmit the next portion of the
// data buffer.
//
uDMAChannelTransferSet(pState->ui32TxChannel,
UDMA_MODE_BASIC,
pState->pui8Buffer,
(void *)(pState->ui32Base +
SSI_O_DR),
(pState->ui32WriteCount > 1024) ?
1024 : pState->ui32WriteCount - 1);
//
// Enable the uDMA transmit channel.
//
uDMAChannelEnable(pState->ui32TxChannel);
}
}
}
else
{
//
// There are 1024 or less bytes left to transfer, so the uDMA
// transfer that just copmleted was for one less than the remaining
// transfer count. If a page program is being performed, then the
// data buffer pointer needs to be incremented.
//
if(pState->ui16Cmd == CMD_PP)
{
pState->pui8Buffer += (pState->ui32WriteCount - 1);
}
//
// Set the remaining transfer count to 1. The final byte will be
// transferred with PIO since the end of frame flag needs to be set
// first.
//
pState->ui32WriteCount = 1;
}
//
// Clear the uDMA transmit complete interrupt.
//
HWREG(pState->ui32Base + SSI_O_ICR) = SSI_ICR_DMATXIC;
}
//
// See if the uDMA receive complete interrupt has asserted.
//
if(ui32Data & SSI_MIS_DMARXMIS)
{
//
// Determine the size of the uDMA transfer based on the number of bytes
// left to read.
//
if(pState->ui32ReadCount >= 1024)
{
//
// There are 1024 or more bytes left to transfer, so the uDMA
// transfer that just completed was for a full 1024 bytes.
//
pState->ui32ReadCount -= 1024;
if(pState->ui32WriteCount != 0)
{
pState->ui32WriteCount -= 1024;
}
//
// The data buffer pointer needs to be incremented as well.
//
pState->pui8Buffer += 1024;
//
// See if there is additional data to transfer.
//
if(pState->ui32ReadCount != 0)
{
//
// Configure the transmit uDMA if there is more than one byte
// left to write.
//
if(pState->ui32WriteCount > 1)
{
//
// Configure the uDMA to transmit the next portion of the
// data buffer.
//
uDMAChannelTransferSet(pState->ui32TxChannel,
UDMA_MODE_BASIC, pState->pui8Buffer,
(void *)(pState->ui32Base +
SSI_O_DR),
(pState->ui32WriteCount > 1024) ?
1024 : pState->ui32WriteCount - 1);
//
// Enable the uDMA transmit channel.
//
uDMAChannelEnable(pState->ui32TxChannel);
}
//
// Configure the uDMA to receive the next portion of the data
// buffer.
//
uDMAChannelTransferSet(pState->ui32RxChannel, UDMA_MODE_BASIC,
(void *)(pState->ui32Base + SSI_O_DR),
pState->pui8Buffer,
(pState->ui32ReadCount >= 1024) ?
1024 : pState->ui32ReadCount);
//
// Enable the uDMA receive channel.
//
uDMAChannelEnable(pState->ui32RxChannel);
//
// If this is the final receive uDMA buffer and there is a
// transmit uDMA buffer associated, enable the DMA transmit
// interrupt.
//
if((pState->ui32ReadCount <= 1024) &&
(pState->ui32WriteCount > 1))
{
HWREG(pState->ui32Base + SSI_O_ICR) = SSI_ICR_DMATXIC;
HWREG(pState->ui32Base + SSI_O_IM) = SSI_IM_DMATXIM;
}
}
}
else
{
//
// There are less than 1024 bytes left to transfer, so the uDMA
// transfer that copmleted was for the remaining transfer count.
//
pState->ui32ReadCount = 0;
}
//
// Clear the uDMA receive complete interrupt.
//
HWREG(pState->ui32Base + SSI_O_ICR) = SSI_ICR_DMARXIC;
}
//
// Drain the receive FIFO is not using uDMA.
//
if(!pState->bUseDMA)
{
//
// Loop while there is more data in the receive FIFO and more data to
// be read.
//
while((pState->ui32ReadCount != 0) &&
(MAP_SSIDataGetNonBlocking(pState->ui32Base, &ui32Data) != 0))
{
//
// Save this byte into the data buffer.
//
*(pState->pui8Buffer)++ = ui32Data & 0xff;
//
// Decrement the read count.
//
pState->ui32ReadCount--;
}
}
//
// The SPI flash state machine. Loop forever; the state machine will
// explicitly return to the caller when there is no further work that can
// be done without stalling.
//
while(1)
{
//
// Determine the current state.
//
switch(pState->ui16State)
{
//
// The state machine is idle.
//
case STATE_IDLE:
{
//
// Return indicating that the state machine is idle. This
// should never happen since no further interrupts should occur
// once the transfer has completed and the state machine goes
// into the idle state.
//
return(SPI_FLASH_IDLE);
}
//
// The state machine is in the command state.
//
case STATE_CMD:
{
//
// Set the SSI module into write-only mode.
//
MAP_SSIAdvModeSet(pState->ui32Base, SSI_ADV_MODE_WRITE);
//
// Attempt to write the command byte into the FIFO.
//
if(ui32Count == 0)
{
return(SPI_FLASH_WORKING);
}
if(MAP_SSIDataPutNonBlocking(pState->ui32Base,
pState->ui16Cmd) == 0)
{
//
// The command byte could not be written, so return
// indicating that the transfer is still in progress.
//
return(SPI_FLASH_WORKING);
}
else
{
//
// The command byte has been written, so move to the first
// address byte state.
//
pState->ui16State = STATE_ADDR1;
//
// Decrement the count of bytes that have been written.
//
ui32Count--;
}
//
// Done with this state.
//
break;
}
//
// The state machine is in the first address byte state.
//
case STATE_ADDR1:
{
//
// Attempt to write the first address byte into the FIFO.
//
if(ui32Count == 0)
{
return(SPI_FLASH_WORKING);
}
if(MAP_SSIDataPutNonBlocking(pState->ui32Base,
(pState->ui32Addr >> 16) &
0xff) == 0)
{
//
// The first address byte could not be written, so return
// indicating that the transfer is still in progress.
//
return(SPI_FLASH_WORKING);
}
else
{
//
// The first address byte has been written, so move to the
// second address byte state.
//
pState->ui16State = STATE_ADDR2;
//
// Decrement the count of bytes that have been written.
//
ui32Count--;
}
//
// Done with this state.
//
break;
}
//
// The state machine is in the second address byte state.
//
case STATE_ADDR2:
{
//
// Attempt to write the second address byte into the FIFO.
//
if(ui32Count == 0)
{
return(SPI_FLASH_WORKING);
}
if(MAP_SSIDataPutNonBlocking(pState->ui32Base,
(pState->ui32Addr >> 8) & 0xff) ==
0)
{
//
// The second address byte could not be written, so return
// indicating that the transfer is still in progress.
//
return(SPI_FLASH_WORKING);
}
else
{
//
// The second address byte has been written, so move to the
// third address byte state.
//
pState->ui16State = STATE_ADDR3;
//
// Decrement the count of bytes that have been written.
//
ui32Count--;
}
//
// Done with this state.
//
break;
}
//
// The state machine is in the third address byte state.
//
case STATE_ADDR3:
{
//
// Attempt to write the third address byte into the FIFO.
//
if(ui32Count == 0)
{
return(SPI_FLASH_WORKING);
}
if(MAP_SSIDataPutNonBlocking(pState->ui32Base,
pState->ui32Addr & 0xff) == 0)
{
//
// The third address byte could not be written, so return
// indicating that the transfer is still in progress.
//
return(SPI_FLASH_WORKING);
}
else
{
//
// The third address byte has been written, so determine
// the next state based on the command byte.
//
if(pState->ui16Cmd == CMD_PP)
{
//
// A page program is being performed, so move to the
// write data setup state.
//
pState->ui16State = STATE_WRITE_DATA_SETUP;
}
else if(pState->ui16Cmd == CMD_READ)
{
//
// A read is being performed, so move to the read data
// setup state.
//
pState->ui16State = STATE_READ_DATA_SETUP;
}
else
{
//
// The other forms of read (fast read, dual read, and
// quad read) all require a dummy byte. Move to the
// dummy byte state.
//
pState->ui16State = STATE_READ_DUMMY;
}
//
// Decrement the count of bytes that have been written.
//
ui32Count--;
}
//
// Done with this state.
//
break;
}
//
// The state machine is in the dummy byte state.
//
case STATE_READ_DUMMY:
{
//
// Attempt to write the dummy byte into the FIFO.
//
if(ui32Count == 0)
{
return(SPI_FLASH_WORKING);
}
if(MAP_SSIDataPutNonBlocking(pState->ui32Base, 0) == 0)
{
//
// THe dummy byte could not be written, so return
// indicating that the transfer is still in progress.
//
return(SPI_FLASH_WORKING);
}
else
{
//
// The dummy byte has been written, so move to the read
// data setup state.
//
pState->ui16State = STATE_READ_DATA_SETUP;
//
// Decrement the count of bytes that have been written.
//
ui32Count--;
}
//
// Done with this state.
//
break;
}
//
// The state machine is in the read data setup state.
//
case STATE_READ_DATA_SETUP:
{
//
// Set the SSI module into the appropriate mode based on the
// command byte.
//
if(pState->ui16Cmd == CMD_DREAD)
{
//
// Bi-SPI read mode is used for the dual read command.
//
MAP_SSIAdvModeSet(pState->ui32Base, SSI_ADV_MODE_BI_READ);
}
else if(pState->ui16Cmd == CMD_QREAD)
{
//
// Quad-SPI read mode is used for the quad read command.
//
MAP_SSIAdvModeSet(pState->ui32Base,
SSI_ADV_MODE_QUAD_READ);
}
else
{
//
// Advanced read/write mode is used for the read and fast
// read commands.
//
MAP_SSIAdvModeSet(pState->ui32Base,
SSI_ADV_MODE_READ_WRITE);
}
//
// See if a single byte is being transferred.
//
if(pState->ui32ReadCount == 1)
{
//
// Disable the use of uDMA.
//
pState->bUseDMA = false;
//
// Move to the read data end state to transfer the single
// byte. This uses PIO even if uDMA has been requested.
//
pState->ui16State = STATE_READ_DATA_END;
}
//
// See if uDMA has been requested for this transfer.
//
else if(!pState->bUseDMA || (pState->ui32ReadCount < 4))
{
//
// Disable the use of uDMA.
//
pState->bUseDMA = false;
//
// Move to the read data state.
//
pState->ui16State = STATE_READ_DATA;
}
//
// This transfer should use uDMA.
//
else
{
//
// If the transfer is larger than 1024 bytes, enable the
// uDMA receive complete interrupt which will be used to
// move to the next block of the transfer. Otherwise,
// enable the uDMA transmit complete interrupt which will
// be used to complete the transaction.
//
if(pState->ui32ReadCount > 1024)
{
HWREG(pState->ui32Base + SSI_O_IM) = SSI_IM_DMARXIM;
}
else
{
HWREG(pState->ui32Base + SSI_O_IM) = SSI_IM_DMATXIM;
}
//
// Disable the uDMA channels.
//
HWREG(UDMA_ENACLR) = ((1 << pState->ui32TxChannel) |
(1 << pState->ui32RxChannel));
//
// Configure the attributes for the transmit uDMA channel.
//
HWREG(UDMA_USEBURSTSET) = ((1 << pState->ui32TxChannel) |
(1 << pState->ui32RxChannel));
HWREG(UDMA_ALTCLR) = ((1 << pState->ui32TxChannel) |
(1 << pState->ui32RxChannel));
HWREG(UDMA_PRIOCLR) = 1 << pState->ui32TxChannel;
HWREG(UDMA_PRIOSET) = 1 << pState->ui32RxChannel;
HWREG(UDMA_REQMASKCLR) = ((1 << pState->ui32TxChannel) |
(1 << pState->ui32RxChannel));
//
// Configure the control parameters of the uDMA channels.
//
uDMAChannelControlSet(pState->ui32TxChannel,
UDMA_SRC_INC_NONE |
UDMA_DST_INC_NONE |
UDMA_SIZE_8 | UDMA_ARB_2);
uDMAChannelControlSet(pState->ui32RxChannel,
UDMA_SRC_INC_NONE |
UDMA_DST_INC_8 |
UDMA_SIZE_8 | UDMA_ARB_4);
//
// Configure the uDMA receive channel to transfer the first
// portion of the data buffer.
//
uDMAChannelTransferSet(pState->ui32RxChannel,
UDMA_MODE_BASIC,
(void *)(pState->ui32Base +
SSI_O_DR),
pState->pui8Buffer,
(pState->ui32ReadCount >= 1024) ?
1024 : pState->ui32ReadCount);
//
// Enable the uDMA receive channel.
//
uDMAChannelEnable(pState->ui32RxChannel);
//
// Configure the uDMA channel to transfer the dummy bytes
// for the first portion of the data buffer. The last
// dummy byte will not be included since it must be treated
// special.
//
uDMAChannelTransferSet(pState->ui32TxChannel,
UDMA_MODE_BASIC,
pState->pui8Buffer,
(void *)(pState->ui32Base +
SSI_O_DR),
(pState->ui32WriteCount > 1024) ?
1024 : pState->ui32WriteCount - 1);
//
// Enable the uDMA transmit channel.
//
uDMAChannelEnable(pState->ui32TxChannel);
//
// Clear any previously pending uDMA completion interrupt.
//
HWREG(pState->ui32Base + SSI_O_ICR) = SSI_ICR_DMARXIC;
//
// Enable uDMA transmit and receive in the SSI module.
//
MAP_SSIDMAEnable(pState->ui32Base,
SSI_DMA_TX | SSI_DMA_RX);
//
// Move to the uDMA data read state.
//
pState->ui16State = STATE_READ_DATA_DMA;
}
//
// Done with this state.
//
break;
}
//
// The state machine is in the read data state.
//
case STATE_READ_DATA:
{
//
// Loop while there is more than one byte left to write.
//
while(pState->ui32WriteCount != 1)
{
//
// Dummy bytes are written into the FIFO in order to
// trigger the read operation. Attempt to write another
// dummy byte into the FIFO.
//
if(ui32Count == 0)
{
return(SPI_FLASH_WORKING);
}
if(MAP_SSIDataPutNonBlocking(pState->ui32Base, 0) == 0)
{
//
// The dummy byte could not be written, so return
// indicating that the transfer is still in progress.
//
return(SPI_FLASH_WORKING);
}
//
// Decrement the count of dummy bytes to write.
//
pState->ui32WriteCount--;
//
// Decrement the count of bytes that have been written.
//
ui32Count--;
}
//
// Move to the read data end state.
//
pState->ui16State = STATE_READ_DATA_END;
//
// Done with this state.
//
break;
}
//
// The state machine is in the uDMA read data state.
//
case STATE_READ_DATA_DMA:
{
//
// See if the write count is greater than one.
//
if(pState->ui32WriteCount > 1)
{
//
// Return indicating that the transfer is still in
// progress.
//
return(SPI_FLASH_WORKING);
}
//
// Disable uDMA transmit in the SSI module.
//
MAP_SSIDMADisable(pState->ui32Base, SSI_DMA_TX);
//
// Enable the uDMA receive done and FIFO transmit interrupt.
//
HWREG(pState->ui32Base + SSI_O_IM) =
SSI_IM_DMARXIM | SSI_IM_TXIM;
//
// Move to the read data end state.
//
pState->ui16State = STATE_READ_DATA_END;
//
// Done with this state.
//
break;
}
//
// The state machine is in the data read end state.
//
case STATE_READ_DATA_END:
{
//
// See if the final dummy byte still needs to be written.
//
if(pState->ui32WriteCount != 0)
{
//
// Attempt to write the final dummy byte into the FIFO and
// mark it as the end of the frame.
//
if(ui32Count == 0)
{
return(SPI_FLASH_WORKING);
}
if(MAP_SSIAdvDataPutFrameEndNonBlocking(pState->ui32Base,
0) == 0)
{
//
// The dummy byte could not be written, so return
// indicating that the transfer is still in progress.
//
return(SPI_FLASH_WORKING);
}
//
// The write portion of the transfer has completed.
//
pState->ui32WriteCount = 0;
//
// Disable the transmit interrupt now that the write
// write portion of the transfer has completed.
//
HWREG(pState->ui32Base + SSI_O_IM) &= ~(SSI_IM_TXIM);
}
//
// Return indicating that the transfer is still in progress if
// there are still data bytes to be read.
//
if(pState->ui32ReadCount != 0)
{
return(SPI_FLASH_WORKING);
}
//
// Disable uDMA receive in the SSI module.
//
MAP_SSIDMADisable(pState->ui32Base, SSI_DMA_RX);
//
// The transfer is complete, so disable all interrupts.
//
HWREG(pState->ui32Base + SSI_O_IM) = 0;
//
// Move to the idle state.
//
pState->ui16State = STATE_IDLE;
//
// Return indicating that the transfer has completed.
//
return(SPI_FLASH_DONE);
}
//
// The state machine is in the write data setup state.
//
case STATE_WRITE_DATA_SETUP:
{
//
// See if a single data byte is being transferred.
//
if(pState->ui32WriteCount == 1)
{
//
// Disable the use of uDMA.
//
pState->bUseDMA = false;
//
// Move to the write data end state to transfer the single
// byte. This uses PIO even if uDMA has been requested.
//
pState->ui16State = STATE_WRITE_DATA_END;
}
//
// See if uDMA has been requested for this transfer.
//
else if(!pState->bUseDMA || (pState->ui32WriteCount < 4))
{
//
// Disable the use of uDMA.
//
pState->bUseDMA = false;
//
// uDMA is not being used, so move to the write data state.
//
pState->ui16State = STATE_WRITE_DATA;
}
//
// This transfer should use uDMA.
//
else
{
//
// Enable the uDMA transmit complete interrupt.
//
HWREG(pState->ui32Base + SSI_O_IM) = SSI_IM_DMATXIM;
//
// Disable the transmit uDMA channel.
//
HWREG(UDMA_ENACLR) = 1 << pState->ui32TxChannel;
//
// Configure the attributes for the transmit uDMA channel.
//
HWREG(UDMA_USEBURSTSET) = 1 << pState->ui32TxChannel;
HWREG(UDMA_ALTCLR) = 1 << pState->ui32TxChannel;
HWREG(UDMA_PRIOCLR) = 1 << pState->ui32TxChannel;
HWREG(UDMA_REQMASKCLR) = 1 << pState->ui32TxChannel;
//
// Configure the control parameters of the uDMA channel.
//
uDMAChannelControlSet(pState->ui32TxChannel,
UDMA_SRC_INC_8 |
UDMA_DST_INC_NONE |
UDMA_SIZE_8 | UDMA_ARB_4);
//
// Configure the uDMA channel to transfer the next portion
// of the data buffer. The last byte in the buffer will
// not be included since it must be treated special.
//
uDMAChannelTransferSet(pState->ui32TxChannel,
UDMA_MODE_BASIC,
pState->pui8Buffer,
(void *)(pState->ui32Base +
SSI_O_DR),
(pState->ui32WriteCount > 1024) ?
1024 : pState->ui32WriteCount - 1);