drv_spi.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-08-23 Mr.Tiger first version
  9. * 2021-11-04 Sherman ADD complete_event
  10. * 2022-12-7 Vandoul ADD sci spi support
  11. */
  12. /**< Note : Turn on any DMA mode and all SPIs will turn on DMA */
  13. #include "drv_spi.h"
  14. #ifdef RT_USING_SPI
  15. //#define DRV_DEBUG
  16. #define DBG_TAG "drv.spi"
  17. #ifdef DRV_DEBUG
  18. #define DBG_LVL DBG_LOG
  19. #else
  20. #define DBG_LVL DBG_INFO
  21. #endif /* DRV_DEBUG */
  22. #include <rtdbg.h>
  23. #if defined(BSP_USING_SPI0) || defined(BSP_USING_SPI1)
  24. #define RA_SPI0_EVENT 0x01
  25. #define RA_SPI1_EVENT 0x02
  26. static struct rt_event complete_event = {0};
  27. static struct ra_spi_handle spi_handle[] =
  28. {
  29. #ifdef BSP_USING_SPI0
  30. {.bus_name = "spi0", .spi_ctrl_t = &g_spi0_ctrl, .spi_cfg_t = &g_spi0_cfg,},
  31. #endif
  32. #ifdef BSP_USING_SPI1
  33. {.bus_name = "spi1", .spi_ctrl_t = &g_spi1_ctrl, .spi_cfg_t = &g_spi1_cfg,},
  34. #endif
  35. };
  36. static struct ra_spi spi_config[sizeof(spi_handle) / sizeof(spi_handle[0])] = {0};
  37. void spi0_callback(spi_callback_args_t *p_args)
  38. {
  39. rt_interrupt_enter();
  40. if (SPI_EVENT_TRANSFER_COMPLETE == p_args->event)
  41. {
  42. rt_event_send(&complete_event, RA_SPI0_EVENT);
  43. }
  44. rt_interrupt_leave();
  45. }
  46. void spi1_callback(spi_callback_args_t *p_args)
  47. {
  48. rt_interrupt_enter();
  49. if (SPI_EVENT_TRANSFER_COMPLETE == p_args->event)
  50. {
  51. rt_event_send(&complete_event, RA_SPI1_EVENT);
  52. }
  53. rt_interrupt_leave();
  54. }
  55. static rt_err_t ra_wait_complete(rt_event_t event, const char bus_name[RT_NAME_MAX])
  56. {
  57. rt_uint32_t recved = 0x00;
  58. if (bus_name[3] == '0')
  59. {
  60. return rt_event_recv(event,
  61. RA_SPI0_EVENT,
  62. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  63. RT_WAITING_FOREVER,
  64. &recved);
  65. }
  66. else if (bus_name[3] == '1')
  67. {
  68. return rt_event_recv(event,
  69. RA_SPI1_EVENT,
  70. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  71. RT_WAITING_FOREVER,
  72. &recved);
  73. }
  74. return -RT_EINVAL;
  75. }
  76. static spi_bit_width_t ra_width_shift(rt_uint8_t data_width)
  77. {
  78. spi_bit_width_t bit_width = SPI_BIT_WIDTH_8_BITS;
  79. if(data_width == 1)
  80. bit_width = SPI_BIT_WIDTH_8_BITS;
  81. else if(data_width == 2)
  82. bit_width = SPI_BIT_WIDTH_16_BITS;
  83. else if(data_width == 4)
  84. bit_width = SPI_BIT_WIDTH_32_BITS;
  85. return bit_width;
  86. }
  87. static rt_err_t ra_write_message(struct rt_spi_device *device, const void *send_buf, const rt_size_t len)
  88. {
  89. RT_ASSERT(device != NULL);
  90. RT_ASSERT(device->parent.user_data != NULL);
  91. RT_ASSERT(send_buf != NULL);
  92. RT_ASSERT(len > 0);
  93. rt_err_t err = RT_EOK;
  94. struct ra_spi *spi_dev = rt_container_of(device->bus, struct ra_spi, bus);
  95. spi_bit_width_t bit_width = ra_width_shift(spi_dev->rt_spi_cfg_t->data_width);
  96. /**< send msessage */
  97. err = R_SPI_Write((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, send_buf, len, bit_width);
  98. if (RT_EOK != err)
  99. {
  100. LOG_E("%s write failed.", spi_dev->ra_spi_handle_t->bus_name);
  101. return -RT_ERROR;
  102. }
  103. /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
  104. ra_wait_complete(&complete_event, spi_dev->ra_spi_handle_t->bus_name);
  105. return len;
  106. }
  107. static rt_err_t ra_read_message(struct rt_spi_device *device, void *recv_buf, const rt_size_t len)
  108. {
  109. RT_ASSERT(device != NULL);
  110. RT_ASSERT(device->parent.user_data != NULL);
  111. RT_ASSERT(recv_buf != NULL);
  112. RT_ASSERT(len > 0);
  113. rt_err_t err = RT_EOK;
  114. struct ra_spi *spi_dev = rt_container_of(device->bus, struct ra_spi, bus);
  115. spi_bit_width_t bit_width = ra_width_shift(spi_dev->rt_spi_cfg_t->data_width);
  116. /**< receive message */
  117. err = R_SPI_Read((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, recv_buf, len, bit_width);
  118. if (RT_EOK != err)
  119. {
  120. LOG_E("\n%s write failed.\n", spi_dev->ra_spi_handle_t->bus_name);
  121. return -RT_ERROR;
  122. }
  123. /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
  124. ra_wait_complete(&complete_event, spi_dev->ra_spi_handle_t->bus_name);
  125. return len;
  126. }
  127. static rt_err_t ra_write_read_message(struct rt_spi_device *device, struct rt_spi_message *message)
  128. {
  129. RT_ASSERT(device != NULL);
  130. RT_ASSERT(message != NULL);
  131. RT_ASSERT(message->length > 0);
  132. rt_err_t err = RT_EOK;
  133. struct ra_spi *spi_dev = rt_container_of(device->bus, struct ra_spi, bus);
  134. spi_bit_width_t bit_width = ra_width_shift(spi_dev->rt_spi_cfg_t->data_width);
  135. /**< write and receive message */
  136. err = R_SPI_WriteRead((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, message->send_buf, message->recv_buf, message->length, bit_width);
  137. if (RT_EOK != err)
  138. {
  139. LOG_E("%s write and read failed.", spi_dev->ra_spi_handle_t->bus_name);
  140. return -RT_ERROR;
  141. }
  142. /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
  143. ra_wait_complete(&complete_event, spi_dev->ra_spi_handle_t->bus_name);
  144. return message->length;
  145. }
  146. /**< init spi TODO : MSB does not support modification */
  147. static rt_err_t ra_hw_spi_configure(struct rt_spi_device *device,
  148. struct rt_spi_configuration *configuration)
  149. {
  150. RT_ASSERT(device != NULL);
  151. RT_ASSERT(configuration != NULL);
  152. rt_err_t err = RT_EOK;
  153. struct ra_spi *spi_dev = rt_container_of(device->bus, struct ra_spi, bus);
  154. spi_dev->cs_pin = (rt_uint32_t)device->parent.user_data;
  155. /**< data_width : 1 -> 8 bits , 2 -> 16 bits, 4 -> 32 bits, default 32 bits*/
  156. rt_uint8_t data_width = configuration->data_width / 8;
  157. RT_ASSERT(data_width == 1 || data_width == 2 || data_width == 4);
  158. configuration->data_width = configuration->data_width / 8;
  159. spi_dev->rt_spi_cfg_t = configuration;
  160. spi_extended_cfg_t *spi_cfg = (spi_extended_cfg_t *)spi_dev->ra_spi_handle_t->spi_cfg_t->p_extend;
  161. /**< Configure Select Line */
  162. rt_pin_write(spi_dev->cs_pin, PIN_HIGH);
  163. /**< config bitrate */
  164. R_SPI_CalculateBitrate(spi_dev->rt_spi_cfg_t->max_hz, &spi_cfg->spck_div);
  165. /**< init */
  166. err = R_SPI_Open((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, (spi_cfg_t const * const)spi_dev->ra_spi_handle_t->spi_cfg_t);
  167. /* handle error */
  168. if (RT_EOK != err)
  169. {
  170. LOG_E("%s init failed.", spi_dev->ra_spi_handle_t->bus_name);
  171. return -RT_ERROR;
  172. }
  173. return RT_EOK;
  174. }
  175. static rt_uint32_t ra_spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  176. {
  177. RT_ASSERT(device != RT_NULL);
  178. RT_ASSERT(device->bus != RT_NULL);
  179. RT_ASSERT(message != RT_NULL);
  180. rt_err_t err = RT_EOK;
  181. struct ra_spi *spi_dev = rt_container_of(device->bus, struct ra_spi, bus);
  182. spi_dev->cs_pin = (rt_uint32_t)device->parent.user_data;
  183. if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS))
  184. {
  185. if (device->config.mode & RT_SPI_CS_HIGH)
  186. rt_pin_write(spi_dev->cs_pin, PIN_HIGH);
  187. else
  188. rt_pin_write(spi_dev->cs_pin, PIN_LOW);
  189. }
  190. if (message->length > 0)
  191. {
  192. if (message->send_buf == RT_NULL && message->recv_buf != RT_NULL)
  193. {
  194. /**< receive message */
  195. err = ra_read_message(device, (void *)message->recv_buf, (const rt_size_t)message->length);
  196. }
  197. else if (message->send_buf != RT_NULL && message->recv_buf == RT_NULL)
  198. {
  199. /**< send message */
  200. err = ra_write_message(device, (const void *)message->send_buf, (const rt_size_t)message->length);
  201. }
  202. else if (message->send_buf != RT_NULL && message->recv_buf != RT_NULL)
  203. {
  204. /**< send and receive message */
  205. err = ra_write_read_message(device, message);
  206. }
  207. }
  208. if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS))
  209. {
  210. if (device->config.mode & RT_SPI_CS_HIGH)
  211. rt_pin_write(spi_dev->cs_pin, PIN_LOW);
  212. else
  213. rt_pin_write(spi_dev->cs_pin, PIN_HIGH);
  214. }
  215. return err;
  216. }
  217. static const struct rt_spi_ops ra_spi_ops =
  218. {
  219. .configure = ra_hw_spi_configure,
  220. .xfer = ra_spixfer,
  221. };
  222. int ra_hw_spi_init(void)
  223. {
  224. for (rt_uint8_t spi_index = 0; spi_index < sizeof(spi_handle) / sizeof(spi_handle[0]); spi_index++)
  225. {
  226. spi_config[spi_index].ra_spi_handle_t = &spi_handle[spi_index];
  227. /**< register spi bus */
  228. rt_err_t err = rt_spi_bus_register(&spi_config[spi_index].bus, spi_handle[spi_index].bus_name, &ra_spi_ops);
  229. if (RT_EOK != err)
  230. {
  231. LOG_E("%s bus register failed.", spi_config[spi_index].ra_spi_handle_t->bus_name);
  232. return -RT_ERROR;
  233. }
  234. }
  235. if (RT_EOK != rt_event_init(&complete_event, "ra_spi", RT_IPC_FLAG_PRIO))
  236. {
  237. LOG_E("SPI transfer event init fail!");
  238. return -RT_ERROR;
  239. }
  240. return RT_EOK;
  241. }
  242. INIT_BOARD_EXPORT(ra_hw_spi_init);
  243. #endif
  244. void rt_hw_spi_device_attach(struct rt_spi_device *device, const char *device_name, const char *bus_name, void *user_data)
  245. {
  246. RT_ASSERT(device != NULL);
  247. RT_ASSERT(device_name != NULL);
  248. RT_ASSERT(bus_name != NULL);
  249. RT_ASSERT(user_data != NULL);
  250. rt_err_t err = rt_spi_bus_attach_device(device, device_name, bus_name, user_data);
  251. if (RT_EOK != err)
  252. {
  253. LOG_E("%s attach failed.", bus_name);
  254. }
  255. }
  256. #endif /* RT_USING_SPI */