drv_spi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright (c) 2006-2022, 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. rt_sem_t xfer_sem;
  58. lpspi_master_handle_t spi_normal;
  59. struct dma_config *dma;
  60. rt_uint8_t dma_flag;
  61. rt_uint16_t masterclock;
  62. };
  63. static struct imxrt_spi lpspis[] =
  64. {
  65. #ifdef BSP_USING_SPI1
  66. {
  67. .bus_name = "spi1",
  68. .base = LPSPI1,
  69. .dma = RT_NULL,
  70. .dma_flag = RT_FALSE,
  71. .masterclock = 171,
  72. },
  73. #endif
  74. #ifdef BSP_USING_SPI2
  75. {
  76. .bus_name = "spi2",
  77. .base = LPSPI2,
  78. .dma = RT_NULL,
  79. .dma_flag = RT_FALSE,
  80. .masterclock = 172,
  81. },
  82. #endif
  83. #ifdef BSP_USING_SPI3
  84. {
  85. .bus_name = "spi3",
  86. .base = LPSPI3,
  87. .dma = RT_NULL,
  88. .dma_flag = RT_FALSE,
  89. .masterclock = 173,
  90. },
  91. #endif
  92. #ifdef BSP_USING_SPI4
  93. {
  94. .bus_name = "spi4",
  95. .base = LPSPI4,
  96. .dma = RT_NULL,
  97. .dma_flag = RT_FALSE,
  98. .masterclock = 174,
  99. },
  100. #endif
  101. };
  102. static void spi_get_dma_config(void)
  103. {
  104. #ifdef BSP_SPI1_USING_DMA
  105. static struct dma_config spi1_dma =
  106. {
  107. .rx_request = kDmaRequestMuxLPSPI1Rx,
  108. .rx_channel = BSP_SPI1_RX_DMA_CHANNEL,
  109. .tx_request = kDmaRequestMuxLPSPI1Tx,
  110. .tx_channel = BSP_SPI1_TX_DMA_CHANNEL,
  111. };
  112. lpspis[SPI1_INDEX].dma = &spi1_dma;
  113. lpspis[SPI1_INDEX].dma_flag = RT_TRUE;
  114. #endif
  115. #ifdef BSP_SPI2_USING_DMA
  116. static struct dma_config spi2_dma =
  117. {
  118. .rx_request = kDmaRequestMuxLPSPI2Rx,
  119. .rx_channel = BSP_SPI2_RX_DMA_CHANNEL,
  120. .tx_request = kDmaRequestMuxLPSPI2Tx,
  121. .tx_channel = BSP_SPI2_TX_DMA_CHANNEL,
  122. };
  123. lpspis[SPI2_INDEX].dma = &spi2_dma;
  124. lpspis[SPI2_INDEX].dma_flag = RT_TRUE;
  125. #endif
  126. #ifdef BSP_SPI3_USING_DMA
  127. static struct dma_config spi3_dma =
  128. {
  129. .rx_request = kDmaRequestMuxLPSPI3Rx,
  130. .rx_channel = BSP_SPI3_RX_DMA_CHANNEL,
  131. .tx_request = kDmaRequestMuxLPSPI3Tx,
  132. .tx_channel = BSP_SPI3_TX_DMA_CHANNEL,
  133. };
  134. lpspis[SPI3_INDEX].dma = &spi3_dma;
  135. lpspis[SPI3_INDEX].dma_flag = RT_TRUE;
  136. #endif
  137. #ifdef BSP_SPI4_USING_DMA
  138. static struct dma_config spi4_dma =
  139. {
  140. .rx_request = kDmaRequestMuxLPSPI4Rx,
  141. .rx_channel = BSP_SPI4_RX_DMA_CHANNEL,
  142. .tx_request = kDmaRequestMuxLPSPI4Tx,
  143. .tx_channel = BSP_SPI4_TX_DMA_CHANNEL,
  144. };
  145. lpspis[SPI4_INDEX].dma = &spi4_dma;
  146. lpspis[SPI4_INDEX].dma_flag = RT_TRUE;
  147. #endif
  148. }
  149. void normal_xfer_callback(LPSPI_Type *base, lpspi_master_handle_t *handle, status_t status, void *userData)
  150. {
  151. /* xfer complete callback */
  152. struct imxrt_spi *spi = (struct imxrt_spi *)userData;
  153. rt_sem_release(spi->xfer_sem);
  154. }
  155. void edma_xfer_callback(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, status_t status, void *userData)
  156. {
  157. /* xfer complete callback */
  158. struct imxrt_spi *spi = (struct imxrt_spi *)userData;
  159. rt_sem_release(spi->xfer_sem);
  160. }
  161. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t pin)
  162. {
  163. rt_err_t ret = RT_EOK;
  164. struct rt_spi_device *spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  165. RT_ASSERT(spi_device != RT_NULL);
  166. struct imxrt_sw_spi_cs *cs_pin = (struct imxrt_sw_spi_cs *)rt_malloc(sizeof(struct imxrt_sw_spi_cs));
  167. RT_ASSERT(cs_pin != RT_NULL);
  168. cs_pin->pin = pin;
  169. rt_pin_mode(pin, PIN_MODE_OUTPUT);
  170. rt_pin_write(pin, PIN_HIGH);
  171. ret = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  172. return ret;
  173. }
  174. static uint32_t imxrt_get_lpspi_freq(void)
  175. {
  176. uint32_t freq = 0;
  177. /* CLOCK_GetMux(kCLOCK_LpspiMux):
  178. 00b: derive clock from PLL3 PFD1 720M
  179. 01b: derive clock from PLL3 PFD0 720M
  180. 10b: derive clock from PLL2 528M
  181. 11b: derive clock from PLL2 PFD2 396M
  182. */
  183. switch(CLOCK_GetMux(kCLOCK_LpspiMux))
  184. {
  185. case 0:
  186. freq = CLOCK_GetFreq(kCLOCK_Usb1PllPfd1Clk);
  187. break;
  188. case 1:
  189. freq = CLOCK_GetFreq(kCLOCK_Usb1PllPfd0Clk);
  190. break;
  191. case 2:
  192. freq = CLOCK_GetFreq(kCLOCK_SysPllClk);
  193. break;
  194. case 3:
  195. freq = CLOCK_GetFreq(kCLOCK_SysPllPfd2Clk);
  196. break;
  197. }
  198. freq /= (CLOCK_GetDiv(kCLOCK_LpspiDiv) + 1U);
  199. return freq;
  200. }
  201. static void lpspi_normal_config(struct imxrt_spi *spi)
  202. {
  203. RT_ASSERT(spi != RT_NULL);
  204. LPSPI_MasterTransferCreateHandle(spi->base,
  205. &spi->spi_normal,
  206. normal_xfer_callback,
  207. spi);
  208. LOG_D(LOG_TAG" %s normal config done\n", spi->bus_name);
  209. }
  210. static void lpspi_dma_config(struct imxrt_spi *spi)
  211. {
  212. RT_ASSERT(spi != RT_NULL);
  213. DMAMUX_SetSource(DMAMUX, spi->dma->rx_channel, spi->dma->rx_request);
  214. DMAMUX_EnableChannel(DMAMUX, spi->dma->rx_channel);
  215. EDMA_CreateHandle(&spi->dma->rx_edma, DMA0, spi->dma->rx_channel);
  216. DMAMUX_SetSource(DMAMUX, spi->dma->tx_channel, spi->dma->tx_request);
  217. DMAMUX_EnableChannel(DMAMUX, spi->dma->tx_channel);
  218. EDMA_CreateHandle(&spi->dma->tx_edma, DMA0, spi->dma->tx_channel);
  219. LPSPI_MasterTransferCreateHandleEDMA(spi->base,
  220. &spi->dma->spi_edma,
  221. edma_xfer_callback,
  222. spi,
  223. &spi->dma->rx_edma,
  224. &spi->dma->tx_edma);
  225. LOG_D("%s dma config done\n", spi->bus_name);
  226. }
  227. static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
  228. {
  229. lpspi_master_config_t masterConfig;
  230. struct imxrt_spi *spi = RT_NULL;
  231. RT_ASSERT(cfg != RT_NULL);
  232. RT_ASSERT(device != RT_NULL);
  233. spi = (struct imxrt_spi *)(device->bus->parent.user_data);
  234. RT_ASSERT(spi != RT_NULL);
  235. if(cfg->data_width != 8 && cfg->data_width != 16 && cfg->data_width != 32)
  236. {
  237. return RT_EINVAL;
  238. }
  239. LPSPI_MasterGetDefaultConfig(&masterConfig);
  240. if(cfg->max_hz > 40*1000*1000)
  241. {
  242. cfg->max_hz = 40*1000*1000;
  243. }
  244. masterConfig.baudRate = cfg->max_hz;
  245. masterConfig.bitsPerFrame = cfg->data_width;
  246. if(cfg->mode & RT_SPI_MSB)
  247. {
  248. masterConfig.direction = kLPSPI_MsbFirst;
  249. }
  250. else
  251. {
  252. masterConfig.direction = kLPSPI_LsbFirst;
  253. }
  254. if(cfg->mode & RT_SPI_CPHA)
  255. {
  256. masterConfig.cpha = kLPSPI_ClockPhaseSecondEdge;
  257. }
  258. else
  259. {
  260. masterConfig.cpha = kLPSPI_ClockPhaseFirstEdge;
  261. }
  262. if(cfg->mode & RT_SPI_CPOL)
  263. {
  264. masterConfig.cpol = kLPSPI_ClockPolarityActiveLow;
  265. }
  266. else
  267. {
  268. masterConfig.cpol = kLPSPI_ClockPolarityActiveHigh;
  269. }
  270. masterConfig.whichPcs = kLPSPI_Pcs0;
  271. #if defined(SOC_IMXRT1170_SERIES)
  272. freq = CLOCK_GetFreqFromObs(spi->masterclock, 2);
  273. LPSPI_MasterInit(spi->base, &masterConfig, freq);
  274. #else
  275. masterConfig.pinCfg = kLPSPI_SdiInSdoOut;
  276. masterConfig.pcsToSckDelayInNanoSec = 1000000000 / masterConfig.baudRate;
  277. masterConfig.lastSckToPcsDelayInNanoSec = 1000000000 / masterConfig.baudRate;
  278. masterConfig.betweenTransferDelayInNanoSec = 1000000000 / masterConfig.baudRate;
  279. LPSPI_MasterInit(spi->base, &masterConfig, imxrt_get_lpspi_freq());
  280. spi->base->CFGR1 |= LPSPI_CFGR1_PCSCFG_MASK;
  281. #endif
  282. return RT_EOK;
  283. }
  284. static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  285. {
  286. lpspi_transfer_t transfer;
  287. status_t status;
  288. RT_ASSERT(device != RT_NULL);
  289. RT_ASSERT(device->bus != RT_NULL);
  290. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  291. struct imxrt_spi *spi = (struct imxrt_spi *)(device->bus->parent.user_data);
  292. struct imxrt_sw_spi_cs *cs = device->parent.user_data;
  293. if(message->cs_take)
  294. {
  295. rt_pin_write(cs->pin, PIN_LOW);
  296. }
  297. transfer.dataSize = message->length;
  298. transfer.rxData = (uint8_t *)(message->recv_buf);
  299. transfer.txData = (uint8_t *)(message->send_buf);
  300. transfer.configFlags =
  301. kLPSPI_MasterPcs0 | kLPSPI_MasterByteSwap | kLPSPI_MasterPcsContinuous;
  302. if(RT_FALSE == spi->dma_flag)
  303. {
  304. #ifdef BSP_USING_BLOCKING_SPI
  305. status = LPSPI_MasterTransferBlocking(spi->base, &transfer);
  306. #else
  307. status = LPSPI_MasterTransferNonBlocking(spi->base, &spi->spi_normal, &transfer);
  308. #endif
  309. }
  310. else
  311. {
  312. status = LPSPI_MasterTransferEDMA(spi->base,&spi->dma->spi_edma,&transfer);
  313. }
  314. rt_sem_take(spi->xfer_sem, RT_WAITING_FOREVER);
  315. if(message->cs_release)
  316. {
  317. rt_pin_write(cs->pin, PIN_HIGH);
  318. }
  319. if (status != kStatus_Success)
  320. {
  321. LOG_E("%s transfer error : %d", spi->bus_name,status);
  322. message->length = 0;
  323. }
  324. return message->length;
  325. }
  326. static struct rt_spi_ops imxrt_spi_ops =
  327. {
  328. .configure = spi_configure,
  329. .xfer = spixfer
  330. };
  331. int rt_hw_spi_bus_init(void)
  332. {
  333. int i;
  334. rt_err_t ret = RT_EOK;
  335. spi_get_dma_config();
  336. for (i = 0; i < sizeof(lpspis) / sizeof(lpspis[0]); i++)
  337. {
  338. lpspis[i].spi_bus.parent.user_data = &lpspis[i];
  339. ret = rt_spi_bus_register(&lpspis[i].spi_bus, lpspis[i].bus_name, &imxrt_spi_ops);
  340. if(RT_TRUE == lpspis[i].dma_flag)
  341. {
  342. lpspi_dma_config(&lpspis[i]);
  343. }
  344. else
  345. {
  346. lpspi_normal_config(&lpspis[i]);
  347. }
  348. char sem_name[RT_NAME_MAX];
  349. rt_sprintf(sem_name, "%s_s", lpspis[i].bus_name);
  350. lpspis[i].xfer_sem = rt_sem_create(sem_name, 0, RT_IPC_FLAG_PRIO);
  351. }
  352. return ret;
  353. }
  354. INIT_BOARD_EXPORT(rt_hw_spi_bus_init);
  355. #endif /* BSP_USING_SPI */