drv_spi.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-07-18 Rbb666 first version
  9. * 2023-03-30 Rbb666 update spi driver
  10. */
  11. #include <drv_spi.h>
  12. #ifdef RT_USING_SPI
  13. //#define DRV_DEBUG
  14. #define DBG_TAG "drv.spi"
  15. #ifdef DRV_DEBUG
  16. #define DBG_LVL DBG_LOG
  17. #else
  18. #define DBG_LVL DBG_INFO
  19. #endif /* DRV_DEBUG */
  20. #include <rtdbg.h>
  21. #ifdef BSP_USING_SPI0
  22. static struct rt_spi_bus spi_bus0;
  23. #endif
  24. #ifdef BSP_USING_SPI3
  25. static struct rt_spi_bus spi_bus3;
  26. #endif
  27. #ifdef BSP_USING_SPI6
  28. static struct rt_spi_bus spi_bus6;
  29. #endif
  30. static struct ifx_spi_handle spi_bus_obj[] =
  31. {
  32. #if defined(BSP_USING_SPI0)
  33. {
  34. .bus_name = "spi0",
  35. .sck_pin = GET_PIN(0, 4),
  36. .miso_pin = GET_PIN(0, 3),
  37. .mosi_pin = GET_PIN(0, 2),
  38. },
  39. #endif
  40. #if defined(BSP_USING_SPI3)
  41. {
  42. .bus_name = "spi3",
  43. .sck_pin = GET_PIN(6, 2),
  44. .miso_pin = GET_PIN(6, 1),
  45. .mosi_pin = GET_PIN(6, 0),
  46. },
  47. #endif
  48. #if defined(BSP_USING_SPI6)
  49. {
  50. .bus_name = "spi6",
  51. .sck_pin = GET_PIN(12, 2),
  52. .miso_pin = GET_PIN(12, 1),
  53. .mosi_pin = GET_PIN(12, 0),
  54. },
  55. #endif
  56. };
  57. static struct ifx_spi spi_config[sizeof(spi_bus_obj) / sizeof(spi_bus_obj[0])] = {0};
  58. /* private rt-thread spi ops function */
  59. static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
  60. static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message);
  61. static struct rt_spi_ops ifx_spi_ops =
  62. {
  63. .configure = spi_configure,
  64. .xfer = spixfer,
  65. };
  66. static void spi_interrupt_callback(void *arg, cyhal_spi_event_t event)
  67. {
  68. struct ifx_spi *spi_drv = (struct ifx_spi *)arg;
  69. rt_interrupt_enter();
  70. if ((event & CYHAL_SPI_IRQ_DONE) != 0u)
  71. {
  72. /* Transmission is complete. Handle Event */
  73. rt_completion_done(&spi_drv->cpt);
  74. }
  75. rt_interrupt_leave();
  76. }
  77. static void ifx_spi_init(struct ifx_spi *spi_device)
  78. {
  79. RT_ASSERT(spi_device != RT_NULL);
  80. rt_err_t result = RT_EOK;
  81. static uint8_t init_flag = 1;
  82. if (init_flag)
  83. {
  84. result = cyhal_spi_init(spi_device->spi_handle_t->spi_obj, spi_device->spi_handle_t->mosi_pin, spi_device->spi_handle_t->miso_pin,
  85. spi_device->spi_handle_t->sck_pin, NC, NULL, spi_device->spi_handle_t->spi_obj->data_bits,
  86. spi_device->spi_handle_t->spi_obj->mode, false);
  87. if (result != RT_EOK)
  88. {
  89. LOG_E("spi%s init fail", spi_device->spi_handle_t->bus_name);
  90. return;
  91. }
  92. result = cyhal_spi_set_frequency(spi_device->spi_handle_t->spi_obj, spi_device->spi_handle_t->freq);
  93. if (result == CYHAL_SPI_RSLT_CLOCK_ERROR)
  94. {
  95. LOG_E("%s set frequency fail", spi_device->spi_handle_t->bus_name);
  96. return;
  97. }
  98. LOG_I("[%s] freq:[%d]HZ\n", spi_device->spi_handle_t->bus_name, spi_device->spi_handle_t->freq);
  99. /* Register a callback function to be called when the interrupt fires */
  100. cyhal_spi_register_callback(spi_device->spi_handle_t->spi_obj, spi_interrupt_callback, spi_device);
  101. /* Enable the events that will trigger the call back function */
  102. cyhal_spi_enable_event(spi_device->spi_handle_t->spi_obj, CYHAL_SPI_IRQ_DONE, 4, true);
  103. }
  104. init_flag = 0;
  105. }
  106. static rt_err_t spi_configure(struct rt_spi_device *device,
  107. struct rt_spi_configuration *configuration)
  108. {
  109. RT_ASSERT(device != RT_NULL);
  110. RT_ASSERT(configuration != RT_NULL);
  111. struct ifx_spi *spi_device = rt_container_of(device->bus, struct ifx_spi, spi_bus);
  112. /* data_width */
  113. if (configuration->data_width <= 8)
  114. {
  115. spi_device->spi_handle_t->spi_obj->data_bits = 8;
  116. }
  117. else if (configuration->data_width <= 16)
  118. {
  119. spi_device->spi_handle_t->spi_obj->data_bits = 16;
  120. }
  121. else
  122. {
  123. return -RT_EIO;
  124. }
  125. uint32_t max_hz;
  126. max_hz = configuration->max_hz;
  127. spi_device->spi_handle_t->freq = max_hz;
  128. /* MSB or LSB */
  129. switch (configuration->mode & RT_SPI_MODE_3)
  130. {
  131. case RT_SPI_MODE_0:
  132. spi_device->spi_handle_t->spi_obj->mode = CYHAL_SPI_MODE_00_MSB;
  133. break;
  134. case RT_SPI_MODE_1:
  135. spi_device->spi_handle_t->spi_obj->mode = CYHAL_SPI_MODE_01_MSB;
  136. break;
  137. case RT_SPI_MODE_2:
  138. spi_device->spi_handle_t->spi_obj->mode = CYHAL_SPI_MODE_10_MSB;
  139. break;
  140. case RT_SPI_MODE_3:
  141. spi_device->spi_handle_t->spi_obj->mode = CYHAL_SPI_MODE_11_MSB;
  142. break;
  143. }
  144. ifx_spi_init(spi_device);
  145. return RT_EOK;
  146. }
  147. static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  148. {
  149. RT_ASSERT(device != NULL);
  150. RT_ASSERT(message != NULL);
  151. struct ifx_spi *spi_device = rt_container_of(device->bus, struct ifx_spi, spi_bus);
  152. /* take CS */
  153. if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
  154. {
  155. if (device->config.mode & RT_SPI_CS_HIGH)
  156. {
  157. cyhal_gpio_write(device->cs_pin, PIN_HIGH);
  158. }
  159. else
  160. {
  161. cyhal_gpio_write(device->cs_pin, PIN_LOW);
  162. }
  163. LOG_D("spi take cs\n");
  164. }
  165. int result = RT_EOK;
  166. if (message->length > 0)
  167. {
  168. if (message->send_buf == RT_NULL && message->recv_buf != RT_NULL)
  169. {
  170. /**< receive message */
  171. result = cyhal_spi_transfer(spi_device->spi_handle_t->spi_obj, RT_NULL, 0x00, message->recv_buf, message->length, 0x00);
  172. }
  173. else if (message->send_buf != RT_NULL && message->recv_buf == RT_NULL)
  174. {
  175. /**< send message */
  176. result = cyhal_spi_transfer(spi_device->spi_handle_t->spi_obj, message->send_buf, message->length, RT_NULL, 0x00, 0x00);
  177. }
  178. else if (message->send_buf != RT_NULL && message->recv_buf != RT_NULL)
  179. {
  180. /**< send and receive message */
  181. result = cyhal_spi_transfer(spi_device->spi_handle_t->spi_obj, message->send_buf, message->length, message->recv_buf, message->length, 0x00);
  182. }
  183. /* blocking the thread,and the other tasks can run */
  184. rt_completion_wait(&spi_device->cpt, RT_WAITING_FOREVER);
  185. }
  186. if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
  187. {
  188. if (device->config.mode & RT_SPI_CS_HIGH)
  189. cyhal_gpio_write(device->cs_pin, PIN_LOW);
  190. else
  191. cyhal_gpio_write(device->cs_pin, PIN_HIGH);
  192. }
  193. return message->length;
  194. }
  195. /**
  196. * Attach the spi device to SPI bus, this function must be used after initialization.
  197. */
  198. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin)
  199. {
  200. RT_ASSERT(bus_name != RT_NULL);
  201. RT_ASSERT(device_name != RT_NULL);
  202. rt_err_t result;
  203. struct rt_spi_device *spi_device;
  204. /* attach the device to spi bus*/
  205. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  206. RT_ASSERT(spi_device != RT_NULL);
  207. result = rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, cs_pin, RT_NULL);
  208. if (result != RT_EOK)
  209. {
  210. LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
  211. }
  212. RT_ASSERT(result == RT_EOK);
  213. LOG_D("%s attach to %s done", device_name, bus_name);
  214. return result;
  215. }
  216. int rt_hw_spi_init(void)
  217. {
  218. int result = RT_EOK;
  219. for (int spi_index = 0; spi_index < sizeof(spi_bus_obj) / sizeof(spi_bus_obj[0]); spi_index++)
  220. {
  221. spi_bus_obj[spi_index].spi_obj = rt_malloc(sizeof(cyhal_spi_t));
  222. RT_ASSERT(spi_bus_obj[spi_index].spi_obj != RT_NULL);
  223. spi_config[spi_index].spi_handle_t = &spi_bus_obj[spi_index];
  224. rt_err_t err = rt_spi_bus_register(&spi_config[spi_index].spi_bus, spi_bus_obj[spi_index].bus_name, &ifx_spi_ops);
  225. if (RT_EOK != err)
  226. {
  227. LOG_E("%s bus register failed.", spi_config[spi_index].spi_handle_t->bus_name);
  228. return -RT_ERROR;
  229. }
  230. LOG_D("MOSI PIN:[%d], MISO PIN[%d], CLK PIN[%d]\n",
  231. spi_bus_obj[spi_index].mosi_pin, spi_bus_obj[spi_index].miso_pin,
  232. spi_bus_obj[spi_index].sck_pin);
  233. /* initialize completion object */
  234. rt_completion_init(&spi_config[spi_index].cpt);
  235. }
  236. return result;
  237. }
  238. INIT_BOARD_EXPORT(rt_hw_spi_init);
  239. #endif /* RT_USING_SPI */