1
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
205
206
207
208
209
210
211
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
282
283
284
285
286
287
288
289
290
291
292
293
294
312
313
316
317
318
319
325
326
327
328
329
330
336
337
338
339
340
341
342
343
344
345
346
347
348
351
352
353
354
357
358
359
360
361
362
380
381
382
383
384
385
386
389
390
391
392
396
397
398
399
400
401
402
409
410
411
412
413
414
418
419
420
421
424
425
426
440
441
442
443
444
448
449
450
451
454
455
456
457
460
461
462
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
494
495
496
497
500
501
502
503
504
505
506
507
510
511
517
518
519
520
521
522
523
524
527
528
529
530
531
532
533
534
535
536
537
538
539
540
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
570
571
572
573
574
575
576
577
578
579
580
581
584
585
586
590
591
592
593
596
597
598
601
604
605
606
607
608
609
610
611
612
621
622
626
627
628
629
630
631
632
633
634
635
636
648
649
650
659
660
661
662
663
667
668
671
672
673
677
678
679
683
684
685
688
689
690
691
692
693
694
701
702
708
715
716
717
718
719
720
721
722
723
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
750
755
760
761
762
763
764
765
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
796
797
798
799
800
801
802
803
804
805
806
809
810
811
814
815
816
817
818
819
820
821
822
823
824
825
826
830
831
832
833
834
835
836
837
838
839
840
841
846
847
852
853
854
855
856
857
858
859
860
861
862
864
865
866
869
870
871
872
873
874
875
876
879
880
881
882
883
884
885
888
889
890
891
892
893
894
895
896
897
898
899
900
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
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
951
960
961
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
988
989
990
991
992
993
994
995
996
997
1000
1004
1007
1008
1009
1010
1011
1012
1015
1016
1017
1018
1019
1025
1026
1027
1028
1029
1030
1031
1032
1033
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1061
1062
1063
1064
1065
1066
1067
1068
1071
1072
1073
1074
1075
1076
1077
1078
1079
1082
1083
1084
1085
1092
1093
1094
1095
1096
1097
1098
1099
1100
1103
1104
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1128
1129
1130
1131
1137
1138
1139
1140
1141
1143
1144
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1158
1159
1160
1161
1162
1163
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1178
1179
1180
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1200
1201
1202
1203
1204
1205
1207
1210
1211
1212
1213
1216
1217
1218
1221
1222
1223
1224
1225
1226
1227
1228
1230
1231
1232
1233
1234
1235
1236
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1264
1265
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1283
1284
1285
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1303
1304
1305
1306
1307
1308
1311
1312
1313
1314
1315
1316
1317
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1335
1336
1337
1342
1343
1344
1345
1346
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1365
1366
1367
1368
1369
1370
1373
1374
1377
1387
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1419
1420
1421
1422
1423
1424
1425
1426
1427
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1443
1447
1448
1449
1450
1451
1452
1453
1454
1455
1458
1459
1460
1461
1462
1463
1464
1465
1468
1469
1470
1475
1476
1477
1478
1479
1480
1481
1484
1485
1486
1487
1488
1491
1498
1499
1500
1501
1502
1503
1507
1508
1509
1510
1511
1512
1513
1514
1518
1521
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1541
1542
1543
1544
1545
1546
1550
1551
1552
1553
1554
1555
1556
1557
1558
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1575
1576
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1595
1596
1597
1598
1599
1600
1601
1602
1603
1607
1608
1609
1610
1611
1612
1613
1614
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1630
1631
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1672
1677
1678
1679
1680
1681
1682
1683
1684
1687
1688
1689
1690
1691
1692
1693
1694
1697
1698
1699
1700
1701
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1734
1735
1736
1737
1742
1743
1744
1745
1750
1751
1752
1757
1758
1759
1760
1761
1762
1763
1764
1765
1770
1771
1772
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1874
1875
1876
1877
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1895
1896
1897
1898
1899
1900
/* ... */
/* ... */
#include <string.h>
#include <sys/param.h>
#include "esp_private/periph_ctrl.h"
#include "esp_private/spi_common_internal.h"
#include "esp_private/spi_master_internal.h"
#include "driver/spi_master.h"
#include "esp_clk_tree.h"
#include "clk_ctrl_os.h"
#include "esp_log.h"
#include "esp_check.h"
#include "esp_ipc.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "soc/soc_memory_layout.h"
#include "driver/gpio.h"
#include "hal/spi_hal.h"
#include "hal/spi_ll.h"
#include "hal/hal_utils.h"
#include "esp_heap_caps.h"19 includes
#if SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
#include "esp_cache.h"
#endif
#ifdef CONFIG_SPI_MASTER_ISR_IN_IRAM
#define SPI_MASTER_ISR_ATTR IRAM_ATTR
#else
#define SPI_MASTER_ISR_ATTR
#endif
#ifdef CONFIG_SPI_MASTER_IN_IRAM
#define SPI_MASTER_ATTR IRAM_ATTR
#else
#define SPI_MASTER_ATTR
#endif
#if SOC_PERIPH_CLK_CTRL_SHARED
#define SPI_MASTER_PERI_CLOCK_ATOMIC() PERIPH_RCC_ATOMIC()
#else
#define SPI_MASTER_PERI_CLOCK_ATOMIC()
#endif
static const char *SPI_TAG = "spi_master";
#define SPI_CHECK(a, str, ret_val, ...) ESP_RETURN_ON_FALSE_ISR(a, ret_val, SPI_TAG, str, ##__VA_ARGS__)
typedef struct spi_device_t spi_device_t;
typedef struct {
spi_transaction_t *trans;
const uint32_t *buffer_to_send;
uint32_t *buffer_to_rcv;
#if SOC_SPI_SCT_SUPPORTED
uint32_t reserved[2];
#endif
}{ ... } spi_trans_priv_t;
#if SOC_SPI_SCT_SUPPORTED
typedef struct {
spi_dma_desc_t *tx_seg_head;
spi_dma_desc_t *rx_seg_head;
spi_multi_transaction_t *sct_trans_desc_head;
uint32_t *sct_conf_buffer;
uint16_t tx_used_desc_num;
uint16_t rx_used_desc_num;
}{...} spi_sct_trans_priv_t;
_Static_assert(sizeof(spi_trans_priv_t) == sizeof(spi_sct_trans_priv_t));
typedef struct {
uint32_t tx_free_desc_num;
uint32_t rx_free_desc_num;
spi_dma_desc_t *cur_tx_seg_link;
spi_dma_desc_t *cur_rx_seg_link;
spi_dma_desc_t *tx_seg_link_tail;
spi_dma_desc_t *rx_seg_link_tail;
}{...} spi_sct_desc_ctx_t;
static void spi_hal_sct_tx_dma_desc_recycle(spi_sct_desc_ctx_t *desc_ctx, uint32_t recycle_num);
static void spi_hal_sct_rx_dma_desc_recycle(spi_sct_desc_ctx_t *desc_ctx, uint32_t recycle_num);/* ... */
#endif
typedef struct {
int id;
spi_device_t* device[DEV_NUM_MAX];
intr_handle_t intr;
spi_hal_context_t hal;
spi_trans_priv_t cur_trans_buf;
#if SOC_SPI_SCT_SUPPORTED
spi_sct_desc_ctx_t sct_desc_pool;
spi_sct_trans_priv_t cur_sct_trans;/* ... */
#endif
int cur_cs;
const spi_bus_attr_t* bus_attr;
const spi_dma_ctx_t *dma_ctx;
bool sct_mode_enabled;
/* ... */
spi_device_t* device_acquiring_lock;
portMUX_TYPE spinlock;
bool polling;
}{ ... } spi_host_t;
struct spi_device_t {
int id;
QueueHandle_t trans_queue;
QueueHandle_t ret_queue;
spi_device_interface_config_t cfg;
spi_hal_dev_config_t hal_dev;
spi_host_t *host;
spi_bus_lock_dev_handle_t dev_lock;
}{ ... };
static spi_host_t* bus_driver_ctx[SOC_SPI_PERIPH_NUM] = {};
static void spi_intr(void *arg);
static void spi_bus_intr_enable(void *host);
static void spi_bus_intr_disable(void *host);
static esp_err_t spi_master_deinit_driver(void* arg);
static inline bool is_valid_host(spi_host_device_t host)
{
#if CONFIG_IDF_TARGET_ESP32
return host >= SPI1_HOST && host <= SPI3_HOST;
#elif (SOC_SPI_PERIPH_NUM == 2)
return host == SPI2_HOST;
#elif (SOC_SPI_PERIPH_NUM == 3)
return host >= SPI2_HOST && host <= SPI3_HOST;
#endif
}{ ... }
#if (SOC_CPU_CORES_NUM > 1) && (!CONFIG_FREERTOS_UNICORE)
typedef struct {
spi_host_t *spi_host;
esp_err_t *err;
}{ ... } spi_ipc_param_t;
static void ipc_isr_reg_to_core(void *args)
{
spi_host_t *host = ((spi_ipc_param_t *)args)->spi_host;
const spi_bus_attr_t* bus_attr = host->bus_attr;
*((spi_ipc_param_t *)args)->err = esp_intr_alloc(spicommon_irqsource_for_host(host->id), bus_attr->bus_cfg.intr_flags | ESP_INTR_FLAG_INTRDISABLED, spi_intr, host, &host->intr);
}{ ... }
#endif/* ... */
static esp_err_t spi_master_init_driver(spi_host_device_t host_id)
{
esp_err_t err = ESP_OK;
const spi_bus_attr_t* bus_attr = spi_bus_get_attr(host_id);
const spi_dma_ctx_t *dma_ctx = spi_bus_get_dma_ctx(host_id);
SPI_CHECK(bus_attr != NULL, "host_id not initialized", ESP_ERR_INVALID_STATE);
SPI_CHECK(bus_attr->lock != NULL, "SPI Master cannot attach to bus. (Check CONFIG_SPI_FLASH_SHARE_SPI1_BUS)", ESP_ERR_INVALID_ARG);
spi_host_t* host = heap_caps_malloc(sizeof(spi_host_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
if (host == NULL) {
err = ESP_ERR_NO_MEM;
goto cleanup;
}{...}
*host = (spi_host_t) {
.id = host_id,
.cur_cs = DEV_NUM_MAX,
.polling = false,
.device_acquiring_lock = NULL,
.spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED,
.bus_attr = bus_attr,
.dma_ctx = dma_ctx,
}{...};
if (host_id != SPI1_HOST) {
#if (SOC_CPU_CORES_NUM > 1) && (!CONFIG_FREERTOS_UNICORE)
if (bus_attr->bus_cfg.isr_cpu_id > ESP_INTR_CPU_AFFINITY_AUTO) {
SPI_CHECK(bus_attr->bus_cfg.isr_cpu_id <= ESP_INTR_CPU_AFFINITY_1, "invalid core id", ESP_ERR_INVALID_ARG);
spi_ipc_param_t ipc_arg = {
.spi_host = host,
.err = &err,
}{...};
esp_ipc_call_blocking(ESP_INTR_CPU_AFFINITY_TO_CORE_ID(bus_attr->bus_cfg.isr_cpu_id), ipc_isr_reg_to_core, (void *) &ipc_arg);
}{...} else
#endif
{
err = esp_intr_alloc(spicommon_irqsource_for_host(host_id), bus_attr->bus_cfg.intr_flags | ESP_INTR_FLAG_INTRDISABLED, spi_intr, host, &host->intr);
}{...}
if (err != ESP_OK) {
goto cleanup;
}{...}
}{...}
SPI_MASTER_PERI_CLOCK_ATOMIC() {
spi_ll_enable_clock(host_id, true);
}{...}
spi_hal_init(&host->hal, host_id);
spi_hal_config_io_default_level(&host->hal, bus_attr->bus_cfg.data_io_default_level);
if (host_id != SPI1_HOST) {
spi_bus_lock_handle_t lock = spi_bus_lock_get_by_id(host_id);
spi_bus_lock_set_bg_control(lock, spi_bus_intr_enable, spi_bus_intr_disable, host);
spi_bus_register_destroy_func(host_id, spi_master_deinit_driver, host);
}{...}
bus_driver_ctx[host_id] = host;
return ESP_OK;
cleanup:
if (host) {
spi_hal_deinit(&host->hal);
if (host->intr) {
esp_intr_free(host->intr);
}{...}
}{...}
free(host);
return err;
}{ ... }
static esp_err_t spi_master_deinit_driver(void* arg)
{
spi_host_t *host = (spi_host_t*)arg;
SPI_CHECK(host != NULL, "host_id not in use", ESP_ERR_INVALID_STATE);
int host_id = host->id;
SPI_CHECK(is_valid_host(host_id), "invalid host_id", ESP_ERR_INVALID_ARG);
for (int x = 0; x < DEV_NUM_MAX; x++) {
SPI_CHECK(host->device[x] == NULL, "not all CSses freed", ESP_ERR_INVALID_STATE);
}{...}
spi_hal_deinit(&host->hal);
if (host->intr) {
esp_intr_free(host->intr);
}{...}
free(host);
bus_driver_ctx[host_id] = NULL;
return ESP_OK;
}{ ... }
void spi_get_timing(bool gpio_is_used, int input_delay_ns, int eff_clk, int* dummy_o, int* cycles_remain_o)
{
#ifdef CONFIG_IDF_TARGET_ESP32
int timing_dummy;
int timing_miso_delay;
spi_hal_cal_timing(APB_CLK_FREQ, eff_clk, gpio_is_used, input_delay_ns, &timing_dummy, &timing_miso_delay);
if (dummy_o) {
*dummy_o = timing_dummy;
}{...}
if (cycles_remain_o) {
*cycles_remain_o = timing_miso_delay;
}{...}
/* ... */#else
ESP_LOGW(SPI_TAG, "This func temporary not supported for current target!");/* ... */
#endif
}{ ... }
int spi_get_freq_limit(bool gpio_is_used, int input_delay_ns)
{
#ifdef CONFIG_IDF_TARGET_ESP32
return spi_hal_get_freq_limit(gpio_is_used, input_delay_ns);
#else
ESP_LOGW(SPI_TAG, "This func temporary not supported for current target!");
return 0;/* ... */
#endif
}{ ... }
/* ... */
esp_err_t spi_bus_add_device(spi_host_device_t host_id, const spi_device_interface_config_t *dev_config, spi_device_handle_t *handle)
{
spi_device_t *dev = NULL;
esp_err_t err = ESP_OK;
SPI_CHECK(is_valid_host(host_id), "invalid host", ESP_ERR_INVALID_ARG);
if (bus_driver_ctx[host_id] == NULL) {
err = spi_master_init_driver(host_id);
if (err != ESP_OK) {
return err;
}{...}
}{...}
spi_host_t *host = bus_driver_ctx[host_id];
const spi_bus_attr_t* bus_attr = host->bus_attr;
SPI_CHECK(dev_config->spics_io_num < 0 || GPIO_IS_VALID_OUTPUT_GPIO(dev_config->spics_io_num), "spics pin invalid", ESP_ERR_INVALID_ARG);
#if SOC_SPI_SUPPORT_CLK_RC_FAST
if (dev_config->clock_source == SPI_CLK_SRC_RC_FAST) {
SPI_CHECK(periph_rtc_dig_clk8m_enable(), "the selected clock not available", ESP_ERR_INVALID_STATE);
}{...}
#endif/* ... */
spi_clock_source_t clk_src = SPI_CLK_SRC_DEFAULT;
uint32_t clock_source_hz = 0;
uint32_t clock_source_div = 1;
if (dev_config->clock_source) {
clk_src = dev_config->clock_source;
}{...}
esp_clk_tree_src_get_freq_hz(clk_src, ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &clock_source_hz);
#if SPI_LL_SUPPORT_CLK_SRC_PRE_DIV
SPI_CHECK((dev_config->clock_speed_hz > 0) && (dev_config->clock_speed_hz <= MIN(clock_source_hz / 2, (80 * 1000000))), "invalid sclk speed", ESP_ERR_INVALID_ARG);
if (clock_source_hz / 2 > (80 * 1000000)) {
hal_utils_clk_info_t clk_cfg = {
.src_freq_hz = clock_source_hz,
.exp_freq_hz = dev_config->clock_speed_hz * 2,
.round_opt = HAL_DIV_ROUND,
.min_integ = 1,
.max_integ = SPI_LL_CLK_SRC_PRE_DIV_MAX / 2,
}{...};
hal_utils_calc_clk_div_integer(&clk_cfg, &clock_source_div);
}{...}
clock_source_div *= 2;
clock_source_hz /= clock_source_div; /* ... */
#else
SPI_CHECK((dev_config->clock_speed_hz > 0) && (dev_config->clock_speed_hz <= clock_source_hz), "invalid sclk speed", ESP_ERR_INVALID_ARG);
#endif
#ifdef CONFIG_IDF_TARGET_ESP32
SPI_CHECK(dev_config->cs_ena_pretrans <= 1 || (dev_config->address_bits == 0 && dev_config->command_bits == 0) ||
(dev_config->flags & SPI_DEVICE_HALFDUPLEX), "In full-duplex mode, only support cs pretrans delay = 1 and without address_bits and command_bits", ESP_ERR_INVALID_ARG);/* ... */
#endif
if (dev_config->flags & SPI_DEVICE_NO_RETURN_RESULT) {
SPI_CHECK(dev_config->post_cb != NULL, "use feature flag 'SPI_DEVICE_NO_RETURN_RESULT' but no post callback function sets", ESP_ERR_INVALID_ARG);
}{...}
uint32_t lock_flag = ((dev_config->spics_io_num != -1) ? SPI_BUS_LOCK_DEV_FLAG_CS_REQUIRED : 0);
spi_bus_lock_dev_config_t lock_config = {
.flags = lock_flag,
}{...};
spi_bus_lock_dev_handle_t dev_handle;
err = spi_bus_lock_register_dev(bus_attr->lock, &lock_config, &dev_handle);
if (err != ESP_OK) {
goto nomem;
}{...}
int freecs = spi_bus_lock_get_dev_id(dev_handle);
SPI_CHECK(freecs != -1, "no free cs pins for the host", ESP_ERR_NOT_FOUND);
int half_duplex = dev_config->flags & SPI_DEVICE_HALFDUPLEX ? 1 : 0;
int no_compensate = dev_config->flags & SPI_DEVICE_NO_DUMMY ? 1 : 0;
int duty_cycle = (dev_config->duty_cycle_pos == 0) ? 128 : dev_config->duty_cycle_pos;
int use_gpio = !(bus_attr->flags & SPICOMMON_BUSFLAG_IOMUX_PINS);
spi_hal_timing_param_t timing_param = {
.half_duplex = half_duplex,
.no_compensate = no_compensate,
.clk_src_hz = clock_source_hz,
.expected_freq = dev_config->clock_speed_hz,
.duty_cycle = duty_cycle,
.input_delay_ns = dev_config->input_delay_ns,
.use_gpio = use_gpio
}{...};
spi_hal_timing_conf_t temp_timing_conf;
esp_err_t ret = spi_hal_cal_clock_conf(&timing_param, &temp_timing_conf);
SPI_CHECK(ret == ESP_OK, "assigned clock speed not supported", ret);
temp_timing_conf.clock_source = clk_src;
temp_timing_conf.source_pre_div = clock_source_div;
temp_timing_conf.rx_sample_point = dev_config->sample_point;
if (temp_timing_conf.rx_sample_point == SPI_SAMPLING_POINT_PHASE_1) {
SPI_CHECK(spi_ll_master_is_rx_std_sample_supported(), "SPI_SAMPLING_POINT_PHASE_1 is not supported on this chip", ESP_ERR_NOT_SUPPORTED);
}{...}
dev = malloc(sizeof(spi_device_t));
if (dev == NULL) {
goto nomem;
}{...}
memset(dev, 0, sizeof(spi_device_t));
dev->id = freecs;
dev->dev_lock = dev_handle;
dev->trans_queue = xQueueCreate(dev_config->queue_size, sizeof(spi_trans_priv_t));
if (!dev->trans_queue) {
goto nomem;
}{...}
if (!(dev_config->flags & SPI_DEVICE_NO_RETURN_RESULT)) {
dev->ret_queue = xQueueCreate(dev_config->queue_size, sizeof(spi_trans_priv_t));
if (!dev->ret_queue) {
goto nomem;
}{...}
}{...}
memcpy(&dev->cfg, dev_config, sizeof(spi_device_interface_config_t));
dev->cfg.duty_cycle_pos = duty_cycle;
if (dev_config->spics_io_num >= 0) {
spicommon_cs_initialize(host_id, dev_config->spics_io_num, freecs, use_gpio);
}{...}
host->device[freecs] = dev;
dev->host = host;
spi_hal_dev_config_t *hal_dev = &(dev->hal_dev);
hal_dev->mode = dev_config->mode;
hal_dev->cs_setup = dev_config->cs_ena_pretrans;
hal_dev->cs_hold = dev_config->cs_ena_posttrans;
if (hal_dev->cs_hold == 0) {
hal_dev->cs_hold = 1;
}{...}
hal_dev->cs_pin_id = dev->id;
hal_dev->timing_conf = temp_timing_conf;
hal_dev->sio = (dev_config->flags) & SPI_DEVICE_3WIRE ? 1 : 0;
hal_dev->half_duplex = dev_config->flags & SPI_DEVICE_HALFDUPLEX ? 1 : 0;
hal_dev->tx_lsbfirst = dev_config->flags & SPI_DEVICE_TXBIT_LSBFIRST ? 1 : 0;
hal_dev->rx_lsbfirst = dev_config->flags & SPI_DEVICE_RXBIT_LSBFIRST ? 1 : 0;
hal_dev->no_compensate = dev_config->flags & SPI_DEVICE_NO_DUMMY ? 1 : 0;
#if SOC_SPI_AS_CS_SUPPORTED
hal_dev->as_cs = dev_config->flags & SPI_DEVICE_CLK_AS_CS ? 1 : 0;
#endif
hal_dev->positive_cs = dev_config->flags & SPI_DEVICE_POSITIVE_CS ? 1 : 0;
*handle = dev;
ESP_LOGD(SPI_TAG, "SPI%d: New device added to CS%d, effective clock: %d Hz", host_id + 1, freecs, temp_timing_conf.real_freq);
return ESP_OK;
nomem:
if (dev) {
if (dev->trans_queue) {
vQueueDelete(dev->trans_queue);
}{...}
if (dev->ret_queue) {
vQueueDelete(dev->ret_queue);
}{...}
spi_bus_lock_unregister_dev(dev->dev_lock);
}{...}
free(dev);
return ESP_ERR_NO_MEM;
}{ ... }
esp_err_t spi_bus_remove_device(spi_device_handle_t handle)
{
SPI_CHECK(handle != NULL, "invalid handle", ESP_ERR_INVALID_ARG);
SPI_CHECK(uxQueueMessagesWaiting(handle->trans_queue) == 0, "Have unfinished transactions", ESP_ERR_INVALID_STATE);
SPI_CHECK(handle->host->cur_cs == DEV_NUM_MAX || handle->host->device[handle->host->cur_cs] != handle, "Have unfinished transactions", ESP_ERR_INVALID_STATE);
if (handle->ret_queue) {
SPI_CHECK(uxQueueMessagesWaiting(handle->ret_queue) == 0, "Have unfinished transactions", ESP_ERR_INVALID_STATE);
}{...}
#if SOC_SPI_SUPPORT_CLK_RC_FAST
if (handle->cfg.clock_source == SPI_CLK_SRC_RC_FAST) {
periph_rtc_dig_clk8m_disable();
}{...}
#endif/* ... */
int spics_io_num = handle->cfg.spics_io_num;
if (spics_io_num >= 0) {
spicommon_cs_free_io(spics_io_num);
}{...}
if (handle->trans_queue) {
vQueueDelete(handle->trans_queue);
}{...}
if (handle->ret_queue) {
vQueueDelete(handle->ret_queue);
}{...}
spi_bus_lock_unregister_dev(handle->dev_lock);
assert(handle->host->device[handle->id] == handle);
handle->host->device[handle->id] = NULL;
free(handle);
return ESP_OK;
}{ ... }
esp_err_t spi_device_get_actual_freq(spi_device_handle_t handle, int* freq_khz)
{
if ((spi_device_t *)handle == NULL || freq_khz == NULL) {
return ESP_ERR_INVALID_ARG;
}{...}
*freq_khz = handle->hal_dev.timing_conf.real_freq / 1000;
return ESP_OK;
}{ ... }
int spi_get_actual_clock(int fapb, int hz, int duty_cycle)
{
return spi_hal_master_cal_clock(fapb, hz, duty_cycle);
}{ ... }
static SPI_MASTER_ISR_ATTR void spi_setup_device(spi_device_t *dev)
{
spi_bus_lock_dev_handle_t dev_lock = dev->dev_lock;
spi_hal_context_t *hal = &dev->host->hal;
spi_hal_dev_config_t *hal_dev = &(dev->hal_dev);
if (spi_bus_lock_touch(dev_lock)) {
spi_hal_setup_device(hal, hal_dev);
SPI_MASTER_PERI_CLOCK_ATOMIC() {
#if SPI_LL_SUPPORT_CLK_SRC_PRE_DIV
spi_ll_clk_source_pre_div(hal->hw, hal_dev->timing_conf.source_pre_div / 2, 2);/* ... */
#endif
spi_ll_set_clk_source(hal->hw, hal_dev->timing_conf.clock_source);
}{...}
}{...}
}{ ... }
static SPI_MASTER_ISR_ATTR spi_device_t *get_acquiring_dev(spi_host_t *host)
{
spi_bus_lock_dev_handle_t dev_lock = spi_bus_lock_get_acquiring_dev(host->bus_attr->lock);
if (!dev_lock) {
return NULL;
}{...}
return host->device[spi_bus_lock_get_dev_id(dev_lock)];
}{ ... }
static inline SPI_MASTER_ISR_ATTR bool spi_bus_device_is_polling(spi_device_t *dev)
{
return get_acquiring_dev(dev->host) == dev && dev->host->polling;
}{ ... }
/* ... */
static void SPI_MASTER_ISR_ATTR spi_bus_intr_enable(void *host)
{
esp_intr_enable(((spi_host_t*)host)->intr);
}{ ... }
static void SPI_MASTER_ISR_ATTR spi_bus_intr_disable(void *host)
{
esp_intr_disable(((spi_host_t*)host)->intr);
}{ ... }
#if SOC_GDMA_SUPPORTED
#define spi_dma_reset gdma_reset
#define spi_dma_start(chan, addr) gdma_start(chan, (intptr_t)(addr))/* ... */
#endif
static void SPI_MASTER_ISR_ATTR s_spi_dma_prepare_data(spi_host_t *host, spi_hal_context_t *hal, const spi_hal_dev_config_t *dev, const spi_hal_trans_config_t *trans)
{
const spi_dma_ctx_t *dma_ctx = host->dma_ctx;
if (trans->rcv_buffer) {
spicommon_dma_desc_setup_link(dma_ctx->dmadesc_rx, trans->rcv_buffer, ((trans->rx_bitlen + 7) / 8), true);
spi_dma_reset(dma_ctx->rx_dma_chan);
spi_hal_hw_prepare_rx(hal->hw);
spi_dma_start(dma_ctx->rx_dma_chan, dma_ctx->dmadesc_rx);
}{...}
#if CONFIG_IDF_TARGET_ESP32
else if (!dev->half_duplex) {
spi_ll_dma_rx_enable(hal->hw, 1);
spi_dma_start(dma_ctx->rx_dma_chan, NULL);
}{...}
#endif/* ... */
if (trans->send_buffer) {
spicommon_dma_desc_setup_link(dma_ctx->dmadesc_tx, trans->send_buffer, (trans->tx_bitlen + 7) / 8, false);
spi_dma_reset(dma_ctx->tx_dma_chan);
spi_hal_hw_prepare_tx(hal->hw);
spi_dma_start(dma_ctx->tx_dma_chan, dma_ctx->dmadesc_tx);
}{...}
}{ ... }
static void SPI_MASTER_ISR_ATTR s_spi_prepare_data(spi_device_t *dev, const spi_hal_trans_config_t *hal_trans)
{
spi_host_t *host = dev->host;
spi_hal_dev_config_t *hal_dev = &(dev->hal_dev);
spi_hal_context_t *hal = &(host->hal);
if (host->bus_attr->dma_enabled) {
s_spi_dma_prepare_data(host, hal, hal_dev, hal_trans);
}{...} else {
spi_hal_push_tx_buffer(hal, hal_trans);
}{...}
spi_hal_enable_data_line(hal->hw, (!hal_dev->half_duplex && hal_trans->rcv_buffer) || hal_trans->send_buffer, !!hal_trans->rcv_buffer);
}{ ... }
static void SPI_MASTER_ISR_ATTR spi_format_hal_trans_struct(spi_device_t *dev, spi_trans_priv_t *trans_buf, spi_hal_trans_config_t *hal_trans)
{
spi_host_t *host = dev->host;
spi_transaction_t *trans = trans_buf->trans;
hal_trans->tx_bitlen = trans->length;
hal_trans->rx_bitlen = trans->rxlength;
hal_trans->rcv_buffer = (uint8_t*)host->cur_trans_buf.buffer_to_rcv;
hal_trans->send_buffer = (uint8_t*)host->cur_trans_buf.buffer_to_send;
hal_trans->cmd = trans->cmd;
hal_trans->addr = trans->addr;
if (trans->flags & SPI_TRANS_VARIABLE_CMD) {
hal_trans->cmd_bits = ((spi_transaction_ext_t *)trans)->command_bits;
}{...} else {
hal_trans->cmd_bits = dev->cfg.command_bits;
}{...}
if (trans->flags & SPI_TRANS_VARIABLE_ADDR) {
hal_trans->addr_bits = ((spi_transaction_ext_t *)trans)->address_bits;
}{...} else {
hal_trans->addr_bits = dev->cfg.address_bits;
}{...}
if (trans->flags & SPI_TRANS_VARIABLE_DUMMY) {
hal_trans->dummy_bits = ((spi_transaction_ext_t *)trans)->dummy_bits;
}{...} else {
hal_trans->dummy_bits = dev->cfg.dummy_bits;
}{...}
hal_trans->cs_keep_active = (trans->flags & SPI_TRANS_CS_KEEP_ACTIVE) ? 1 : 0;
hal_trans->line_mode.data_lines = (trans->flags & SPI_TRANS_MODE_DIO) ? 2 : (trans->flags & SPI_TRANS_MODE_QIO) ? 4 : 1;
#if SOC_SPI_SUPPORT_OCT
if (trans->flags & SPI_TRANS_MODE_OCT) {
hal_trans->line_mode.data_lines = 8;
}{...}
#endif/* ... */
hal_trans->line_mode.addr_lines = (trans->flags & SPI_TRANS_MULTILINE_ADDR) ? hal_trans->line_mode.data_lines : 1;
hal_trans->line_mode.cmd_lines = (trans->flags & SPI_TRANS_MULTILINE_CMD) ? hal_trans->line_mode.data_lines : 1;
}{ ... }
static void SPI_MASTER_ISR_ATTR spi_new_trans(spi_device_t *dev, spi_trans_priv_t *trans_buf)
{
spi_transaction_t *trans = trans_buf->trans;
spi_hal_context_t *hal = &(dev->host->hal);
spi_hal_dev_config_t *hal_dev = &(dev->hal_dev);
dev->host->cur_cs = dev->id;
spi_setup_device(dev);
spi_hal_trans_config_t hal_trans = {};
spi_format_hal_trans_struct(dev, trans_buf, &hal_trans);
spi_hal_setup_trans(hal, hal_dev, &hal_trans);
s_spi_prepare_data(dev, &hal_trans);
if (dev->cfg.pre_cb) {
dev->cfg.pre_cb(trans);
}{...}
spi_hal_user_start(hal);
}{ ... }
static void SPI_MASTER_ISR_ATTR spi_post_trans(spi_host_t *host)
{
spi_transaction_t *cur_trans = host->cur_trans_buf.trans;
if (!host->bus_attr->dma_enabled) {
spi_hal_fetch_result(&host->hal);
}{...}
spi_device_t* dev = host->device[host->cur_cs];
if (dev->cfg.post_cb) {
dev->cfg.post_cb(cur_trans);
}{...}
host->cur_cs = DEV_NUM_MAX;
}{ ... }
#if SOC_SPI_SCT_SUPPORTED
static void SPI_MASTER_ISR_ATTR spi_sct_set_hal_trans_config(spi_multi_transaction_t *trans_header, spi_hal_trans_config_t *hal_trans)
{
spi_transaction_t *trans = &trans_header->base;
hal_trans->line_mode.data_lines = (trans->flags & SPI_TRANS_MODE_DIO) ? 2 : (trans->flags & SPI_TRANS_MODE_QIO) ? 4 : 1;
#if SOC_SPI_SUPPORT_OCT
if (trans->flags & SPI_TRANS_MODE_OCT) {
hal_trans->line_mode.data_lines = 8;
}{...}
/* ... */#endif
hal_trans->line_mode.addr_lines = (trans->flags & SPI_TRANS_MULTILINE_ADDR) ? hal_trans->line_mode.data_lines : 1;
hal_trans->line_mode.cmd_lines = (trans->flags & SPI_TRANS_MULTILINE_CMD) ? hal_trans->line_mode.data_lines : 1;
}{...}
static void SPI_MASTER_ISR_ATTR s_sct_load_dma_link(spi_device_t *dev, spi_dma_desc_t *rx_seg_head, spi_dma_desc_t *tx_seg_head)
{
spi_hal_context_t *hal = &dev->host->hal;
const spi_dma_ctx_t *dma_ctx = dev->host->dma_ctx;
spi_hal_clear_intr_mask(hal, SPI_LL_INTR_SEG_DONE);
if (rx_seg_head) {
spi_dma_reset(dma_ctx->rx_dma_chan);
spi_hal_hw_prepare_rx(hal->hw);
spi_dma_start(dma_ctx->rx_dma_chan, rx_seg_head);
}{...}
if (tx_seg_head) {
spi_dma_reset(dma_ctx->tx_dma_chan);
spi_hal_hw_prepare_tx(hal->hw);
spi_dma_start(dma_ctx->tx_dma_chan, tx_seg_head);
}{...}
}{...}
static void SPI_MASTER_ISR_ATTR spi_new_sct_trans(spi_device_t *dev, spi_sct_trans_priv_t *cur_sct_trans)
{
dev->host->cur_cs = dev->id;
spi_setup_device(dev);
#if !CONFIG_IDF_TARGET_ESP32S2
spi_hal_sct_set_conf_bits_len(&dev->host->hal, cur_sct_trans->sct_trans_desc_head->sct_gap_len);/* ... */
#endif
s_sct_load_dma_link(dev, cur_sct_trans->rx_seg_head, cur_sct_trans->tx_seg_head);
if (dev->cfg.pre_cb) {
dev->cfg.pre_cb((spi_transaction_t *)cur_sct_trans->sct_trans_desc_head);
}{...}
spi_hal_user_start(&dev->host->hal);
}{...}
static void SPI_MASTER_ISR_ATTR spi_post_sct_trans(spi_host_t *host)
{
if (host->cur_sct_trans.rx_seg_head == NULL) {
assert(host->cur_sct_trans.rx_used_desc_num == 0);
}{...}
free(host->cur_sct_trans.sct_conf_buffer);
portENTER_CRITICAL_ISR(&host->spinlock);
spi_hal_sct_tx_dma_desc_recycle(&host->sct_desc_pool, host->cur_sct_trans.tx_used_desc_num);
spi_hal_sct_rx_dma_desc_recycle(&host->sct_desc_pool, host->cur_sct_trans.rx_used_desc_num);
portEXIT_CRITICAL_ISR(&host->spinlock);
if (host->device[host->cur_cs]->cfg.post_cb) {
host->device[host->cur_cs]->cfg.post_cb((spi_transaction_t *)host->cur_sct_trans.sct_trans_desc_head);
}{...}
host->cur_cs = DEV_NUM_MAX;
}{...}
/* ... */#endif
static void SPI_MASTER_ISR_ATTR spi_intr(void *arg)
{
BaseType_t do_yield = pdFALSE;
spi_host_t *host = (spi_host_t *)arg;
const spi_bus_attr_t* bus_attr = host->bus_attr;
#if CONFIG_IDF_TARGET_ESP32
const spi_dma_ctx_t *dma_ctx = host->dma_ctx;/* ... */
#endif
#if SOC_SPI_SCT_SUPPORTED
assert(spi_hal_usr_is_done(&host->hal) || spi_hal_get_intr_mask(&host->hal, SPI_LL_INTR_SEG_DONE));
#else
assert(spi_hal_usr_is_done(&host->hal));
#endif
/* ... */
if (!spi_bus_lock_bg_entry(bus_attr->lock)) {
assert(host->cur_cs != DEV_NUM_MAX);
const int cs = host->cur_cs;
if (bus_attr->dma_enabled) {
#if CONFIG_IDF_TARGET_ESP32
spicommon_dmaworkaround_idle(dma_ctx->tx_dma_chan.chan_id);/* ... */
#endif
#if SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
if (host->cur_trans_buf.buffer_to_rcv) {
uint16_t alignment = bus_attr->internal_mem_align_size;
uint32_t buffer_byte_len = (host->cur_trans_buf.trans->rxlength + 7) / 8;
buffer_byte_len = (buffer_byte_len + alignment - 1) & (~(alignment - 1));
esp_err_t ret = esp_cache_msync((void *)host->cur_trans_buf.buffer_to_rcv, buffer_byte_len, ESP_CACHE_MSYNC_FLAG_DIR_M2C);
assert(ret == ESP_OK);
}{...}
#endif/* ... */
}{...}
#if SOC_SPI_SCT_SUPPORTED
if (host->sct_mode_enabled) {
spi_post_sct_trans(host);
if (!(host->device[cs]->cfg.flags & SPI_DEVICE_NO_RETURN_RESULT)) {
xQueueSendFromISR(host->device[cs]->ret_queue, &host->cur_sct_trans, &do_yield);
}{...}
}{...} else
#endif
{
spi_post_trans(host);
if (!(host->device[cs]->cfg.flags & SPI_DEVICE_NO_RETURN_RESULT)) {
xQueueSendFromISR(host->device[cs]->ret_queue, &host->cur_trans_buf, &do_yield);
}{...}
}{...}
#ifdef CONFIG_PM_ENABLE
esp_pm_lock_release(bus_attr->pm_lock);/* ... */
#endif
}{...}
assert(host->cur_cs == DEV_NUM_MAX);
spi_bus_lock_handle_t lock = host->bus_attr->lock;
BaseType_t trans_found = pdFALSE;
BUS_LOCK_DEBUG_EXECUTE_CHECK(spi_bus_lock_bg_req_exist(lock));
do {
spi_bus_lock_dev_handle_t acq_dev_lock = spi_bus_lock_get_acquiring_dev(lock);
spi_bus_lock_dev_handle_t desired_dev = acq_dev_lock;
bool resume_task = false;
spi_device_t* device_to_send = NULL;
if (!acq_dev_lock) {
resume_task = spi_bus_lock_bg_check_dev_acq(lock, &desired_dev);
}{...}
if (!resume_task) {
assert(desired_dev);
bool dev_has_req = spi_bus_lock_bg_check_dev_req(desired_dev);
if (dev_has_req) {
device_to_send = host->device[spi_bus_lock_get_dev_id(desired_dev)];
#if SOC_SPI_SCT_SUPPORTED
if (host->sct_mode_enabled) {
trans_found = xQueueReceiveFromISR(device_to_send->trans_queue, &host->cur_sct_trans, &do_yield);
}{...} else
#endif
{
trans_found = xQueueReceiveFromISR(device_to_send->trans_queue, &host->cur_trans_buf, &do_yield);
}{...}
if (!trans_found) {
spi_bus_lock_bg_clear_req(desired_dev);
}{...}
}{...}
}{...}
if (trans_found) {
#if SOC_SPI_SCT_SUPPORTED
if (host->sct_mode_enabled) {
spi_new_sct_trans(device_to_send, &host->cur_sct_trans);
}{...} else
#endif
{
spi_trans_priv_t *const cur_trans_buf = &host->cur_trans_buf;
#if CONFIG_IDF_TARGET_ESP32
if (bus_attr->dma_enabled && (cur_trans_buf->buffer_to_rcv || cur_trans_buf->buffer_to_send)) {
spicommon_dmaworkaround_transfer_active(dma_ctx->tx_dma_chan.chan_id);
}{...}
#endif/* ... */
spi_new_trans(device_to_send, cur_trans_buf);
}{...}
}{...}
}{...} while (!spi_bus_lock_bg_exit(lock, trans_found, &do_yield));
if (do_yield) {
portYIELD_FROM_ISR();
}{...}
}{ ... }
static SPI_MASTER_ISR_ATTR esp_err_t check_trans_valid(spi_device_handle_t handle, spi_transaction_t *trans_desc)
{
SPI_CHECK(handle != NULL, "invalid dev handle", ESP_ERR_INVALID_ARG);
spi_host_t *host = handle->host;
const spi_bus_attr_t* bus_attr = host->bus_attr;
bool tx_enabled = (trans_desc->flags & SPI_TRANS_USE_TXDATA) || (trans_desc->tx_buffer);
bool rx_enabled = (trans_desc->flags & SPI_TRANS_USE_RXDATA) || (trans_desc->rx_buffer);
spi_transaction_ext_t *t_ext = (spi_transaction_ext_t *)trans_desc;
bool dummy_enabled = (((trans_desc->flags & SPI_TRANS_VARIABLE_DUMMY) ? t_ext->dummy_bits : handle->cfg.dummy_bits) != 0);
bool extra_dummy_enabled = handle->hal_dev.timing_conf.timing_dummy;
bool is_half_duplex = ((handle->cfg.flags & SPI_DEVICE_HALFDUPLEX) != 0);
SPI_CHECK((trans_desc->flags & SPI_TRANS_USE_RXDATA) == 0 || trans_desc->rxlength <= 32, "SPI_TRANS_USE_RXDATA only available for rxdata transfer <= 32 bits", ESP_ERR_INVALID_ARG);
SPI_CHECK((trans_desc->flags & SPI_TRANS_USE_TXDATA) == 0 || trans_desc->length <= 32, "SPI_TRANS_USE_TXDATA only available for txdata transfer <= 32 bits", ESP_ERR_INVALID_ARG);
SPI_CHECK(trans_desc->length <= bus_attr->max_transfer_sz * 8, "txdata transfer > host maximum", ESP_ERR_INVALID_ARG);
SPI_CHECK(trans_desc->rxlength <= bus_attr->max_transfer_sz * 8, "rxdata transfer > host maximum", ESP_ERR_INVALID_ARG);
SPI_CHECK(is_half_duplex || trans_desc->rxlength <= trans_desc->length, "rx length > tx length in full duplex mode", ESP_ERR_INVALID_ARG);
#if SOC_SPI_SUPPORT_OCT
SPI_CHECK(!(host->id == SPI3_HOST && trans_desc->flags & SPI_TRANS_MODE_OCT), "SPI3 does not support octal mode", ESP_ERR_INVALID_ARG);
SPI_CHECK(!((trans_desc->flags & SPI_TRANS_MODE_OCT) && (handle->cfg.flags & SPI_DEVICE_3WIRE)), "Incompatible when setting to both Octal mode and 3-wire-mode", ESP_ERR_INVALID_ARG);
SPI_CHECK(!((trans_desc->flags & SPI_TRANS_MODE_OCT) && !is_half_duplex), "Incompatible when setting to both Octal mode and half duplex mode", ESP_ERR_INVALID_ARG);/* ... */
#endif
SPI_CHECK(!((trans_desc->flags & (SPI_TRANS_MODE_DIO | SPI_TRANS_MODE_QIO)) && (handle->cfg.flags & SPI_DEVICE_3WIRE)), "Incompatible when setting to both multi-line mode and 3-wire-mode", ESP_ERR_INVALID_ARG);
SPI_CHECK(!((trans_desc->flags & (SPI_TRANS_MODE_DIO | SPI_TRANS_MODE_QIO)) && !is_half_duplex), "Incompatible when setting to both multi-line mode and half duplex mode", ESP_ERR_INVALID_ARG);
#ifdef CONFIG_IDF_TARGET_ESP32
SPI_CHECK(!is_half_duplex || !bus_attr->dma_enabled || !rx_enabled || !tx_enabled, "SPI half duplex mode does not support using DMA with both MOSI and MISO phases.", ESP_ERR_INVALID_ARG);
#endif
#if !SOC_SPI_HD_BOTH_INOUT_SUPPORTED
SPI_CHECK(!is_half_duplex || !tx_enabled || !rx_enabled, "SPI half duplex mode is not supported when both MOSI and MISO phases are enabled.", ESP_ERR_INVALID_ARG);
SPI_CHECK(!is_half_duplex || !trans_desc->length || !trans_desc->rxlength, "SPI half duplex mode is not supported when both MOSI and MISO phases are enabled.", ESP_ERR_INVALID_ARG);/* ... */
#endif
SPI_CHECK(trans_desc->length != 0 || !tx_enabled, "trans tx_buffer should be NULL and SPI_TRANS_USE_TXDATA should be cleared to skip MOSI phase.", ESP_ERR_INVALID_ARG);
SPI_CHECK(!is_half_duplex || trans_desc->rxlength != 0 || !rx_enabled, "trans rx_buffer should be NULL and SPI_TRANS_USE_RXDATA should be cleared to skip MISO phase.", ESP_ERR_INVALID_ARG);
if (trans_desc->rxlength == 0 && !is_half_duplex) {
trans_desc->rxlength = trans_desc->length;
}{...}
SPI_CHECK(!tx_enabled || !rx_enabled || !dummy_enabled || !extra_dummy_enabled, "Dummy phase is not available when both data out and in are enabled", ESP_ERR_INVALID_ARG);
if (bus_attr->dma_enabled) {
SPI_CHECK(trans_desc->length <= SPI_LL_DMA_MAX_BIT_LEN, "txdata transfer > hardware max supported len", ESP_ERR_INVALID_ARG);
SPI_CHECK(trans_desc->rxlength <= SPI_LL_DMA_MAX_BIT_LEN, "rxdata transfer > hardware max supported len", ESP_ERR_INVALID_ARG);
}{...} else {
SPI_CHECK(trans_desc->length <= SPI_LL_CPU_MAX_BIT_LEN, "txdata transfer > hardware max supported len", ESP_ERR_INVALID_ARG);
SPI_CHECK(trans_desc->rxlength <= SPI_LL_CPU_MAX_BIT_LEN, "rxdata transfer > hardware max supported len", ESP_ERR_INVALID_ARG);
}{...}
return ESP_OK;
}{ ... }
static SPI_MASTER_ISR_ATTR void uninstall_priv_desc(spi_trans_priv_t* trans_buf)
{
spi_transaction_t *trans_desc = trans_buf->trans;
if ((void *)trans_buf->buffer_to_send != &trans_desc->tx_data[0] &&
trans_buf->buffer_to_send != trans_desc->tx_buffer) {
free((void *)trans_buf->buffer_to_send);
}{...}
if (trans_buf->buffer_to_rcv && (void *)trans_buf->buffer_to_rcv != &trans_desc->rx_data[0] && trans_buf->buffer_to_rcv != trans_desc->rx_buffer) {
if (trans_desc->flags & SPI_TRANS_USE_RXDATA) {
memcpy((uint8_t *) & trans_desc->rx_data[0], trans_buf->buffer_to_rcv, (trans_desc->rxlength + 7) / 8);
}{...} else {
memcpy(trans_desc->rx_buffer, trans_buf->buffer_to_rcv, (trans_desc->rxlength + 7) / 8);
}{...}
free(trans_buf->buffer_to_rcv);
}{...}
}{ ... }
static SPI_MASTER_ISR_ATTR esp_err_t setup_priv_desc(spi_host_t *host, spi_trans_priv_t* priv_desc)
{
spi_transaction_t *trans_desc = priv_desc->trans;
const spi_bus_attr_t *bus_attr = host->bus_attr;
uint16_t alignment = bus_attr->internal_mem_align_size;
uint32_t* rcv_ptr;
if (trans_desc->flags & SPI_TRANS_USE_RXDATA) {
rcv_ptr = (uint32_t *)&trans_desc->rx_data[0];
}{...} else {
rcv_ptr = trans_desc->rx_buffer;
}{...}
const uint32_t *send_ptr;
if (trans_desc->flags & SPI_TRANS_USE_TXDATA) {
send_ptr = (uint32_t *)&trans_desc->tx_data[0];
}{...} else {
send_ptr = trans_desc->tx_buffer ;
}{...}
uint32_t tx_byte_len = (trans_desc->length + 7) / 8;
uint32_t rx_byte_len = (trans_desc->rxlength + 7) / 8;
#if SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
bool tx_unaligned = ((((uint32_t)send_ptr) | tx_byte_len) & (alignment - 1));
bool rx_unaligned = ((((uint32_t)rcv_ptr) | rx_byte_len) & (alignment - 1));/* ... */
#else
bool tx_unaligned = false;
bool rx_unaligned = (((uint32_t)rcv_ptr) & (alignment - 1));/* ... */
#endif
if (send_ptr && bus_attr->dma_enabled) {
if ((!esp_ptr_dma_capable(send_ptr) || tx_unaligned)) {
ESP_RETURN_ON_FALSE(!(trans_desc->flags & SPI_TRANS_DMA_BUFFER_ALIGN_MANUAL), ESP_ERR_INVALID_ARG, SPI_TAG, "Set flag SPI_TRANS_DMA_BUFFER_ALIGN_MANUAL but TX buffer addr&len not align to %d byte, or not dma_capable", alignment);
ESP_EARLY_LOGD(SPI_TAG, "Allocate TX buffer for DMA");
tx_byte_len = (tx_byte_len + alignment - 1) & (~(alignment - 1));
uint32_t *temp = heap_caps_aligned_alloc(alignment, tx_byte_len, MALLOC_CAP_DMA);
if (temp == NULL) {
goto clean_up;
}{...}
memcpy(temp, send_ptr, (trans_desc->length + 7) / 8);
send_ptr = temp;
}{...}
#if SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
esp_err_t ret = esp_cache_msync((void *)send_ptr, tx_byte_len, ESP_CACHE_MSYNC_FLAG_DIR_C2M);
assert(ret == ESP_OK);/* ... */
#endif
}{...}
if (rcv_ptr && bus_attr->dma_enabled) {
if ((!esp_ptr_dma_capable(rcv_ptr) || rx_unaligned)) {
ESP_RETURN_ON_FALSE(!(trans_desc->flags & SPI_TRANS_DMA_BUFFER_ALIGN_MANUAL), ESP_ERR_INVALID_ARG, SPI_TAG, "Set flag SPI_TRANS_DMA_BUFFER_ALIGN_MANUAL but RX buffer addr&len not align to %d byte, or not dma_capable", alignment);
ESP_EARLY_LOGD(SPI_TAG, "Allocate RX buffer for DMA");
rx_byte_len = (rx_byte_len + alignment - 1) & (~(alignment - 1));
rcv_ptr = heap_caps_aligned_alloc(alignment, rx_byte_len, MALLOC_CAP_DMA);
if (rcv_ptr == NULL) {
goto clean_up;
}{...}
}{...}
#if SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
esp_err_t ret = esp_cache_msync((void *)rcv_ptr, rx_byte_len, ESP_CACHE_MSYNC_FLAG_DIR_M2C);
assert(ret == ESP_OK);/* ... */
#endif
}{...}
priv_desc->buffer_to_send = send_ptr;
priv_desc->buffer_to_rcv = rcv_ptr;
return ESP_OK;
clean_up:
uninstall_priv_desc(priv_desc);
return ESP_ERR_NO_MEM;
}{ ... }
esp_err_t SPI_MASTER_ATTR spi_device_queue_trans(spi_device_handle_t handle, spi_transaction_t *trans_desc, TickType_t ticks_to_wait)
{
esp_err_t ret = check_trans_valid(handle, trans_desc);
if (ret != ESP_OK) {
return ret;
}{...}
spi_host_t *host = handle->host;
SPI_CHECK(!spi_bus_device_is_polling(handle), "Cannot queue new transaction while previous polling transaction is not terminated.", ESP_ERR_INVALID_STATE);
/* ... */
if (host->device_acquiring_lock != handle && (trans_desc->flags & SPI_TRANS_CS_KEEP_ACTIVE)) {
return ESP_ERR_INVALID_ARG;
}{...}
spi_trans_priv_t trans_buf = { .trans = trans_desc, };
ret = setup_priv_desc(host, &trans_buf);
if (ret != ESP_OK) {
return ret;
}{...}
#ifdef CONFIG_PM_ENABLE
esp_pm_lock_acquire(host->bus_attr->pm_lock);/* ... */
#endif
BaseType_t r = xQueueSend(handle->trans_queue, (void *)&trans_buf, ticks_to_wait);
if (!r) {
ret = ESP_ERR_TIMEOUT;
#ifdef CONFIG_PM_ENABLE
esp_pm_lock_release(host->bus_attr->pm_lock);/* ... */
#endif
goto clean_up;
}{...}
ret = spi_bus_lock_bg_request(handle->dev_lock);
if (ret != ESP_OK) {
goto clean_up;
}{...}
return ESP_OK;
clean_up:
uninstall_priv_desc(&trans_buf);
return ret;
}{ ... }
esp_err_t SPI_MASTER_ATTR spi_device_get_trans_result(spi_device_handle_t handle, spi_transaction_t **trans_desc, TickType_t ticks_to_wait)
{
BaseType_t r;
spi_trans_priv_t trans_buf;
SPI_CHECK(handle != NULL, "invalid dev handle", ESP_ERR_INVALID_ARG);
bool use_dma = handle->host->bus_attr->dma_enabled;
SPI_CHECK(!(handle->cfg.flags & SPI_DEVICE_NO_RETURN_RESULT), "API not Supported!", ESP_ERR_NOT_SUPPORTED);
r = xQueueReceive(handle->ret_queue, (void*)&trans_buf, ticks_to_wait);
if (!r) {
return ESP_ERR_TIMEOUT;
}{...}
if (use_dma) {
uninstall_priv_desc(&trans_buf);
}{...}
(*trans_desc) = trans_buf.trans;
return ESP_OK;
}{ ... }
esp_err_t SPI_MASTER_ATTR spi_device_transmit(spi_device_handle_t handle, spi_transaction_t *trans_desc)
{
esp_err_t ret;
spi_transaction_t *ret_trans;
ret = spi_device_queue_trans(handle, trans_desc, portMAX_DELAY);
if (ret != ESP_OK) {
return ret;
}{...}
ret = spi_device_get_trans_result(handle, &ret_trans, portMAX_DELAY);
if (ret != ESP_OK) {
return ret;
}{...}
assert(ret_trans == trans_desc);
return ESP_OK;
}{ ... }
esp_err_t SPI_MASTER_ISR_ATTR spi_device_acquire_bus(spi_device_t *device, TickType_t wait)
{
spi_host_t *const host = device->host;
SPI_CHECK(wait == portMAX_DELAY, "acquire finite time not supported now.", ESP_ERR_INVALID_ARG);
SPI_CHECK(!spi_bus_device_is_polling(device), "Cannot acquire bus when a polling transaction is in progress.", ESP_ERR_INVALID_STATE);
esp_err_t ret = spi_bus_lock_acquire_start(device->dev_lock, wait);
if (ret != ESP_OK) {
return ret;
}{...}
host->device_acquiring_lock = device;
ESP_LOGD(SPI_TAG, "device%d locked the bus", device->id);
#ifdef CONFIG_PM_ENABLE
esp_pm_lock_acquire(host->bus_attr->pm_lock);/* ... */
#endif
spi_setup_device(host->device[device->id]);
#if CONFIG_IDF_TARGET_ESP32
if (host->bus_attr->dma_enabled) {
spicommon_dmaworkaround_transfer_active(host->dma_ctx->tx_dma_chan.chan_id);
}{...}
#endif/* ... */
return ESP_OK;
}{ ... }
void SPI_MASTER_ISR_ATTR spi_device_release_bus(spi_device_t *dev)
{
spi_host_t *host = dev->host;
if (spi_bus_device_is_polling(dev)) {
ESP_EARLY_LOGE(SPI_TAG, "Cannot release bus when a polling transaction is in progress.");
assert(0);
}{...}
#if CONFIG_IDF_TARGET_ESP32
if (host->bus_attr->dma_enabled) {
spicommon_dmaworkaround_idle(host->dma_ctx->tx_dma_chan.chan_id);
}{...}
/* ... */
#endif
#ifdef CONFIG_PM_ENABLE
esp_pm_lock_release(host->bus_attr->pm_lock);/* ... */
#endif
ESP_LOGD(SPI_TAG, "device%d release bus", dev->id);
host->device_acquiring_lock = NULL;
esp_err_t ret = spi_bus_lock_acquire_end(dev->dev_lock);
assert(ret == ESP_OK);
(void) ret;
}{ ... }
esp_err_t SPI_MASTER_ISR_ATTR spi_device_polling_start(spi_device_handle_t handle, spi_transaction_t *trans_desc, TickType_t ticks_to_wait)
{
esp_err_t ret;
SPI_CHECK(ticks_to_wait == portMAX_DELAY, "currently timeout is not available for polling transactions", ESP_ERR_INVALID_ARG);
ret = check_trans_valid(handle, trans_desc);
if (ret != ESP_OK) {
return ret;
}{...}
SPI_CHECK(!spi_bus_device_is_polling(handle), "Cannot send polling transaction while the previous polling transaction is not terminated.", ESP_ERR_INVALID_STATE);
spi_host_t *host = handle->host;
spi_trans_priv_t priv_polling_trans = { .trans = trans_desc, };
ret = setup_priv_desc(host, &priv_polling_trans);
if (ret != ESP_OK) {
return ret;
}{...}
/* ... */
if (host->device_acquiring_lock != handle) {
if ((trans_desc->flags & SPI_TRANS_CS_KEEP_ACTIVE) != 0) {
ret = ESP_ERR_INVALID_ARG;
}{...} else {
ret = spi_bus_lock_acquire_start(handle->dev_lock, ticks_to_wait);
}{...}
}{...} else {
ret = spi_bus_lock_wait_bg_done(handle->dev_lock, ticks_to_wait);
}{...}
if (ret != ESP_OK) {
uninstall_priv_desc(&priv_polling_trans);
ESP_LOGE(SPI_TAG, "polling can't get buslock");
return ret;
}{...}
host->polling = true;
host->cur_trans_buf = priv_polling_trans;
ESP_LOGV(SPI_TAG, "polling trans");
spi_new_trans(handle, &host->cur_trans_buf);
return ESP_OK;
}{ ... }
esp_err_t SPI_MASTER_ISR_ATTR spi_device_polling_end(spi_device_handle_t handle, TickType_t ticks_to_wait)
{
SPI_CHECK(handle != NULL, "invalid dev handle", ESP_ERR_INVALID_ARG);
spi_host_t *host = handle->host;
assert(host->cur_cs == handle->id);
assert(handle == get_acquiring_dev(host));
TickType_t start = xTaskGetTickCount();
while (!spi_hal_usr_is_done(&host->hal)) {
TickType_t end = xTaskGetTickCount();
if (end - start > ticks_to_wait) {
return ESP_ERR_TIMEOUT;
}{...}
}{...}
#if SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
const spi_bus_attr_t *bus_attr = host->bus_attr;
if (bus_attr->dma_enabled && host->cur_trans_buf.buffer_to_rcv) {
uint16_t alignment = bus_attr->internal_mem_align_size;
uint32_t buffer_byte_len = (host->cur_trans_buf.trans->rxlength + 7) / 8;
buffer_byte_len = (buffer_byte_len + alignment - 1) & (~(alignment - 1));
esp_err_t ret = esp_cache_msync((void *)host->cur_trans_buf.buffer_to_rcv, buffer_byte_len, ESP_CACHE_MSYNC_FLAG_DIR_M2C);
if (ret != ESP_OK) {
return ret;
}{...}
}{...}
#endif/* ... */
ESP_LOGV(SPI_TAG, "polling trans done");
spi_post_trans(host);
uninstall_priv_desc(&host->cur_trans_buf);
host->polling = false;
/* ... */
if (host->device_acquiring_lock != handle) {
assert(host->device_acquiring_lock == NULL);
spi_bus_lock_acquire_end(handle->dev_lock);
}{...}
return ESP_OK;
}{ ... }
esp_err_t SPI_MASTER_ISR_ATTR spi_device_polling_transmit(spi_device_handle_t handle, spi_transaction_t* trans_desc)
{
esp_err_t ret;
ret = spi_device_polling_start(handle, trans_desc, portMAX_DELAY);
if (ret != ESP_OK) {
return ret;
}{...}
return spi_device_polling_end(handle, portMAX_DELAY);
}{ ... }
esp_err_t spi_bus_get_max_transaction_len(spi_host_device_t host_id, size_t *max_bytes)
{
SPI_CHECK(is_valid_host(host_id), "invalid host", ESP_ERR_INVALID_ARG);
if (bus_driver_ctx[host_id] == NULL || max_bytes == NULL) {
return ESP_ERR_INVALID_ARG;
}{...}
spi_host_t *host = bus_driver_ctx[host_id];
if (host->bus_attr->dma_enabled) {
*max_bytes = MIN(host->bus_attr->max_transfer_sz, (SPI_LL_DMA_MAX_BIT_LEN / 8));
}{...} else {
*max_bytes = MIN(host->bus_attr->max_transfer_sz, (SPI_LL_CPU_MAX_BIT_LEN / 8));
}{...}
return ESP_OK;
}{ ... }
#if SOC_SPI_SCT_SUPPORTED
/* ... */
static SPI_MASTER_ISR_ATTR spi_dma_desc_t *s_sct_setup_desc_anywhere(spi_dma_desc_t *desc_root, spi_dma_desc_t *desc_start, uint32_t desc_num, const void *data, int len, bool is_rx)
{
while (len) {
int dmachunklen = len;
if (dmachunklen > LLDESC_MAX_NUM_PER_DESC) {
dmachunklen = LLDESC_MAX_NUM_PER_DESC;
}{...}
if (is_rx) {
desc_start->dw0.size = (dmachunklen + 3) & (~3);
}{...} else {
desc_start->dw0.size = dmachunklen;
desc_start->dw0.length = dmachunklen;
}{...}
desc_start->buffer = (uint8_t *)data;
desc_start->dw0.suc_eof = 0;
desc_start->dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_DMA;
len -= dmachunklen;
data += dmachunklen;
if (len) {
desc_start->next = (desc_start == &desc_root[desc_num - 1]) ? desc_root : &desc_start[1];
desc_start = desc_start->next;
}{...}
}{...}
desc_start->dw0.suc_eof = 1;
desc_start->next = NULL;
return desc_start;
}{...}
static SPI_MASTER_ISR_ATTR int s_sct_desc_get_required_num(uint32_t bytes_len)
{
return (bytes_len + LLDESC_MAX_NUM_PER_DESC - 1) / LLDESC_MAX_NUM_PER_DESC;
}{...}
/* ... */
static void SPI_MASTER_ISR_ATTR spi_hal_sct_tx_dma_desc_recycle(spi_sct_desc_ctx_t *desc_ctx, uint32_t recycle_num)
{
desc_ctx->tx_free_desc_num += recycle_num;
}{...}
static void SPI_MASTER_ISR_ATTR s_sct_prepare_tx_seg(spi_sct_desc_ctx_t *desc_ctx, const uint32_t conf_buffer[SOC_SPI_SCT_BUFFER_NUM_MAX], const void *send_buffer, uint32_t buf_len_bytes, spi_dma_desc_t **trans_head)
{
const spi_dma_ctx_t *dma_ctx = __containerof(desc_ctx, spi_host_t, sct_desc_pool)->dma_ctx;
*trans_head = desc_ctx->cur_tx_seg_link;
s_sct_setup_desc_anywhere(dma_ctx->dmadesc_tx, desc_ctx->cur_tx_seg_link, dma_ctx->dma_desc_num, conf_buffer, SOC_SPI_SCT_BUFFER_NUM_MAX * 4, false);
spi_dma_desc_t *conf_buffer_link = desc_ctx->cur_tx_seg_link;
desc_ctx->tx_free_desc_num -= 1;
desc_ctx->tx_seg_link_tail = desc_ctx->cur_tx_seg_link;
desc_ctx->cur_tx_seg_link++;
if (desc_ctx->cur_tx_seg_link == dma_ctx->dmadesc_tx + dma_ctx->dma_desc_num) {
desc_ctx->cur_tx_seg_link = dma_ctx->dmadesc_tx;
}{...}
if (send_buffer && buf_len_bytes) {
desc_ctx->tx_seg_link_tail = s_sct_setup_desc_anywhere(dma_ctx->dmadesc_tx, desc_ctx->cur_tx_seg_link, dma_ctx->dma_desc_num, send_buffer, buf_len_bytes, false);
conf_buffer_link->next = desc_ctx->cur_tx_seg_link;
desc_ctx->cur_tx_seg_link = desc_ctx->tx_seg_link_tail + 1;
if (desc_ctx->cur_tx_seg_link == dma_ctx->dmadesc_tx + dma_ctx->dma_desc_num) {
desc_ctx->cur_tx_seg_link = dma_ctx->dmadesc_tx;
}{...}
desc_ctx->tx_free_desc_num -= s_sct_desc_get_required_num(buf_len_bytes);
}{...}
}{...}
static esp_err_t SPI_MASTER_ISR_ATTR spi_hal_sct_new_tx_dma_desc_head(spi_sct_desc_ctx_t *desc_ctx, const uint32_t conf_buffer[SOC_SPI_SCT_BUFFER_NUM_MAX], const void *send_buffer, uint32_t buf_len_bytes, spi_dma_desc_t **trans_head, uint32_t *used_desc_num)
{
int desc_need = 1 + s_sct_desc_get_required_num(buf_len_bytes);
if (desc_ctx->tx_free_desc_num < desc_need) {
return ESP_ERR_NO_MEM;
}{...}
s_sct_prepare_tx_seg(desc_ctx, conf_buffer, send_buffer, buf_len_bytes, trans_head);
*used_desc_num = desc_need;
return ESP_OK;
}{...}
static esp_err_t SPI_MASTER_ISR_ATTR spi_hal_sct_link_tx_seg_dma_desc(spi_sct_desc_ctx_t *desc_ctx, const uint32_t conf_buffer[SOC_SPI_SCT_BUFFER_NUM_MAX], const void *send_buffer, uint32_t buf_len_bytes, uint32_t *used_desc_num)
{
int desc_need = 1 + s_sct_desc_get_required_num(buf_len_bytes);
if (desc_ctx->tx_free_desc_num < desc_need) {
return ESP_ERR_NO_MEM;
}{...}
if (desc_ctx->tx_seg_link_tail) {
desc_ctx->tx_seg_link_tail->next = desc_ctx->cur_tx_seg_link;
}{...}
spi_dma_desc_t *internal_head = NULL;
s_sct_prepare_tx_seg(desc_ctx, conf_buffer, send_buffer, buf_len_bytes, &internal_head);
*used_desc_num += desc_need;
return ESP_OK;
}{...}
static void SPI_MASTER_ISR_ATTR spi_hal_sct_rx_dma_desc_recycle(spi_sct_desc_ctx_t *desc_ctx, uint32_t recycle_num)
{
desc_ctx->rx_free_desc_num += recycle_num;
}{...}
static void SPI_MASTER_ISR_ATTR s_sct_prepare_rx_seg(spi_sct_desc_ctx_t *desc_ctx, const void *recv_buffer, uint32_t buf_len_bytes, spi_dma_desc_t **trans_head)
{
const spi_dma_ctx_t *dma_ctx = __containerof(desc_ctx, spi_host_t, sct_desc_pool)->dma_ctx;
*trans_head = desc_ctx->cur_rx_seg_link;
desc_ctx->rx_seg_link_tail = s_sct_setup_desc_anywhere(dma_ctx->dmadesc_rx, desc_ctx->cur_rx_seg_link, dma_ctx->dma_desc_num, recv_buffer, buf_len_bytes, true);
desc_ctx->cur_rx_seg_link = desc_ctx->rx_seg_link_tail + 1;
if (desc_ctx->cur_rx_seg_link == dma_ctx->dmadesc_rx + dma_ctx->dma_desc_num) {
desc_ctx->cur_rx_seg_link = dma_ctx->dmadesc_rx;
}{...}
desc_ctx->rx_free_desc_num -= s_sct_desc_get_required_num(buf_len_bytes);
}{...}
static esp_err_t SPI_MASTER_ISR_ATTR spi_hal_sct_new_rx_dma_desc_head(spi_sct_desc_ctx_t *desc_ctx, const void *recv_buffer, uint32_t buf_len_bytes, spi_dma_desc_t **trans_head, uint32_t *used_desc_num)
{
int desc_need = s_sct_desc_get_required_num(buf_len_bytes);
if (desc_ctx->rx_free_desc_num < desc_need) {
return ESP_ERR_NO_MEM;
}{...}
s_sct_prepare_rx_seg(desc_ctx, recv_buffer, buf_len_bytes, trans_head);
*used_desc_num = desc_need;
return ESP_OK;
}{...}
static esp_err_t SPI_MASTER_ISR_ATTR spi_hal_sct_link_rx_seg_dma_desc(spi_sct_desc_ctx_t *desc_ctx, const void *recv_buffer, uint32_t buf_len_bytes, uint32_t *used_desc_num)
{
int desc_need = s_sct_desc_get_required_num(buf_len_bytes);
if (desc_ctx->rx_free_desc_num < desc_need) {
return ESP_ERR_NO_MEM;
}{...}
if (desc_ctx->rx_seg_link_tail) {
desc_ctx->rx_seg_link_tail->next = desc_ctx->cur_rx_seg_link;
}{...}
spi_dma_desc_t *internal_head = NULL;
s_sct_prepare_rx_seg(desc_ctx, recv_buffer, buf_len_bytes, &internal_head);
*used_desc_num += desc_need;
return ESP_OK;
}{...}
static void s_spi_sct_reset_dma_pool(const spi_dma_ctx_t *dma_ctx, spi_sct_desc_ctx_t *sct_desc_pool)
{
sct_desc_pool->tx_free_desc_num = dma_ctx->dma_desc_num;
sct_desc_pool->rx_free_desc_num = dma_ctx->dma_desc_num;
sct_desc_pool->cur_tx_seg_link = dma_ctx->dmadesc_tx;
sct_desc_pool->cur_rx_seg_link = dma_ctx->dmadesc_rx;
sct_desc_pool->tx_seg_link_tail = NULL;
sct_desc_pool->rx_seg_link_tail = NULL;
}{...}
/* ... */
esp_err_t spi_bus_multi_trans_mode_enable(spi_device_handle_t handle, bool enable)
{
SPI_CHECK(handle, "Invalid arguments.", ESP_ERR_INVALID_ARG);
SPI_CHECK(SOC_SPI_SCT_SUPPORTED_PERIPH(handle->host->id), "Invalid arguments", ESP_ERR_INVALID_ARG);
SPI_CHECK(handle->cfg.flags & SPI_DEVICE_HALFDUPLEX, "SCT mode only available under Half Duplex mode", ESP_ERR_INVALID_STATE);
SPI_CHECK(!spi_bus_device_is_polling(handle), "Cannot queue new transaction while previous polling transaction is not terminated.", ESP_ERR_INVALID_STATE);
SPI_CHECK(uxQueueMessagesWaiting(handle->trans_queue) == 0, "Cannot enable SCT mode when internal Queue still has items", ESP_ERR_INVALID_STATE);
esp_err_t ret = ESP_OK;
if (enable) {
/* ... */
spi_transaction_t fake_trans = {
.flags = SPI_TRANS_USE_RXDATA | SPI_TRANS_USE_TXDATA,
.length = 8,
.tx_data = {0xff},
}{...};
spi_host_t *host = handle->host;
spi_trans_priv_t trans_buf = { .trans = &fake_trans };
spi_hal_context_t *hal = &handle->host->hal;
spi_hal_dev_config_t *hal_dev = &handle->hal_dev;
ret = setup_priv_desc(host, &trans_buf);
if (ret != ESP_OK) {
return ret;
}{...}
spi_hal_setup_device(hal, hal_dev);
spi_hal_trans_config_t hal_trans = {};
spi_format_hal_trans_struct(handle, &trans_buf, &hal_trans);
spi_hal_setup_trans(hal, hal_dev, &hal_trans);
#if CONFIG_IDF_TARGET_ESP32S2
spi_hal_sct_setup_conf_base(hal, handle->hal_dev.timing_conf.real_freq / 600000);/* ... */
#endif
s_spi_sct_reset_dma_pool(host->dma_ctx, &host->sct_desc_pool);
spi_hal_sct_init(hal);
}{...} else {
spi_hal_sct_deinit(&handle->host->hal);
}{...}
handle->host->sct_mode_enabled = enable;
return ESP_OK;
}{...}
static void SPI_MASTER_ATTR s_sct_init_conf_buffer(spi_hal_context_t *hal, uint32_t *buffer, uint32_t trans_num)
{
uint32_t conf_buffer_img[SOC_SPI_SCT_BUFFER_NUM_MAX];
spi_hal_sct_init_conf_buffer(hal, conf_buffer_img);
for (int i = 0; i < trans_num; i++) {
memcpy(&buffer[i * SOC_SPI_SCT_BUFFER_NUM_MAX], conf_buffer_img, sizeof(conf_buffer_img));
}{...}
}{...}
static void SPI_MASTER_ATTR s_sct_format_conf_buffer(spi_device_handle_t handle, spi_multi_transaction_t *seg_trans_desc, uint32_t *buffer, bool seg_end)
{
spi_hal_context_t *hal = &handle->host->hal;
spi_hal_dev_config_t *hal_dev = &handle->hal_dev;
spi_hal_seg_config_t seg_config = {};
if (seg_trans_desc->seg_trans_flags & SPI_MULTI_TRANS_PREP_LEN_UPDATED) {
seg_config.cs_setup = seg_trans_desc->cs_ena_pretrans;
}{...} else {
seg_config.cs_setup = handle->cfg.cs_ena_pretrans;
}{...}
seg_config.cmd = seg_trans_desc->base.cmd;
if (seg_trans_desc->seg_trans_flags & SPI_MULTI_TRANS_CMD_LEN_UPDATED) {
seg_config.cmd_bits = seg_trans_desc->command_bits;
}{...} else {
seg_config.cmd_bits = handle->cfg.command_bits;
}{...}
seg_config.addr = seg_trans_desc->base.addr;
if (seg_trans_desc->seg_trans_flags & SPI_MULTI_TRANS_ADDR_LEN_UPDATED) {
seg_config.addr_bits = seg_trans_desc->address_bits;
}{...} else {
seg_config.addr_bits = handle->cfg.address_bits;
}{...}
if (seg_trans_desc->seg_trans_flags & SPI_MULTI_TRANS_DUMMY_LEN_UPDATED) {
seg_config.dummy_bits = seg_trans_desc->dummy_bits;
}{...} else {
seg_config.dummy_bits = handle->cfg.dummy_bits;
}{...}
seg_config.tx_bitlen = seg_trans_desc->base.length;
seg_config.rx_bitlen = seg_trans_desc->base.rxlength;
if (seg_trans_desc->seg_trans_flags & SPI_MULTI_TRANS_DONE_LEN_UPDATED) {
seg_config.cs_hold = seg_trans_desc->cs_ena_posttrans;
}{...} else {
seg_config.cs_hold = handle->cfg.cs_ena_posttrans;
}{...}
if (seg_end) {
seg_config.seg_end = true;
}{...}
seg_config.seg_gap_len = seg_trans_desc->sct_gap_len;
spi_sct_set_hal_trans_config(seg_trans_desc, &hal->trans_config);
spi_hal_sct_format_conf_buffer(hal, &seg_config, hal_dev, buffer);
}{...}
esp_err_t SPI_MASTER_ATTR spi_device_queue_multi_trans(spi_device_handle_t handle, spi_multi_transaction_t *seg_trans_desc, uint32_t trans_num, TickType_t ticks_to_wait)
{
SPI_CHECK(handle, "Invalid arguments.", ESP_ERR_INVALID_ARG);
SPI_CHECK(SOC_SPI_SCT_SUPPORTED_PERIPH(handle->host->id), "Invalid arguments", ESP_ERR_INVALID_ARG);
SPI_CHECK(handle->host->sct_mode_enabled == 1, "SCT mode isn't enabled", ESP_ERR_INVALID_STATE);
esp_err_t ret = ESP_OK;
uint16_t alignment = handle->host->bus_attr->internal_mem_align_size;
uint32_t *conf_buffer = heap_caps_aligned_alloc(alignment, (trans_num * SOC_SPI_SCT_BUFFER_NUM_MAX * sizeof(uint32_t)), MALLOC_CAP_DMA);
SPI_CHECK(conf_buffer, "No enough memory", ESP_ERR_NO_MEM);
for (int i = 0; i < trans_num; i++) {
ret = check_trans_valid(handle, (spi_transaction_t *)&seg_trans_desc[i]);
if (ret != ESP_OK) {
return ret;
}{...}
}{...}
SPI_CHECK(!spi_bus_device_is_polling(handle), "Cannot queue new transaction while previous polling transaction is not terminated.", ESP_ERR_INVALID_STATE);
spi_hal_context_t *hal = &handle->host->hal;
s_sct_init_conf_buffer(hal, conf_buffer, trans_num);
static esp_err_t dma_desc_status = ESP_FAIL;
spi_dma_desc_t *tx_seg_head = NULL;
uint32_t tx_used_dma_desc_num = 0;
uint32_t tx_buf_len = 0;
spi_dma_desc_t *rx_seg_head = NULL;
uint32_t rx_used_dma_desc_num = 0;
uint32_t rx_buf_len = 0;
s_sct_format_conf_buffer(handle, &seg_trans_desc[0], conf_buffer, (trans_num == 1));
tx_buf_len = (seg_trans_desc[0].base.length + 8 - 1) / 8;
portENTER_CRITICAL(&handle->host->spinlock);
dma_desc_status = spi_hal_sct_new_tx_dma_desc_head(&handle->host->sct_desc_pool, conf_buffer, seg_trans_desc[0].base.tx_buffer, tx_buf_len, &tx_seg_head, &tx_used_dma_desc_num);
portEXIT_CRITICAL(&handle->host->spinlock);
SPI_CHECK(dma_desc_status == ESP_OK, "No available dma descriptors, increase the `max_transfer_sz`, or wait queued transactions are done", ESP_ERR_INVALID_STATE);
rx_buf_len = (seg_trans_desc[0].base.rxlength + 8 - 1) / 8;
if (seg_trans_desc[0].base.rx_buffer) {
portENTER_CRITICAL(&handle->host->spinlock);
dma_desc_status = spi_hal_sct_new_rx_dma_desc_head(&handle->host->sct_desc_pool, seg_trans_desc[0].base.rx_buffer, rx_buf_len, &rx_seg_head, &rx_used_dma_desc_num);
portEXIT_CRITICAL(&handle->host->spinlock);
SPI_CHECK(dma_desc_status == ESP_OK, "No available dma descriptors, increase the `max_transfer_sz`, or wait queued transactions are done", ESP_ERR_INVALID_STATE);
}{...}
--------------Get segment head
for (int i = 1; i < trans_num; i++) {
s_sct_format_conf_buffer(handle, &seg_trans_desc[i], &conf_buffer[i * SOC_SPI_SCT_BUFFER_NUM_MAX], (i == (trans_num - 1)));
tx_buf_len = (seg_trans_desc[i].base.length + 8 - 1) / 8;
portENTER_CRITICAL(&handle->host->spinlock);
dma_desc_status = spi_hal_sct_link_tx_seg_dma_desc(&handle->host->sct_desc_pool, &conf_buffer[i * SOC_SPI_SCT_BUFFER_NUM_MAX], seg_trans_desc[i].base.tx_buffer, tx_buf_len, &tx_used_dma_desc_num);
portEXIT_CRITICAL(&handle->host->spinlock);
SPI_CHECK(dma_desc_status == ESP_OK, "No available dma descriptors, increase the `max_transfer_sz`, or wait queued transactions are done", ESP_ERR_INVALID_STATE);
if (seg_trans_desc[i].base.rx_buffer) {
rx_buf_len = (seg_trans_desc[i].base.rxlength + 8 - 1) / 8;
portENTER_CRITICAL(&handle->host->spinlock);
dma_desc_status = spi_hal_sct_link_rx_seg_dma_desc(&handle->host->sct_desc_pool, seg_trans_desc[i].base.rx_buffer, rx_buf_len, &rx_used_dma_desc_num);
portEXIT_CRITICAL(&handle->host->spinlock);
}{...}
}{...}
#ifdef CONFIG_PM_ENABLE
esp_pm_lock_acquire(handle->host->bus_attr->pm_lock);
#endif
spi_sct_trans_priv_t sct_desc = {
.tx_seg_head = tx_seg_head,
.rx_seg_head = rx_seg_head,
.sct_trans_desc_head = seg_trans_desc,
.sct_conf_buffer = conf_buffer,
.tx_used_desc_num = tx_used_dma_desc_num,
.rx_used_desc_num = rx_used_dma_desc_num,
}{...};
BaseType_t r = xQueueSend(handle->trans_queue, (void *)&sct_desc, ticks_to_wait);
if (!r) {
#ifdef CONFIG_PM_ENABLE
esp_pm_lock_release(handle->host->bus_attr->pm_lock);/* ... */
#endif
return ESP_ERR_TIMEOUT;
}{...}
ret = spi_bus_lock_bg_request(handle->dev_lock);
if (ret != ESP_OK) {
return ret;
}{...}
return ESP_OK;
}{...}
esp_err_t SPI_MASTER_ATTR spi_device_get_multi_trans_result(spi_device_handle_t handle, spi_multi_transaction_t **seg_trans_desc, TickType_t ticks_to_wait)
{
SPI_CHECK(handle, "Invalid arguments.", ESP_ERR_INVALID_ARG);
SPI_CHECK(SOC_SPI_SCT_SUPPORTED_PERIPH(handle->host->id), "Invalid arguments", ESP_ERR_INVALID_ARG);
SPI_CHECK(handle->host->sct_mode_enabled == 1, "SCT mode isn't enabled", ESP_ERR_INVALID_STATE);
spi_sct_trans_priv_t sct_desc = {};
BaseType_t r = xQueueReceive(handle->ret_queue, (void *)&sct_desc, ticks_to_wait);
if (!r) {
return ESP_ERR_TIMEOUT;
}{...}
*seg_trans_desc = sct_desc.sct_trans_desc_head;
return ESP_OK;
}{...}
/* ... */#endif