luat_spi_rtt.c 6.9 KB

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