drv_spi.c 8.7 KB

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