luat_spi_rtt.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include "luat_base.h"
  2. #include "luat_spi.h"
  3. #include "luat_log.h"
  4. #include "rtthread.h"
  5. #include "rthw.h"
  6. #include "rtdevice.h"
  7. #ifdef SOC_FAMILY_STM32
  8. #include "drv_gpio.h"
  9. #endif
  10. #define LUAT_LOG_TAG "spi"
  11. #include "luat_log.h"
  12. #ifdef RT_USING_SPI
  13. static struct rt_spi_device * findDev(int id) {
  14. char buff[7] = {0};
  15. sprintf_(buff, "spi%02d", id);
  16. rt_device_t drv = rt_device_find(buff);
  17. if (drv == NULL) {
  18. return RT_NULL;
  19. }
  20. if (drv->type != RT_Device_Class_SPIDevice) {
  21. LLOGW("not spi device %s", buff);
  22. }
  23. return (struct rt_spi_device *)drv;
  24. }
  25. int luat_spi_exist(int id) {
  26. return findDev(id) == RT_NULL ? 0 : 1;
  27. }
  28. int luat_spi_device_config(luat_spi_device_t* spi_dev) {
  29. int ret = 0;
  30. struct rt_spi_configuration cfg;
  31. cfg.data_width = spi_dev->spi_config.dataw;
  32. if(spi_dev->spi_config.master == 1)
  33. cfg.mode |= RT_SPI_MASTER;
  34. else
  35. cfg.mode |= RT_SPI_SLAVE;
  36. if(spi_dev->spi_config.bit_dict == 1)
  37. cfg.mode |= RT_SPI_MSB;
  38. else
  39. cfg.mode |= RT_SPI_LSB;
  40. if(spi_dev->spi_config.CPHA)
  41. cfg.mode |= RT_SPI_CPHA;
  42. if(spi_dev->spi_config.CPOL)
  43. cfg.mode |= RT_SPI_CPOL;
  44. cfg.max_hz = spi_dev->spi_config.bandrate;
  45. ret = rt_spi_configure(spi_dev, &cfg);
  46. return ret;
  47. }
  48. int luat_spi_bus_setup(luat_spi_device_t* spi_dev){
  49. char bus_name[8] = {0};
  50. char device_name[8] = {0};
  51. int ret = 0;
  52. struct rt_spi_device *spi_device = NULL; /* SPI 设备句柄 */
  53. sprintf_(bus_name, "spi%d", spi_dev->spi_config.id / 10);
  54. sprintf_(device_name, "spi%02d", spi_dev->spi_config.id);
  55. #ifdef SOC_W60X
  56. wm_spi_bus_attach_device(bus_name, device_name, spi_dev->spi_config.cs);
  57. spi_device = findDev(spi_dev->spi_config.id);
  58. #else
  59. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  60. ret = rt_spi_bus_attach_device(spi_device, bus_name, device_name, NULL);
  61. if (ret) {
  62. rt_free(spi_device);
  63. LLOGE("fail to attach_device %s", device_name);
  64. return ret;
  65. }
  66. #endif
  67. return ret;
  68. }
  69. //初始化配置SPI各项参数,并打开SPI
  70. //成功返回0
  71. int luat_spi_setup(luat_spi_t* spi) {
  72. char bus_name[8] = {0};
  73. char device_name[8] = {0};
  74. int ret = 0;
  75. struct rt_spi_device *spi_dev = NULL; /* SPI 设备句柄 */
  76. sprintf_(bus_name, "spi%d", spi->id / 10);
  77. sprintf_(device_name, "spi%02d", spi->id);
  78. #ifdef SOC_W60X
  79. wm_spi_bus_attach_device(bus_name, device_name, spi->cs);
  80. spi_dev = findDev(spi->id);
  81. #else
  82. spi_dev = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  83. ret = rt_spi_bus_attach_device(spi_dev, bus_name, device_name, NULL);
  84. if (ret) {
  85. rt_free(spi_dev);
  86. LLOGE("fail to attach_device %s", device_name);
  87. return ret;
  88. }
  89. #endif
  90. struct rt_spi_configuration cfg;
  91. cfg.data_width = spi->dataw;
  92. if(spi->master == 1)
  93. cfg.mode |= RT_SPI_MASTER;
  94. else
  95. cfg.mode |= RT_SPI_SLAVE;
  96. if(spi->bit_dict == 1)
  97. cfg.mode |= RT_SPI_MSB;
  98. else
  99. cfg.mode |= RT_SPI_LSB;
  100. if(spi->CPHA)
  101. cfg.mode |= RT_SPI_CPHA;
  102. if(spi->CPOL)
  103. cfg.mode |= RT_SPI_CPOL;
  104. cfg.max_hz = spi->bandrate;
  105. ret = rt_spi_configure(spi_dev, &cfg);
  106. return ret;
  107. }
  108. //关闭SPI,成功返回0
  109. int luat_spi_close(int spi_id) {
  110. return 0;
  111. }
  112. //收发SPI数据,返回接收字节数
  113. int luat_spi_transfer(int spi_id, const char* send_buf, size_t send_length, char* recv_buf, size_t recv_length) {
  114. struct rt_spi_device * drv = findDev(spi_id);
  115. if (drv == NULL)
  116. return -1;
  117. return rt_spi_send_then_recv(drv, send_buf, send_length, recv_buf, recv_length);
  118. }
  119. //收SPI数据,返回接收字节数
  120. int luat_spi_recv(int spi_id, char* recv_buf, size_t length) {
  121. struct rt_spi_device * drv = findDev(spi_id);
  122. if (drv == NULL)
  123. return -1;
  124. return rt_spi_recv(drv, recv_buf, length);
  125. }
  126. //发SPI数据,返回发送字节数
  127. int luat_spi_send(int spi_id, const char* send_buf, size_t length) {
  128. struct rt_spi_device * drv = findDev(spi_id);
  129. if (drv == NULL)
  130. return -1;
  131. return rt_spi_send(drv, send_buf, length);
  132. }
  133. #else
  134. //初始化配置SPI各项参数,并打开SPI
  135. //成功返回0
  136. int luat_spi_setup(luat_spi_t* spi) {
  137. LLOGE("spi not enable/support at this device");
  138. return -1;
  139. }
  140. //关闭SPI,成功返回0
  141. int luat_spi_close(int spi_id) {
  142. LLOGE("spi not enable/support at this device");
  143. return -1;
  144. }
  145. //收发SPI数据,返回接收字节数
  146. int luat_spi_transfer(int spi_id, const char* send_buf, size_t send_length, char* recv_buf, size_t recv_length) {
  147. //LLOGE("spi not enable/support at this device");
  148. return -1;
  149. }
  150. //收SPI数据,返回接收字节数
  151. int luat_spi_recv(int spi_id, char* recv_buf, size_t length) {
  152. //LLOGE("spi not enable/support at this device");
  153. return -1;
  154. }
  155. //发SPI数据,返回发送字节数
  156. int luat_spi_send(int spi_id, const char* send_buf, size_t length) {
  157. //LLOGE("spi not enable/support at this device");
  158. return -1;
  159. }
  160. #endif