drv_spi.c 8.4 KB

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