drv_spi.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. 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,
  82. spi_device->spi_handle_t->sck_pin, NC, NULL, spi_device->spi_handle_t->spi_obj->data_bits,
  83. spi_device->spi_handle_t->spi_obj->mode, false);
  84. if (result != RT_EOK)
  85. {
  86. LOG_E("spi%s init fail", spi_device->spi_handle_t->bus_name);
  87. return;
  88. }
  89. LOG_I("[%s] freq:[%d]HZ\n", spi_device->spi_handle_t->bus_name, spi_device->spi_handle_t->freq);
  90. result = cyhal_spi_set_frequency(spi_device->spi_handle_t->spi_obj, spi_device->spi_handle_t->freq);
  91. if (result == CYHAL_SPI_RSLT_CLOCK_ERROR)
  92. {
  93. LOG_E("%s set frequency fail", spi_device->spi_handle_t->bus_name);
  94. return;
  95. }
  96. /* Register a callback function to be called when the interrupt fires */
  97. cyhal_spi_register_callback(spi_device->spi_handle_t->spi_obj, spi_interrupt_callback, spi_device);
  98. /* Enable the events that will trigger the call back function */
  99. cyhal_spi_enable_event(spi_device->spi_handle_t->spi_obj, CYHAL_SPI_IRQ_DONE, 4, true);
  100. }
  101. static rt_err_t spi_configure(struct rt_spi_device *device,
  102. struct rt_spi_configuration *configuration)
  103. {
  104. RT_ASSERT(device != RT_NULL);
  105. RT_ASSERT(configuration != RT_NULL);
  106. struct ifx_spi *spi_device = rt_container_of(device->bus, struct ifx_spi, spi_bus);
  107. /* data_width */
  108. if (configuration->data_width <= 8)
  109. {
  110. spi_device->spi_handle_t->spi_obj->data_bits = 8;
  111. }
  112. else if (configuration->data_width <= 16)
  113. {
  114. spi_device->spi_handle_t->spi_obj->data_bits = 16;
  115. }
  116. else
  117. {
  118. return -RT_EIO;
  119. }
  120. uint32_t max_hz;
  121. max_hz = configuration->max_hz;
  122. spi_device->spi_handle_t->freq = max_hz;
  123. /* MSB or LSB */
  124. switch (configuration->mode & RT_SPI_MODE_3)
  125. {
  126. case RT_SPI_MODE_0:
  127. spi_device->spi_handle_t->spi_obj->mode = CYHAL_SPI_MODE_00_MSB;
  128. break;
  129. case RT_SPI_MODE_1:
  130. spi_device->spi_handle_t->spi_obj->mode = CYHAL_SPI_MODE_01_MSB;
  131. break;
  132. case RT_SPI_MODE_2:
  133. spi_device->spi_handle_t->spi_obj->mode = CYHAL_SPI_MODE_10_MSB;
  134. break;
  135. case RT_SPI_MODE_3:
  136. spi_device->spi_handle_t->spi_obj->mode = CYHAL_SPI_MODE_11_MSB;
  137. break;
  138. }
  139. ifx_spi_init(spi_device);
  140. return RT_EOK;
  141. }
  142. static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  143. {
  144. RT_ASSERT(device != NULL);
  145. RT_ASSERT(message != NULL);
  146. struct ifx_spi *spi_device = rt_container_of(device->bus, struct ifx_spi, spi_bus);
  147. /* take CS */
  148. if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
  149. {
  150. if (device->config.mode & RT_SPI_CS_HIGH)
  151. {
  152. cyhal_gpio_write(device->cs_pin, PIN_HIGH);
  153. }
  154. else
  155. {
  156. cyhal_gpio_write(device->cs_pin, PIN_LOW);
  157. }
  158. LOG_D("spi take cs\n");
  159. }
  160. int result = RT_EOK;
  161. if (message->length > 0)
  162. {
  163. if (message->send_buf == RT_NULL && message->recv_buf != RT_NULL)
  164. {
  165. /**< receive message */
  166. result = cyhal_spi_transfer(spi_device->spi_handle_t->spi_obj, RT_NULL, 0x00, message->recv_buf, message->length, 0x00);
  167. }
  168. else if (message->send_buf != RT_NULL && message->recv_buf == RT_NULL)
  169. {
  170. /**< send message */
  171. result = cyhal_spi_transfer(spi_device->spi_handle_t->spi_obj, message->send_buf, message->length, RT_NULL, 0x00, 0x00);
  172. }
  173. else if (message->send_buf != RT_NULL && message->recv_buf != RT_NULL)
  174. {
  175. /**< send and receive message */
  176. result = cyhal_spi_transfer(spi_device->spi_handle_t->spi_obj, message->send_buf, message->length, message->recv_buf, message->length, 0x00);
  177. }
  178. /* blocking the thread,and the other tasks can run */
  179. rt_completion_wait(&spi_device->cpt, RT_WAITING_FOREVER);
  180. }
  181. if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
  182. {
  183. if (device->config.mode & RT_SPI_CS_HIGH)
  184. cyhal_gpio_write(device->cs_pin, PIN_LOW);
  185. else
  186. cyhal_gpio_write(device->cs_pin, PIN_HIGH);
  187. }
  188. return message->length;
  189. }
  190. /**
  191. * Attach the spi device to SPI bus, this function must be used after initialization.
  192. */
  193. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin)
  194. {
  195. RT_ASSERT(bus_name != RT_NULL);
  196. RT_ASSERT(device_name != RT_NULL);
  197. rt_err_t result;
  198. struct rt_spi_device *spi_device;
  199. /* attach the device to spi bus*/
  200. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  201. RT_ASSERT(spi_device != RT_NULL);
  202. result = rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, cs_pin, RT_NULL);
  203. if (result != RT_EOK)
  204. {
  205. LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
  206. }
  207. RT_ASSERT(result == RT_EOK);
  208. LOG_D("%s attach to %s done", device_name, bus_name);
  209. return result;
  210. }
  211. int rt_hw_spi_init(void)
  212. {
  213. int result = RT_EOK;
  214. for (int spi_index = 0; spi_index < sizeof(spi_bus_obj) / sizeof(spi_bus_obj[0]); spi_index++)
  215. {
  216. spi_bus_obj[spi_index].spi_obj = rt_malloc(sizeof(cyhal_spi_t));
  217. RT_ASSERT(spi_bus_obj[spi_index].spi_obj != RT_NULL);
  218. spi_config[spi_index].spi_handle_t = &spi_bus_obj[spi_index];
  219. rt_err_t err = rt_spi_bus_register(&spi_config[spi_index].spi_bus, spi_bus_obj[spi_index].bus_name, &ifx_spi_ops);
  220. if (RT_EOK != err)
  221. {
  222. LOG_E("%s bus register failed.", spi_config[spi_index].spi_handle_t->bus_name);
  223. return -RT_ERROR;
  224. }
  225. LOG_D("MOSI PIN:[%d], MISO PIN[%d], CLK PIN[%d]\n",
  226. spi_bus_obj[spi_index].mosi_pin, spi_bus_obj[spi_index].miso_pin,
  227. spi_bus_obj[spi_index].sck_pin);
  228. /* initialize completion object */
  229. rt_completion_init(&spi_config[spi_index].cpt);
  230. }
  231. return result;
  232. }
  233. INIT_BOARD_EXPORT(rt_hw_spi_init);
  234. #endif /* RT_USING_SPI */