drv_spi.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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_SPI3
  18. SPI3_INDEX,
  19. #endif
  20. #ifdef BSP_USING_SPI7
  21. SPI7_INDEX,
  22. #endif
  23. };
  24. struct lpc_spi
  25. {
  26. struct rt_spi_bus parent;
  27. LPSPI_Type *LPSPIx;
  28. clock_attach_id_t clock_attach_id;
  29. clock_div_name_t clock_div_name;
  30. clock_name_t clock_name;
  31. DMA_Type *DMAx;
  32. uint8_t tx_dma_chl;
  33. uint8_t rx_dma_chl;
  34. edma_handle_t dma_tx_handle;
  35. edma_handle_t dma_rx_handle;
  36. dma_request_source_t tx_dma_request;
  37. dma_request_source_t rx_dma_request;
  38. lpspi_master_edma_handle_t spi_dma_handle;
  39. rt_sem_t sem;
  40. char *name;
  41. };
  42. static struct lpc_spi lpc_obj[] =
  43. {
  44. #ifdef BSP_USING_SPI3
  45. {
  46. .LPSPIx = LPSPI3,
  47. .clock_attach_id = kFRO_HF_DIV_to_FLEXCOMM3,
  48. .clock_div_name = kCLOCK_DivFlexcom3Clk,
  49. .clock_name = kCLOCK_FroHf,
  50. .tx_dma_request = kDmaRequestMuxLpFlexcomm3Tx,
  51. .rx_dma_request = kDmaRequestMuxLpFlexcomm3Rx,
  52. .DMAx = DMA0,
  53. .tx_dma_chl = 2,
  54. .rx_dma_chl = 3,
  55. .name = "spi3",
  56. },
  57. #endif
  58. #ifdef BSP_USING_SPI7
  59. {
  60. .LPSPIx = LPSPI7,
  61. .clock_attach_id = kFRO_HF_DIV_to_FLEXCOMM7,
  62. .clock_div_name = kCLOCK_DivFlexcom7Clk,
  63. .clock_name = kCLOCK_FroHf,
  64. .tx_dma_request = kDmaRequestMuxLpFlexcomm7Tx,
  65. .rx_dma_request = kDmaRequestMuxLpFlexcomm7Rx,
  66. .DMAx = DMA0,
  67. .tx_dma_chl = 2,
  68. .rx_dma_chl = 3,
  69. .name = "spi7",
  70. },
  71. #endif
  72. };
  73. struct lpc_sw_spi_cs
  74. {
  75. rt_uint32_t pin;
  76. };
  77. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t pin)
  78. {
  79. rt_err_t ret = RT_EOK;
  80. struct rt_spi_device *spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  81. struct lpc_sw_spi_cs *cs_pin = (struct lpc_sw_spi_cs *)rt_malloc(sizeof(struct lpc_sw_spi_cs));
  82. cs_pin->pin = pin;
  83. rt_pin_mode(pin, PIN_MODE_OUTPUT);
  84. rt_pin_write(pin, PIN_HIGH);
  85. ret = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  86. return ret;
  87. }
  88. static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
  89. {
  90. rt_err_t ret = RT_EOK;
  91. // struct lpc_spi *spi = RT_NULL;
  92. // spi = (struct lpc_spi *)(device->bus->parent.user_data);
  93. // ret = lpc_spi_init(spi->SPIx, cfg);
  94. return ret;
  95. }
  96. static void LPSPI_MasterUserCallback(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, status_t status, void *userData)
  97. {
  98. struct lpc_spi *spi = (struct lpc_spi*)userData;
  99. rt_sem_release(spi->sem);
  100. }
  101. static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  102. {
  103. int i;
  104. lpspi_transfer_t transfer = {0};
  105. RT_ASSERT(device != RT_NULL);
  106. RT_ASSERT(device->bus != RT_NULL);
  107. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  108. struct lpc_spi *spi = (struct lpc_spi *)(device->bus->parent.user_data);
  109. // struct lpc_sw_spi_cs *cs = device->parent.user_data;
  110. if(message->cs_take)
  111. {
  112. rt_pin_write(device->cs_pin, PIN_LOW);
  113. }
  114. transfer.dataSize = message->length;
  115. transfer.rxData = (uint8_t *)(message->recv_buf);
  116. transfer.txData = (uint8_t *)(message->send_buf);
  117. // if(message->length < MAX_DMA_TRANSFER_SIZE)
  118. if(0)
  119. {
  120. // SPI_MasterTransferBlocking(spi->SPIx, &transfer);
  121. }
  122. else
  123. {
  124. uint32_t block, remain;
  125. block = message->length / DMA_MAX_TRANSFER_COUNT;
  126. remain = message->length % DMA_MAX_TRANSFER_COUNT;
  127. for(i=0; i<block; i++)
  128. {
  129. transfer.dataSize = DMA_MAX_TRANSFER_COUNT;
  130. if(message->recv_buf) transfer.rxData = (uint8_t *)(message->recv_buf + i*DMA_MAX_TRANSFER_COUNT);
  131. if(message->send_buf) transfer.txData = (uint8_t *)(message->send_buf + i*DMA_MAX_TRANSFER_COUNT);
  132. LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer);
  133. rt_sem_take(spi->sem, RT_WAITING_FOREVER);
  134. }
  135. if(remain)
  136. {
  137. transfer.dataSize = remain;
  138. if(message->recv_buf) transfer.rxData = (uint8_t *)(message->recv_buf + i*DMA_MAX_TRANSFER_COUNT);
  139. if(message->send_buf) transfer.txData = (uint8_t *)(message->send_buf + i*DMA_MAX_TRANSFER_COUNT);
  140. LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer);
  141. rt_sem_take(spi->sem, RT_WAITING_FOREVER);
  142. }
  143. }
  144. if(message->cs_release)
  145. {
  146. rt_pin_write(device->cs_pin, PIN_HIGH);
  147. }
  148. return message->length;
  149. }
  150. static struct rt_spi_ops lpc_spi_ops =
  151. {
  152. .configure = spi_configure,
  153. .xfer = spixfer
  154. };
  155. int rt_hw_spi_init(void)
  156. {
  157. int i;
  158. for(i=0; i<ARRAY_SIZE(lpc_obj); i++)
  159. {
  160. CLOCK_SetClkDiv(lpc_obj[i].clock_div_name, 1u);
  161. CLOCK_AttachClk(lpc_obj[i].clock_attach_id);
  162. lpc_obj[i].parent.parent.user_data = &lpc_obj[i];
  163. lpc_obj[i].sem = rt_sem_create("sem_spi", 0, RT_IPC_FLAG_FIFO);
  164. lpspi_master_config_t masterConfig;
  165. LPSPI_MasterGetDefaultConfig(&masterConfig);
  166. masterConfig.baudRate = 24*1000*1000;
  167. masterConfig.pcsToSckDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
  168. masterConfig.lastSckToPcsDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
  169. masterConfig.betweenTransferDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
  170. LPSPI_MasterInit(lpc_obj[i].LPSPIx, &masterConfig, CLOCK_GetFreq(lpc_obj[i].clock_name));
  171. EDMA_CreateHandle(&lpc_obj[i].dma_tx_handle, lpc_obj[i].DMAx, lpc_obj[i].tx_dma_chl);
  172. EDMA_CreateHandle(&lpc_obj[i].dma_rx_handle, lpc_obj[i].DMAx, lpc_obj[i].rx_dma_chl);
  173. EDMA_SetChannelMux(lpc_obj[i].DMAx, lpc_obj[i].tx_dma_chl, lpc_obj[i].tx_dma_request);
  174. EDMA_SetChannelMux(lpc_obj[i].DMAx, lpc_obj[i].rx_dma_chl, lpc_obj[i].rx_dma_request);
  175. 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);
  176. rt_spi_bus_register(&lpc_obj[i].parent, lpc_obj[i].name, &lpc_spi_ops);
  177. }
  178. return RT_EOK;
  179. }
  180. INIT_DEVICE_EXPORT(rt_hw_spi_init);