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
221
222
226
227
228
229
230
231
232
233
236
237
241
244
245
256
257
258
259
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
284
285
286
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
348
349
352
355
356
357
361
362
363
364
365
372
373
374
375
376
377
378
379
381
382
383
384
385
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
440
441
442
444
445
446
447
448
449
450
451
452
453
454
455
459
460
461
462
463
464
465
466
467
468
469
470
474
475
476
480
481
482
483
484
485
486
490
494
495
496
497
498
503
508
509
514
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
569
570
571
572
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
618
627
628
634
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
674
675
676
677
678
679
680
683
686
689
692
695
698
701
707
708
709
727
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
765
768
771
774
777
780
783
789
790
791
809
816
817
818
819
820
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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
903
904
905
906
907
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
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
983
984
985
986
987
988
989
990
991
992
993
994
995
996
1001
1002
1003
1004
1005
1006
1007
1011
1016
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
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1094
1098
1102
1103
1104
1105
1106
1107
1108
1109
1110
1115
1116
1117
1118
1119
1120
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1156
1160
1164
1165
1166
1167
1168
1169
1174
1175
1176
1177
1178
1179
1180
1184
1185
1186
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1226
1227
1228
1229
1230
1231
1232
1233
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1265
1266
1267
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1327
1328
1329
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1434
1435
1436
1437
1438
1439
1440
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1490
1491
1492
1493
1494
1495
1496
1497
1498
1503
1504
1505
1506
1507
1508
1509
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1532
1533
1534
1535
1541
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1804
1805
1806
1807
1813
1814
1815
1816
1817
1818
1821
1822
1823
1829
1830
1831
1832
1833
1834
1837
1838
1839
1845
1846
1847
1848
1849
1850
1853
1854
1855
1861
1862
1863
1864
1865
1866
1869
1870
1871
1877
1878
1879
1880
1881
1882
1885
1886
1887
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1912
1916
1917
1923
1927
1930
1931
1934
1935
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1962
1967
1968
1969
1970
1971
1972
1973
1977
1978
1983
1989
1990
1991
1992
1993
1999
2005
2011
2017
2019
2020
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2054
2059
2060
2061
2062
2063
2064
2065
2066
2070
2075
2076
2077
2078
2083
2088
2093
2098
2100
2101
2102
2103
2104
2105
2111
2112
2113
2114
2115
2116
2120
2125
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2149
2150
2155
2156
2160
2165
2166
2167
2168
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2224
2225
2226
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2544
2545
2546
2547
2550
2551
2554
/* ... */
#include "stm32f4xx_hal.h"
/* ... */
/* ... */
#ifdef HAL_SAI_MODULE_ENABLED
#if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F413xx) ||\
defined(STM32F423xx)
/* ... */
typedef enum
{
SAI_MODE_DMA,
SAI_MODE_IT
...} SAI_ModeTypedef;
/* ... */
/* ... */
#define SAI_DEFAULT_TIMEOUT 4U
#define SAI_LONG_TIMEOUT 1000U
/* ... */
Private define
/* ... */
static void SAI_FillFifo(SAI_HandleTypeDef *hsai);
static uint32_t SAI_InterruptFlag(const SAI_HandleTypeDef *hsai, uint32_t mode);
static HAL_StatusTypeDef SAI_InitI2S(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot);
static HAL_StatusTypeDef SAI_InitPCM(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot);
static HAL_StatusTypeDef SAI_Disable(SAI_HandleTypeDef *hsai);
static void SAI_Transmit_IT8Bit(SAI_HandleTypeDef *hsai);
static void SAI_Transmit_IT16Bit(SAI_HandleTypeDef *hsai);
static void SAI_Transmit_IT32Bit(SAI_HandleTypeDef *hsai);
static void SAI_Receive_IT8Bit(SAI_HandleTypeDef *hsai);
static void SAI_Receive_IT16Bit(SAI_HandleTypeDef *hsai);
static void SAI_Receive_IT32Bit(SAI_HandleTypeDef *hsai);
static void SAI_DMATxCplt(DMA_HandleTypeDef *hdma);
static void SAI_DMATxHalfCplt(DMA_HandleTypeDef *hdma);
static void SAI_DMARxCplt(DMA_HandleTypeDef *hdma);
static void SAI_DMARxHalfCplt(DMA_HandleTypeDef *hdma);
static void SAI_DMAError(DMA_HandleTypeDef *hdma);
static void SAI_DMAAbort(DMA_HandleTypeDef *hdma);
/* ... */
Private function prototypes
/* ... */
/* ... */
/* ... */
HAL_StatusTypeDef HAL_SAI_InitProtocol(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot)
{
HAL_StatusTypeDef status = HAL_OK;
assert_param(IS_SAI_SUPPORTED_PROTOCOL(protocol));
assert_param(IS_SAI_PROTOCOL_DATASIZE(datasize));
switch (protocol)
{
case SAI_I2S_STANDARD :
case SAI_I2S_MSBJUSTIFIED :
case SAI_I2S_LSBJUSTIFIED :
status = SAI_InitI2S(hsai, protocol, datasize, nbslot);
break;case SAI_I2S_LSBJUSTIFIED :
case SAI_PCM_LONG :
case SAI_PCM_SHORT :
status = SAI_InitPCM(hsai, protocol, datasize, nbslot);
break;case SAI_PCM_SHORT :
default :
status = HAL_ERROR;
break;default
}switch (protocol) { ... }
if (status == HAL_OK)
{
status = HAL_SAI_Init(hsai);
}if (status == HAL_OK) { ... }
return status;
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_SAI_Init(SAI_HandleTypeDef *hsai)
{
uint32_t tmpregisterGCR = 0U;
uint32_t freq = 0U;
/* ... */
uint32_t ckstr_bits = 0U;
uint32_t syncen_bits = 0U;
if (hsai == NULL)
{
return HAL_ERROR;
}if (hsai == NULL) { ... }
assert_param(IS_SAI_ALL_INSTANCE(hsai->Instance));
assert_param(IS_SAI_AUDIO_FREQUENCY(hsai->Init.AudioFrequency));
assert_param(IS_SAI_BLOCK_PROTOCOL(hsai->Init.Protocol));
assert_param(IS_SAI_BLOCK_MODE(hsai->Init.AudioMode));
assert_param(IS_SAI_BLOCK_SYNCEXT(hsai->Init.SynchroExt));
assert_param(IS_SAI_BLOCK_DATASIZE(hsai->Init.DataSize));
assert_param(IS_SAI_BLOCK_FIRST_BIT(hsai->Init.FirstBit));
assert_param(IS_SAI_BLOCK_CLOCK_STROBING(hsai->Init.ClockStrobing));
assert_param(IS_SAI_BLOCK_SYNCHRO(hsai->Init.Synchro));
assert_param(IS_SAI_BLOCK_OUTPUT_DRIVE(hsai->Init.OutputDrive));
assert_param(IS_SAI_BLOCK_NODIVIDER(hsai->Init.NoDivider));
assert_param(IS_SAI_BLOCK_FIFO_THRESHOLD(hsai->Init.FIFOThreshold));
assert_param(IS_SAI_MONO_STEREO_MODE(hsai->Init.MonoStereoMode));
assert_param(IS_SAI_BLOCK_COMPANDING_MODE(hsai->Init.CompandingMode));
assert_param(IS_SAI_BLOCK_TRISTATE_MANAGEMENT(hsai->Init.TriState));
assert_param(IS_SAI_BLOCK_FRAME_LENGTH(hsai->FrameInit.FrameLength));
assert_param(IS_SAI_BLOCK_ACTIVE_FRAME(hsai->FrameInit.ActiveFrameLength));
assert_param(IS_SAI_BLOCK_FS_DEFINITION(hsai->FrameInit.FSDefinition));
assert_param(IS_SAI_BLOCK_FS_POLARITY(hsai->FrameInit.FSPolarity));
assert_param(IS_SAI_BLOCK_FS_OFFSET(hsai->FrameInit.FSOffset));
assert_param(IS_SAI_BLOCK_FIRSTBIT_OFFSET(hsai->SlotInit.FirstBitOffset));
assert_param(IS_SAI_BLOCK_SLOT_SIZE(hsai->SlotInit.SlotSize));
assert_param(IS_SAI_BLOCK_SLOT_NUMBER(hsai->SlotInit.SlotNumber));
assert_param(IS_SAI_SLOT_ACTIVE(hsai->SlotInit.SlotActive));
if (hsai->State == HAL_SAI_STATE_RESET)
{
hsai->Lock = HAL_UNLOCKED;
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->RxCpltCallback = HAL_SAI_RxCpltCallback;
hsai->RxHalfCpltCallback = HAL_SAI_RxHalfCpltCallback;
hsai->TxCpltCallback = HAL_SAI_TxCpltCallback;
hsai->TxHalfCpltCallback = HAL_SAI_TxHalfCpltCallback;
hsai->ErrorCallback = HAL_SAI_ErrorCallback;
if (hsai->MspInitCallback == NULL)
{
hsai->MspInitCallback = HAL_SAI_MspInit;
}if (hsai->MspInitCallback == NULL) { ... }
hsai->MspInitCallback(hsai);/* ... */
#else
HAL_SAI_MspInit(hsai);/* ... */
#endif
}if (hsai->State == HAL_SAI_STATE_RESET) { ... }
hsai->State = HAL_SAI_STATE_BUSY;
SAI_Disable(hsai);
SAI_BlockSynchroConfig(hsai);
/* ... */
if (hsai->Init.AudioFrequency != SAI_AUDIO_FREQUENCY_MCKDIV)
{
freq = SAI_GetInputClock(hsai);
tmpregisterGCR = (((freq * 10U) / ((hsai->Init.AudioFrequency) * 512U)));
hsai->Init.Mckdiv = tmpregisterGCR / 10U;
if ((tmpregisterGCR % 10U) > 8U)
{
hsai->Init.Mckdiv += 1U;
}if ((tmpregisterGCR % 10U) > 8U) { ... }
if (hsai->Init.Protocol == SAI_SPDIF_PROTOCOL)
{
hsai->Init.Mckdiv = hsai->Init.Mckdiv >> 1;
}if (hsai->Init.Protocol == SAI_SPDIF_PROTOCOL) { ... }
}if (hsai->Init.AudioFrequency != SAI_AUDIO_FREQUENCY_MCKDIV) { ... }
assert_param(IS_SAI_BLOCK_MASTER_DIVIDER(hsai->Init.Mckdiv));
if ((hsai->Init.AudioMode == SAI_MODEMASTER_TX) || (hsai->Init.AudioMode == SAI_MODESLAVE_TX))
{
ckstr_bits = (hsai->Init.ClockStrobing == SAI_CLOCKSTROBING_RISINGEDGE) ? 0U : SAI_xCR1_CKSTR;
}if ((hsai->Init.AudioMode == SAI_MODEMASTER_TX) || (hsai->Init.AudioMode == SAI_MODESLAVE_TX)) { ... }
else
{
ckstr_bits = (hsai->Init.ClockStrobing == SAI_CLOCKSTROBING_RISINGEDGE) ? SAI_xCR1_CKSTR : 0U;
}else { ... }
SAI Block Synchro Configuration
switch (hsai->Init.Synchro)
{
case SAI_ASYNCHRONOUS :
{
syncen_bits = 0U;
...}
break;case SAI_ASYNCHRONOUS :
case SAI_SYNCHRONOUS :
{
syncen_bits = SAI_xCR1_SYNCEN_0;
...}
break;case SAI_SYNCHRONOUS :
case SAI_SYNCHRONOUS_EXT_SAI1 :
case SAI_SYNCHRONOUS_EXT_SAI2 :
{
syncen_bits = SAI_xCR1_SYNCEN_1;
...}
break;case SAI_SYNCHRONOUS_EXT_SAI2 :
default:
break;default
}switch (hsai->Init.Synchro) { ... }
hsai->Instance->CR1 &= ~(SAI_xCR1_MODE | SAI_xCR1_PRTCFG | SAI_xCR1_DS | \
SAI_xCR1_LSBFIRST | SAI_xCR1_CKSTR | SAI_xCR1_SYNCEN | \
SAI_xCR1_MONO | SAI_xCR1_OUTDRIV | SAI_xCR1_DMAEN | \
SAI_xCR1_NODIV | SAI_xCR1_MCKDIV);
hsai->Instance->CR1 |= (hsai->Init.AudioMode | hsai->Init.Protocol | \
hsai->Init.DataSize | hsai->Init.FirstBit | \
ckstr_bits | syncen_bits | \
hsai->Init.MonoStereoMode | hsai->Init.OutputDrive | \
hsai->Init.NoDivider | (hsai->Init.Mckdiv << 20U));
hsai->Instance->CR2 &= ~(SAI_xCR2_FTH | SAI_xCR2_FFLUSH | SAI_xCR2_COMP | SAI_xCR2_CPL);
hsai->Instance->CR2 |= (hsai->Init.FIFOThreshold | hsai->Init.CompandingMode | hsai->Init.TriState);
SAI Block Configuration
hsai->Instance->FRCR &= (~(SAI_xFRCR_FRL | SAI_xFRCR_FSALL | SAI_xFRCR_FSDEF | \
SAI_xFRCR_FSPOL | SAI_xFRCR_FSOFF));
hsai->Instance->FRCR |= ((hsai->FrameInit.FrameLength - 1U) |
hsai->FrameInit.FSOffset |
hsai->FrameInit.FSDefinition |
hsai->FrameInit.FSPolarity |
((hsai->FrameInit.ActiveFrameLength - 1U) << 8U));
SAI Frame Configuration
hsai->Instance->SLOTR &= ~(SAI_xSLOTR_FBOFF | SAI_xSLOTR_SLOTSZ | \
SAI_xSLOTR_NBSLOT | SAI_xSLOTR_SLOTEN);
hsai->Instance->SLOTR |= hsai->SlotInit.FirstBitOffset | hsai->SlotInit.SlotSize | \
(hsai->SlotInit.SlotActive << 16U) | ((hsai->SlotInit.SlotNumber - 1U) << 8U);
hsai->ErrorCode = HAL_SAI_ERROR_NONE;
hsai->State = HAL_SAI_STATE_READY;
__HAL_UNLOCK(hsai);
return HAL_OK;
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_SAI_DeInit(SAI_HandleTypeDef *hsai)
{
if (hsai == NULL)
{
return HAL_ERROR;
}if (hsai == NULL) { ... }
hsai->State = HAL_SAI_STATE_BUSY;
hsai->Instance->IMR = 0U;
hsai->Instance->CLRFR = 0xFFFFFFFFU;
SAI_Disable(hsai);
SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
if (hsai->MspDeInitCallback == NULL)
{
hsai->MspDeInitCallback = HAL_SAI_MspDeInit;
}if (hsai->MspDeInitCallback == NULL) { ... }
hsai->MspDeInitCallback(hsai);/* ... */
#else
HAL_SAI_MspDeInit(hsai);
#endif
hsai->ErrorCode = HAL_SAI_ERROR_NONE;
hsai->State = HAL_SAI_STATE_RESET;
__HAL_UNLOCK(hsai);
return HAL_OK;
}{ ... }
/* ... */
__weak void HAL_SAI_MspInit(SAI_HandleTypeDef *hsai)
{
UNUSED(hsai);
/* ... */
}{ ... }
/* ... */
__weak void HAL_SAI_MspDeInit(SAI_HandleTypeDef *hsai)
{
UNUSED(hsai);
/* ... */
}{ ... }
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
/* ... */
HAL_StatusTypeDef HAL_SAI_RegisterCallback(SAI_HandleTypeDef *hsai,
HAL_SAI_CallbackIDTypeDef CallbackID,
pSAI_CallbackTypeDef pCallback)
{
HAL_StatusTypeDef status = HAL_OK;
if (pCallback == NULL)
{
hsai->ErrorCode |= HAL_SAI_ERROR_INVALID_CALLBACK;
status = HAL_ERROR;
}if (pCallback == NULL) { ... }
else
{
if (HAL_SAI_STATE_READY == hsai->State)
{
switch (CallbackID)
{
case HAL_SAI_RX_COMPLETE_CB_ID :
hsai->RxCpltCallback = pCallback;
break;case HAL_SAI_RX_COMPLETE_CB_ID :
case HAL_SAI_RX_HALFCOMPLETE_CB_ID :
hsai->RxHalfCpltCallback = pCallback;
break;case HAL_SAI_RX_HALFCOMPLETE_CB_ID :
case HAL_SAI_TX_COMPLETE_CB_ID :
hsai->TxCpltCallback = pCallback;
break;case HAL_SAI_TX_COMPLETE_CB_ID :
case HAL_SAI_TX_HALFCOMPLETE_CB_ID :
hsai->TxHalfCpltCallback = pCallback;
break;case HAL_SAI_TX_HALFCOMPLETE_CB_ID :
case HAL_SAI_ERROR_CB_ID :
hsai->ErrorCallback = pCallback;
break;case HAL_SAI_ERROR_CB_ID :
case HAL_SAI_MSPINIT_CB_ID :
hsai->MspInitCallback = pCallback;
break;case HAL_SAI_MSPINIT_CB_ID :
case HAL_SAI_MSPDEINIT_CB_ID :
hsai->MspDeInitCallback = pCallback;
break;case HAL_SAI_MSPDEINIT_CB_ID :
default :
hsai->ErrorCode |= HAL_SAI_ERROR_INVALID_CALLBACK;
status = HAL_ERROR;
break;default
}switch (CallbackID) { ... }
}if (HAL_SAI_STATE_READY == hsai->State) { ... }
else if (HAL_SAI_STATE_RESET == hsai->State)
{
switch (CallbackID)
{
case HAL_SAI_MSPINIT_CB_ID :
hsai->MspInitCallback = pCallback;
break;case HAL_SAI_MSPINIT_CB_ID :
case HAL_SAI_MSPDEINIT_CB_ID :
hsai->MspDeInitCallback = pCallback;
break;case HAL_SAI_MSPDEINIT_CB_ID :
default :
hsai->ErrorCode |= HAL_SAI_ERROR_INVALID_CALLBACK;
status = HAL_ERROR;
break;default
}switch (CallbackID) { ... }
}else if (HAL_SAI_STATE_RESET == hsai->State) { ... }
else
{
hsai->ErrorCode |= HAL_SAI_ERROR_INVALID_CALLBACK;
status = HAL_ERROR;
}else { ... }
}else { ... }
return status;
}HAL_SAI_RegisterCallback (SAI_HandleTypeDef *hsai, HAL_SAI_CallbackIDTypeDef CallbackID, pSAI_CallbackTypeDef pCallback) { ... }
/* ... */
HAL_StatusTypeDef HAL_SAI_UnRegisterCallback(SAI_HandleTypeDef *hsai,
HAL_SAI_CallbackIDTypeDef CallbackID)
{
HAL_StatusTypeDef status = HAL_OK;
if (HAL_SAI_STATE_READY == hsai->State)
{
switch (CallbackID)
{
case HAL_SAI_RX_COMPLETE_CB_ID :
hsai->RxCpltCallback = HAL_SAI_RxCpltCallback;
break;case HAL_SAI_RX_COMPLETE_CB_ID :
case HAL_SAI_RX_HALFCOMPLETE_CB_ID :
hsai->RxHalfCpltCallback = HAL_SAI_RxHalfCpltCallback;
break;case HAL_SAI_RX_HALFCOMPLETE_CB_ID :
case HAL_SAI_TX_COMPLETE_CB_ID :
hsai->TxCpltCallback = HAL_SAI_TxCpltCallback;
break;case HAL_SAI_TX_COMPLETE_CB_ID :
case HAL_SAI_TX_HALFCOMPLETE_CB_ID :
hsai->TxHalfCpltCallback = HAL_SAI_TxHalfCpltCallback;
break;case HAL_SAI_TX_HALFCOMPLETE_CB_ID :
case HAL_SAI_ERROR_CB_ID :
hsai->ErrorCallback = HAL_SAI_ErrorCallback;
break;case HAL_SAI_ERROR_CB_ID :
case HAL_SAI_MSPINIT_CB_ID :
hsai->MspInitCallback = HAL_SAI_MspInit;
break;case HAL_SAI_MSPINIT_CB_ID :
case HAL_SAI_MSPDEINIT_CB_ID :
hsai->MspDeInitCallback = HAL_SAI_MspDeInit;
break;case HAL_SAI_MSPDEINIT_CB_ID :
default :
hsai->ErrorCode |= HAL_SAI_ERROR_INVALID_CALLBACK;
status = HAL_ERROR;
break;default
}switch (CallbackID) { ... }
}if (HAL_SAI_STATE_READY == hsai->State) { ... }
else if (HAL_SAI_STATE_RESET == hsai->State)
{
switch (CallbackID)
{
case HAL_SAI_MSPINIT_CB_ID :
hsai->MspInitCallback = HAL_SAI_MspInit;
break;case HAL_SAI_MSPINIT_CB_ID :
case HAL_SAI_MSPDEINIT_CB_ID :
hsai->MspDeInitCallback = HAL_SAI_MspDeInit;
break;case HAL_SAI_MSPDEINIT_CB_ID :
default :
hsai->ErrorCode |= HAL_SAI_ERROR_INVALID_CALLBACK;
status = HAL_ERROR;
break;default
}switch (CallbackID) { ... }
}else if (HAL_SAI_STATE_RESET == hsai->State) { ... }
else
{
hsai->ErrorCode |= HAL_SAI_ERROR_INVALID_CALLBACK;
status = HAL_ERROR;
}else { ... }
return status;
}HAL_SAI_UnRegisterCallback (SAI_HandleTypeDef *hsai, HAL_SAI_CallbackIDTypeDef CallbackID) { ... }
/* ... */#endif
/* ... */
/* ... */
/* ... */
HAL_StatusTypeDef HAL_SAI_Transmit(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size, uint32_t Timeout)
{
uint32_t tickstart = HAL_GetTick();
if ((pData == NULL) || (Size == 0))
{
return HAL_ERROR;
}if ((pData == NULL) || (Size == 0)) { ... }
if (hsai->State == HAL_SAI_STATE_READY)
{
__HAL_LOCK(hsai);
hsai->XferSize = Size;
hsai->XferCount = Size;
hsai->pBuffPtr = pData;
hsai->State = HAL_SAI_STATE_BUSY_TX;
hsai->ErrorCode = HAL_SAI_ERROR_NONE;
if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET)
{
SAI_FillFifo(hsai);
__HAL_SAI_ENABLE(hsai);
}if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET) { ... }
while (hsai->XferCount > 0U)
{
if ((hsai->Instance->SR & SAI_xSR_FLVL) != SAI_FIFOSTATUS_FULL)
{
if ((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
{
hsai->Instance->DR = (*hsai->pBuffPtr++);
}if ((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING)) { ... }
else if (hsai->Init.DataSize <= SAI_DATASIZE_16)
{
hsai->Instance->DR = *((uint16_t *)hsai->pBuffPtr);
hsai->pBuffPtr += 2U;
}else if (hsai->Init.DataSize <= SAI_DATASIZE_16) { ... }
else
{
hsai->Instance->DR = *((uint32_t *)hsai->pBuffPtr);
hsai->pBuffPtr += 4U;
}else { ... }
hsai->XferCount--;
}if ((hsai->Instance->SR & SAI_xSR_FLVL) != SAI_FIFOSTATUS_FULL) { ... }
else
{
if ((Timeout != HAL_MAX_DELAY) && ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout)))
{
hsai->ErrorCode |= HAL_SAI_ERROR_TIMEOUT;
hsai->Instance->CLRFR = 0xFFFFFFFFU;
SAI_Disable(hsai);
SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
hsai->State = HAL_SAI_STATE_READY;
__HAL_UNLOCK(hsai);
return HAL_ERROR;
}if ((Timeout != HAL_MAX_DELAY) && ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))) { ... }
}else { ... }
}while (hsai->XferCount > 0U) { ... }
hsai->State = HAL_SAI_STATE_READY;
__HAL_UNLOCK(hsai);
return HAL_OK;
}if (hsai->State == HAL_SAI_STATE_READY) { ... }
else
{
return HAL_BUSY;
}else { ... }
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_SAI_Receive(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size, uint32_t Timeout)
{
uint32_t tickstart = HAL_GetTick();
if ((pData == NULL) || (Size == 0))
{
return HAL_ERROR;
}if ((pData == NULL) || (Size == 0)) { ... }
if (hsai->State == HAL_SAI_STATE_READY)
{
__HAL_LOCK(hsai);
hsai->pBuffPtr = pData;
hsai->XferSize = Size;
hsai->XferCount = Size;
hsai->State = HAL_SAI_STATE_BUSY_RX;
hsai->ErrorCode = HAL_SAI_ERROR_NONE;
if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET)
{
__HAL_SAI_ENABLE(hsai);
}if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET) { ... }
while (hsai->XferCount > 0U)
{
if ((hsai->Instance->SR & SAI_xSR_FLVL) != SAI_FIFOSTATUS_EMPTY)
{
if ((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
{
(*hsai->pBuffPtr++) = hsai->Instance->DR;
}if ((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING)) { ... }
else if (hsai->Init.DataSize <= SAI_DATASIZE_16)
{
*((uint16_t *)hsai->pBuffPtr) = hsai->Instance->DR;
hsai->pBuffPtr += 2U;
}else if (hsai->Init.DataSize <= SAI_DATASIZE_16) { ... }
else
{
*((uint32_t *)hsai->pBuffPtr) = hsai->Instance->DR;
hsai->pBuffPtr += 4U;
}else { ... }
hsai->XferCount--;
}if ((hsai->Instance->SR & SAI_xSR_FLVL) != SAI_FIFOSTATUS_EMPTY) { ... }
else
{
if ((Timeout != HAL_MAX_DELAY) && ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout)))
{
hsai->ErrorCode |= HAL_SAI_ERROR_TIMEOUT;
hsai->Instance->CLRFR = 0xFFFFFFFFU;
SAI_Disable(hsai);
SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
hsai->State = HAL_SAI_STATE_READY;
__HAL_UNLOCK(hsai);
return HAL_ERROR;
}if ((Timeout != HAL_MAX_DELAY) && ((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))) { ... }
}else { ... }
}while (hsai->XferCount > 0U) { ... }
hsai->State = HAL_SAI_STATE_READY;
__HAL_UNLOCK(hsai);
return HAL_OK;
}if (hsai->State == HAL_SAI_STATE_READY) { ... }
else
{
return HAL_BUSY;
}else { ... }
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_SAI_Transmit_IT(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size)
{
if ((pData == NULL) || (Size == 0))
{
return HAL_ERROR;
}if ((pData == NULL) || (Size == 0)) { ... }
if (hsai->State == HAL_SAI_STATE_READY)
{
__HAL_LOCK(hsai);
hsai->pBuffPtr = pData;
hsai->XferSize = Size;
hsai->XferCount = Size;
hsai->ErrorCode = HAL_SAI_ERROR_NONE;
hsai->State = HAL_SAI_STATE_BUSY_TX;
if ((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
{
hsai->InterruptServiceRoutine = SAI_Transmit_IT8Bit;
}if ((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING)) { ... }
else if (hsai->Init.DataSize <= SAI_DATASIZE_16)
{
hsai->InterruptServiceRoutine = SAI_Transmit_IT16Bit;
}else if (hsai->Init.DataSize <= SAI_DATASIZE_16) { ... }
else
{
hsai->InterruptServiceRoutine = SAI_Transmit_IT32Bit;
}else { ... }
SAI_FillFifo(hsai);
__HAL_SAI_ENABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET)
{
__HAL_SAI_ENABLE(hsai);
}if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET) { ... }
__HAL_UNLOCK(hsai);
return HAL_OK;
}if (hsai->State == HAL_SAI_STATE_READY) { ... }
else
{
return HAL_BUSY;
}else { ... }
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_SAI_Receive_IT(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size)
{
if ((pData == NULL) || (Size == 0))
{
return HAL_ERROR;
}if ((pData == NULL) || (Size == 0)) { ... }
if (hsai->State == HAL_SAI_STATE_READY)
{
__HAL_LOCK(hsai);
hsai->pBuffPtr = pData;
hsai->XferSize = Size;
hsai->XferCount = Size;
hsai->ErrorCode = HAL_SAI_ERROR_NONE;
hsai->State = HAL_SAI_STATE_BUSY_RX;
if ((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
{
hsai->InterruptServiceRoutine = SAI_Receive_IT8Bit;
}if ((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING)) { ... }
else if (hsai->Init.DataSize <= SAI_DATASIZE_16)
{
hsai->InterruptServiceRoutine = SAI_Receive_IT16Bit;
}else if (hsai->Init.DataSize <= SAI_DATASIZE_16) { ... }
else
{
hsai->InterruptServiceRoutine = SAI_Receive_IT32Bit;
}else { ... }
__HAL_SAI_ENABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET)
{
__HAL_SAI_ENABLE(hsai);
}if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET) { ... }
__HAL_UNLOCK(hsai);
return HAL_OK;
}if (hsai->State == HAL_SAI_STATE_READY) { ... }
else
{
return HAL_BUSY;
}else { ... }
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_SAI_DMAPause(SAI_HandleTypeDef *hsai)
{
__HAL_LOCK(hsai);
hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
__HAL_UNLOCK(hsai);
return HAL_OK;
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_SAI_DMAResume(SAI_HandleTypeDef *hsai)
{
__HAL_LOCK(hsai);
hsai->Instance->CR1 |= SAI_xCR1_DMAEN;
if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET)
{
__HAL_SAI_ENABLE(hsai);
}if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET) { ... }
__HAL_UNLOCK(hsai);
return HAL_OK;
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_SAI_DMAStop(SAI_HandleTypeDef *hsai)
{
HAL_StatusTypeDef status = HAL_OK;
__HAL_LOCK(hsai);
SAI_Disable(hsai);
hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
if ((hsai->hdmatx != NULL) && (hsai->State == HAL_SAI_STATE_BUSY_TX))
{
if (HAL_DMA_Abort(hsai->hdmatx) != HAL_OK)
{
if (hsai->hdmatx->ErrorCode != HAL_DMA_ERROR_NO_XFER)
{
status = HAL_ERROR;
hsai->ErrorCode |= HAL_SAI_ERROR_DMA;
}if (hsai->hdmatx->ErrorCode != HAL_DMA_ERROR_NO_XFER) { ... }
}if (HAL_DMA_Abort(hsai->hdmatx) != HAL_OK) { ... }
}if ((hsai->hdmatx != NULL) && (hsai->State == HAL_SAI_STATE_BUSY_TX)) { ... }
if ((hsai->hdmarx != NULL) && (hsai->State == HAL_SAI_STATE_BUSY_RX))
{
if (HAL_DMA_Abort(hsai->hdmarx) != HAL_OK)
{
if (hsai->hdmarx->ErrorCode != HAL_DMA_ERROR_NO_XFER)
{
status = HAL_ERROR;
hsai->ErrorCode |= HAL_SAI_ERROR_DMA;
}if (hsai->hdmarx->ErrorCode != HAL_DMA_ERROR_NO_XFER) { ... }
}if (HAL_DMA_Abort(hsai->hdmarx) != HAL_OK) { ... }
}if ((hsai->hdmarx != NULL) && (hsai->State == HAL_SAI_STATE_BUSY_RX)) { ... }
SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
hsai->State = HAL_SAI_STATE_READY;
__HAL_UNLOCK(hsai);
return status;
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_SAI_Abort(SAI_HandleTypeDef *hsai)
{
HAL_StatusTypeDef status = HAL_OK;
__HAL_LOCK(hsai);
SAI_Disable(hsai);
if ((hsai->Instance->CR1 & SAI_xCR1_DMAEN) == SAI_xCR1_DMAEN)
{
hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
if ((hsai->hdmatx != NULL) && (hsai->State == HAL_SAI_STATE_BUSY_TX))
{
if (HAL_DMA_Abort(hsai->hdmatx) != HAL_OK)
{
if (hsai->hdmatx->ErrorCode != HAL_DMA_ERROR_NO_XFER)
{
status = HAL_ERROR;
hsai->ErrorCode |= HAL_SAI_ERROR_DMA;
}if (hsai->hdmatx->ErrorCode != HAL_DMA_ERROR_NO_XFER) { ... }
}if (HAL_DMA_Abort(hsai->hdmatx) != HAL_OK) { ... }
}if ((hsai->hdmatx != NULL) && (hsai->State == HAL_SAI_STATE_BUSY_TX)) { ... }
if ((hsai->hdmarx != NULL) && (hsai->State == HAL_SAI_STATE_BUSY_RX))
{
if (HAL_DMA_Abort(hsai->hdmarx) != HAL_OK)
{
if (hsai->hdmarx->ErrorCode != HAL_DMA_ERROR_NO_XFER)
{
status = HAL_ERROR;
hsai->ErrorCode |= HAL_SAI_ERROR_DMA;
}if (hsai->hdmarx->ErrorCode != HAL_DMA_ERROR_NO_XFER) { ... }
}if (HAL_DMA_Abort(hsai->hdmarx) != HAL_OK) { ... }
}if ((hsai->hdmarx != NULL) && (hsai->State == HAL_SAI_STATE_BUSY_RX)) { ... }
}if ((hsai->Instance->CR1 & SAI_xCR1_DMAEN) == SAI_xCR1_DMAEN) { ... }
hsai->Instance->IMR = 0U;
hsai->Instance->CLRFR = 0xFFFFFFFFU;
SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
hsai->State = HAL_SAI_STATE_READY;
__HAL_UNLOCK(hsai);
return status;
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_SAI_Transmit_DMA(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size)
{
uint32_t tickstart = HAL_GetTick();
if ((pData == NULL) || (Size == 0))
{
return HAL_ERROR;
}if ((pData == NULL) || (Size == 0)) { ... }
if (hsai->State == HAL_SAI_STATE_READY)
{
__HAL_LOCK(hsai);
hsai->pBuffPtr = pData;
hsai->XferSize = Size;
hsai->XferCount = Size;
hsai->ErrorCode = HAL_SAI_ERROR_NONE;
hsai->State = HAL_SAI_STATE_BUSY_TX;
hsai->hdmatx->XferHalfCpltCallback = SAI_DMATxHalfCplt;
hsai->hdmatx->XferCpltCallback = SAI_DMATxCplt;
hsai->hdmatx->XferErrorCallback = SAI_DMAError;
hsai->hdmatx->XferAbortCallback = NULL;
if (HAL_DMA_Start_IT(hsai->hdmatx, (uint32_t)hsai->pBuffPtr, (uint32_t)&hsai->Instance->DR, hsai->XferSize) != HAL_OK)
{
__HAL_UNLOCK(hsai);
return HAL_ERROR;
}if (HAL_DMA_Start_IT(hsai->hdmatx, (uint32_t)hsai->pBuffPtr, (uint32_t)&hsai->Instance->DR, hsai->XferSize) != HAL_OK) { ... }
__HAL_SAI_ENABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_DMA));
hsai->Instance->CR1 |= SAI_xCR1_DMAEN;
while ((hsai->Instance->SR & SAI_xSR_FLVL) == SAI_FIFOSTATUS_EMPTY)
{
if ((HAL_GetTick() - tickstart) > SAI_LONG_TIMEOUT)
{
hsai->ErrorCode |= HAL_SAI_ERROR_TIMEOUT;
__HAL_UNLOCK(hsai);
return HAL_TIMEOUT;
}if ((HAL_GetTick() - tickstart) > SAI_LONG_TIMEOUT) { ... }
}while ((hsai->Instance->SR & SAI_xSR_FLVL) == SAI_FIFOSTATUS_EMPTY) { ... }
if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == 0U)
{
__HAL_SAI_ENABLE(hsai);
}if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == 0U) { ... }
__HAL_UNLOCK(hsai);
return HAL_OK;
}if (hsai->State == HAL_SAI_STATE_READY) { ... }
else
{
return HAL_BUSY;
}else { ... }
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_SAI_Receive_DMA(SAI_HandleTypeDef *hsai, uint8_t *pData, uint16_t Size)
{
if ((pData == NULL) || (Size == 0))
{
return HAL_ERROR;
}if ((pData == NULL) || (Size == 0)) { ... }
if (hsai->State == HAL_SAI_STATE_READY)
{
__HAL_LOCK(hsai);
hsai->pBuffPtr = pData;
hsai->XferSize = Size;
hsai->XferCount = Size;
hsai->ErrorCode = HAL_SAI_ERROR_NONE;
hsai->State = HAL_SAI_STATE_BUSY_RX;
hsai->hdmarx->XferHalfCpltCallback = SAI_DMARxHalfCplt;
hsai->hdmarx->XferCpltCallback = SAI_DMARxCplt;
hsai->hdmarx->XferErrorCallback = SAI_DMAError;
hsai->hdmarx->XferAbortCallback = NULL;
if (HAL_DMA_Start_IT(hsai->hdmarx, (uint32_t)&hsai->Instance->DR, (uint32_t)hsai->pBuffPtr, hsai->XferSize) != HAL_OK)
{
__HAL_UNLOCK(hsai);
return HAL_ERROR;
}if (HAL_DMA_Start_IT(hsai->hdmarx, (uint32_t)&hsai->Instance->DR, (uint32_t)hsai->pBuffPtr, hsai->XferSize) != HAL_OK) { ... }
__HAL_SAI_ENABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_DMA));
hsai->Instance->CR1 |= SAI_xCR1_DMAEN;
if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET)
{
__HAL_SAI_ENABLE(hsai);
}if ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) == RESET) { ... }
__HAL_UNLOCK(hsai);
return HAL_OK;
}if (hsai->State == HAL_SAI_STATE_READY) { ... }
else
{
return HAL_BUSY;
}else { ... }
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_SAI_EnableTxMuteMode(SAI_HandleTypeDef *hsai, uint16_t val)
{
assert_param(IS_SAI_BLOCK_MUTE_VALUE(val));
if (hsai->State != HAL_SAI_STATE_RESET)
{
CLEAR_BIT(hsai->Instance->CR2, SAI_xCR2_MUTEVAL | SAI_xCR2_MUTE);
SET_BIT(hsai->Instance->CR2, SAI_xCR2_MUTE | val);
return HAL_OK;
}if (hsai->State != HAL_SAI_STATE_RESET) { ... }
return HAL_ERROR;
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_SAI_DisableTxMuteMode(SAI_HandleTypeDef *hsai)
{
if (hsai->State != HAL_SAI_STATE_RESET)
{
CLEAR_BIT(hsai->Instance->CR2, SAI_xCR2_MUTEVAL | SAI_xCR2_MUTE);
return HAL_OK;
}if (hsai->State != HAL_SAI_STATE_RESET) { ... }
return HAL_ERROR;
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_SAI_EnableRxMuteMode(SAI_HandleTypeDef *hsai, SAIcallback callback, uint16_t counter)
{
assert_param(IS_SAI_BLOCK_MUTE_COUNTER(counter));
if (hsai->State != HAL_SAI_STATE_RESET)
{
CLEAR_BIT(hsai->Instance->CR2, SAI_xCR2_MUTECNT);
SET_BIT(hsai->Instance->CR2, (uint32_t)((uint32_t)counter << SAI_xCR2_MUTECNT_Pos));
hsai->mutecallback = callback;
__HAL_SAI_ENABLE_IT(hsai, SAI_IT_MUTEDET);
return HAL_OK;
}if (hsai->State != HAL_SAI_STATE_RESET) { ... }
return HAL_ERROR;
}{ ... }
/* ... */
HAL_StatusTypeDef HAL_SAI_DisableRxMuteMode(SAI_HandleTypeDef *hsai)
{
if (hsai->State != HAL_SAI_STATE_RESET)
{
hsai->mutecallback = (SAIcallback)NULL;
__HAL_SAI_DISABLE_IT(hsai, SAI_IT_MUTEDET);
return HAL_OK;
}if (hsai->State != HAL_SAI_STATE_RESET) { ... }
return HAL_ERROR;
}{ ... }
/* ... */
void HAL_SAI_IRQHandler(SAI_HandleTypeDef *hsai)
{
if (hsai->State != HAL_SAI_STATE_RESET)
{
uint32_t itflags = hsai->Instance->SR;
uint32_t itsources = hsai->Instance->IMR;
uint32_t cr1config = hsai->Instance->CR1;
uint32_t tmperror;
if (((itflags & SAI_xSR_FREQ) == SAI_xSR_FREQ) && ((itsources & SAI_IT_FREQ) == SAI_IT_FREQ))
{
hsai->InterruptServiceRoutine(hsai);
}if (((itflags & SAI_xSR_FREQ) == SAI_xSR_FREQ) && ((itsources & SAI_IT_FREQ) == SAI_IT_FREQ)) { ... }
SAI Fifo request interrupt occurred
else if (((itflags & SAI_FLAG_OVRUDR) == SAI_FLAG_OVRUDR) && ((itsources & SAI_IT_OVRUDR) == SAI_IT_OVRUDR))
{
__HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_OVRUDR);
tmperror = ((hsai->State == HAL_SAI_STATE_BUSY_RX) ? HAL_SAI_ERROR_OVR : HAL_SAI_ERROR_UDR);
hsai->ErrorCode |= tmperror;
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->ErrorCallback(hsai);
#else
HAL_SAI_ErrorCallback(hsai);
#endif
}else if (((itflags & SAI_FLAG_OVRUDR) == SAI_FLAG_OVRUDR) && ((itsources & SAI_IT_OVRUDR) == SAI_IT_OVRUDR)) { ... }
SAI Overrun error interrupt occurred
else if (((itflags & SAI_FLAG_MUTEDET) == SAI_FLAG_MUTEDET) && ((itsources & SAI_IT_MUTEDET) == SAI_IT_MUTEDET))
{
__HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_MUTEDET);
if (hsai->mutecallback != (SAIcallback)NULL)
{
hsai->mutecallback();
}if (hsai->mutecallback != (SAIcallback)NULL) { ... }
}else if (((itflags & SAI_FLAG_MUTEDET) == SAI_FLAG_MUTEDET) && ((itsources & SAI_IT_MUTEDET) == SAI_IT_MUTEDET)) { ... }
SAI mutedet interrupt occurred
else if (((itflags & SAI_FLAG_AFSDET) == SAI_FLAG_AFSDET) && ((itsources & SAI_IT_AFSDET) == SAI_IT_AFSDET))
{
__HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_AFSDET);
hsai->ErrorCode |= HAL_SAI_ERROR_AFSDET;
if ((cr1config & SAI_xCR1_DMAEN) == SAI_xCR1_DMAEN)
{
if (hsai->hdmatx != NULL)
{
hsai->hdmatx->XferAbortCallback = SAI_DMAAbort;
HAL_DMA_Abort_IT(hsai->hdmatx);
}if (hsai->hdmatx != NULL) { ... }
else if (hsai->hdmarx != NULL)
{
hsai->hdmarx->XferAbortCallback = SAI_DMAAbort;
HAL_DMA_Abort_IT(hsai->hdmarx);
}else if (hsai->hdmarx != NULL) { ... }
}if ((cr1config & SAI_xCR1_DMAEN) == SAI_xCR1_DMAEN) { ... }
else
{
HAL_SAI_Abort(hsai);
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->ErrorCallback(hsai);
#else
HAL_SAI_ErrorCallback(hsai);
#endif
}else { ... }
}else if (((itflags & SAI_FLAG_AFSDET) == SAI_FLAG_AFSDET) && ((itsources & SAI_IT_AFSDET) == SAI_IT_AFSDET)) { ... }
SAI AFSDET interrupt occurred
else if (((itflags & SAI_FLAG_LFSDET) == SAI_FLAG_LFSDET) && ((itsources & SAI_IT_LFSDET) == SAI_IT_LFSDET))
{
__HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_LFSDET);
hsai->ErrorCode |= HAL_SAI_ERROR_LFSDET;
if ((cr1config & SAI_xCR1_DMAEN) == SAI_xCR1_DMAEN)
{
if (hsai->hdmatx != NULL)
{
hsai->hdmatx->XferAbortCallback = SAI_DMAAbort;
HAL_DMA_Abort_IT(hsai->hdmatx);
}if (hsai->hdmatx != NULL) { ... }
else if (hsai->hdmarx != NULL)
{
hsai->hdmarx->XferAbortCallback = SAI_DMAAbort;
HAL_DMA_Abort_IT(hsai->hdmarx);
}else if (hsai->hdmarx != NULL) { ... }
}if ((cr1config & SAI_xCR1_DMAEN) == SAI_xCR1_DMAEN) { ... }
else
{
HAL_SAI_Abort(hsai);
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->ErrorCallback(hsai);
#else
HAL_SAI_ErrorCallback(hsai);
#endif
}else { ... }
}else if (((itflags & SAI_FLAG_LFSDET) == SAI_FLAG_LFSDET) && ((itsources & SAI_IT_LFSDET) == SAI_IT_LFSDET)) { ... }
SAI LFSDET interrupt occurred
else if (((itflags & SAI_FLAG_WCKCFG) == SAI_FLAG_WCKCFG) && ((itsources & SAI_IT_WCKCFG) == SAI_IT_WCKCFG))
{
__HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_WCKCFG);
hsai->ErrorCode |= HAL_SAI_ERROR_WCKCFG;
if ((cr1config & SAI_xCR1_DMAEN) == SAI_xCR1_DMAEN)
{
if (hsai->hdmatx != NULL)
{
hsai->hdmatx->XferAbortCallback = SAI_DMAAbort;
HAL_DMA_Abort_IT(hsai->hdmatx);
}if (hsai->hdmatx != NULL) { ... }
else if (hsai->hdmarx != NULL)
{
hsai->hdmarx->XferAbortCallback = SAI_DMAAbort;
HAL_DMA_Abort_IT(hsai->hdmarx);
}else if (hsai->hdmarx != NULL) { ... }
}if ((cr1config & SAI_xCR1_DMAEN) == SAI_xCR1_DMAEN) { ... }
else
{
hsai->Instance->IMR = 0U;
hsai->Instance->CLRFR = 0xFFFFFFFFU;
hsai->State = HAL_SAI_STATE_READY;
hsai->XferCount = 0U;
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->ErrorCallback(hsai);
#else
HAL_SAI_ErrorCallback(hsai);
#endif
}else { ... }
}else if (((itflags & SAI_FLAG_WCKCFG) == SAI_FLAG_WCKCFG) && ((itsources & SAI_IT_WCKCFG) == SAI_IT_WCKCFG)) { ... }
SAI WCKCFG interrupt occurred
else if (((itflags & SAI_FLAG_CNRDY) == SAI_FLAG_CNRDY) && ((itsources & SAI_IT_CNRDY) == SAI_IT_CNRDY))
{
__HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_CNRDY);
hsai->ErrorCode |= HAL_SAI_ERROR_CNREADY;
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->ErrorCallback(hsai);
#else
HAL_SAI_ErrorCallback(hsai);
#endif
}else if (((itflags & SAI_FLAG_CNRDY) == SAI_FLAG_CNRDY) && ((itsources & SAI_IT_CNRDY) == SAI_IT_CNRDY)) { ... }
else
{
}else { ... }
}if (hsai->State != HAL_SAI_STATE_RESET) { ... }
}{ ... }
/* ... */
__weak void HAL_SAI_TxCpltCallback(SAI_HandleTypeDef *hsai)
{
UNUSED(hsai);
/* ... */
}{ ... }
/* ... */
__weak void HAL_SAI_TxHalfCpltCallback(SAI_HandleTypeDef *hsai)
{
UNUSED(hsai);
/* ... */
}{ ... }
/* ... */
__weak void HAL_SAI_RxCpltCallback(SAI_HandleTypeDef *hsai)
{
UNUSED(hsai);
/* ... */
}{ ... }
/* ... */
__weak void HAL_SAI_RxHalfCpltCallback(SAI_HandleTypeDef *hsai)
{
UNUSED(hsai);
/* ... */
}{ ... }
/* ... */
__weak void HAL_SAI_ErrorCallback(SAI_HandleTypeDef *hsai)
{
UNUSED(hsai);
/* ... */
}{ ... }
/* ... */
/* ... */
/* ... */
HAL_SAI_StateTypeDef HAL_SAI_GetState(const SAI_HandleTypeDef *hsai)
{
return hsai->State;
}{ ... }
/* ... */
uint32_t HAL_SAI_GetError(const SAI_HandleTypeDef *hsai)
{
return hsai->ErrorCode;
}{ ... }
/* ... */
/* ... */
/* ... */
/* ... */
static HAL_StatusTypeDef SAI_InitI2S(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot)
{
hsai->Init.Protocol = SAI_FREE_PROTOCOL;
hsai->Init.FirstBit = SAI_FIRSTBIT_MSB;
if ((hsai->Init.AudioMode == SAI_MODEMASTER_TX) || (hsai->Init.AudioMode == SAI_MODESLAVE_TX))
{
hsai->Init.ClockStrobing = SAI_CLOCKSTROBING_FALLINGEDGE;
}if ((hsai->Init.AudioMode == SAI_MODEMASTER_TX) || (hsai->Init.AudioMode == SAI_MODESLAVE_TX)) { ... }
else
{
hsai->Init.ClockStrobing = SAI_CLOCKSTROBING_RISINGEDGE;
}else { ... }
hsai->FrameInit.FSDefinition = SAI_FS_CHANNEL_IDENTIFICATION;
hsai->SlotInit.SlotActive = SAI_SLOTACTIVE_ALL;
hsai->SlotInit.FirstBitOffset = 0U;
hsai->SlotInit.SlotNumber = nbslot;
if ((nbslot & 0x1U) != 0U)
{
return HAL_ERROR;
}if ((nbslot & 0x1U) != 0U) { ... }
if (protocol == SAI_I2S_STANDARD)
{
hsai->FrameInit.FSPolarity = SAI_FS_ACTIVE_LOW;
hsai->FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT;
}if (protocol == SAI_I2S_STANDARD) { ... }
else
{
hsai->FrameInit.FSPolarity = SAI_FS_ACTIVE_HIGH;
hsai->FrameInit.FSOffset = SAI_FS_FIRSTBIT;
}else { ... }
switch (datasize)
{
case SAI_PROTOCOL_DATASIZE_16BIT:
hsai->Init.DataSize = SAI_DATASIZE_16;
hsai->FrameInit.FrameLength = 32U * (nbslot / 2U);
hsai->FrameInit.ActiveFrameLength = 16U * (nbslot / 2U);
hsai->SlotInit.SlotSize = SAI_SLOTSIZE_16B;
break;case SAI_PROTOCOL_DATASIZE_16BIT:
case SAI_PROTOCOL_DATASIZE_16BITEXTENDED :
hsai->Init.DataSize = SAI_DATASIZE_16;
hsai->FrameInit.FrameLength = 64U * (nbslot / 2U);
hsai->FrameInit.ActiveFrameLength = 32U * (nbslot / 2U);
hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
break;case SAI_PROTOCOL_DATASIZE_16BITEXTENDED :
case SAI_PROTOCOL_DATASIZE_24BIT:
hsai->Init.DataSize = SAI_DATASIZE_24;
hsai->FrameInit.FrameLength = 64U * (nbslot / 2U);
hsai->FrameInit.ActiveFrameLength = 32U * (nbslot / 2U);
hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
break;case SAI_PROTOCOL_DATASIZE_24BIT:
case SAI_PROTOCOL_DATASIZE_32BIT:
hsai->Init.DataSize = SAI_DATASIZE_32;
hsai->FrameInit.FrameLength = 64U * (nbslot / 2U);
hsai->FrameInit.ActiveFrameLength = 32U * (nbslot / 2U);
hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
break;case SAI_PROTOCOL_DATASIZE_32BIT:
default :
return HAL_ERROR;default
}switch (datasize) { ... }
if (protocol == SAI_I2S_LSBJUSTIFIED)
{
if (datasize == SAI_PROTOCOL_DATASIZE_16BITEXTENDED)
{
hsai->SlotInit.FirstBitOffset = 16U;
}if (datasize == SAI_PROTOCOL_DATASIZE_16BITEXTENDED) { ... }
if (datasize == SAI_PROTOCOL_DATASIZE_24BIT)
{
hsai->SlotInit.FirstBitOffset = 8U;
}if (datasize == SAI_PROTOCOL_DATASIZE_24BIT) { ... }
}if (protocol == SAI_I2S_LSBJUSTIFIED) { ... }
return HAL_OK;
}{ ... }
/* ... */
static HAL_StatusTypeDef SAI_InitPCM(SAI_HandleTypeDef *hsai, uint32_t protocol, uint32_t datasize, uint32_t nbslot)
{
hsai->Init.Protocol = SAI_FREE_PROTOCOL;
hsai->Init.FirstBit = SAI_FIRSTBIT_MSB;
if ((hsai->Init.AudioMode == SAI_MODEMASTER_TX) || (hsai->Init.AudioMode == SAI_MODESLAVE_TX))
{
hsai->Init.ClockStrobing = SAI_CLOCKSTROBING_RISINGEDGE;
}if ((hsai->Init.AudioMode == SAI_MODEMASTER_TX) || (hsai->Init.AudioMode == SAI_MODESLAVE_TX)) { ... }
else
{
hsai->Init.ClockStrobing = SAI_CLOCKSTROBING_FALLINGEDGE;
}else { ... }
hsai->FrameInit.FSDefinition = SAI_FS_STARTFRAME;
hsai->FrameInit.FSPolarity = SAI_FS_ACTIVE_HIGH;
hsai->FrameInit.FSOffset = SAI_FS_BEFOREFIRSTBIT;
hsai->SlotInit.FirstBitOffset = 0U;
hsai->SlotInit.SlotNumber = nbslot;
hsai->SlotInit.SlotActive = SAI_SLOTACTIVE_ALL;
if (protocol == SAI_PCM_SHORT)
{
hsai->FrameInit.ActiveFrameLength = 1;
}if (protocol == SAI_PCM_SHORT) { ... }
else
{
hsai->FrameInit.ActiveFrameLength = 13;
}else { ... }
switch (datasize)
{
case SAI_PROTOCOL_DATASIZE_16BIT:
hsai->Init.DataSize = SAI_DATASIZE_16;
hsai->FrameInit.FrameLength = 16U * nbslot;
hsai->SlotInit.SlotSize = SAI_SLOTSIZE_16B;
break;case SAI_PROTOCOL_DATASIZE_16BIT:
case SAI_PROTOCOL_DATASIZE_16BITEXTENDED :
hsai->Init.DataSize = SAI_DATASIZE_16;
hsai->FrameInit.FrameLength = 32U * nbslot;
hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
break;case SAI_PROTOCOL_DATASIZE_16BITEXTENDED :
case SAI_PROTOCOL_DATASIZE_24BIT :
hsai->Init.DataSize = SAI_DATASIZE_24;
hsai->FrameInit.FrameLength = 32U * nbslot;
hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
break;case SAI_PROTOCOL_DATASIZE_24BIT :
case SAI_PROTOCOL_DATASIZE_32BIT:
hsai->Init.DataSize = SAI_DATASIZE_32;
hsai->FrameInit.FrameLength = 32U * nbslot;
hsai->SlotInit.SlotSize = SAI_SLOTSIZE_32B;
break;case SAI_PROTOCOL_DATASIZE_32BIT:
default :
return HAL_ERROR;default
}switch (datasize) { ... }
return HAL_OK;
}{ ... }
/* ... */
static void SAI_FillFifo(SAI_HandleTypeDef *hsai)
{
while (((hsai->Instance->SR & SAI_xSR_FLVL) != SAI_FIFOSTATUS_FULL) && (hsai->XferCount > 0U))
{
if ((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING))
{
hsai->Instance->DR = (*hsai->pBuffPtr++);
}if ((hsai->Init.DataSize == SAI_DATASIZE_8) && (hsai->Init.CompandingMode == SAI_NOCOMPANDING)) { ... }
else if (hsai->Init.DataSize <= SAI_DATASIZE_16)
{
hsai->Instance->DR = *((uint32_t *)hsai->pBuffPtr);
hsai->pBuffPtr += 2U;
}else if (hsai->Init.DataSize <= SAI_DATASIZE_16) { ... }
else
{
hsai->Instance->DR = *((uint32_t *)hsai->pBuffPtr);
hsai->pBuffPtr += 4U;
}else { ... }
hsai->XferCount--;
}while (((hsai->Instance->SR & SAI_xSR_FLVL) != SAI_FIFOSTATUS_FULL) && (hsai->XferCount > 0U)) { ... }
}{ ... }
/* ... */
static uint32_t SAI_InterruptFlag(const SAI_HandleTypeDef *hsai, uint32_t mode)
{
uint32_t tmpIT = SAI_IT_OVRUDR;
if (mode == SAI_MODE_IT)
{
tmpIT |= SAI_IT_FREQ;
}if (mode == SAI_MODE_IT) { ... }
if ((hsai->Init.Protocol == SAI_AC97_PROTOCOL) &&
((hsai->Init.AudioMode == SAI_MODESLAVE_RX) || (hsai->Init.AudioMode == SAI_MODEMASTER_RX)))
{
tmpIT |= SAI_IT_CNRDY;
}if ((hsai->Init.Protocol == SAI_AC97_PROTOCOL) && ((hsai->Init.AudioMode == SAI_MODESLAVE_RX) || (hsai->Init.AudioMode == SAI_MODEMASTER_RX))) { ... }
if ((hsai->Init.AudioMode == SAI_MODESLAVE_RX) || (hsai->Init.AudioMode == SAI_MODESLAVE_TX))
{
tmpIT |= SAI_IT_AFSDET | SAI_IT_LFSDET;
}if ((hsai->Init.AudioMode == SAI_MODESLAVE_RX) || (hsai->Init.AudioMode == SAI_MODESLAVE_TX)) { ... }
else
{
tmpIT |= SAI_IT_WCKCFG;
}else { ... }
return tmpIT;
}{ ... }
/* ... */
static HAL_StatusTypeDef SAI_Disable(SAI_HandleTypeDef *hsai)
{
uint32_t count = SAI_DEFAULT_TIMEOUT * (SystemCoreClock / 7U / 1000U);
HAL_StatusTypeDef status = HAL_OK;
__HAL_SAI_DISABLE(hsai);
do
{
if (count-- == 0U)
{
hsai->ErrorCode |= HAL_SAI_ERROR_TIMEOUT;
status = HAL_TIMEOUT;
break;
}if (count-- == 0U) { ... }
...}
while ((hsai->Instance->CR1 & SAI_xCR1_SAIEN) != RESET);
return status;
}{ ... }
/* ... */
static void SAI_Transmit_IT8Bit(SAI_HandleTypeDef *hsai)
{
if (hsai->XferCount == 0U)
{
__HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
hsai->State = HAL_SAI_STATE_READY;
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->TxCpltCallback(hsai);
#else
HAL_SAI_TxCpltCallback(hsai);
#endif
}if (hsai->XferCount == 0U) { ... }
else
{
hsai->Instance->DR = (*hsai->pBuffPtr++);
hsai->XferCount--;
}else { ... }
}{ ... }
/* ... */
static void SAI_Transmit_IT16Bit(SAI_HandleTypeDef *hsai)
{
if (hsai->XferCount == 0U)
{
__HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
hsai->State = HAL_SAI_STATE_READY;
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->TxCpltCallback(hsai);
#else
HAL_SAI_TxCpltCallback(hsai);
#endif
}if (hsai->XferCount == 0U) { ... }
else
{
hsai->Instance->DR = *(uint16_t *)hsai->pBuffPtr;
hsai->pBuffPtr += 2U;
hsai->XferCount--;
}else { ... }
}{ ... }
/* ... */
static void SAI_Transmit_IT32Bit(SAI_HandleTypeDef *hsai)
{
if (hsai->XferCount == 0U)
{
__HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
hsai->State = HAL_SAI_STATE_READY;
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->TxCpltCallback(hsai);
#else
HAL_SAI_TxCpltCallback(hsai);
#endif
}if (hsai->XferCount == 0U) { ... }
else
{
hsai->Instance->DR = *(uint32_t *)hsai->pBuffPtr;
hsai->pBuffPtr += 4U;
hsai->XferCount--;
}else { ... }
}{ ... }
/* ... */
static void SAI_Receive_IT8Bit(SAI_HandleTypeDef *hsai)
{
(*hsai->pBuffPtr++) = hsai->Instance->DR;
hsai->XferCount--;
if (hsai->XferCount == 0U)
{
__HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
__HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_OVRUDR);
hsai->State = HAL_SAI_STATE_READY;
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->RxCpltCallback(hsai);
#else
HAL_SAI_RxCpltCallback(hsai);
#endif
}if (hsai->XferCount == 0U) { ... }
}{ ... }
/* ... */
static void SAI_Receive_IT16Bit(SAI_HandleTypeDef *hsai)
{
*(uint16_t *)hsai->pBuffPtr = hsai->Instance->DR;
hsai->pBuffPtr += 2U;
hsai->XferCount--;
if (hsai->XferCount == 0U)
{
__HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
__HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_OVRUDR);
hsai->State = HAL_SAI_STATE_READY;
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->RxCpltCallback(hsai);
#else
HAL_SAI_RxCpltCallback(hsai);
#endif
}if (hsai->XferCount == 0U) { ... }
}{ ... }
/* ... */
static void SAI_Receive_IT32Bit(SAI_HandleTypeDef *hsai)
{
*(uint32_t *)hsai->pBuffPtr = hsai->Instance->DR;
hsai->pBuffPtr += 4U;
hsai->XferCount--;
if (hsai->XferCount == 0U)
{
__HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_IT));
__HAL_SAI_CLEAR_FLAG(hsai, SAI_FLAG_OVRUDR);
hsai->State = HAL_SAI_STATE_READY;
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->RxCpltCallback(hsai);
#else
HAL_SAI_RxCpltCallback(hsai);
#endif
}if (hsai->XferCount == 0U) { ... }
}{ ... }
/* ... */
static void SAI_DMATxCplt(DMA_HandleTypeDef *hdma)
{
SAI_HandleTypeDef *hsai = (SAI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
if (hdma->Init.Mode != DMA_CIRCULAR)
{
hsai->XferCount = 0U;
hsai->Instance->CR1 &= (uint32_t)(~SAI_xCR1_DMAEN);
__HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_DMA));
hsai->State = HAL_SAI_STATE_READY;
}if (hdma->Init.Mode != DMA_CIRCULAR) { ... }
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->TxCpltCallback(hsai);
#else
HAL_SAI_TxCpltCallback(hsai);
#endif
}{ ... }
/* ... */
static void SAI_DMATxHalfCplt(DMA_HandleTypeDef *hdma)
{
SAI_HandleTypeDef *hsai = (SAI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->TxHalfCpltCallback(hsai);
#else
HAL_SAI_TxHalfCpltCallback(hsai);
#endif
}{ ... }
/* ... */
static void SAI_DMARxCplt(DMA_HandleTypeDef *hdma)
{
SAI_HandleTypeDef *hsai = (SAI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
if (hdma->Init.Mode != DMA_CIRCULAR)
{
hsai->Instance->CR1 &= (uint32_t)(~SAI_xCR1_DMAEN);
hsai->XferCount = 0U;
__HAL_SAI_DISABLE_IT(hsai, SAI_InterruptFlag(hsai, SAI_MODE_DMA));
hsai->State = HAL_SAI_STATE_READY;
}if (hdma->Init.Mode != DMA_CIRCULAR) { ... }
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->RxCpltCallback(hsai);
#else
HAL_SAI_RxCpltCallback(hsai);
#endif
}{ ... }
/* ... */
static void SAI_DMARxHalfCplt(DMA_HandleTypeDef *hdma)
{
SAI_HandleTypeDef *hsai = (SAI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->RxHalfCpltCallback(hsai);
#else
HAL_SAI_RxHalfCpltCallback(hsai);
#endif
}{ ... }
/* ... */
static void SAI_DMAError(DMA_HandleTypeDef *hdma)
{
SAI_HandleTypeDef *hsai = (SAI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
hsai->ErrorCode |= HAL_SAI_ERROR_DMA;
if ((hsai->hdmatx->ErrorCode == HAL_DMA_ERROR_TE) || (hsai->hdmarx->ErrorCode == HAL_DMA_ERROR_TE))
{
hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
SAI_Disable(hsai);
hsai->State = HAL_SAI_STATE_READY;
hsai->XferCount = 0U;
}if ((hsai->hdmatx->ErrorCode == HAL_DMA_ERROR_TE) || (hsai->hdmarx->ErrorCode == HAL_DMA_ERROR_TE)) { ... }
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->ErrorCallback(hsai);
#else
HAL_SAI_ErrorCallback(hsai);
#endif
}{ ... }
/* ... */
static void SAI_DMAAbort(DMA_HandleTypeDef *hdma)
{
SAI_HandleTypeDef *hsai = (SAI_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
hsai->Instance->CR1 &= ~SAI_xCR1_DMAEN;
hsai->Instance->IMR = 0U;
hsai->Instance->CLRFR = 0xFFFFFFFFU;
if (hsai->ErrorCode != HAL_SAI_ERROR_WCKCFG)
{
SAI_Disable(hsai);
SET_BIT(hsai->Instance->CR2, SAI_xCR2_FFLUSH);
}if (hsai->ErrorCode != HAL_SAI_ERROR_WCKCFG) { ... }
hsai->State = HAL_SAI_STATE_READY;
hsai->XferCount = 0U;
#if (USE_HAL_SAI_REGISTER_CALLBACKS == 1)
hsai->ErrorCallback(hsai);
#else
HAL_SAI_ErrorCallback(hsai);
#endif
}{ ... }
/* ... */
/* ... */
#endif /* ... */
#endif
/* ... */
/* ... */