drv_spi.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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_lpspi.h"
  13. #ifdef RT_USING_SPI
  14. #define DBG_TAG "drv.spi"
  15. #define DBG_LVL DBG_INFO
  16. #include <rtdbg.h>
  17. #if (defined(CPU_MCXA346VLH) || defined(CPU_MCXA346VLL) || defined(CPU_MCXA346VLQ) || defined(CPU_MCXA346VPN) || \
  18. defined(CPU_MCXA366VLH) || defined(CPU_MCXA366VLL) || defined(CPU_MCXA366VLQ) || defined(CPU_MCXA366VPN))
  19. #define MCXA_SPI_USE_FRO_LF_DIV
  20. #endif
  21. enum
  22. {
  23. #ifdef BSP_USING_SPI0
  24. SPI0_INDEX,
  25. #endif
  26. #ifdef BSP_USING_SPI1
  27. SPI1_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. 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. #if defined(MCXA_SPI_USE_FRO_LF_DIV)
  46. kFRO_LF_DIV_to_LPSPI0,
  47. #else
  48. .clock_attach_id = kFRO12M_to_LPSPI0,
  49. #endif
  50. .clock_div_name = kCLOCK_DivLPSPI0,
  51. .clock_name = kCLOCK_Fro12M,
  52. .name = "spi0",
  53. },
  54. #endif
  55. #ifdef BSP_USING_SPI1
  56. {
  57. .LPSPIx = LPSPI1,
  58. #if defined(MCXA_SPI_USE_FRO_LF_DIV)
  59. kFRO_LF_DIV_to_LPSPI1,
  60. #else
  61. .clock_attach_id = kFRO12M_to_LPSPI1,
  62. #endif
  63. .clock_div_name = kCLOCK_DivLPSPI1,
  64. .clock_name = kCLOCK_Fro12M,
  65. .name = "spi1",
  66. },
  67. #endif
  68. };
  69. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t pin)
  70. {
  71. struct rt_spi_device *spi_device = rt_malloc(sizeof(struct rt_spi_device));
  72. rt_err_t ret;
  73. if (!spi_device)
  74. {
  75. return -RT_ENOMEM;
  76. }
  77. ret = rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, pin, NULL);
  78. if (ret != RT_EOK)
  79. {
  80. rt_free(spi_device);
  81. }
  82. return ret;
  83. }
  84. static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
  85. {
  86. return RT_EOK;
  87. }
  88. static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  89. {
  90. lpspi_transfer_t transfer = {0};
  91. status_t status;
  92. RT_ASSERT(device != RT_NULL);
  93. RT_ASSERT(device->bus != RT_NULL);
  94. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  95. struct lpc_spi *spi = device->bus->parent.user_data;
  96. if (message->cs_take)
  97. {
  98. rt_pin_write(device->cs_pin, PIN_LOW);
  99. }
  100. transfer.dataSize = message->length;
  101. transfer.rxData = (uint8_t *)(message->recv_buf);
  102. transfer.txData = (uint8_t *)(message->send_buf);
  103. transfer.configFlags = kLPSPI_MasterPcs0;
  104. // Use blocking transfer instead of DMA
  105. status = LPSPI_MasterTransferBlocking(spi->LPSPIx, &transfer);
  106. if (message->cs_release)
  107. {
  108. rt_pin_write(device->cs_pin, PIN_HIGH);
  109. }
  110. if (status != kStatus_Success)
  111. {
  112. return 0; // Transfer failed
  113. }
  114. return message->length;
  115. }
  116. static struct rt_spi_ops lpc_spi_ops =
  117. {
  118. .configure = spi_configure,
  119. .xfer = spixfer
  120. };
  121. int rt_hw_spi_init(void)
  122. {
  123. int i;
  124. for (i = 0; i < ARRAY_SIZE(lpc_obj); i++)
  125. {
  126. CLOCK_SetClockDiv(lpc_obj[i].clock_div_name, 1u);
  127. CLOCK_AttachClk(lpc_obj[i].clock_attach_id);
  128. lpc_obj[i].parent.parent.user_data = &lpc_obj[i];
  129. lpc_obj[i].sem = rt_sem_create("sem_spi", 0, RT_IPC_FLAG_FIFO);
  130. lpspi_master_config_t masterConfig;
  131. LPSPI_MasterGetDefaultConfig(&masterConfig);
  132. masterConfig.baudRate = 10 * 1000 * 1000;
  133. masterConfig.pcsToSckDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
  134. masterConfig.lastSckToPcsDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
  135. masterConfig.betweenTransferDelayInNanoSec = 1000000000U / masterConfig.baudRate * 1U;
  136. LPSPI_MasterInit(lpc_obj[i].LPSPIx, &masterConfig, CLOCK_GetFreq(lpc_obj[i].clock_name));
  137. rt_spi_bus_register(&lpc_obj[i].parent, lpc_obj[i].name, &lpc_spi_ops);
  138. }
  139. return RT_EOK;
  140. }
  141. INIT_DEVICE_EXPORT(rt_hw_spi_init);
  142. #endif /* RT_USING_SPI */