luat_spi_idf5.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. #include "luat_base.h"
  2. #include "luat_gpio.h"
  3. #include "luat_spi.h"
  4. #include "luat_malloc.h"
  5. #include "driver/spi_master.h"
  6. #include "driver/gpio.h"
  7. #include "idf5_io_def.h"
  8. #define LUAT_LOG_TAG "spi"
  9. #include "luat_log.h"
  10. #if defined(CONFIG_IDF_TARGET_ESP32)||defined(CONFIG_IDF_TARGET_ESP32S2)||defined(CONFIG_IDF_TARGET_ESP32S3)
  11. #define SOC_SPI_NUM 2
  12. #else
  13. #define SOC_SPI_NUM 1
  14. #endif
  15. static spi_device_interface_config_t spi_config[SOC_SPI_NUM] = {0};
  16. static spi_device_handle_t spi_handle[SOC_SPI_NUM] = {0};
  17. int luat_spi_setup(luat_spi_t *spi){
  18. esp_err_t ret = -1;
  19. spi_bus_config_t buscfg = {
  20. .quadwp_io_num = -1,
  21. .quadhd_io_num = -1,
  22. .max_transfer_sz = SOC_SPI_MAXIMUM_BUFFER_SIZE
  23. };
  24. if (spi->id == 2){
  25. buscfg.miso_io_num = SPI2_MISO_IO_NUM;
  26. buscfg.mosi_io_num = SPI2_MOSI_IO_NUM;
  27. buscfg.sclk_io_num = SPI2_SCLK_IO_NUM;
  28. }
  29. #if defined(CONFIG_IDF_TARGET_ESP32)||defined(CONFIG_IDF_TARGET_ESP32S2)||defined(CONFIG_IDF_TARGET_ESP32S3)
  30. else if(spi->id == 3){
  31. buscfg.miso_io_num = SPI3_MISO_IO_NUM;
  32. buscfg.mosi_io_num = SPI3_MOSI_IO_NUM;
  33. buscfg.sclk_io_num = SPI3_SCLK_IO_NUM;
  34. }
  35. #endif
  36. else{
  37. return -1;
  38. }
  39. ret = spi_bus_initialize(spi->id-1, &buscfg, SPI_DMA_CH_AUTO);
  40. if (ret != 0){
  41. return ret;
  42. }
  43. spi_device_interface_config_t* dev_config = &spi_config[spi->id-2];
  44. memset(dev_config, 0, sizeof(spi_device_interface_config_t));
  45. if (spi->CPHA == 0){
  46. if (spi->CPOL == 0)
  47. dev_config->mode = 0;
  48. if (spi->CPOL == 1)
  49. dev_config->mode = 1;
  50. }else{
  51. if (spi->CPOL == 0)
  52. dev_config->mode = 2;
  53. if (spi->CPOL == 1)
  54. dev_config->mode = 3;
  55. }
  56. if (spi->mode == 0){
  57. dev_config->flags |= SPI_DEVICE_HALFDUPLEX;
  58. }
  59. dev_config->clock_speed_hz = spi->bandrate;
  60. if (spi->cs == Luat_GPIO_MAX_ID)
  61. dev_config->spics_io_num = -1;
  62. else
  63. dev_config->spics_io_num = spi->cs;
  64. dev_config->queue_size = 7;
  65. ret = spi_bus_add_device(spi->id-1, dev_config, &spi_handle[spi->id-2]);
  66. return ret;
  67. }
  68. int luat_spi_close(int spi_id){
  69. esp_err_t ret = -1;
  70. #if defined(CONFIG_IDF_TARGET_ESP32)||defined(CONFIG_IDF_TARGET_ESP32S2)||defined(CONFIG_IDF_TARGET_ESP32S3)
  71. if (spi_id > 3 || spi_id < 2){
  72. return -1;
  73. }
  74. #else
  75. if (spi_id != 2){
  76. return -1;
  77. }
  78. #endif
  79. ret = spi_bus_remove_device(spi_handle[spi_id-2]);
  80. if (ret != 0){
  81. return ret;
  82. }
  83. ret = spi_bus_free(spi_id-1);
  84. return ret;
  85. }
  86. int luat_spi_transfer(int spi_id, const char *send_buf, size_t send_length, char *recv_buf, size_t recv_length){
  87. esp_err_t ret = -1;
  88. #if defined(CONFIG_IDF_TARGET_ESP32)||defined(CONFIG_IDF_TARGET_ESP32S2)||defined(CONFIG_IDF_TARGET_ESP32S3)
  89. if (spi_id > 3 || spi_id < 2){
  90. return -1;
  91. }
  92. #else
  93. if (spi_id != 2){
  94. return -1;
  95. }
  96. #endif
  97. spi_transaction_t send;
  98. memset(&send, 0, sizeof(send));
  99. if ((spi_config[spi_id-2].flags & SPI_DEVICE_HALFDUPLEX) != 0){
  100. while (send_length > 0) {
  101. memset(&send, 0, sizeof(send));
  102. if (send_length > SOC_SPI_MAXIMUM_BUFFER_SIZE ) {
  103. send.length = SOC_SPI_MAXIMUM_BUFFER_SIZE * 8;
  104. send.tx_buffer = send_buf;
  105. ret = spi_device_polling_transmit(spi_handle[spi_id-2], &send);
  106. send_buf += SOC_SPI_MAXIMUM_BUFFER_SIZE ;
  107. send_length -= SOC_SPI_MAXIMUM_BUFFER_SIZE ;
  108. }
  109. else {
  110. send.length = send_length * 8;
  111. send.tx_buffer = send_buf;
  112. ret = spi_device_polling_transmit(spi_handle[spi_id-2], &send);
  113. break;
  114. }
  115. }
  116. if (ret != 0){
  117. return -2;
  118. }
  119. spi_transaction_t recv;
  120. memset(&recv, 0, sizeof(recv));
  121. recv.length = recv_length * 8;
  122. recv.rxlength = recv_length * 8;
  123. recv.rx_buffer = recv_buf;
  124. ret = spi_device_polling_transmit(spi_handle[spi_id-2], &recv);
  125. }else{
  126. while (send_length > 0) {
  127. memset(&send, 0, sizeof(send));
  128. if (send_length > SOC_SPI_MAXIMUM_BUFFER_SIZE ) {
  129. send.length = SOC_SPI_MAXIMUM_BUFFER_SIZE * 8;
  130. send.tx_buffer = send_buf;
  131. send.rxlength = SOC_SPI_MAXIMUM_BUFFER_SIZE * 8;
  132. send.rx_buffer = recv_buf;
  133. ret = spi_device_polling_transmit(spi_handle[spi_id-2], &send);
  134. send_buf += SOC_SPI_MAXIMUM_BUFFER_SIZE ;
  135. send_length -= SOC_SPI_MAXIMUM_BUFFER_SIZE ;
  136. recv_buf += SOC_SPI_MAXIMUM_BUFFER_SIZE ;
  137. send_length -= SOC_SPI_MAXIMUM_BUFFER_SIZE ;
  138. }
  139. else {
  140. send.length = send_length * 8;
  141. send.tx_buffer = send_buf;
  142. send.rxlength = send_length * 8;
  143. send.rx_buffer = recv_buf;
  144. ret = spi_device_polling_transmit(spi_handle[spi_id-2], &send);
  145. break;
  146. }
  147. }
  148. if (ret != 0){
  149. return -2;
  150. }
  151. }
  152. return ret == 0 ? recv_length : -1;
  153. }
  154. int luat_spi_recv(int spi_id, char *recv_buf, size_t length){
  155. esp_err_t ret = -1;
  156. #if defined(CONFIG_IDF_TARGET_ESP32)||defined(CONFIG_IDF_TARGET_ESP32S2)||defined(CONFIG_IDF_TARGET_ESP32S3)
  157. if (spi_id > 3 || spi_id < 2){
  158. return -1;
  159. }
  160. #else
  161. if (spi_id != 2){
  162. return -1;
  163. }
  164. #endif
  165. spi_transaction_t t;
  166. while (length > 0) {
  167. memset(&t, 0, sizeof(t));
  168. if (length > SOC_SPI_MAXIMUM_BUFFER_SIZE ) {
  169. t.length = SOC_SPI_MAXIMUM_BUFFER_SIZE * 8;
  170. t.rxlength = SOC_SPI_MAXIMUM_BUFFER_SIZE * 8;
  171. t.rx_buffer = recv_buf;
  172. ret = spi_device_polling_transmit(spi_handle[spi_id-2], &t);
  173. recv_buf += SOC_SPI_MAXIMUM_BUFFER_SIZE ;
  174. length -= SOC_SPI_MAXIMUM_BUFFER_SIZE ;
  175. }
  176. else {
  177. t.length = length * 8;
  178. t.rxlength = length * 8;
  179. t.rx_buffer = recv_buf;
  180. ret = spi_device_polling_transmit(spi_handle[spi_id-2], &t);
  181. break;
  182. }
  183. }
  184. return ret == 0 ? length : -1;
  185. }
  186. int luat_spi_send(int spi_id, const char *send_buf, size_t length){
  187. spi_transaction_t t;
  188. esp_err_t ret = -1;
  189. #if defined(CONFIG_IDF_TARGET_ESP32)||defined(CONFIG_IDF_TARGET_ESP32S2)||defined(CONFIG_IDF_TARGET_ESP32S3)
  190. if (spi_id > 3 || spi_id < 2){
  191. return -1;
  192. }
  193. #else
  194. if (spi_id != 2){
  195. return -1;
  196. }
  197. #endif
  198. while (length > 0) {
  199. memset(&t, 0, sizeof(t));
  200. if (length > SOC_SPI_MAXIMUM_BUFFER_SIZE ) {
  201. t.length = SOC_SPI_MAXIMUM_BUFFER_SIZE * 8;
  202. t.tx_buffer = send_buf;
  203. ret = spi_device_polling_transmit(spi_handle[spi_id-2], &t);
  204. send_buf += SOC_SPI_MAXIMUM_BUFFER_SIZE ;
  205. length -= SOC_SPI_MAXIMUM_BUFFER_SIZE ;
  206. }
  207. else {
  208. t.length = length * 8;
  209. t.tx_buffer = send_buf;
  210. ret = spi_device_polling_transmit(spi_handle[spi_id-2], &t);
  211. break;
  212. }
  213. }
  214. return ret == 0 ? length : -1;
  215. }
  216. #define LUAT_SPI_CS_SELECT 0
  217. #define LUAT_SPI_CS_CLEAR 1
  218. static uint8_t spi_bus[SOC_SPI_NUM] = {0};
  219. int luat_spi_device_setup(luat_spi_device_t *spi_dev){
  220. esp_err_t ret = -1;
  221. int bus_id = spi_dev->bus_id;
  222. spi_device_handle_t *spi_device = luat_heap_malloc(sizeof(spi_device_handle_t));
  223. if (spi_device == NULL)
  224. return ret;
  225. spi_dev->user_data = (void *)spi_device;
  226. #if defined(CONFIG_IDF_TARGET_ESP32)||defined(CONFIG_IDF_TARGET_ESP32S2)||defined(CONFIG_IDF_TARGET_ESP32S3)
  227. if (bus_id > 3 || bus_id < 2){
  228. return -1;
  229. }
  230. #else
  231. if (bus_id != 2){
  232. return -1;
  233. }
  234. #endif
  235. if (spi_bus[bus_id-2] == 0){
  236. spi_bus_config_t buscfg = {
  237. .quadwp_io_num = -1,
  238. .quadhd_io_num = -1,
  239. .max_transfer_sz = SOC_SPI_MAXIMUM_BUFFER_SIZE
  240. };
  241. if (bus_id == 2){
  242. buscfg.miso_io_num = SPI2_MISO_IO_NUM;
  243. buscfg.mosi_io_num = SPI2_MOSI_IO_NUM;
  244. buscfg.sclk_io_num = SPI2_SCLK_IO_NUM;
  245. }
  246. #if defined(CONFIG_IDF_TARGET_ESP32)||defined(CONFIG_IDF_TARGET_ESP32S2)||defined(CONFIG_IDF_TARGET_ESP32S3)
  247. else{
  248. buscfg.miso_io_num = SPI3_MISO_IO_NUM;
  249. buscfg.mosi_io_num = SPI3_MOSI_IO_NUM;
  250. buscfg.sclk_io_num = SPI3_SCLK_IO_NUM;
  251. }
  252. #endif
  253. ret = spi_bus_initialize(bus_id-1, &buscfg, SPI_DMA_CH_AUTO);
  254. if (ret != 0){
  255. return ret;
  256. }
  257. spi_bus[bus_id-2] = 1;
  258. }
  259. spi_device_interface_config_t* dev_config = &spi_config[bus_id-2];
  260. memset(dev_config, 0, sizeof(spi_device_interface_config_t));
  261. if (spi_dev->spi_config.CPHA == 0){
  262. if (spi_dev->spi_config.CPOL == 0)
  263. dev_config->mode = 0;
  264. if (spi_dev->spi_config.CPOL == 1)
  265. dev_config->mode = 1;
  266. }else{
  267. if (spi_dev->spi_config.CPOL == 0)
  268. dev_config->mode = 2;
  269. if (spi_dev->spi_config.CPOL == 1)
  270. dev_config->mode = 3;
  271. }
  272. if (spi_dev->spi_config.mode == 0){
  273. dev_config->flags |= SPI_DEVICE_HALFDUPLEX;
  274. }
  275. dev_config->clock_speed_hz = spi_dev->spi_config.bandrate;
  276. dev_config->spics_io_num = -1;
  277. dev_config->queue_size = 7;
  278. ret = spi_bus_add_device(bus_id-1, dev_config, spi_device);
  279. if (ret != 0)
  280. luat_heap_free(spi_device);
  281. luat_gpio_mode(spi_dev->spi_config.cs, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH);
  282. return ret;
  283. }
  284. int luat_spi_device_close(luat_spi_device_t *spi_dev){
  285. esp_err_t ret = -1;
  286. int bus_id = spi_dev->bus_id;
  287. #if defined(CONFIG_IDF_TARGET_ESP32)||defined(CONFIG_IDF_TARGET_ESP32S2)||defined(CONFIG_IDF_TARGET_ESP32S3)
  288. if (bus_id > 3 || bus_id < 2){
  289. return -1;
  290. }
  291. #else
  292. if (bus_id != 2){
  293. return -1;
  294. }
  295. #endif
  296. ret = spi_bus_remove_device(*(spi_device_handle_t *)(spi_dev->user_data));
  297. luat_heap_free((spi_device_handle_t *)(spi_dev->user_data));
  298. return ret;
  299. }
  300. int luat_spi_device_transfer(luat_spi_device_t *spi_dev, const char *send_buf, size_t send_length, char *recv_buf, size_t recv_length){
  301. if (spi_dev->spi_config.cs != 255)
  302. luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_SELECT);
  303. esp_err_t ret = -1;
  304. int bus_id = spi_dev->bus_id;
  305. #if defined(CONFIG_IDF_TARGET_ESP32)||defined(CONFIG_IDF_TARGET_ESP32S2)||defined(CONFIG_IDF_TARGET_ESP32S3)
  306. if (bus_id > 3 || bus_id < 2){
  307. return -1;
  308. }
  309. #else
  310. if (bus_id != 2){
  311. return -1;
  312. }
  313. #endif
  314. spi_transaction_t send;
  315. memset(&send, 0, sizeof(send));
  316. while (send_length > 0) {
  317. memset(&send, 0, sizeof(send));
  318. if (send_length > SOC_SPI_MAXIMUM_BUFFER_SIZE ) {
  319. send.length = SOC_SPI_MAXIMUM_BUFFER_SIZE * 8;
  320. send.tx_buffer = send_buf;
  321. ret = spi_device_polling_transmit(*(spi_device_handle_t *)(spi_dev->user_data), &send);
  322. send_buf += SOC_SPI_MAXIMUM_BUFFER_SIZE ;
  323. send_length -= SOC_SPI_MAXIMUM_BUFFER_SIZE ;
  324. }else {
  325. send.length = send_length * 8;
  326. send.tx_buffer = send_buf;
  327. ret = spi_device_polling_transmit(*(spi_device_handle_t *)(spi_dev->user_data), &send);
  328. break;
  329. }
  330. }
  331. if (ret != 0){
  332. return -2;
  333. }
  334. spi_transaction_t recv;
  335. memset(&recv, 0, sizeof(recv));
  336. recv.length = recv_length * 8;
  337. recv.rxlength = recv_length * 8;
  338. recv.rx_buffer = recv_buf;
  339. ret = spi_device_polling_transmit(*(spi_device_handle_t *)(spi_dev->user_data), &recv);
  340. if (spi_dev->spi_config.cs != 255)
  341. luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_CLEAR);
  342. return ret == 0 ? recv_length : -1;
  343. }
  344. int luat_spi_device_recv(luat_spi_device_t *spi_dev, char *recv_buf, size_t length){
  345. if (spi_dev->spi_config.cs != 255)
  346. luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_SELECT);
  347. esp_err_t ret = -1;
  348. int bus_id = spi_dev->bus_id;
  349. #if defined(CONFIG_IDF_TARGET_ESP32)||defined(CONFIG_IDF_TARGET_ESP32S2)||defined(CONFIG_IDF_TARGET_ESP32S3)
  350. if (bus_id > 3 || bus_id < 2){
  351. return -1;
  352. }
  353. #else
  354. if (bus_id != 2){
  355. return -1;
  356. }
  357. #endif
  358. spi_transaction_t t;
  359. while (length > 0) {
  360. memset(&t, 0, sizeof(t));
  361. if (length > SOC_SPI_MAXIMUM_BUFFER_SIZE ) {
  362. t.length = SOC_SPI_MAXIMUM_BUFFER_SIZE * 8;
  363. t.rxlength = SOC_SPI_MAXIMUM_BUFFER_SIZE * 8;
  364. t.rx_buffer = recv_buf;
  365. ret = spi_device_polling_transmit(*(spi_device_handle_t *)(spi_dev->user_data), &t);
  366. recv_buf += SOC_SPI_MAXIMUM_BUFFER_SIZE ;
  367. length -= SOC_SPI_MAXIMUM_BUFFER_SIZE ;
  368. }
  369. else {
  370. t.length = length * 8;
  371. t.rxlength = length * 8;
  372. t.rx_buffer = recv_buf;
  373. ret = spi_device_polling_transmit(*(spi_device_handle_t *)(spi_dev->user_data), &t);
  374. break;
  375. }
  376. }
  377. if (spi_dev->spi_config.cs != 255)
  378. luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_CLEAR);
  379. return ret == 0 ? length : -1;
  380. }
  381. int luat_spi_device_send(luat_spi_device_t *spi_dev, const char *send_buf, size_t length){
  382. if (spi_dev->spi_config.cs != 255)
  383. luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_SELECT);
  384. esp_err_t ret = -1;
  385. int bus_id = spi_dev->bus_id;
  386. #if defined(CONFIG_IDF_TARGET_ESP32)||defined(CONFIG_IDF_TARGET_ESP32S2)||defined(CONFIG_IDF_TARGET_ESP32S3)
  387. if (bus_id > 3 || bus_id < 2){
  388. return -1;
  389. }
  390. #else
  391. if (bus_id != 2){
  392. return -1;
  393. }
  394. #endif
  395. spi_transaction_t t;
  396. memset(&t, 0, sizeof(t));
  397. while (length > 0) {
  398. memset(&t, 0, sizeof(t));
  399. if (length > SOC_SPI_MAXIMUM_BUFFER_SIZE ) {
  400. t.length = SOC_SPI_MAXIMUM_BUFFER_SIZE * 8;
  401. t.tx_buffer = send_buf;
  402. ret = spi_device_polling_transmit(*(spi_device_handle_t *)(spi_dev->user_data), &t);
  403. send_buf += SOC_SPI_MAXIMUM_BUFFER_SIZE ;
  404. length -= SOC_SPI_MAXIMUM_BUFFER_SIZE ;
  405. }else {
  406. t.length = length * 8;
  407. t.tx_buffer = send_buf;
  408. ret = spi_device_polling_transmit(*(spi_device_handle_t *)(spi_dev->user_data), &t);
  409. break;
  410. }
  411. }
  412. if (spi_dev->spi_config.cs != 255)
  413. luat_gpio_set(spi_dev->spi_config.cs, LUAT_SPI_CS_CLEAR);
  414. return ret == 0 ? length : -1;
  415. }
  416. int luat_spi_device_config(luat_spi_device_t* spi_dev){
  417. return 0;
  418. }
  419. int luat_spi_config_dma(int spi_id, uint32_t tx_channel, uint32_t rx_channel) {
  420. return 0;
  421. }
  422. int luat_spi_change_speed(int spi_id, uint32_t speed) {
  423. spi_device_interface_config_t* dev_config = &spi_config[spi_id-2];
  424. dev_config->clock_speed_hz = speed;
  425. int ret = spi_bus_remove_device(spi_handle[spi_id-2]);
  426. if (ret != 0){
  427. return ret;
  428. }
  429. ret = spi_bus_add_device(spi_id-1, dev_config, &spi_handle[spi_id-2]);
  430. return ret;
  431. // return 0;
  432. }