drv_spi.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "drv_spi.h"
  7. #include "fsl_common.h"
  8. #include "fsl_iocon.h"
  9. #include "fsl_spi.h"
  10. struct lpc_spi
  11. {
  12. SPI_Type *base;
  13. struct rt_spi_configuration *cfg;
  14. };
  15. static uint32_t get_spi_freq(SPI_Type *base)
  16. {
  17. uint32_t freq = 0;
  18. #if defined(BSP_USING_SPI2)
  19. if(base == SPI2)
  20. {
  21. freq = CLOCK_GetFreq(kCLOCK_Flexcomm2);
  22. }
  23. #endif
  24. return freq;
  25. }
  26. static rt_err_t spi_init(SPI_Type *base, struct rt_spi_configuration *cfg)
  27. {
  28. spi_master_config_t masterConfig = {0};
  29. RT_ASSERT(cfg != RT_NULL);
  30. if(cfg->data_width != 8 && cfg->data_width != 16)
  31. {
  32. return (-RT_EINVAL);
  33. }
  34. SPI_MasterGetDefaultConfig(&masterConfig);
  35. if(cfg->max_hz > 12*1000*1000)
  36. {
  37. cfg->max_hz = 12*1000*1000;
  38. }
  39. masterConfig.baudRate_Bps = cfg->max_hz;
  40. if(cfg->data_width == 8)
  41. {
  42. masterConfig.dataWidth = kSPI_Data8Bits;
  43. }
  44. else if(cfg->data_width == 16)
  45. {
  46. masterConfig.dataWidth = kSPI_Data16Bits;
  47. }
  48. if(cfg->mode & RT_SPI_MSB)
  49. {
  50. masterConfig.direction = kSPI_MsbFirst;
  51. }
  52. else
  53. {
  54. masterConfig.direction = kSPI_LsbFirst;
  55. }
  56. if(cfg->mode & RT_SPI_CPHA)
  57. {
  58. masterConfig.phase = kSPI_ClockPhaseSecondEdge;
  59. }
  60. else
  61. {
  62. masterConfig.phase = kSPI_ClockPhaseFirstEdge;
  63. }
  64. if(cfg->mode & RT_SPI_CPOL)
  65. {
  66. masterConfig.polarity = kSPI_ClockPolarityActiveLow;
  67. }
  68. else
  69. {
  70. masterConfig.polarity = kSPI_ClockPolarityActiveHigh;
  71. }
  72. masterConfig.txWatermark = kSPI_TxFifo0,
  73. masterConfig.rxWatermark = kSPI_RxFifo1,
  74. // masterConfig.sselNum = kSPI_Ssel3;
  75. SPI_MasterInit(base, &masterConfig, get_spi_freq(base));
  76. return RT_EOK;
  77. }
  78. rt_err_t lpc_spi_bus_attach_device(const char *bus_name, const char *device_name, rt_uint32_t pin)
  79. {
  80. rt_err_t ret = RT_EOK;
  81. struct rt_spi_device *spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  82. RT_ASSERT(spi_device != RT_NULL);
  83. struct lpc_sw_spi_cs *cs_pin = (struct lpc_sw_spi_cs *)rt_malloc(sizeof(struct lpc_sw_spi_cs));
  84. RT_ASSERT(cs_pin != RT_NULL);
  85. cs_pin->pin = pin;
  86. rt_pin_mode(pin, PIN_MODE_OUTPUT);
  87. rt_pin_write(pin, PIN_HIGH);
  88. ret = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  89. return ret;
  90. }
  91. static rt_err_t configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
  92. {
  93. rt_err_t ret = RT_EOK;
  94. struct lpc_spi *spi = RT_NULL;
  95. RT_ASSERT(cfg != RT_NULL);
  96. RT_ASSERT(device != RT_NULL);
  97. spi = (struct lpc_spi *)(device->bus->parent.user_data);
  98. spi->cfg = cfg;
  99. ret = spi_init(spi->base, cfg);
  100. return ret;
  101. }
  102. static rt_ssize_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  103. {
  104. spi_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(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. transfer.configFlags |= kSPI_FrameAssert;
  118. SPI_MasterTransferBlocking(spi->base, &transfer);
  119. if(message->cs_release)
  120. {
  121. rt_pin_write(cs->pin, PIN_HIGH);
  122. }
  123. return message->length;
  124. }
  125. #if defined(BSP_USING_SPI2)
  126. static struct lpc_spi spi2 = {0};
  127. static struct rt_spi_bus spi2_bus = {0};
  128. #endif
  129. static struct rt_spi_ops lpc_spi_ops =
  130. {
  131. configure,
  132. spixfer
  133. };
  134. int rt_hw_spi_init(void)
  135. {
  136. CLOCK_EnableClock(kCLOCK_Iocon);
  137. #if defined(BSP_USING_SPI2)
  138. CLOCK_AttachClk(kFRO12M_to_FLEXCOMM2);
  139. RESET_PeripheralReset(kFC2_RST_SHIFT_RSTn);
  140. spi2.base = SPI2;
  141. spi2.cfg = RT_NULL;
  142. spi2_bus.parent.user_data = &spi2;
  143. IOCON_PinMuxSet(IOCON, 0, 8, (IOCON_FUNC1 | IOCON_MODE_PULLUP | IOCON_GPIO_MODE | IOCON_DIGITAL_EN)); /* SPI2_MOSI */
  144. IOCON_PinMuxSet(IOCON, 0, 9, (IOCON_FUNC1 | IOCON_MODE_PULLUP | IOCON_GPIO_MODE | IOCON_DIGITAL_EN)); /* SPI2_MISO */
  145. IOCON_PinMuxSet(IOCON, 0, 10, (IOCON_FUNC1 | IOCON_MODE_PULLUP | IOCON_GPIO_MODE | IOCON_DIGITAL_EN)); /* SPI2_SCK */
  146. rt_spi_bus_register(&spi2_bus, "spi2", &lpc_spi_ops);
  147. #endif
  148. return RT_EOK;
  149. }
  150. INIT_BOARD_EXPORT(rt_hw_spi_init);