drv_spi.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. * 2024-08-1 hywing The first version for MCXA
  9. */
  10. #include "rtdevice.h"
  11. #include "drv_spi.h"
  12. #include "fsl_common.h"
  13. #include "fsl_lpspi.h"
  14. #include "fsl_lpspi_edma.h"
  15. #define DMA_MAX_TRANSFER_SIZE (32767)
  16. enum
  17. {
  18. #ifdef BSP_USING_SPI0
  19. SPI0_INDEX
  20. #endif
  21. };
  22. struct lpc_spi
  23. {
  24. struct rt_spi_bus parent;
  25. LPSPI_Type *LPSPIx;
  26. clock_attach_id_t clock_attach_id;
  27. clock_div_name_t clock_div_name;
  28. clock_name_t clock_name;
  29. DMA_Type *DMAx;
  30. uint8_t tx_dma_chl;
  31. uint8_t rx_dma_chl;
  32. edma_handle_t dma_tx_handle;
  33. edma_handle_t dma_rx_handle;
  34. dma_request_source_t tx_dma_request;
  35. dma_request_source_t rx_dma_request;
  36. lpspi_master_edma_handle_t spi_dma_handle;
  37. rt_sem_t sem;
  38. char *name;
  39. };
  40. static struct lpc_spi lpc_obj[] =
  41. {
  42. #ifdef BSP_USING_SPI0
  43. {
  44. .LPSPIx = LPSPI0,
  45. .clock_attach_id = kFRO12M_to_LPSPI0,
  46. .clock_div_name = kCLOCK_DivLPSPI0,
  47. .clock_name = kCLOCK_Fro12M,
  48. .tx_dma_request = kDma0RequestLPSPI0Tx,
  49. .rx_dma_request = kDma0RequestLPSPI0Rx,
  50. .DMAx = DMA0,
  51. .tx_dma_chl = 0,
  52. .rx_dma_chl = 1,
  53. .name = "spi0",
  54. },
  55. #endif
  56. };
  57. struct lpc_sw_spi_cs
  58. {
  59. rt_uint32_t pin;
  60. };
  61. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t pin)
  62. {
  63. rt_err_t ret = RT_EOK;
  64. struct rt_spi_device *spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  65. struct lpc_sw_spi_cs *cs_pin = (struct lpc_sw_spi_cs *)rt_malloc(sizeof(struct lpc_sw_spi_cs));
  66. cs_pin->pin = pin;
  67. rt_pin_mode(pin, PIN_MODE_OUTPUT);
  68. rt_pin_write(pin, PIN_HIGH);
  69. ret = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  70. return ret;
  71. }
  72. static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
  73. {
  74. return RT_EOK;
  75. }
  76. static void LPSPI_MasterUserCallback(LPSPI_Type *base, lpspi_master_edma_handle_t *handle, status_t status, void *userData)
  77. {
  78. struct lpc_spi *spi = (struct lpc_spi *)userData;
  79. rt_sem_release(spi->sem);
  80. }
  81. static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  82. {
  83. int i;
  84. lpspi_transfer_t transfer = {0};
  85. RT_ASSERT(device != RT_NULL);
  86. RT_ASSERT(device->bus != RT_NULL);
  87. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  88. struct lpc_spi *spi = (struct lpc_spi *)(device->bus->parent.user_data);
  89. struct lpc_sw_spi_cs *cs = device->parent.user_data;
  90. if (message->cs_take)
  91. {
  92. rt_pin_write(cs->pin, PIN_LOW);
  93. }
  94. transfer.dataSize = message->length;
  95. transfer.rxData = (uint8_t *)(message->recv_buf);
  96. transfer.txData = (uint8_t *)(message->send_buf);
  97. /* if(message->length < MAX_DMA_TRANSFER_SIZE)*/
  98. if (0)
  99. {
  100. LPSPI_MasterTransferBlocking(spi->LPSPIx, &transfer);
  101. }
  102. else
  103. {
  104. uint32_t block, remain;
  105. block = message->length / DMA_MAX_TRANSFER_SIZE;
  106. remain = message->length % DMA_MAX_TRANSFER_SIZE;
  107. for (i = 0; i < block; i++)
  108. {
  109. transfer.dataSize = DMA_MAX_TRANSFER_SIZE;
  110. if (message->recv_buf) transfer.rxData = (uint8_t *)(message->recv_buf + i * DMA_MAX_TRANSFER_SIZE);
  111. if (message->send_buf) transfer.txData = (uint8_t *)(message->send_buf + i * DMA_MAX_TRANSFER_SIZE);
  112. LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer);
  113. rt_sem_take(spi->sem, RT_WAITING_FOREVER);
  114. }
  115. if (remain)
  116. {
  117. transfer.dataSize = remain;
  118. if (message->recv_buf) transfer.rxData = (uint8_t *)(message->recv_buf + i * DMA_MAX_TRANSFER_SIZE);
  119. if (message->send_buf) transfer.txData = (uint8_t *)(message->send_buf + i * DMA_MAX_TRANSFER_SIZE);
  120. LPSPI_MasterTransferEDMA(spi->LPSPIx, &spi->spi_dma_handle, &transfer);
  121. rt_sem_take(spi->sem, RT_WAITING_FOREVER);
  122. }
  123. }
  124. if (message->cs_release)
  125. {
  126. rt_pin_write(cs->pin, PIN_HIGH);
  127. }
  128. return message->length;
  129. }
  130. static struct rt_spi_ops lpc_spi_ops =
  131. {
  132. .configure = spi_configure,
  133. .xfer = spixfer
  134. };
  135. int rt_hw_spi_init(void)
  136. {
  137. int i;
  138. for (i = 0; i < ARRAY_SIZE(lpc_obj); i++)
  139. {
  140. CLOCK_SetClockDiv(lpc_obj[i].clock_div_name, 1u);
  141. CLOCK_AttachClk(lpc_obj[i].clock_attach_id);
  142. lpc_obj[i].parent.parent.user_data = &lpc_obj[i];
  143. lpc_obj[i].sem = rt_sem_create("sem_spi", 0, RT_IPC_FLAG_FIFO);
  144. lpspi_master_config_t masterConfig;
  145. LPSPI_MasterGetDefaultConfig(&masterConfig);
  146. masterConfig.baudRate = 1 * 1000 * 1000;
  147. masterConfig.pcsToSckDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
  148. masterConfig.lastSckToPcsDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
  149. masterConfig.betweenTransferDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
  150. LPSPI_MasterInit(lpc_obj[i].LPSPIx, &masterConfig, CLOCK_GetFreq(lpc_obj[i].clock_name));
  151. EDMA_CreateHandle(&lpc_obj[i].dma_tx_handle, lpc_obj[i].DMAx, lpc_obj[i].tx_dma_chl);
  152. EDMA_CreateHandle(&lpc_obj[i].dma_rx_handle, lpc_obj[i].DMAx, lpc_obj[i].rx_dma_chl);
  153. EDMA_SetChannelMux(lpc_obj[i].DMAx, lpc_obj[i].tx_dma_chl, lpc_obj[i].tx_dma_request);
  154. EDMA_SetChannelMux(lpc_obj[i].DMAx, lpc_obj[i].rx_dma_chl, lpc_obj[i].rx_dma_request);
  155. 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);
  156. rt_spi_bus_register(&lpc_obj[i].parent, lpc_obj[i].name, &lpc_spi_ops);
  157. }
  158. return RT_EOK;
  159. }
  160. INIT_DEVICE_EXPORT(rt_hw_spi_init);