drv_spi.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 = kDmaRequestMuxLpFlexcomm1Tx,
  57. .rx_dma_request = kDmaRequestMuxLpFlexcomm1Rx,
  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 = kDmaRequestMuxLpFlexcomm3Tx,
  71. .rx_dma_request = kDmaRequestMuxLpFlexcomm3Rx,
  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 = kDmaRequestMuxLpFlexcomm6Tx,
  85. .rx_dma_request = kDmaRequestMuxLpFlexcomm6Rx,
  86. .DMAx = DMA0,
  87. .tx_dma_chl = 4,
  88. .rx_dma_chl = 5,
  89. .name = "spi6",
  90. #endif /* BSP_USING_SPI6 */
  91. #ifdef BSP_USING_SPI7
  92. {
  93. .LPSPIx = LPSPI7,
  94. .clock_attach_id = kFRO_HF_DIV_to_FLEXCOMM7,
  95. .clock_div_name = kCLOCK_DivFlexcom7Clk,
  96. .clock_name = kCLOCK_FroHf,
  97. .tx_dma_request = kDmaRequestMuxLpFlexcomm7Tx,
  98. .rx_dma_request = kDmaRequestMuxLpFlexcomm7Rx,
  99. .DMAx = DMA0,
  100. .tx_dma_chl = 2,
  101. .rx_dma_chl = 3,
  102. .name = "spi7",
  103. },
  104. #endif /* BSP_USING_SPI7 */
  105. };
  106. struct lpc_sw_spi_cs
  107. {
  108. rt_uint32_t pin;
  109. };
  110. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t pin)
  111. {
  112. rt_err_t ret = RT_EOK;
  113. struct rt_spi_device *spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  114. struct lpc_sw_spi_cs *cs_pin = (struct lpc_sw_spi_cs *)rt_malloc(sizeof(struct lpc_sw_spi_cs));
  115. cs_pin->pin = pin;
  116. rt_pin_mode(pin, PIN_MODE_OUTPUT);
  117. rt_pin_write(pin, PIN_HIGH);
  118. ret = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  119. return ret;
  120. }
  121. static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
  122. {
  123. rt_err_t ret = RT_EOK;
  124. // struct lpc_spi *spi = RT_NULL;
  125. // spi = (struct lpc_spi *)(device->bus->parent.user_data);
  126. // ret = lpc_spi_init(spi->SPIx, cfg);
  127. return ret;
  128. }
  129. static void LPSPI_MasterUserCallback(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, status_t status, void *userData)
  130. {
  131. struct lpc_spi *spi = (struct lpc_spi*)userData;
  132. rt_sem_release(spi->sem);
  133. }
  134. static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  135. {
  136. int i;
  137. lpspi_transfer_t transfer = {0};
  138. RT_ASSERT(device != RT_NULL);
  139. RT_ASSERT(device->bus != RT_NULL);
  140. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  141. struct lpc_spi *spi = (struct lpc_spi *)(device->bus->parent.user_data);
  142. // struct lpc_sw_spi_cs *cs = device->parent.user_data;
  143. if(message->cs_take)
  144. {
  145. rt_pin_write(device->cs_pin, PIN_LOW);
  146. }
  147. transfer.dataSize = message->length;
  148. transfer.rxData = (uint8_t *)(message->recv_buf);
  149. transfer.txData = (uint8_t *)(message->send_buf);
  150. // if(message->length < MAX_DMA_TRANSFER_SIZE)
  151. if(0)
  152. {
  153. LPSPI_MasterTransferBlocking(spi->LPSPIx, &transfer);
  154. }
  155. else
  156. {
  157. uint32_t block, remain;
  158. block = message->length / DMA_MAX_TRANSFER_COUNT;
  159. remain = message->length % DMA_MAX_TRANSFER_COUNT;
  160. for(i=0; i<block; i++)
  161. {
  162. transfer.dataSize = DMA_MAX_TRANSFER_COUNT;
  163. if(message->recv_buf) transfer.rxData = (uint8_t *)(message->recv_buf + i*DMA_MAX_TRANSFER_COUNT);
  164. if(message->send_buf) transfer.txData = (uint8_t *)(message->send_buf + i*DMA_MAX_TRANSFER_COUNT);
  165. LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer);
  166. rt_sem_take(spi->sem, RT_WAITING_FOREVER);
  167. }
  168. if(remain)
  169. {
  170. transfer.dataSize = remain;
  171. if(message->recv_buf) transfer.rxData = (uint8_t *)(message->recv_buf + i*DMA_MAX_TRANSFER_COUNT);
  172. if(message->send_buf) transfer.txData = (uint8_t *)(message->send_buf + i*DMA_MAX_TRANSFER_COUNT);
  173. LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer);
  174. rt_sem_take(spi->sem, RT_WAITING_FOREVER);
  175. }
  176. }
  177. if(message->cs_release)
  178. {
  179. rt_pin_write(device->cs_pin, PIN_HIGH);
  180. }
  181. return message->length;
  182. }
  183. static struct rt_spi_ops lpc_spi_ops =
  184. {
  185. .configure = spi_configure,
  186. .xfer = spixfer
  187. };
  188. int rt_hw_spi_init(void)
  189. {
  190. int i;
  191. for(i=0; i<ARRAY_SIZE(lpc_obj); i++)
  192. {
  193. CLOCK_SetClkDiv(lpc_obj[i].clock_div_name, 1u);
  194. CLOCK_AttachClk(lpc_obj[i].clock_attach_id);
  195. lpc_obj[i].parent.parent.user_data = &lpc_obj[i];
  196. lpc_obj[i].sem = rt_sem_create("sem_spi", 0, RT_IPC_FLAG_FIFO);
  197. lpspi_master_config_t masterConfig;
  198. LPSPI_MasterGetDefaultConfig(&masterConfig);
  199. masterConfig.baudRate = 24*1000*1000;
  200. masterConfig.pcsToSckDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
  201. masterConfig.lastSckToPcsDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
  202. masterConfig.betweenTransferDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
  203. LPSPI_MasterInit(lpc_obj[i].LPSPIx, &masterConfig, CLOCK_GetFreq(lpc_obj[i].clock_name));
  204. EDMA_CreateHandle(&lpc_obj[i].dma_tx_handle, lpc_obj[i].DMAx, lpc_obj[i].tx_dma_chl);
  205. EDMA_CreateHandle(&lpc_obj[i].dma_rx_handle, lpc_obj[i].DMAx, lpc_obj[i].rx_dma_chl);
  206. EDMA_SetChannelMux(lpc_obj[i].DMAx, lpc_obj[i].tx_dma_chl, lpc_obj[i].tx_dma_request);
  207. EDMA_SetChannelMux(lpc_obj[i].DMAx, lpc_obj[i].rx_dma_chl, lpc_obj[i].rx_dma_request);
  208. 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);
  209. rt_spi_bus_register(&lpc_obj[i].parent, lpc_obj[i].name, &lpc_spi_ops);
  210. }
  211. return RT_EOK;
  212. }
  213. INIT_DEVICE_EXPORT(rt_hw_spi_init);