drv_spi.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-03-28 qiujingbao first version
  9. * 2024/06/08 flyingcys fix transmission failure
  10. */
  11. #include <rtthread.h>
  12. #include <rthw.h>
  13. #include <rtdevice.h>
  14. #include "board.h"
  15. #include "drv_spi.h"
  16. #include "drv_pinmux.h"
  17. #include "drv_ioremap.h"
  18. #define DBG_LEVEL DBG_LOG
  19. #include <rtdbg.h>
  20. #define LOG_TAG "drv.spi"
  21. struct _device_spi
  22. {
  23. struct rt_spi_bus spi_bus;
  24. struct dw_spi dws;
  25. char *device_name;
  26. };
  27. static struct _device_spi _spi_obj[] =
  28. {
  29. #ifdef BSP_USING_SPI0
  30. {
  31. .dws.regs = (void *)DW_SPI0_BASE,
  32. .dws.irq = DW_SPI0_IRQn,
  33. .dws.index = 0,
  34. .device_name = "spi0",
  35. },
  36. #endif /* BSP_USING_SPI0 */
  37. #ifdef BSP_USING_SPI1
  38. {
  39. .dws.regs = (void *)DW_SPI1_BASE,
  40. .dws.irq = DW_SPI1_IRQn,
  41. .dws.index = 0,
  42. .device_name = "spi1",
  43. },
  44. #endif /* BSP_USING_SPI1 */
  45. #ifdef BSP_USING_SPI2
  46. {
  47. .dws.regs = (void *)DW_SPI2_BASE,
  48. .dws.irq = DW_SPI2_IRQn,
  49. .dws.index = 0,
  50. .device_name = "spi2",
  51. },
  52. #endif /* BSP_USING_SPI2 */
  53. #ifdef BSP_USING_SPI3
  54. {
  55. .dws.regs = (void *)DW_SPI3_BASE,
  56. .dws.irq = DW_SPI3_IRQn,
  57. .dws.index = 0,
  58. .device_name = "spi3",
  59. },
  60. #endif /* BSP_USING_SPI3 */
  61. };
  62. static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
  63. {
  64. RT_ASSERT(device != RT_NULL);
  65. RT_ASSERT(device->bus != RT_NULL);
  66. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  67. RT_ASSERT(cfg != RT_NULL);
  68. struct _device_spi *spi = (struct _device_spi *)device->bus->parent.user_data;
  69. struct dw_spi *dws = &spi->dws;
  70. rt_uint8_t mode;
  71. LOG_D("spi_configure input");
  72. if (cfg->mode & RT_SPI_SLAVE)
  73. {
  74. LOG_E("invalid mode: %d", cfg->mode);
  75. return -RT_EINVAL;
  76. }
  77. spi_reset_chip(dws);
  78. spi_hw_init(dws);
  79. spi_enable_chip(dws, 0);
  80. LOG_D("cfg->max_hz: %d", cfg->max_hz);
  81. dw_spi_set_clock(dws, SPI_REF_CLK, cfg->max_hz);
  82. LOG_D("cfg->data_width: %d", cfg->data_width);
  83. if (dw_spi_set_data_frame_len(dws, (uint32_t)cfg->data_width) < 0)
  84. {
  85. LOG_E("dw_spi_set_data_frame_len failed...\n");
  86. return -RT_ERROR;
  87. }
  88. LOG_D("cfg->mode: %08x", cfg->mode);
  89. switch (cfg->mode & RT_SPI_MODE_3)
  90. {
  91. case RT_SPI_MODE_0:
  92. mode = SPI_FORMAT_CPOL0_CPHA0;
  93. break;
  94. case RT_SPI_MODE_1:
  95. mode = SPI_FORMAT_CPOL0_CPHA1;
  96. break;
  97. case RT_SPI_MODE_2:
  98. mode = SPI_FORMAT_CPOL1_CPHA0;
  99. break;
  100. case RT_SPI_MODE_3:
  101. mode = SPI_FORMAT_CPOL1_CPHA1;
  102. break;
  103. default:
  104. LOG_E("spi configure mode error %x\n", cfg->mode);
  105. break;
  106. }
  107. dw_spi_set_polarity_and_phase(dws, mode);
  108. dw_spi_set_cs(dws, 1, 0);
  109. spi_enable_chip(dws, 1);
  110. return RT_EOK;
  111. }
  112. static rt_err_t dw_spi_transfer_one(struct dw_spi *dws, const void *tx_buf, void *rx_buf, uint32_t len, enum transfer_type tran_type)
  113. {
  114. dws->tx = NULL;
  115. dws->tx_end = NULL;
  116. dws->rx = NULL;
  117. dws->rx_end = NULL;
  118. if (tx_buf != NULL) {
  119. dws->tx = tx_buf;
  120. dws->tx_end = dws->tx + len;
  121. }
  122. if (rx_buf != NULL) {
  123. dws->rx = rx_buf;
  124. dws->rx_end = dws->rx + len;
  125. }
  126. dws->rx_len = len / dws->n_bytes;
  127. dws->tx_len = len / dws->n_bytes;
  128. spi_enable_chip(dws, 0);
  129. /* For poll mode just disable all interrupts */
  130. spi_mask_intr(dws, 0xff);
  131. /* set tran mode */
  132. set_tran_mode(dws);
  133. /* cs0 */
  134. dw_spi_set_cs(dws, true, 0);
  135. /* enable spi */
  136. spi_enable_chip(dws, 1);
  137. rt_hw_us_delay(10);
  138. if (tran_type == POLL_TRAN)
  139. {
  140. if (poll_transfer(dws) < 0)
  141. return -RT_ERROR;
  142. }
  143. else
  144. {
  145. return -RT_ENOSYS;
  146. }
  147. return RT_EOK;
  148. }
  149. static rt_ssize_t spi_xfer(struct rt_spi_device *device, struct rt_spi_message *message)
  150. {
  151. RT_ASSERT(device != RT_NULL);
  152. RT_ASSERT(device->bus != RT_NULL);
  153. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  154. RT_ASSERT(message != RT_NULL);
  155. struct _device_spi *spi = (struct _device_spi *)device->bus->parent.user_data;
  156. struct dw_spi *dws = &spi->dws;
  157. int32_t ret = RT_EOK;
  158. if (message->send_buf && message->recv_buf)
  159. {
  160. ret = dw_spi_transfer_one(dws, message->send_buf, message->recv_buf, message->length, POLL_TRAN);
  161. }
  162. else if (message->send_buf)
  163. {
  164. ret = dw_spi_transfer_one(dws, message->send_buf, RT_NULL, message->length, POLL_TRAN);
  165. }
  166. else if (message->recv_buf)
  167. {
  168. ret = dw_spi_transfer_one(dws, RT_NULL, message->recv_buf, message->length, POLL_TRAN);
  169. } else {
  170. return 0;
  171. }
  172. if (ret != RT_EOK)
  173. {
  174. LOG_E("spi transfer error : %d", ret);
  175. return 0;
  176. }
  177. return message->length;
  178. }
  179. static const struct rt_spi_ops _spi_ops =
  180. {
  181. .configure = spi_configure,
  182. .xfer = spi_xfer,
  183. };
  184. #if defined(BOARD_TYPE_MILKV_DUO) || defined(BOARD_TYPE_MILKV_DUO256M)
  185. // For Duo / Duo 256m, only SPI2 are exported on board.
  186. #ifdef BSP_USING_SPI0
  187. static const char *pinname_whitelist_spi0_sck[] = {
  188. NULL,
  189. };
  190. static const char *pinname_whitelist_spi0_sdo[] = {
  191. NULL,
  192. };
  193. static const char *pinname_whitelist_spi0_sdi[] = {
  194. NULL,
  195. };
  196. static const char *pinname_whitelist_spi0_cs[] = {
  197. NULL,
  198. };
  199. #endif
  200. #ifdef BSP_USING_SPI1
  201. static const char *pinname_whitelist_spi1_sck[] = {
  202. NULL,
  203. };
  204. static const char *pinname_whitelist_spi1_sdo[] = {
  205. NULL,
  206. };
  207. static const char *pinname_whitelist_spi1_sdi[] = {
  208. NULL,
  209. };
  210. static const char *pinname_whitelist_spi1_cs[] = {
  211. NULL,
  212. };
  213. #endif
  214. #ifdef BSP_USING_SPI2
  215. static const char *pinname_whitelist_spi2_sck[] = {
  216. "SD1_CLK",
  217. NULL,
  218. };
  219. static const char *pinname_whitelist_spi2_sdo[] = {
  220. "SD1_CMD",
  221. NULL,
  222. };
  223. static const char *pinname_whitelist_spi2_sdi[] = {
  224. "SD1_D0",
  225. NULL,
  226. };
  227. static const char *pinname_whitelist_spi2_cs[] = {
  228. "SD1_D3",
  229. NULL,
  230. };
  231. #endif
  232. #ifdef BSP_USING_SPI3
  233. static const char *pinname_whitelist_spi3_sck[] = {
  234. NULL,
  235. };
  236. static const char *pinname_whitelist_spi3_sdo[] = {
  237. NULL,
  238. };
  239. static const char *pinname_whitelist_spi3_sdi[] = {
  240. NULL,
  241. };
  242. static const char *pinname_whitelist_spi3_cs[] = {
  243. NULL,
  244. };
  245. #endif
  246. #else
  247. #error "Unsupported board type!"
  248. #endif
  249. static void rt_hw_spi_pinmux_config()
  250. {
  251. #ifdef BSP_USING_SPI0
  252. pinmux_config(BSP_SPI0_SCK_PINNAME, SPI0_SCK, pinname_whitelist_spi0_sck);
  253. pinmux_config(BSP_SPI0_SDO_PINNAME, SPI0_SDO, pinname_whitelist_spi0_sdo);
  254. pinmux_config(BSP_SPI0_SDI_PINNAME, SPI0_SDI, pinname_whitelist_spi0_sdi);
  255. pinmux_config(BSP_SPI0_CS_PINNAME, SPI0_CS_X, pinname_whitelist_spi0_cs);
  256. #endif /* BSP_USING_SPI0 */
  257. #ifdef BSP_USING_SPI1
  258. pinmux_config(BSP_SPI1_SCK_PINNAME, SPI1_SCK, pinname_whitelist_spi1_sck);
  259. pinmux_config(BSP_SPI1_SDO_PINNAME, SPI1_SDO, pinname_whitelist_spi1_sdo);
  260. pinmux_config(BSP_SPI1_SDI_PINNAME, SPI1_SDI, pinname_whitelist_spi1_sdi);
  261. pinmux_config(BSP_SPI1_CS_PINNAME, SPI1_CS_X, pinname_whitelist_spi1_cs);
  262. #endif /* BSP_USING_SPI1 */
  263. #ifdef BSP_USING_SPI2
  264. pinmux_config(BSP_SPI2_SCK_PINNAME, SPI2_SCK, pinname_whitelist_spi2_sck);
  265. pinmux_config(BSP_SPI2_SDO_PINNAME, SPI2_SDO, pinname_whitelist_spi2_sdo);
  266. pinmux_config(BSP_SPI2_SDI_PINNAME, SPI2_SDI, pinname_whitelist_spi2_sdi);
  267. pinmux_config(BSP_SPI2_CS_PINNAME, SPI2_CS_X, pinname_whitelist_spi2_cs);
  268. #endif /* BSP_USING_SPI2 */
  269. #ifdef BSP_USING_SPI3
  270. pinmux_config(BSP_SPI3_SCK_PINNAME, SPI3_SCK, pinname_whitelist_spi3_sck);
  271. pinmux_config(BSP_SPI3_SDO_PINNAME, SPI3_SDO, pinname_whitelist_spi3_sdo);
  272. pinmux_config(BSP_SPI3_SDI_PINNAME, SPI3_SDI, pinname_whitelist_spi3_sdi);
  273. pinmux_config(BSP_SPI3_CS_PINNAME, SPI3_CS_X, pinname_whitelist_spi3_cs);
  274. #endif /* BSP_USING_SPI3 */
  275. }
  276. int rt_hw_spi_init(void)
  277. {
  278. rt_err_t ret = RT_EOK;
  279. rt_hw_spi_pinmux_config();
  280. for (rt_size_t i = 0; i < sizeof(_spi_obj) / sizeof(struct _device_spi); i++)
  281. {
  282. _spi_obj[i].dws.regs = (void *)DRV_IOREMAP((void *)_spi_obj[i].dws.regs, 0x1000);
  283. _spi_obj[i].spi_bus.parent.user_data = (void *)&_spi_obj[i];
  284. ret = rt_spi_bus_register(&_spi_obj[i].spi_bus, _spi_obj[i].device_name, &_spi_ops);
  285. }
  286. RT_ASSERT(ret == RT_EOK);
  287. return ret;
  288. }
  289. INIT_DEVICE_EXPORT(rt_hw_spi_init);