drv_sci_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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
  11. */
  12. /**< Note : Turn on any DMA mode and all SPIs will turn on DMA */
  13. #include "drv_sci_spi.h"
  14. #ifdef RT_USING_SPI
  15. //#define DRV_DEBUG
  16. #define DBG_TAG "drv.scispi"
  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. #define RA_SCI_SPI0_EVENT 0x0001
  24. #define RA_SCI_SPI1_EVENT 0x0002
  25. #define RA_SCI_SPI2_EVENT 0x0004
  26. #define RA_SCI_SPI3_EVENT 0x0008
  27. #define RA_SCI_SPI4_EVENT 0x0010
  28. #define RA_SCI_SPI5_EVENT 0x0020
  29. #define RA_SCI_SPI6_EVENT 0x0040
  30. #define RA_SCI_SPI7_EVENT 0x0080
  31. #define RA_SCI_SPI8_EVENT 0x0100
  32. #define RA_SCI_SPI9_EVENT 0x0200
  33. static struct rt_event complete_event = {0};
  34. static struct ra_sci_spi_handle spi_handle[] =
  35. {
  36. #ifdef BSP_USING_SCI_SPI0
  37. {.bus_name = "scpi0", .spi_ctrl_t = &g_sci_spi0_ctrl, .spi_cfg_t = &g_sci_spi0_cfg,},
  38. #endif
  39. #ifdef BSP_USING_SCI_SPI1
  40. {.bus_name = "scpi1", .spi_ctrl_t = &g_sci_spi1_ctrl, .spi_cfg_t = &g_sci_spi1_cfg,},
  41. #endif
  42. #ifdef BSP_USING_SCI_SPI2
  43. {.bus_name = "scpi2", .spi_ctrl_t = &g_sci_spi2_ctrl, .spi_cfg_t = &g_sci_spi2_cfg,},
  44. #endif
  45. #ifdef BSP_USING_SCI_SPI3
  46. {.bus_name = "scpi3", .spi_ctrl_t = &g_sci_spi3_ctrl, .spi_cfg_t = &g_sci_spi3_cfg,},
  47. #endif
  48. #ifdef BSP_USING_SCI_SPI4
  49. {.bus_name = "scpi4", .spi_ctrl_t = &g_sci_spi4_ctrl, .spi_cfg_t = &g_sci_spi4_cfg,},
  50. #endif
  51. #ifdef BSP_USING_SCI_SPI5
  52. {.bus_name = "scpi5", .spi_ctrl_t = &g_sci_spi5_ctrl, .spi_cfg_t = &g_sci_spi5_cfg,},
  53. #endif
  54. #ifdef BSP_USING_SCI_SPI6
  55. {.bus_name = "scpi6", .spi_ctrl_t = &g_sci_spi6_ctrl, .spi_cfg_t = &g_sci_spi6_cfg,},
  56. #endif
  57. #ifdef BSP_USING_SCI_SPI7
  58. {.bus_name = "scpi7", .spi_ctrl_t = &g_sci_spi7_ctrl, .spi_cfg_t = &g_sci_spi7_cfg,},
  59. #endif
  60. #ifdef BSP_USING_SCI_SPI8
  61. {.bus_name = "scpi8", .spi_ctrl_t = &g_sci_spi8_ctrl, .spi_cfg_t = &g_sci_spi8_cfg,},
  62. #endif
  63. #ifdef BSP_USING_SCI_SPI9
  64. {.bus_name = "scpi9", .spi_ctrl_t = &g_sci_spi9_ctrl, .spi_cfg_t = &g_sci_spi9_cfg,},
  65. #endif
  66. };
  67. static struct ra_sci_spi spi_config[sizeof(spi_handle) / sizeof(spi_handle[0])] = {0};
  68. #define SCI_SPIx_CALLBACK(n) \
  69. void sci_spi##n##_callback(spi_callback_args_t *p_args) \
  70. { \
  71. rt_interrupt_enter(); \
  72. if (SPI_EVENT_TRANSFER_COMPLETE == p_args->event) \
  73. { \
  74. rt_event_send(&complete_event, RA_SCI_SPI##n##_EVENT); \
  75. } \
  76. rt_interrupt_leave(); \
  77. }
  78. SCI_SPIx_CALLBACK(0);
  79. SCI_SPIx_CALLBACK(1);
  80. SCI_SPIx_CALLBACK(2);
  81. SCI_SPIx_CALLBACK(3);
  82. SCI_SPIx_CALLBACK(4);
  83. SCI_SPIx_CALLBACK(5);
  84. SCI_SPIx_CALLBACK(6);
  85. SCI_SPIx_CALLBACK(7);
  86. SCI_SPIx_CALLBACK(8);
  87. SCI_SPIx_CALLBACK(9);
  88. #define SCI_SPIx_EVENT_RECV(n) \
  89. rt_event_recv(event, \
  90. RA_SCI_SPI##n##_EVENT, \
  91. RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, \
  92. RT_WAITING_FOREVER, \
  93. &recved);
  94. static rt_err_t ra_wait_complete(rt_event_t event, const char bus_name[RT_NAME_MAX])
  95. {
  96. rt_uint32_t recved = 0x00;
  97. switch (bus_name[6])
  98. {
  99. case '0':
  100. return SCI_SPIx_EVENT_RECV(0);
  101. case '1':
  102. return SCI_SPIx_EVENT_RECV(1);
  103. case '2':
  104. return SCI_SPIx_EVENT_RECV(2);
  105. case '3':
  106. return SCI_SPIx_EVENT_RECV(3);
  107. case '4':
  108. return SCI_SPIx_EVENT_RECV(4);
  109. case '5':
  110. return SCI_SPIx_EVENT_RECV(5);
  111. case '6':
  112. return SCI_SPIx_EVENT_RECV(6);
  113. case '7':
  114. return SCI_SPIx_EVENT_RECV(7);
  115. case '8':
  116. return SCI_SPIx_EVENT_RECV(8);
  117. case '9':
  118. return SCI_SPIx_EVENT_RECV(9);
  119. }
  120. return -RT_EINVAL;
  121. }
  122. static spi_bit_width_t ra_width_shift(rt_uint8_t data_width)
  123. {
  124. spi_bit_width_t bit_width = SPI_BIT_WIDTH_8_BITS;
  125. if(data_width == 1)
  126. bit_width = SPI_BIT_WIDTH_8_BITS;
  127. else if(data_width == 2)
  128. bit_width = SPI_BIT_WIDTH_16_BITS;
  129. else if(data_width == 4)
  130. bit_width = SPI_BIT_WIDTH_32_BITS;
  131. return bit_width;
  132. }
  133. static rt_err_t ra_write_message(struct rt_spi_device *device, const void *send_buf, const rt_size_t len)
  134. {
  135. RT_ASSERT(device != NULL);
  136. RT_ASSERT(device->parent.user_data != NULL);
  137. RT_ASSERT(send_buf != NULL);
  138. RT_ASSERT(len > 0);
  139. rt_err_t err = RT_EOK;
  140. struct ra_sci_spi *spi_dev = rt_container_of(device->bus, struct ra_sci_spi, bus);
  141. spi_bit_width_t bit_width = ra_width_shift(spi_dev->rt_spi_cfg_t->data_width);
  142. /**< send msessage */
  143. err = R_SCI_SPI_Write((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, send_buf, len, bit_width);
  144. if (RT_EOK != err)
  145. {
  146. LOG_E("%s write failed. %d", spi_dev->ra_spi_handle_t->bus_name, err);
  147. return -RT_ERROR;
  148. }
  149. /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
  150. ra_wait_complete(&complete_event, spi_dev->ra_spi_handle_t->bus_name);
  151. return len;
  152. }
  153. static rt_err_t ra_read_message(struct rt_spi_device *device, void *recv_buf, const rt_size_t len)
  154. {
  155. RT_ASSERT(device != NULL);
  156. RT_ASSERT(device->parent.user_data != NULL);
  157. RT_ASSERT(recv_buf != NULL);
  158. RT_ASSERT(len > 0);
  159. rt_err_t err = RT_EOK;
  160. struct ra_sci_spi *spi_dev = rt_container_of(device->bus, struct ra_sci_spi, bus);
  161. spi_bit_width_t bit_width = ra_width_shift(spi_dev->rt_spi_cfg_t->data_width);
  162. /**< receive message */
  163. err = R_SCI_SPI_Read((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, recv_buf, len, bit_width);
  164. if (RT_EOK != err)
  165. {
  166. LOG_E("%s write failed. %d", spi_dev->ra_spi_handle_t->bus_name, err);
  167. return -RT_ERROR;
  168. }
  169. /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
  170. ra_wait_complete(&complete_event, spi_dev->ra_spi_handle_t->bus_name);
  171. return len;
  172. }
  173. static rt_err_t ra_write_read_message(struct rt_spi_device *device, struct rt_spi_message *message)
  174. {
  175. RT_ASSERT(device != NULL);
  176. RT_ASSERT(message != NULL);
  177. RT_ASSERT(message->length > 0);
  178. rt_err_t err = RT_EOK;
  179. struct ra_sci_spi *spi_dev = rt_container_of(device->bus, struct ra_sci_spi, bus);
  180. spi_bit_width_t bit_width = ra_width_shift(spi_dev->rt_spi_cfg_t->data_width);
  181. /**< write and receive message */
  182. err = R_SCI_SPI_WriteRead((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t, message->send_buf, message->recv_buf, message->length, bit_width);
  183. if (RT_EOK != err)
  184. {
  185. LOG_E("%s write and read failed. %d", spi_dev->ra_spi_handle_t->bus_name, err);
  186. return -RT_ERROR;
  187. }
  188. /* Wait for SPI_EVENT_TRANSFER_COMPLETE callback event. */
  189. ra_wait_complete(&complete_event, spi_dev->ra_spi_handle_t->bus_name);
  190. return message->length;
  191. }
  192. /**< init spi TODO : MSB does not support modification */
  193. static rt_err_t ra_hw_spi_configure(struct rt_spi_device *device,
  194. struct rt_spi_configuration *configuration)
  195. {
  196. RT_ASSERT(device != NULL);
  197. RT_ASSERT(configuration != NULL);
  198. rt_err_t err = RT_EOK;
  199. struct ra_sci_spi *spi_dev = rt_container_of(device->bus, struct ra_sci_spi, bus);
  200. spi_dev->cs_pin = (rt_uint32_t)device->parent.user_data;
  201. /**< data_width : 1 -> 8 bits , 2 -> 16 bits, 4 -> 32 bits, default 32 bits*/
  202. rt_uint8_t data_width = configuration->data_width / 8;
  203. RT_ASSERT(data_width == 1 || data_width == 2 || data_width == 4);
  204. configuration->data_width = configuration->data_width / 8;
  205. spi_dev->rt_spi_cfg_t = configuration;
  206. sci_spi_extended_cfg_t *spi_cfg = (sci_spi_extended_cfg_t *)spi_dev->ra_spi_handle_t->spi_cfg_t->p_extend;
  207. /**< Configure Select Line */
  208. rt_pin_write(spi_dev->cs_pin, PIN_HIGH);
  209. /**< config bitrate */
  210. R_SCI_SPI_CalculateBitrate(spi_dev->rt_spi_cfg_t->max_hz, &spi_cfg->clk_div, false);
  211. /**< init */
  212. err = R_SCI_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);
  213. /* handle error */
  214. if(err == FSP_ERR_IN_USE) {
  215. R_SCI_SPI_Close((spi_ctrl_t *)spi_dev->ra_spi_handle_t->spi_ctrl_t);
  216. err = R_SCI_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);
  217. }
  218. if (RT_EOK != err)
  219. {
  220. LOG_E("%s init failed. %d", spi_dev->ra_spi_handle_t->bus_name, err);
  221. return -RT_ERROR;
  222. }
  223. return RT_EOK;
  224. }
  225. static rt_uint32_t ra_spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  226. {
  227. RT_ASSERT(device != RT_NULL);
  228. RT_ASSERT(device->bus != RT_NULL);
  229. RT_ASSERT(message != RT_NULL);
  230. rt_err_t err = RT_EOK;
  231. struct ra_sci_spi *spi_dev = rt_container_of(device->bus, struct ra_sci_spi, bus);
  232. spi_dev->cs_pin = (rt_uint32_t)device->parent.user_data;
  233. if (message->cs_take && !(device->config.mode & RT_SPI_NO_CS))
  234. {
  235. if (device->config.mode & RT_SPI_CS_HIGH)
  236. rt_pin_write(spi_dev->cs_pin, PIN_HIGH);
  237. else
  238. rt_pin_write(spi_dev->cs_pin, PIN_LOW);
  239. }
  240. if (message->length > 0)
  241. {
  242. if (message->send_buf == RT_NULL && message->recv_buf != RT_NULL)
  243. {
  244. /**< receive message */
  245. err = ra_read_message(device, (void *)message->recv_buf, (const rt_size_t)message->length);
  246. }
  247. else if (message->send_buf != RT_NULL && message->recv_buf == RT_NULL)
  248. {
  249. /**< send message */
  250. err = ra_write_message(device, (const void *)message->send_buf, (const rt_size_t)message->length);
  251. }
  252. else if (message->send_buf != RT_NULL && message->recv_buf != RT_NULL)
  253. {
  254. /**< send and receive message */
  255. err = ra_write_read_message(device, message);
  256. }
  257. }
  258. if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS))
  259. {
  260. if (device->config.mode & RT_SPI_CS_HIGH)
  261. rt_pin_write(spi_dev->cs_pin, PIN_LOW);
  262. else
  263. rt_pin_write(spi_dev->cs_pin, PIN_HIGH);
  264. }
  265. return err;
  266. }
  267. static const struct rt_spi_ops ra_spi_ops =
  268. {
  269. .configure = ra_hw_spi_configure,
  270. .xfer = ra_spixfer,
  271. };
  272. int ra_hw_sci_spi_init(void)
  273. {
  274. for (rt_uint8_t spi_index = 0; spi_index < sizeof(spi_handle) / sizeof(spi_handle[0]); spi_index++)
  275. {
  276. spi_config[spi_index].ra_spi_handle_t = &spi_handle[spi_index];
  277. /**< register spi bus */
  278. rt_err_t err = rt_spi_bus_register(&spi_config[spi_index].bus, spi_handle[spi_index].bus_name, &ra_spi_ops);
  279. if (RT_EOK != err)
  280. {
  281. LOG_E("%s bus register failed. %d", spi_config[spi_index].ra_spi_handle_t->bus_name, err);
  282. return -RT_ERROR;
  283. }
  284. }
  285. if (RT_EOK != rt_event_init(&complete_event, "ra_scispi", RT_IPC_FLAG_PRIO))
  286. {
  287. LOG_E("SPI transfer event init fail!");
  288. return -RT_ERROR;
  289. }
  290. return RT_EOK;
  291. }
  292. INIT_BOARD_EXPORT(ra_hw_sci_spi_init);
  293. #endif /* RT_USING_SPI */