drv_spi.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-03-27 Liuguang the first version.
  9. */
  10. #include <rtthread.h>
  11. #ifdef BSP_USING_SPI
  12. #include "drv_spi.h"
  13. #include "fsl_common.h"
  14. #include "fsl_iomuxc.h"
  15. #include "fsl_lpspi.h"
  16. #include "fsl_lpspi_edma.h"
  17. #include "fsl_dmamux.h"
  18. #define LOG_TAG "drv.spi"
  19. #include <drv_log.h>
  20. #if defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL
  21. #error "Please don't define 'FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL'!"
  22. #endif
  23. enum
  24. {
  25. #ifdef BSP_USING_SPI1
  26. SPI1_INDEX,
  27. #endif
  28. #ifdef BSP_USING_SPI2
  29. SPI2_INDEX,
  30. #endif
  31. #ifdef BSP_USING_SPI3
  32. SPI3_INDEX,
  33. #endif
  34. #ifdef BSP_USING_SPI4
  35. SPI4_INDEX,
  36. #endif
  37. };
  38. struct imxrt_sw_spi_cs
  39. {
  40. rt_uint32_t pin;
  41. };
  42. struct dma_config
  43. {
  44. lpspi_master_edma_handle_t spi_edma;
  45. edma_handle_t rx_edma;
  46. dma_request_source_t rx_request;
  47. rt_uint8_t rx_channel;
  48. edma_handle_t tx_edma;
  49. dma_request_source_t tx_request;
  50. rt_uint8_t tx_channel;
  51. };
  52. struct imxrt_spi
  53. {
  54. char *bus_name;
  55. LPSPI_Type *base;
  56. struct rt_spi_bus spi_bus;
  57. struct dma_config *dma;
  58. rt_uint8_t dma_flag;
  59. };
  60. static struct imxrt_spi lpspis[] =
  61. {
  62. #ifdef BSP_USING_SPI1
  63. {
  64. .bus_name = "spi1",
  65. .base = LPSPI1,
  66. .dma = RT_NULL,
  67. .dma_flag = RT_FALSE,
  68. },
  69. #endif
  70. #ifdef BSP_USING_SPI2
  71. {
  72. .bus_name = "spi2",
  73. .base = LPSPI2,
  74. .dma = RT_NULL,
  75. .dma_flag = RT_FALSE,
  76. },
  77. #endif
  78. #ifdef BSP_USING_SPI3
  79. {
  80. .bus_name = "spi3",
  81. .base = LPSPI3,
  82. .dma = RT_NULL,
  83. .dma_flag = RT_FALSE,
  84. },
  85. #endif
  86. #ifdef BSP_USING_SPI4
  87. {
  88. .bus_name = "spi4",
  89. .base = LPSPI4,
  90. .dma = RT_NULL,
  91. .dma_flag = RT_FALSE,
  92. },
  93. #endif
  94. };
  95. static void spi_get_dma_config(void)
  96. {
  97. #ifdef BSP_SPI1_USING_DMA
  98. static struct dma_config spi1_dma =
  99. {
  100. .rx_request = kDmaRequestMuxLPSPI1Rx,
  101. .rx_channel = BSP_SPI1_RX_DMA_CHANNEL,
  102. .tx_request = kDmaRequestMuxLPSPI1Tx,
  103. .tx_channel = BSP_SPI1_TX_DMA_CHANNEL,
  104. };
  105. lpspis[SPI1_INDEX].dma = &spi1_dma;
  106. lpspis[SPI1_INDEX].dma_flag = RT_TRUE;
  107. #endif
  108. #ifdef BSP_SPI2_USING_DMA
  109. static struct dma_config spi2_dma =
  110. {
  111. .rx_request = kDmaRequestMuxLPSPI2Rx,
  112. .rx_channel = BSP_SPI2_RX_DMA_CHANNEL,
  113. .tx_request = kDmaRequestMuxLPSPI2Tx,
  114. .tx_channel = BSP_SPI2_TX_DMA_CHANNEL,
  115. };
  116. lpspis[SPI2_INDEX].dma = &spi2_dma;
  117. lpspis[SPI2_INDEX].dma_flag = RT_TRUE;
  118. #endif
  119. #ifdef BSP_SPI3_USING_DMA
  120. static struct dma_config spi3_dma =
  121. {
  122. .rx_request = kDmaRequestMuxLPSPI3Rx,
  123. .rx_channel = BSP_SPI3_RX_DMA_CHANNEL,
  124. .tx_request = kDmaRequestMuxLPSPI3Tx,
  125. .tx_channel = BSP_SPI3_TX_DMA_CHANNEL,
  126. };
  127. lpspis[SPI3_INDEX].dma = &spi3_dma;
  128. lpspis[SPI3_INDEX].dma_flag = RT_TRUE;
  129. #endif
  130. #ifdef BSP_SPI4_USING_DMA
  131. static struct dma_config spi4_dma =
  132. {
  133. .rx_request = kDmaRequestMuxLPSPI4Rx,
  134. .rx_channel = BSP_SPI4_RX_DMA_CHANNEL,
  135. .tx_request = kDmaRequestMuxLPSPI4Tx,
  136. .tx_channel = BSP_SPI4_TX_DMA_CHANNEL,
  137. };
  138. lpspis[SPI4_INDEX].dma = &spi4_dma;
  139. lpspis[SPI4_INDEX].dma_flag = RT_TRUE;
  140. #endif
  141. }
  142. void edma_xfer_callback(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, status_t status, void *userData)
  143. {
  144. /* xfer complete callback */
  145. }
  146. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t pin)
  147. {
  148. rt_err_t ret = RT_EOK;
  149. struct rt_spi_device *spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  150. RT_ASSERT(spi_device != RT_NULL);
  151. struct imxrt_sw_spi_cs *cs_pin = (struct imxrt_sw_spi_cs *)rt_malloc(sizeof(struct imxrt_sw_spi_cs));
  152. RT_ASSERT(cs_pin != RT_NULL);
  153. cs_pin->pin = pin;
  154. rt_pin_mode(pin, PIN_MODE_OUTPUT);
  155. rt_pin_write(pin, PIN_HIGH);
  156. ret = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  157. return ret;
  158. }
  159. static uint32_t imxrt_get_lpspi_freq(void)
  160. {
  161. uint32_t freq = 0;
  162. /* CLOCK_GetMux(kCLOCK_LpspiMux):
  163. 00b: derive clock from PLL3 PFD1 720M
  164. 01b: derive clock from PLL3 PFD0 720M
  165. 10b: derive clock from PLL2 528M
  166. 11b: derive clock from PLL2 PFD2 396M
  167. */
  168. switch(CLOCK_GetMux(kCLOCK_LpspiMux))
  169. {
  170. case 0:
  171. freq = CLOCK_GetFreq(kCLOCK_Usb1PllPfd1Clk);
  172. break;
  173. case 1:
  174. freq = CLOCK_GetFreq(kCLOCK_Usb1PllPfd0Clk);
  175. break;
  176. case 2:
  177. freq = CLOCK_GetFreq(kCLOCK_SysPllClk);
  178. break;
  179. case 3:
  180. freq = CLOCK_GetFreq(kCLOCK_SysPllPfd2Clk);
  181. break;
  182. }
  183. freq /= (CLOCK_GetDiv(kCLOCK_LpspiDiv) + 1U);
  184. return freq;
  185. }
  186. static void lpspi_dma_config(struct imxrt_spi *spi)
  187. {
  188. RT_ASSERT(spi != RT_NULL);
  189. DMAMUX_SetSource(DMAMUX, spi->dma->rx_channel, spi->dma->rx_request);
  190. DMAMUX_EnableChannel(DMAMUX, spi->dma->rx_channel);
  191. EDMA_CreateHandle(&spi->dma->rx_edma, DMA0, spi->dma->rx_channel);
  192. DMAMUX_SetSource(DMAMUX, spi->dma->tx_channel, spi->dma->tx_request);
  193. DMAMUX_EnableChannel(DMAMUX, spi->dma->tx_channel);
  194. EDMA_CreateHandle(&spi->dma->tx_edma, DMA0, spi->dma->tx_channel);
  195. LPSPI_MasterTransferCreateHandleEDMA(spi->base,
  196. &spi->dma->spi_edma,
  197. edma_xfer_callback,
  198. spi,
  199. &spi->dma->rx_edma,
  200. &spi->dma->tx_edma);
  201. LOG_D("%s dma config done\n", spi->bus_name);
  202. }
  203. static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
  204. {
  205. lpspi_master_config_t masterConfig;
  206. struct imxrt_spi *spi = RT_NULL;
  207. RT_ASSERT(cfg != RT_NULL);
  208. RT_ASSERT(device != RT_NULL);
  209. spi = (struct imxrt_spi *)(device->bus->parent.user_data);
  210. RT_ASSERT(spi != RT_NULL);
  211. if(cfg->data_width != 8 && cfg->data_width != 16 && cfg->data_width != 32)
  212. {
  213. return RT_EINVAL;
  214. }
  215. LPSPI_MasterGetDefaultConfig(&masterConfig);
  216. if(cfg->max_hz > 40*1000*1000)
  217. {
  218. cfg->max_hz = 40*1000*1000;
  219. }
  220. masterConfig.baudRate = cfg->max_hz;
  221. masterConfig.bitsPerFrame = cfg->data_width;
  222. if(cfg->mode & RT_SPI_MSB)
  223. {
  224. masterConfig.direction = kLPSPI_MsbFirst;
  225. }
  226. else
  227. {
  228. masterConfig.direction = kLPSPI_LsbFirst;
  229. }
  230. if(cfg->mode & RT_SPI_CPHA)
  231. {
  232. masterConfig.cpha = kLPSPI_ClockPhaseSecondEdge;
  233. }
  234. else
  235. {
  236. masterConfig.cpha = kLPSPI_ClockPhaseFirstEdge;
  237. }
  238. if(cfg->mode & RT_SPI_CPOL)
  239. {
  240. masterConfig.cpol = kLPSPI_ClockPolarityActiveLow;
  241. }
  242. else
  243. {
  244. masterConfig.cpol = kLPSPI_ClockPolarityActiveHigh;
  245. }
  246. masterConfig.pinCfg = kLPSPI_SdiInSdoOut;
  247. masterConfig.dataOutConfig = kLpspiDataOutTristate;
  248. masterConfig.pcsToSckDelayInNanoSec = 1000000000 / masterConfig.baudRate;
  249. masterConfig.lastSckToPcsDelayInNanoSec = 1000000000 / masterConfig.baudRate;
  250. masterConfig.betweenTransferDelayInNanoSec = 1000000000 / masterConfig.baudRate;
  251. LPSPI_MasterInit(spi->base, &masterConfig, imxrt_get_lpspi_freq());
  252. spi->base->CFGR1 |= LPSPI_CFGR1_PCSCFG_MASK;
  253. return RT_EOK;
  254. }
  255. static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  256. {
  257. lpspi_transfer_t transfer;
  258. status_t status;
  259. RT_ASSERT(device != RT_NULL);
  260. RT_ASSERT(device->bus != RT_NULL);
  261. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  262. struct imxrt_spi *spi = (struct imxrt_spi *)(device->bus->parent.user_data);
  263. struct imxrt_sw_spi_cs *cs = device->parent.user_data;
  264. if(message->cs_take)
  265. {
  266. rt_pin_write(cs->pin, PIN_LOW);
  267. }
  268. transfer.dataSize = message->length;
  269. transfer.rxData = (uint8_t *)(message->recv_buf);
  270. transfer.txData = (uint8_t *)(message->send_buf);
  271. if(RT_FALSE == spi->dma_flag)
  272. {
  273. status = LPSPI_MasterTransferBlocking(spi->base, &transfer);
  274. }
  275. else
  276. {
  277. status = LPSPI_MasterTransferEDMA(spi->base,&spi->dma->spi_edma,&transfer);
  278. }
  279. if(message->cs_release)
  280. {
  281. rt_pin_write(cs->pin, PIN_HIGH);
  282. }
  283. if (status != kStatus_Success)
  284. {
  285. LOG_E("%s transfer error : %d", spi->bus_name,status);
  286. message->length = 0;
  287. }
  288. return message->length;
  289. }
  290. static struct rt_spi_ops imxrt_spi_ops =
  291. {
  292. .configure = spi_configure,
  293. .xfer = spixfer
  294. };
  295. int rt_hw_spi_bus_init(void)
  296. {
  297. int i;
  298. rt_err_t ret = RT_EOK;
  299. spi_get_dma_config();
  300. for (i = 0; i < sizeof(lpspis) / sizeof(lpspis[0]); i++)
  301. {
  302. lpspis[i].spi_bus.parent.user_data = &lpspis[i];
  303. ret = rt_spi_bus_register(&lpspis[i].spi_bus, lpspis[i].bus_name, &imxrt_spi_ops);
  304. if(RT_TRUE == lpspis[i].dma_flag)
  305. {
  306. lpspi_dma_config(&lpspis[i]);
  307. }
  308. }
  309. return ret;
  310. }
  311. INIT_BOARD_EXPORT(rt_hw_spi_bus_init);
  312. #endif /* BSP_USING_SPI */