drv_spi.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. */
  10. /**< Note : Turn on any DMA mode and all SPIs will turn on DMA */
  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. static struct ra_spi_handle spi_handle[] =
  22. {
  23. #ifdef BSP_USING_SPI0
  24. {.bus_name = "spi0", .spi_ctrl_t = &g_spi0_ctrl, .spi_cfg_t = &g_spi0_cfg,},
  25. #endif
  26. #ifdef BSP_USING_SPI1
  27. {.bus_name = "spi1", .spi_ctrl_t = &g_spi1_ctrl, .spi_cfg_t = &g_spi1_cfg,},
  28. #endif
  29. };
  30. static struct ra_spi spi_config[sizeof(spi_handle) / sizeof(spi_handle[0])] = {0};
  31. void g_spi0_callback(spi_callback_args_t *p_args)
  32. {
  33. rt_interrupt_enter();
  34. if (SPI_EVENT_TRANSFER_COMPLETE == p_args->event)
  35. {
  36. LOG_D("SPI0 cb");
  37. }
  38. rt_interrupt_leave();
  39. }
  40. void g_spi1_callback(spi_callback_args_t *p_args)
  41. {
  42. rt_interrupt_enter();
  43. if (SPI_EVENT_TRANSFER_COMPLETE == p_args->event)
  44. {
  45. LOG_D("SPI1 cb");
  46. }
  47. rt_interrupt_leave();
  48. }
  49. static rt_err_t ra_write_message(struct rt_spi_device *device, const void *send_buf, const rt_size_t len)
  50. {
  51. RT_ASSERT(device != NULL);
  52. RT_ASSERT(device->parent.user_data != NULL);
  53. RT_ASSERT(send_buf != NULL);
  54. RT_ASSERT(len > 0);
  55. rt_err_t err = RT_EOK;
  56. struct ra_spi *spi_dev = rt_container_of(device->bus, struct ra_spi, bus);
  57. spi_dev->cs_pin = *(rt_uint32_t *)device->parent.user_data;
  58. /**< Configure Select Line */
  59. R_BSP_PinWrite(spi_dev->cs_pin, BSP_IO_LEVEL_HIGH);
  60. /* Start a write transfer */
  61. R_BSP_PinWrite(spi_dev->cs_pin, BSP_IO_LEVEL_LOW);
  62. /**< send msessage */
  63. 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);
  64. if (RT_EOK != err)
  65. {
  66. LOG_E("%s write failed.", spi_dev->ra_spi_handle_t->bus_name);
  67. return -RT_ERROR;
  68. }
  69. return len;
  70. }
  71. static rt_err_t ra_read_message(struct rt_spi_device *device, void *recv_buf, const rt_size_t len)
  72. {
  73. RT_ASSERT(device != NULL);
  74. RT_ASSERT(device->parent.user_data != NULL);
  75. RT_ASSERT(recv_buf != NULL);
  76. RT_ASSERT(len > 0);
  77. rt_err_t err = RT_EOK;
  78. struct ra_spi *spi_dev = rt_container_of(device->bus, struct ra_spi, bus);
  79. spi_dev->cs_pin = *(rt_uint32_t *)device->parent.user_data;
  80. /**< Configure Select Line */
  81. R_BSP_PinWrite(spi_dev->cs_pin, BSP_IO_LEVEL_HIGH);
  82. /* Start read transfer */
  83. R_BSP_PinWrite(spi_dev->cs_pin, BSP_IO_LEVEL_LOW);
  84. /**< receive message */
  85. 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);
  86. if (RT_EOK != err)
  87. {
  88. LOG_E("\n%s write failed.\n", spi_dev->ra_spi_handle_t->bus_name);
  89. return -RT_ERROR;
  90. }
  91. return len;
  92. }
  93. static rt_err_t ra_write_read_message(struct rt_spi_device *device, struct rt_spi_message *message)
  94. {
  95. RT_ASSERT(device != NULL);
  96. RT_ASSERT(message != NULL);
  97. RT_ASSERT(message->length > 0);
  98. rt_err_t err = RT_EOK;
  99. struct ra_spi *spi_dev = rt_container_of(device->bus, struct ra_spi, bus);
  100. /**< write and receive message */
  101. 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);
  102. if (RT_EOK != err)
  103. {
  104. LOG_E("%s write and read failed.", spi_dev->ra_spi_handle_t->bus_name);
  105. return -RT_ERROR;
  106. }
  107. return message->length;
  108. }
  109. /**< init spi TODO : MSB does not support modification */
  110. static rt_err_t ra_hw_spi_configure(struct rt_spi_device *device,
  111. struct rt_spi_configuration *configuration)
  112. {
  113. RT_ASSERT(device != NULL);
  114. RT_ASSERT(configuration != NULL);
  115. rt_err_t err = RT_EOK;
  116. struct ra_spi *spi_dev = rt_container_of(device->bus, struct ra_spi, bus);
  117. spi_dev->cs_pin = (rt_uint32_t)device->parent.user_data;
  118. /**< data_width : 1 -> 8 bits , 2 -> 16 bits, 4 -> 32 bits, default 32 bits*/
  119. rt_uint8_t data_width = configuration->data_width / 8;
  120. RT_ASSERT(data_width == 1 || data_width == 2 || data_width == 4);
  121. configuration->data_width = configuration->data_width / 8;
  122. spi_dev->rt_spi_cfg_t = configuration;
  123. spi_extended_cfg_t *spi_cfg = (spi_extended_cfg_t *)spi_dev->ra_spi_handle_t->spi_cfg_t->p_extend;
  124. /**< Configure Select Line */
  125. R_BSP_PinWrite(spi_dev->cs_pin, BSP_IO_LEVEL_HIGH);
  126. /**< config bitrate */
  127. R_SPI_CalculateBitrate(spi_dev->rt_spi_cfg_t->max_hz, &spi_cfg->spck_div);
  128. /**< init */
  129. 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);
  130. /* handle error */
  131. if (RT_EOK != err)
  132. {
  133. LOG_E("%s init failed.", spi_dev->ra_spi_handle_t->bus_name);
  134. return -RT_ERROR;
  135. }
  136. return RT_EOK;
  137. }
  138. static rt_uint32_t ra_spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  139. {
  140. RT_ASSERT(device != NULL);
  141. RT_ASSERT(message != NULL);
  142. rt_err_t err = RT_EOK;
  143. if (message->length <= 0)
  144. {
  145. LOG_E("buf length err.");
  146. }
  147. else
  148. {
  149. if (message->send_buf == RT_NULL && message->recv_buf != RT_NULL)
  150. {
  151. /**< receive message */
  152. err = ra_read_message(device, (void *)message->recv_buf, (const rt_size_t)message->length);
  153. }
  154. else if (message->send_buf != RT_NULL && message->recv_buf == RT_NULL)
  155. {
  156. /**< send message */
  157. err = ra_write_message(device, (const void *)message->send_buf, (const rt_size_t)message->length);
  158. }
  159. else if (message->send_buf != RT_NULL && message->recv_buf != RT_NULL)
  160. {
  161. /**< send and receive message */
  162. err = ra_write_read_message(device, message);
  163. }
  164. }
  165. return err;
  166. }
  167. static const struct rt_spi_ops ra_spi_ops =
  168. {
  169. .configure = ra_hw_spi_configure,
  170. .xfer = ra_spixfer,
  171. };
  172. void rt_hw_spi_device_attach(struct rt_spi_device *device, const char *device_name, const char *bus_name, void *user_data)
  173. {
  174. RT_ASSERT(device != NULL);
  175. RT_ASSERT(device_name != NULL);
  176. RT_ASSERT(bus_name != NULL);
  177. RT_ASSERT(user_data != NULL);
  178. rt_err_t err = rt_spi_bus_attach_device(device, device_name, bus_name, user_data);
  179. if (RT_EOK != err)
  180. {
  181. LOG_E("%s attach failed.", bus_name);
  182. }
  183. }
  184. int ra_hw_spi_init(void)
  185. {
  186. for (rt_uint8_t spi_index = 0; spi_index < sizeof(spi_handle) / sizeof(spi_handle[0]); spi_index++)
  187. {
  188. spi_config[spi_index].ra_spi_handle_t = &spi_handle[spi_index];
  189. /**< register spi bus */
  190. rt_err_t err = rt_spi_bus_register(&spi_config[spi_index].bus, spi_handle[spi_index].bus_name, &ra_spi_ops);
  191. if (RT_EOK != err)
  192. {
  193. LOG_E("%s bus register failed.", spi_config[spi_index].ra_spi_handle_t->bus_name);
  194. }
  195. }
  196. return RT_EOK;
  197. }
  198. INIT_BOARD_EXPORT(ra_hw_spi_init);
  199. #endif /* RT_USING_SPI */