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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
|
/*
******************************************************************************
* @file HAL_Spi.c
* @version V1.0.0
* @date 2020
* @brief SPI HAL module driver.
* This file provides firmware functions to manage the following
* functionalities of the Serial Peripheral Interface (SPI) peripheral.
* @ Initialization and de-initialization functions
* @ IO operation functions
* @ Peripheral Control functions
******************************************************************************
*/
#include "ACM32Fxx_HAL.h"
#define SPI_RX_TIMEOUT 2000
#define SPI_TX_DMA_TIMEOUT 2000
volatile uint32_t lu32_ReceiveTimeOut = SPI_RX_TIMEOUT;
volatile uint32_t lu32_TX_DMA_TimeOut = SPI_TX_DMA_TIMEOUT;
/************************************************************************
* function : HAL_SPI_IRQHandler
* Description: This function handles SPI interrupt request.
* input : hspi : pointer to a SPI_HandleTypeDef structure that contains
* the configuration information for SPI module
************************************************************************/
__weak void HAL_SPI_IRQHandler(SPI_HandleTypeDef *hspi)
{
uint8_t lu8_TempValue = 0;
uint32_t lu32_Length = 0;
/*
NOTE : This function should be modified by the user.
*/
if ( (hspi->Instance->STATUS & SPI_STATUS_RX_NOT_EMPTY) && ((hspi->Instance->IE) & SPI_STATUS_RX_NOT_EMPTY) )
{
/* In master mode */
if (hspi->Instance->CTL & SPI_CTL_MST_MODE)
{
while (hspi->Instance->STATUS & SPI_STATUS_RX_NOT_EMPTY)
{
hspi->Rx_Buffer[hspi->Rx_Count++] = hspi->Instance->DAT;
if (hspi->Rx_Count >= hspi->Rx_Size)
{
/* Wait Transmit Done */
while (!(hspi->Instance->STATUS & SPI_STATUS_RX_BATCH_DONE));
/* Clear Batch Done Flag */
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_BATCH_DONE);
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
/* Rx Disable */
hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
/* Receive End */
hspi->Instance->CS &= (~SPI_CS_CS0);
/* Disable Rx Not Empty Interrupt */
CLEAR_BIT(hspi->Instance->IE, SPI_STATUS_RX_NOT_EMPTY);
if(hspi->Instance == SPI1)
NVIC_ClearPendingIRQ(SPI1_IRQn);
else if(hspi->Instance == SPI2)
NVIC_ClearPendingIRQ(SPI2_IRQn);
else if(hspi->Instance == SPI3)
NVIC_ClearPendingIRQ(SPI3_IRQn);
else if(hspi->Instance == SPI4)
NVIC_ClearPendingIRQ(SPI4_IRQn);
/* Clear Batch Done Flag */
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_BATCH_DONE);
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
/* Set machine is DILE */
hspi->RxState = SPI_RX_STATE_IDLE;
}
}
}
/* In Slave mode */
else
{
while ((hspi->Rx_Count < hspi->Rx_Size) && (lu32_ReceiveTimeOut > 0) )
{
if (hspi->Instance->STATUS & SPI_STATUS_RX_NOT_EMPTY)
{
hspi->Rx_Buffer[hspi->Rx_Count++] = hspi->Instance->DAT;
lu32_ReceiveTimeOut = SPI_RX_TIMEOUT; //If recieve data, Reset the timeout value
}
else
{
lu32_ReceiveTimeOut--;
}
}
/* Rx Disable */
hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
/* Disable Rx Not Empty Interrupt */
CLEAR_BIT(hspi->Instance->IE, SPI_STATUS_RX_NOT_EMPTY);
if(hspi->Instance == SPI1)
NVIC_ClearPendingIRQ(SPI1_IRQn);
else if(hspi->Instance == SPI2)
NVIC_ClearPendingIRQ(SPI2_IRQn);
else if(hspi->Instance == SPI3)
NVIC_ClearPendingIRQ(SPI3_IRQn);
else if(hspi->Instance == SPI4)
NVIC_ClearPendingIRQ(SPI4_IRQn);
/* Clear Batch Done Flag */
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_BATCH_DONE);
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
/* Set machine is DILE */
hspi->RxState = SPI_RX_STATE_IDLE;
}
}
if ( (hspi->Instance->STATUS & SPI_STATUS_TX_FIFO_HALF_EMPTY) && ((hspi->Instance->IE) & SPI_IE_TX_FIFO_HALF_EMPTY_EN) )
{
while (hspi->Tx_Count < hspi->Tx_Size)
{
if (!(hspi->Instance->STATUS & SPI_STATUS_TX_FIFO_FULL))
{
hspi->Instance->DAT = hspi->Tx_Buffer[hspi->Tx_Count++];
}
else
{
break;
}
}
/* Clear Tx FIFO half empty Flag */
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_FIFO_HALF_EMPTY);
if(hspi->Tx_Count == hspi->Tx_Size)
{
/* Disable Tx FIFO half empty Interrupt */
CLEAR_BIT(hspi->Instance->IE, SPI_IE_TX_FIFO_HALF_EMPTY_EN);
}
}
if ((hspi->Instance->STATUS & SPI_STATUS_TX_BATCH_DONE) && ((hspi->Instance->IE) & SPI_IE_TX_BATCH_DONE_EN) )
{
/* Clear Batch Done Flag */
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_BATCH_DONE);
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
/* Disable TX Batch Done Interrupt */
CLEAR_BIT(hspi->Instance->IE, SPI_STATUS_TX_BATCH_DONE);
/* Disable Tx FIFO half empty Interrupt */
CLEAR_BIT(hspi->Instance->IE, SPI_IE_TX_FIFO_HALF_EMPTY_EN);
if(hspi->Instance == SPI1)
NVIC_ClearPendingIRQ(SPI1_IRQn);
else if(hspi->Instance == SPI2)
NVIC_ClearPendingIRQ(SPI2_IRQn);
else if(hspi->Instance == SPI3)
NVIC_ClearPendingIRQ(SPI3_IRQn);
else if(hspi->Instance == SPI4)
NVIC_ClearPendingIRQ(SPI4_IRQn);
lu32_TX_DMA_TimeOut = SPI_TX_DMA_TIMEOUT;
while (hspi->Instance->STATUS & SPI_STATUS_TX_BUSY)
{
lu32_TX_DMA_TimeOut--;
if(0 == lu32_TX_DMA_TimeOut)
{
break;
}
}
/* Tx Disable */
hspi->Instance->TX_CTL &= (~SPI_TX_CTL_EN);
hspi->Instance->TX_CTL &= (~SPI_TX_CTL_DMA_REQ_EN);
if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
{
/* Transmit End */
hspi->Instance->CS &= (~SPI_CS_CS0);
}
/* Tx Disable */
hspi->Instance->TX_CTL &= (~SPI_TX_CTL_EN);
hspi->TxState = SPI_TX_STATE_IDLE;
}
if ( (hspi->Instance->STATUS & SPI_STATUS_RX_BATCH_DONE) && ((hspi->Instance->IE) & SPI_STATUS_RX_BATCH_DONE) )
{
/* Clear Batch Done Flag */
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_BATCH_DONE);
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
/* Disable RX Batch Done Interrupt */
CLEAR_BIT(hspi->Instance->IE, SPI_STATUS_RX_BATCH_DONE);
if(hspi->Instance == SPI1)
NVIC_ClearPendingIRQ(SPI1_IRQn);
else if(hspi->Instance == SPI2)
NVIC_ClearPendingIRQ(SPI2_IRQn);
else if(hspi->Instance == SPI3)
NVIC_ClearPendingIRQ(SPI3_IRQn);
else if(hspi->Instance == SPI4)
NVIC_ClearPendingIRQ(SPI4_IRQn);
/* Rx Disable */
hspi->Instance->RX_CTL &= (~SPI_RX_CTL_DMA_REQ_EN);
hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
{
/* Receive End */
hspi->Instance->CS &= (~SPI_CS_CS0);
}
hspi->RxState = SPI_RX_STATE_IDLE;
}
}
/************************************************************************
* function : HAL_SPI_MspInit
* Description:
* input : hspi : pointer to a SPI_HandleTypeDef structure that contains
* the configuration information for SPI module
************************************************************************/
__weak void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
{
/*
NOTE : This function should be modified by the user.
*/
/* For Example */
GPIO_InitTypeDef GPIO_Handle;
/* SPI1 */
if (hspi->Instance == SPI1)
{
}
/* SPI2 */
else if (hspi->Instance == SPI2)
{
}
/* SPI3 */
else if (hspi->Instance == SPI3)
{
/* Enable Clock */
System_Module_Enable(EN_SPI3);
/* SPI3 CS PortA Pin15 */
/* SPI3 CLK PortC Pin10 */
/* SPI3 MOSI PortC Pin12 */
/* SPI3 MISO PortC Pin11 */
GPIO_Handle.Pin = GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
GPIO_Handle.Mode = GPIO_MODE_AF_PP;
GPIO_Handle.Pull = GPIO_PULLUP;
GPIO_Handle.Alternate = GPIO_FUNCTION_3;
HAL_GPIO_Init(GPIOC, &GPIO_Handle);
GPIO_Handle.Pin = GPIO_PIN_15;
GPIO_Handle.Mode = GPIO_MODE_AF_PP;
GPIO_Handle.Pull = GPIO_PULLUP;
GPIO_Handle.Alternate = GPIO_FUNCTION_5;
HAL_GPIO_Init(GPIOA, &GPIO_Handle);
if (hspi->Init.X_Mode == SPI_4X_MODE)
{
/* SPI3 IO3 PortC Pin8 */
/* SPI3 IO2 PortC Pin9 */
GPIO_Handle.Pin = GPIO_PIN_8 | GPIO_PIN_9;
GPIO_Handle.Mode = GPIO_MODE_AF_PP;
GPIO_Handle.Pull = GPIO_PULLUP;
GPIO_Handle.Alternate = GPIO_FUNCTION_3;
HAL_GPIO_Init(GPIOC, &GPIO_Handle);
}
/* Clear Pending Interrupt */
NVIC_ClearPendingIRQ(SPI3_IRQn);
/* Enable External Interrupt */
NVIC_EnableIRQ(SPI3_IRQn);
}
}
/************************************************************************
* function : HAL_SPI_MspDeInit
* Description: SPI De-Initialize the SPI clock, GPIO, IRQ.
* input : hspi : pointer to a SPI_HandleTypeDef structure that contains
* the configuration information for SPI module
************************************************************************/
__weak void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
{
/*
NOTE : This function should be modified by the user.
*/
/* For Example */
/* SPI1 */
if (hspi->Instance == SPI1)
{
}
/* SPI2 */
else if (hspi->Instance == SPI2)
{
}
/* SPI3 */
else if (hspi->Instance == SPI3)
{
/* Disable Clock */
System_Module_Disable(EN_SPI3);
/* Reset the used GPIO to analog */
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12);
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_15);
if (hspi->Init.X_Mode == SPI_4X_MODE)
{
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_8 | GPIO_PIN_9);
}
/* Clear Pending Interrupt */
NVIC_ClearPendingIRQ(SPI3_IRQn);
/* Disable External Interrupt */
NVIC_DisableIRQ(SPI3_IRQn);
}
}
/************************************************************************
* function : HAL_SPI_Init
* Description: SPI initial with parameters.
* input : hspi : pointer to a SPI_HandleTypeDef structure that contains
* the configuration information for SPI module
************************************************************************/
HAL_StatusTypeDef HAL_SPI_Init(SPI_HandleTypeDef *hspi)
{
/* Check SPI Parameter */
if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
if (!IS_SPI_ALL_MODE(hspi->Init.SPI_Mode)) return HAL_ERROR;
if (!IS_SPI_WORK_MODE(hspi->Init.SPI_Work_Mode)) return HAL_ERROR;
if (!IS_SPI_X_MODE(hspi->Init.X_Mode)) return HAL_ERROR;
if (!IS_SPI_FIRST_BIT(hspi->Init.First_Bit)) return HAL_ERROR;
if (!IS_SPI_BAUDRATE_PRESCALER(hspi->Init.BaudRate_Prescaler)) return HAL_ERROR;
/* Init the low level hardware : GPIO, CLOCK, NVIC */
HAL_SPI_MspInit(hspi);
/* Automatic change direction */
hspi->Instance->CTL |= (SPI_CTL_IO_MODE);
/* Set SPI Work mode */
if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
{
SET_BIT(hspi->Instance->CTL, SPI_CTL_MST_MODE);
}
else
{
CLEAR_BIT(hspi->Instance->CTL, SPI_CTL_MST_MODE);
hspi->Instance->BATCH = (hspi->Instance->BATCH & (~0x000FFFFFU)) | (1 << 0);
hspi->Instance->TX_CTL |= SPI_TX_CTL_MODE | (0x88 << 8); // dummy data = 0x88
if (hspi->Init.X_Mode != SPI_1X_MODE)
{
hspi->Instance->CTL |= SPI_CTL_SFILTER;
}
/* Slave Alternate Enable */
hspi->Instance->CTL |= SPI_CTL_SLAVE_EN;
/* Slave Mode Enable Rx By Default */
hspi->Instance->RX_CTL |= SPI_RX_CTL_EN;
}
/* Set SPI First Bit */
if (hspi->Init.First_Bit == SPI_FIRSTBIT_LSB)
SET_BIT(hspi->Instance->CTL, SPI_CTL_LSB_FIRST);
else
CLEAR_BIT(hspi->Instance->CTL, SPI_CTL_LSB_FIRST);
/* Set SPI Work Mode */
hspi->Instance->CTL = ((hspi->Instance->CTL) & (~(SPI_CTL_CPHA | SPI_CTL_CPOL))) | (hspi->Init.SPI_Work_Mode);
/* Set SPI X_Mode */
hspi->Instance->CTL = ((hspi->Instance->CTL) & (~SPI_CTL_X_MODE)) | (hspi->Init.X_Mode);
/* Set SPI BaudRate Prescaler */
hspi->Instance->BAUD = ((hspi->Instance->BAUD) & (~0x0000FFFF)) | (hspi->Init.BaudRate_Prescaler);
/* Disable All Interrupt */
hspi->Instance->IE = 0x00000000;
return HAL_OK;
}
/************************************************************************
* function : HAL_SPI_DeInit
* Description: De-Initialize the SPI peripheral.
* input : hspi : pointer to a SPI_HandleTypeDef structure that contains
* the configuration information for SPI module
* return : HAL_StatusTypeDef : HAL status
************************************************************************/
HAL_StatusTypeDef HAL_SPI_DeInit(SPI_HandleTypeDef *hspi)
{
/* Check the SPI handle allocation */
if (hspi == NULL)
{
return HAL_ERROR;
}
/* Check SPI Instance parameter */
if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
hspi->RxState = SPI_RX_STATE_IDLE;
hspi->TxState = SPI_TX_STATE_IDLE;
/* DeInit the low level hardware: GPIO, CLOCK, NVIC... */
HAL_SPI_MspDeInit(hspi);
hspi->Rx_Size = 0;
hspi->Tx_Size = 0;
hspi->Rx_Count = 0;
hspi->Tx_Count = 0;
return HAL_OK;
}
/************************************************************************
* function : HAL_SPI_Transmit
* Description: Transmits an amount of data in blocking mode.
* input : hspi : pointer to a SPI_HandleTypeDef structure that contains
* the configuration information for SPI module
* pData : Pointer to data buffer
* Size : Amount of data to be sent
* Timeout : Transmit Timeout
************************************************************************/
HAL_StatusTypeDef HAL_SPI_Transmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint32_t Size, uint32_t Timeout)
{
uint32_t i;
HAL_StatusTypeDef Status = HAL_OK;
__IO uint32_t uiTimeout;
/* Check SPI Parameter */
if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
if(!Size) return HAL_ERROR;
if (pData == NULL) return HAL_ERROR;
hspi->Tx_Count = 0;
hspi->Tx_Size = Size;
hspi->Tx_Buffer = pData;
uiTimeout = Timeout;
/* Clear Batch Done Flag */
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_BATCH_DONE);
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
/* Clear TX FIFO */
SET_BIT(hspi->Instance->TX_CTL, SPI_TX_CTL_FIFO_RESET);
CLEAR_BIT(hspi->Instance->TX_CTL, SPI_TX_CTL_FIFO_RESET);
/* Set Data Size */
hspi->Instance->BATCH = Size;
/* Tx Enable */
hspi->Instance->TX_CTL |= SPI_TX_CTL_EN;
if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
{
/* Transmit Start */
hspi->Instance->CS |= SPI_CS_CS0;
}
else
{
/* Rx Disable */
hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
}
while(hspi->Tx_Size > 0)
{
/* Wait Tx FIFO Not Full */
while (hspi->Instance->STATUS & SPI_STATUS_TX_FIFO_FULL)
{
if(uiTimeout)
{
uiTimeout--;
if (uiTimeout == 0)
{
Status = HAL_TIMEOUT;
goto End;
}
}
}
hspi->Instance->DAT = hspi->Tx_Buffer[hspi->Tx_Count++];
hspi->Tx_Size--;
uiTimeout = Timeout;
}
if (hspi->Init.SPI_Mode == SPI_MODE_SLAVE)
{
/* Wait Transmit Done */
while (!(hspi->Instance->STATUS & SPI_STATUS_TX_BUSY));
while (hspi->Instance->STATUS & SPI_STATUS_TX_BUSY)
{
if(uiTimeout)
{
uiTimeout--;
if (uiTimeout == 0)
{
Status = HAL_TIMEOUT;
goto End;
}
}
}
}
else
{
/* Wait Transmit Done */
while (!(hspi->Instance->STATUS & SPI_STATUS_TX_BATCH_DONE));
Status = HAL_OK;
}
End:
/* Clear Batch Done Flag */
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_BATCH_DONE);
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
/* Tx Disable */
hspi->Instance->TX_CTL &= (~SPI_TX_CTL_EN);
if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
{
/* Transmit End */
hspi->Instance->CS &= (~SPI_CS_CS0);
}
return Status;
}
/************************************************************************
* function : HAL_SPI_Transmit_DMA
* Description: Transmits an amount of data in blocking mode with DMA.
* input : hspi : pointer to a SPI_HandleTypeDef structure that contains
* the configuration information for SPI module
* pData : Pointer to data buffer
* Size : Amount of data to be sent
************************************************************************/
HAL_StatusTypeDef HAL_SPI_Transmit_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint32_t Size)
{
/* Check SPI Parameter */
if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
/* Rx machine is running */
if (hspi->TxState != SPI_TX_STATE_IDLE)
{
return HAL_ERROR;
}
/* Set machine is Sending */
hspi->TxState = SPI_TX_STATE_SENDING;
/* Clear Batch Done Flag */
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_BATCH_DONE);
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
/* Enable Tx Batch Done Interrupt */
SET_BIT(hspi->Instance->IE, SPI_STATUS_TX_BATCH_DONE);
/* Set Data Size */
hspi->Instance->BATCH = Size;
/* Tx FIFO */
hspi->Instance->TX_CTL &= ~SPI_TX_CTL_DMA_LEVEL;
hspi->Instance->TX_CTL |= SPI_TX_CTL_DMA_LEVEL_0;
/* Tx Enable */
hspi->Instance->TX_CTL |= SPI_TX_CTL_EN;
if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
{
/* Transmit Start */
hspi->Instance->CS |= SPI_CS_CS0;
}
else
{
/* Rx Disable */
hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
}
HAL_DMA_Start(hspi->HDMA_Tx, (uint32_t)pData, (uint32_t)&hspi->Instance->DAT, Size);
hspi->Instance->TX_CTL |= SPI_TX_CTL_DMA_REQ_EN;
return HAL_OK;
}
/************************************************************************
* function : HAL_SPI_Receive
* Description: Receive an amount of data in blocking mode.
* input : hspi : pointer to a SPI_HandleTypeDef structure that contains
* the configuration information for SPI module
* pData : Pointer to data buffer
* Size : Amount of data to be Receive
* Timeout : Receive Timeout
************************************************************************/
HAL_StatusTypeDef HAL_SPI_Receive(SPI_HandleTypeDef *hspi, uint8_t *pData, uint32_t Size, uint32_t Timeout)
{
uint32_t i;
HAL_StatusTypeDef Status = HAL_OK;
__IO uint32_t uiTimeout;
/* Check SPI Parameter */
if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
if (pData == NULL) return HAL_ERROR;
hspi->Rx_Count = 0;
hspi->Rx_Size = Size;
hspi->Rx_Buffer = pData;
uiTimeout = Timeout;
if (hspi->Init.SPI_Mode == SPI_MODE_SLAVE)
{
hspi->Instance->BATCH = 1;
/* Rx Enable */
hspi->Instance->RX_CTL |= SPI_RX_CTL_EN;
while ( hspi->Rx_Size > 0)
{
while (READ_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_FIFO_EMPTY))
{
if(uiTimeout)
{
uiTimeout--;
if (uiTimeout == 0)
{
/* Rx Disable */
hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
return HAL_TIMEOUT;
}
}
}
hspi->Rx_Buffer[hspi->Rx_Count++] = hspi->Instance->DAT;
hspi->Rx_Size--;
uiTimeout = Timeout;
}
/* Rx Disable */
hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
return HAL_OK;
}
/* Clear Batch Done Flag */
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_BATCH_DONE);
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
/* Set Data Size */
hspi->Instance->BATCH = Size;
/* Rx Enable */
hspi->Instance->RX_CTL |= SPI_RX_CTL_EN;
/* Receive Start */
hspi->Instance->CS |= SPI_CS_CS0;
while(hspi->Rx_Size > 0)
{
/* have no timeout */
if (uiTimeout == 0)
{
/* Wait Rx FIFO Not Empty */
while (hspi->Instance->STATUS & SPI_STATUS_RX_FIFO_EMPTY);
}
else
{
while (hspi->Instance->STATUS & SPI_STATUS_RX_FIFO_EMPTY)
{
if (uiTimeout-- == 0)
{
Status = HAL_TIMEOUT;
goto End;
}
}
}
hspi->Rx_Buffer[hspi->Rx_Count++] = hspi->Instance->DAT;
hspi->Rx_Size--;
}
Status = HAL_OK;
/* Wait Transmit Done */
while (!(hspi->Instance->STATUS & SPI_STATUS_RX_BATCH_DONE));
End:
/* Clear Batch Done Flag */
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_BATCH_DONE);
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
/* Rx Disable */
hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
/* Receive End */
hspi->Instance->CS &= (~SPI_CS_CS0);
return Status;
}
/************************************************************************
* function : HAL_SPI_Receive_DMA
* Description: Receive an amount of data in blocking mode with DMA.
* input : hspi : pointer to a SPI_HandleTypeDef structure that contains
* the configuration information for SPI module
* pData : Pointer to data buffer
* Size : Amount of data to be Receive
************************************************************************/
HAL_StatusTypeDef HAL_SPI_Receive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pData, uint32_t Size)
{
/* Check SPI Parameter */
if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
/* Rx machine is running */
if (hspi->RxState != SPI_RX_STATE_IDLE)
{
return HAL_ERROR;
}
/* Set Slave machine is receiving */
hspi->RxState = SPI_RX_STATE_RECEIVING;
/* Clear Batch Done Flag */
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_BATCH_DONE);
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
/* Enable Rx Batch Done Interrupt */
SET_BIT(hspi->Instance->IE, SPI_STATUS_RX_BATCH_DONE);
/* Set Data Size */
hspi->Instance->BATCH = Size;
/* Rx Enable */
hspi->Instance->RX_CTL |= SPI_RX_CTL_EN;
/* Rx FIFO */
hspi->Instance->RX_CTL |= SPI_RX_CTL_DMA_LEVEL_0;
if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
{
/* Receive Start */
hspi->Instance->CS |= SPI_CS_CS0;
}
HAL_DMA_Start(hspi->HDMA_Rx, (uint32_t)&hspi->Instance->DAT, (uint32_t)pData, Size);
hspi->Instance->RX_CTL |= SPI_RX_CTL_DMA_REQ_EN;
return HAL_OK;
}
/************************************************************************
* function : HAL_SPI_Wire_Config
* Description: SPI Wire Config
* input : hspi : pointer to a SPI_HandleTypeDef structure that contains
* the configuration information for SPI module
* Mode : This parameter can be a value of @ref X_MODE
************************************************************************/
HAL_StatusTypeDef HAL_SPI_Wire_Config(SPI_HandleTypeDef *hspi, uint32_t X_Mode)
{
/* Check SPI Parameter */
if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
/* Set SPI X_Mode */
hspi->Instance->CTL = ((hspi->Instance->CTL) & (~SPI_CTL_X_MODE)) | X_Mode;
return HAL_OK;
}
/************************************************************************
* function : HAL_SPI_Transmit_IT
* Description: Transmit an amount of data in blocking mode with interrupt.
* input : hspi : pointer to a SPI_HandleTypeDef structure that contains
* the configuration information for SPI module
* pData : Pointer to data buffer
************************************************************************/
HAL_StatusTypeDef HAL_SPI_Transmit_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint32_t Size)
{
/* Check SPI Parameter */
if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
/* Tx machine is running */
if (hspi->TxState != SPI_TX_STATE_IDLE)
{
return HAL_ERROR;
}
hspi->Tx_Size = Size;
hspi->Tx_Buffer = pData;
hspi->Tx_Count = 0;
/* Set machine is Sending */
hspi->TxState = SPI_TX_STATE_SENDING;
/* Clear TX FIFO */
SET_BIT(hspi->Instance->TX_CTL, SPI_TX_CTL_FIFO_RESET);
CLEAR_BIT(hspi->Instance->TX_CTL, SPI_TX_CTL_FIFO_RESET);
/* Clear Batch Done Flag */
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_BATCH_DONE);
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
/* Set Data Size */
hspi->Instance->BATCH = Size;
/* Tx Enable */
hspi->Instance->TX_CTL |= SPI_TX_CTL_EN;
if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
{
/* Transmit Start */
hspi->Instance->CS |= SPI_CS_CS0;
}
else
{
/* Rx Disable */
hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
}
while (hspi->Tx_Count < hspi->Tx_Size)
{
if (!(hspi->Instance->STATUS & SPI_STATUS_TX_FIFO_FULL))
hspi->Instance->DAT = hspi->Tx_Buffer[hspi->Tx_Count++];
else
break;
}
/* Clear Tx FIFO half empty Flag */
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_FIFO_HALF_EMPTY);
/* Enable Tx FIFO half empty Interrupt and Tx batch done Interrupt*/
SET_BIT(hspi->Instance->IE, (SPI_IE_TX_FIFO_HALF_EMPTY_EN | SPI_IE_TX_BATCH_DONE_EN));
return HAL_OK;
}
/************************************************************************
* function : HAL_SPI_Receive_IT
* Description: Receive an amount of data in blocking mode with interrupt.
* input : hspi : pointer to a SPI_HandleTypeDef structure that contains
* the configuration information for SPI module
* pData : Pointer to data buffer
************************************************************************/
HAL_StatusTypeDef HAL_SPI_Receive_IT(SPI_HandleTypeDef *hspi, uint8_t *pData, uint32_t Size)
{
/* Check SPI Parameter */
if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
/* Rx machine is running */
if (hspi->RxState != SPI_RX_STATE_IDLE)
{
return HAL_ERROR;
}
/* Set Slave machine is receiving */
hspi->RxState = SPI_RX_STATE_RECEIVING;
if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
{
/* Clear Batch Done Flag */
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_RX_BATCH_DONE);
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
/* Set Data Size */
hspi->Instance->BATCH = Size;
/* Rx Enable */
hspi->Instance->RX_CTL |= SPI_RX_CTL_EN;
/* Receive Start */
hspi->Instance->CS |= SPI_CS_CS0;
}
else
{
/* Reset BATCH register */
hspi->Instance->BATCH = 1;
hspi->Instance->RX_CTL |= SPI_RX_CTL_EN;
}
hspi->Rx_Size = Size;
hspi->Rx_Buffer = pData;
hspi->Rx_Count = 0;
lu32_ReceiveTimeOut = SPI_RX_TIMEOUT;
/* Enable Rx FIFO Not Empty Interrupt */
SET_BIT(hspi->Instance->IE, SPI_STATUS_RX_NOT_EMPTY);
return HAL_OK;
}
/************************************************************************
* function : HAL_SPI_TransmitReceive
* Description: Transmits and recieve an amount of data in blocking mode.
* input : hspi : pointer to a SPI_HandleTypeDef structure that contains
* the configuration information for SPI module
* pTxData : Pointer to transmit data buffer
* pRxData : Pointer to recieve data buffer
* Size : Amount of data to be sent
* Timeout : TransmitReceive Timeout
************************************************************************/
HAL_StatusTypeDef HAL_SPI_TransmitReceive(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint32_t Size, uint32_t Timeout)
{
uint32_t i;
__IO uint32_t TxFlag = 1U, uiTimeout;
HAL_StatusTypeDef Status = HAL_OK;
/* Check SPI Parameter */
if (!IS_SPI_ALL_INSTANCE(hspi->Instance)) return HAL_ERROR;
if ((pTxData == NULL)||(pRxData == NULL)) return HAL_ERROR;
hspi->Tx_Count = 0;
hspi->Rx_Count = 0;
hspi->Tx_Buffer = pTxData;
hspi->Rx_Buffer = pRxData;
hspi->Tx_Size = Size;
hspi->Rx_Size = Size;
uiTimeout = Timeout;
/* Clear Batch Done Flag */
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_BATCH_DONE);
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
/* Tx Enable */
hspi->Instance->TX_CTL |= SPI_TX_CTL_EN;
/* Rx Enable */
hspi->Instance->RX_CTL |= SPI_RX_CTL_EN;
/* Clear TX FIFO */
SET_BIT(hspi->Instance->TX_CTL, SPI_TX_CTL_FIFO_RESET);
CLEAR_BIT(hspi->Instance->TX_CTL, SPI_TX_CTL_FIFO_RESET);
if (hspi->Init.SPI_Mode == SPI_MODE_SLAVE)
{
hspi->Instance->DAT = hspi->Tx_Buffer[hspi->Tx_Count++];
hspi->Tx_Size--;
TxFlag = 0;
}
else
{
/* Set Data Size */
hspi->Instance->BATCH = hspi->Tx_Size;
/* Transmit Start */
hspi->Instance->CS |= SPI_CS_CS0;
TxFlag = 1;
}
while((hspi->Tx_Size>0) || (hspi->Rx_Size>0))
{
if (hspi->Init.SPI_Mode == SPI_MODE_SLAVE)
{
/* Wait Rx FIFO Not Empty */
if((!(hspi->Instance->STATUS & SPI_STATUS_RX_FIFO_EMPTY)) && (hspi->Rx_Size>0))
{
hspi->Rx_Buffer[hspi->Rx_Count++] = hspi->Instance->DAT;
hspi->Rx_Size--;
TxFlag = 1U;
}
/* Wait Tx FIFO Not Full */
if((!(hspi->Instance->STATUS & SPI_STATUS_TX_FIFO_FULL)) && (hspi->Tx_Size>0) && (TxFlag == 1U))
{
hspi->Instance->DAT = hspi->Tx_Buffer[hspi->Tx_Count++];
hspi->Tx_Size--;
TxFlag = 0;
}
}
else
{
/* Wait Tx FIFO Not Full */
if((!(hspi->Instance->STATUS & SPI_STATUS_TX_FIFO_FULL)) && (hspi->Tx_Size>0) && (TxFlag == 1U))
{
hspi->Instance->DAT = hspi->Tx_Buffer[hspi->Tx_Count++];
hspi->Tx_Size--;
TxFlag = 0;
}
/* Wait Rx FIFO Not Empty */
if((!(hspi->Instance->STATUS & SPI_STATUS_RX_FIFO_EMPTY)) && (hspi->Rx_Size>0))
{
hspi->Rx_Buffer[hspi->Rx_Count++] = hspi->Instance->DAT;
hspi->Rx_Size--;
TxFlag = 1U;
}
}
/* Wait Timeout */
if(uiTimeout)
{
uiTimeout--;
if(uiTimeout == 0)
{
Status = HAL_TIMEOUT;
goto End;
}
}
}
/* Wait Transmit Done */
while (!(hspi->Instance->STATUS & SPI_STATUS_TX_BATCH_DONE));
Status = HAL_OK;
End:
/* Clear Batch Done Flag */
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_TX_BATCH_DONE);
SET_BIT(hspi->Instance->STATUS, SPI_STATUS_BATCH_DONE);
/* Tx Disable */
hspi->Instance->TX_CTL &= (~SPI_TX_CTL_EN);
/* Rx Disable */
hspi->Instance->RX_CTL &= (~SPI_RX_CTL_EN);
if (hspi->Init.SPI_Mode == SPI_MODE_MASTER)
{
/* Transmit End */
hspi->Instance->CS &= (~SPI_CS_CS0);
}
return Status;
}
/************************************************************************
* function : HAL_SPI_GetTxState
* Description: Get Tx state.
* input : hspi : pointer to a SPI_HandleTypeDef structure that contains
* the configuration information for SPI module
************************************************************************/
uint8_t HAL_SPI_GetTxState(SPI_HandleTypeDef *hspi)
{
return hspi->TxState;
}
/************************************************************************
* function : HAL_SPI_GetRxState
* Description: Get Rx state.
* input : hspi : pointer to a SPI_HandleTypeDef structure that contains
* the configuration information for SPI module
************************************************************************/
uint8_t HAL_SPI_GetRxState(SPI_HandleTypeDef *hspi)
{
return hspi->RxState;
}
|