drv_spi.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. #ifdef SOC_SERIES_R7FA8M85
  28. #define R_SPI_Write R_SPI_B_Write
  29. #define R_SPI_Read R_SPI_B_Read
  30. #define R_SPI_WriteRead R_SPI_B_WriteRead
  31. #define R_SPI_Open R_SPI_B_Open
  32. #define spi_extended_cfg_t spi_b_extended_cfg_t
  33. #endif
  34. static struct ra_spi_handle spi_handle[] =
  35. {
  36. #ifdef BSP_USING_SPI0
  37. {.bus_name = "spi0", .spi_ctrl_t = &g_spi0_ctrl, .spi_cfg_t = &g_spi0_cfg,},
  38. #endif
  39. #ifdef BSP_USING_SPI1
  40. {.bus_name = "spi1", .spi_ctrl_t = &g_spi1_ctrl, .spi_cfg_t = &g_spi1_cfg,},
  41. #endif
  42. };
  43. static struct ra_spi spi_config[sizeof(spi_handle) / sizeof(spi_handle[0])] = {0};
  44. void spi0_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_SPI0_EVENT);
  50. }
  51. rt_interrupt_leave();
  52. }
  53. void spi1_callback(spi_callback_args_t *p_args)
  54. {
  55. rt_interrupt_enter();
  56. if (SPI_EVENT_TRANSFER_COMPLETE == p_args->event)
  57. {
  58. rt_event_send(&complete_event, RA_SPI1_EVENT);
  59. }
  60. rt_interrupt_leave();
  61. }
  62. static rt_err_t ra_wait_complete(rt_event_t event, const char bus_name[RT_NAME_MAX])
  63. {
  64. rt_uint32_t recved = 0x00;
  65. if (bus_name[3] == '0')
  66. {
  67. return rt_event_recv(event,
  68. RA_SPI0_EVENT,
  69. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  70. RT_WAITING_FOREVER,
  71. &recved);
  72. }
  73. else if (bus_name[3] == '1')
  74. {
  75. return rt_event_recv(event,
  76. RA_SPI1_EVENT,
  77. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
  78. RT_WAITING_FOREVER,
  79. &recved);
  80. }
  81. return -RT_EINVAL;
  82. }
  83. static spi_bit_width_t ra_width_shift(rt_uint8_t data_width)
  84. {
  85. spi_bit_width_t bit_width = SPI_BIT_WIDTH_8_BITS;
  86. if (data_width == 1)
  87. bit_width = SPI_BIT_WIDTH_8_BITS;
  88. else if (data_width == 2)
  89. bit_width = SPI_BIT_WIDTH_16_BITS;
  90. else if (data_width == 4)
  91. bit_width = SPI_BIT_WIDTH_32_BITS;
  92. return bit_width;
  93. }
  94. static rt_err_t ra_write_message(struct rt_spi_device *device, const void *send_buf, const rt_size_t len)
  95. {
  96. RT_ASSERT(device != NULL);
  97. RT_ASSERT(send_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. spi_bit_width_t bit_width = ra_width_shift(spi_dev->rt_spi_cfg_t->data_width);
  102. /**< send msessage */
  103. err = R_SPI_Write((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, send_buf, len, bit_width);
  104. if (RT_EOK != err)
  105. {
  106. LOG_E("%s write failed.", spi_dev->ra_spi_handle_t->bus_name);
  107. return -RT_ERROR;
  108. }
  109. /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
  110. ra_wait_complete(&complete_event, spi_dev->ra_spi_handle_t->bus_name);
  111. return len;
  112. }
  113. static rt_err_t ra_read_message(struct rt_spi_device *device, void *recv_buf, const rt_size_t len)
  114. {
  115. RT_ASSERT(device != NULL);
  116. RT_ASSERT(recv_buf != NULL);
  117. RT_ASSERT(len > 0);
  118. rt_err_t err = RT_EOK;
  119. struct ra_spi *spi_dev = rt_container_of(device->bus, struct ra_spi, bus);
  120. spi_bit_width_t bit_width = ra_width_shift(spi_dev->rt_spi_cfg_t->data_width);
  121. /**< receive message */
  122. err = R_SPI_Read((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, recv_buf, len, bit_width);
  123. if (RT_EOK != err)
  124. {
  125. LOG_E("\n%s write failed.\n", spi_dev->ra_spi_handle_t->bus_name);
  126. return -RT_ERROR;
  127. }
  128. /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
  129. ra_wait_complete(&complete_event, spi_dev->ra_spi_handle_t->bus_name);
  130. return len;
  131. }
  132. static rt_err_t ra_write_read_message(struct rt_spi_device *device, struct rt_spi_message *message)
  133. {
  134. RT_ASSERT(device != NULL);
  135. RT_ASSERT(message != NULL);
  136. RT_ASSERT(message->length > 0);
  137. rt_err_t err = RT_EOK;
  138. struct ra_spi *spi_dev = rt_container_of(device->bus, struct ra_spi, bus);
  139. spi_bit_width_t bit_width = ra_width_shift(spi_dev->rt_spi_cfg_t->data_width);
  140. /**< write and receive message */
  141. 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);
  142. if (RT_EOK != err)
  143. {
  144. LOG_E("%s write and read failed.", spi_dev->ra_spi_handle_t->bus_name);
  145. return -RT_ERROR;
  146. }
  147. /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
  148. ra_wait_complete(&complete_event, spi_dev->ra_spi_handle_t->bus_name);
  149. return message->length;
  150. }
  151. /**< init spi TODO : MSB does not support modification */
  152. static rt_err_t ra_hw_spi_configure(struct rt_spi_device *device,
  153. struct rt_spi_configuration *configuration)
  154. {
  155. RT_ASSERT(device != NULL);
  156. RT_ASSERT(configuration != NULL);
  157. rt_err_t err = RT_EOK;
  158. struct ra_spi *spi_dev = rt_container_of(device->bus, struct ra_spi, bus);
  159. /**< data_width : 1 -> 8 bits , 2 -> 16 bits, 4 -> 32 bits, default 32 bits*/
  160. rt_uint8_t data_width = configuration->data_width / 8;
  161. RT_ASSERT(data_width == 1 || data_width == 2 || data_width == 4);
  162. configuration->data_width = configuration->data_width / 8;
  163. spi_dev->rt_spi_cfg_t = configuration;
  164. spi_extended_cfg_t spi_cfg = *(spi_extended_cfg_t *)spi_dev->ra_spi_handle_t->spi_cfg_t->p_extend;
  165. /**< Configure Select Line */
  166. rt_pin_write(device->cs_pin, PIN_HIGH);
  167. /**< config bitrate */
  168. #ifdef SOC_SERIES_R7FA8M85
  169. R_SPI_B_CalculateBitrate(spi_dev->rt_spi_cfg_t->max_hz, SPI_B_CLOCK_SOURCE_PCLK, &spi_cfg.spck_div);
  170. #else
  171. R_SPI_CalculateBitrate(spi_dev->rt_spi_cfg_t->max_hz, &spi_cfg.spck_div);
  172. #endif
  173. /**< init */
  174. 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);
  175. /* handle error */
  176. if (RT_EOK != err)
  177. {
  178. LOG_E("%s init failed.", spi_dev->ra_spi_handle_t->bus_name);
  179. return -RT_ERROR;
  180. }
  181. return RT_EOK;
  182. }
  183. static rt_ssize_t ra_spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  184. {
  185. RT_ASSERT(device != RT_NULL);
  186. RT_ASSERT(device->bus != RT_NULL);
  187. RT_ASSERT(message != RT_NULL);
  188. rt_err_t err = RT_EOK;
  189. if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
  190. {
  191. if (device->config.mode & RT_SPI_CS_HIGH)
  192. rt_pin_write(device->cs_pin, PIN_HIGH);
  193. else
  194. rt_pin_write(device->cs_pin, PIN_LOW);
  195. }
  196. if (message->length > 0)
  197. {
  198. if (message->send_buf == RT_NULL && message->recv_buf != RT_NULL)
  199. {
  200. /**< receive message */
  201. err = ra_read_message(device, (void *)message->recv_buf, (const rt_size_t)message->length);
  202. }
  203. else if (message->send_buf != RT_NULL && message->recv_buf == RT_NULL)
  204. {
  205. /**< send message */
  206. err = ra_write_message(device, (const void *)message->send_buf, (const rt_size_t)message->length);
  207. }
  208. else if (message->send_buf != RT_NULL && message->recv_buf != RT_NULL)
  209. {
  210. /**< send and receive message */
  211. err = ra_write_read_message(device, message);
  212. }
  213. }
  214. if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS) && (device->cs_pin != PIN_NONE))
  215. {
  216. if (device->config.mode & RT_SPI_CS_HIGH)
  217. rt_pin_write(device->cs_pin, PIN_LOW);
  218. else
  219. rt_pin_write(device->cs_pin, PIN_HIGH);
  220. }
  221. return err;
  222. }
  223. static const struct rt_spi_ops ra_spi_ops =
  224. {
  225. .configure = ra_hw_spi_configure,
  226. .xfer = ra_spixfer,
  227. };
  228. int ra_hw_spi_init(void)
  229. {
  230. for (rt_uint8_t spi_index = 0; spi_index < sizeof(spi_handle) / sizeof(spi_handle[0]); spi_index++)
  231. {
  232. spi_config[spi_index].ra_spi_handle_t = &spi_handle[spi_index];
  233. /**< register spi bus */
  234. rt_err_t err = rt_spi_bus_register(&spi_config[spi_index].bus, spi_handle[spi_index].bus_name, &ra_spi_ops);
  235. if (RT_EOK != err)
  236. {
  237. LOG_E("%s bus register failed.", spi_config[spi_index].ra_spi_handle_t->bus_name);
  238. return -RT_ERROR;
  239. }
  240. }
  241. if (RT_EOK != rt_event_init(&complete_event, "ra_spi", RT_IPC_FLAG_PRIO))
  242. {
  243. LOG_E("SPI transfer event init fail!");
  244. return -RT_ERROR;
  245. }
  246. return RT_EOK;
  247. }
  248. INIT_BOARD_EXPORT(ra_hw_spi_init);
  249. #endif
  250. /**
  251. * Attach the spi device to SPI bus, this function must be used after initialization.
  252. */
  253. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin)
  254. {
  255. RT_ASSERT(bus_name != RT_NULL);
  256. RT_ASSERT(device_name != RT_NULL);
  257. rt_err_t result;
  258. struct rt_spi_device *spi_device;
  259. /* attach the device to spi bus*/
  260. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  261. RT_ASSERT(spi_device != RT_NULL);
  262. result = rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, cs_pin, RT_NULL);
  263. if (result != RT_EOK)
  264. {
  265. LOG_E("%s attach to %s faild, %d\n", device_name, bus_name, result);
  266. }
  267. RT_ASSERT(result == RT_EOK);
  268. LOG_D("%s attach to %s done", device_name, bus_name);
  269. return result;
  270. }
  271. #endif /* RT_USING_SPI */