drv_spi.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. * 2019-07-15 yandld The first version for MCXN
  9. */
  10. #include "rtdevice.h"
  11. #include "fsl_common.h"
  12. #include "fsl_lpspi.h"
  13. #include "fsl_lpspi_edma.h"
  14. #define DMA_MAX_TRANSFER_COUNT (32767)
  15. enum
  16. {
  17. #ifdef BSP_USING_SPI1
  18. SPI1_INDEX,
  19. #endif
  20. #ifdef BSP_USING_SPI3
  21. SPI3_INDEX,
  22. #endif
  23. #ifdef BSP_USING_SPI6
  24. SPI6_INDEX,
  25. #endif
  26. #ifdef BSP_USING_SPI7
  27. SPI7_INDEX,
  28. #endif
  29. };
  30. struct lpc_spi
  31. {
  32. struct rt_spi_bus parent;
  33. LPSPI_Type *LPSPIx;
  34. clock_attach_id_t clock_attach_id;
  35. clock_div_name_t clock_div_name;
  36. clock_name_t clock_name;
  37. DMA_Type *DMAx;
  38. uint8_t tx_dma_chl;
  39. uint8_t rx_dma_chl;
  40. edma_handle_t dma_tx_handle;
  41. edma_handle_t dma_rx_handle;
  42. dma_request_source_t tx_dma_request;
  43. dma_request_source_t rx_dma_request;
  44. lpspi_master_edma_handle_t spi_dma_handle;
  45. rt_sem_t sem;
  46. char *name;
  47. };
  48. static struct lpc_spi lpc_obj[] =
  49. {
  50. #ifdef BSP_USING_SPI1
  51. {
  52. .LPSPIx = LPSPI1,
  53. .clock_attach_id = kFRO_HF_DIV_to_FLEXCOMM1,
  54. .clock_div_name = kCLOCK_DivFlexcom1Clk,
  55. .clock_name = kCLOCK_FroHf,
  56. .tx_dma_request = kDma0RequestMuxLpFlexcomm1Tx,
  57. .rx_dma_request = kDma0RequestMuxLpFlexcomm1Rx,
  58. .DMAx = DMA0,
  59. .tx_dma_chl = 0,
  60. .rx_dma_chl = 1,
  61. .name = "spi1",
  62. },
  63. #endif
  64. #ifdef BSP_USING_SPI3
  65. {
  66. .LPSPIx = LPSPI3,
  67. .clock_attach_id = kFRO_HF_DIV_to_FLEXCOMM3,
  68. .clock_div_name = kCLOCK_DivFlexcom3Clk,
  69. .clock_name = kCLOCK_FroHf,
  70. .tx_dma_request = kDma0RequestMuxLpFlexcomm3Tx,
  71. .rx_dma_request = kDma0RequestMuxLpFlexcomm3Rx,
  72. .DMAx = DMA0,
  73. .tx_dma_chl = 2,
  74. .rx_dma_chl = 3,
  75. .name = "spi3",
  76. },
  77. #endif /* BSP_USING_SPI3 */
  78. #ifdef BSP_USING_SPI6
  79. {
  80. .LPSPIx = LPSPI6,
  81. .clock_attach_id = kFRO_HF_DIV_to_FLEXCOMM6,
  82. .clock_div_name = kCLOCK_DivFlexcom6Clk,
  83. .clock_name = kCLOCK_FroHf,
  84. .tx_dma_request = kDma0RequestMuxLpFlexcomm6Tx,
  85. .rx_dma_request = kDma0RequestMuxLpFlexcomm6Rx,
  86. .DMAx = DMA0,
  87. .tx_dma_chl = 4,
  88. .rx_dma_chl = 5,
  89. .name = "spi6",
  90. },
  91. #endif /* BSP_USING_SPI6 */
  92. #ifdef BSP_USING_SPI7
  93. {
  94. .LPSPIx = LPSPI7,
  95. .clock_attach_id = kFRO_HF_DIV_to_FLEXCOMM7,
  96. .clock_div_name = kCLOCK_DivFlexcom7Clk,
  97. .clock_name = kCLOCK_FroHf,
  98. .tx_dma_request = kDma0RequestMuxLpFlexcomm7Tx,
  99. .rx_dma_request = kDma0RequestMuxLpFlexcomm7Rx,
  100. .DMAx = DMA0,
  101. .tx_dma_chl = 2,
  102. .rx_dma_chl = 3,
  103. .name = "spi7",
  104. },
  105. #endif /* BSP_USING_SPI7 */
  106. };
  107. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t pin)
  108. {
  109. struct rt_spi_device *spi_device = rt_malloc(sizeof(struct rt_spi_device));
  110. if (!spi_device)
  111. {
  112. return -RT_ENOMEM;
  113. }
  114. return rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, pin, RT_NULL);
  115. }
  116. static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
  117. {
  118. rt_err_t ret = RT_EOK;
  119. // struct lpc_spi *spi = RT_NULL;
  120. // spi = (struct lpc_spi *)(device->bus->parent.user_data);
  121. // ret = lpc_spi_init(spi->SPIx, cfg);
  122. return ret;
  123. }
  124. static void LPSPI_MasterUserCallback(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, status_t status, void *userData)
  125. {
  126. struct lpc_spi *spi = (struct lpc_spi *)userData;
  127. rt_sem_release(spi->sem);
  128. }
  129. static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  130. {
  131. int i;
  132. lpspi_transfer_t transfer = {0};
  133. RT_ASSERT(device != RT_NULL);
  134. RT_ASSERT(device->bus != RT_NULL);
  135. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  136. struct lpc_spi *spi = device->bus->parent.user_data;
  137. if (message->cs_take)
  138. {
  139. rt_pin_write(device->cs_pin, PIN_LOW);
  140. }
  141. transfer.dataSize = message->length;
  142. transfer.rxData = (uint8_t *)(message->recv_buf);
  143. transfer.txData = (uint8_t *)(message->send_buf);
  144. // if(message->length < MAX_DMA_TRANSFER_SIZE)
  145. if (0)
  146. {
  147. LPSPI_MasterTransferBlocking(spi->LPSPIx, &transfer);
  148. }
  149. else
  150. {
  151. uint32_t block, remain;
  152. block = message->length / DMA_MAX_TRANSFER_COUNT;
  153. remain = message->length % DMA_MAX_TRANSFER_COUNT;
  154. for (i = 0; i < block; i++)
  155. {
  156. transfer.dataSize = DMA_MAX_TRANSFER_COUNT;
  157. if (message->recv_buf) transfer.rxData = (uint8_t *)(message->recv_buf + i *DMA_MAX_TRANSFER_COUNT);
  158. if (message->send_buf) transfer.txData = (uint8_t *)(message->send_buf + i *DMA_MAX_TRANSFER_COUNT);
  159. LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer);
  160. rt_sem_take(spi->sem, RT_WAITING_FOREVER);
  161. }
  162. if (remain)
  163. {
  164. transfer.dataSize = remain;
  165. if (message->recv_buf) transfer.rxData = (uint8_t *)(message->recv_buf + i *DMA_MAX_TRANSFER_COUNT);
  166. if (message->send_buf) transfer.txData = (uint8_t *)(message->send_buf + i *DMA_MAX_TRANSFER_COUNT);
  167. LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer);
  168. rt_sem_take(spi->sem, RT_WAITING_FOREVER);
  169. }
  170. }
  171. if (message->cs_release)
  172. {
  173. rt_pin_write(device->cs_pin, PIN_HIGH);
  174. }
  175. return message->length;
  176. }
  177. static struct rt_spi_ops lpc_spi_ops =
  178. {
  179. .configure = spi_configure,
  180. .xfer = spixfer
  181. };
  182. int rt_hw_spi_init(void)
  183. {
  184. int i;
  185. for (i = 0; i < ARRAY_SIZE(lpc_obj); i++)
  186. {
  187. CLOCK_SetClkDiv(lpc_obj[i].clock_div_name, 1u);
  188. CLOCK_AttachClk(lpc_obj[i].clock_attach_id);
  189. lpc_obj[i].parent.parent.user_data = &lpc_obj[i];
  190. lpc_obj[i].sem = rt_sem_create("sem_spi", 0, RT_IPC_FLAG_FIFO);
  191. lpspi_master_config_t masterConfig;
  192. LPSPI_MasterGetDefaultConfig(&masterConfig);
  193. masterConfig.baudRate = 24 * 1000 * 1000;
  194. masterConfig.pcsToSckDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
  195. masterConfig.lastSckToPcsDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
  196. masterConfig.betweenTransferDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
  197. LPSPI_MasterInit(lpc_obj[i].LPSPIx, &masterConfig, CLOCK_GetFreq(lpc_obj[i].clock_name));
  198. EDMA_CreateHandle(&lpc_obj[i].dma_tx_handle, lpc_obj[i].DMAx, lpc_obj[i].tx_dma_chl);
  199. EDMA_CreateHandle(&lpc_obj[i].dma_rx_handle, lpc_obj[i].DMAx, lpc_obj[i].rx_dma_chl);
  200. EDMA_SetChannelMux(lpc_obj[i].DMAx, lpc_obj[i].tx_dma_chl, lpc_obj[i].tx_dma_request);
  201. EDMA_SetChannelMux(lpc_obj[i].DMAx, lpc_obj[i].rx_dma_chl, lpc_obj[i].rx_dma_request);
  202. LPSPI_MasterTransferCreateHandleEDMA(lpc_obj[i].LPSPIx, &lpc_obj[i].spi_dma_handle, LPSPI_MasterUserCallback, &lpc_obj[i], &lpc_obj[i].dma_rx_handle, &lpc_obj[i].dma_tx_handle);
  203. rt_spi_bus_register(&lpc_obj[i].parent, lpc_obj[i].name, &lpc_spi_ops);
  204. }
  205. return RT_EOK;
  206. }
  207. INIT_DEVICE_EXPORT(rt_hw_spi_init);