drv_spi.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-02-14 supperthomas first version
  9. */
  10. #include <stdint.h>
  11. #include <string.h>
  12. #include "board.h"
  13. #include "drv_spi.h"
  14. #define DBG_LEVEL DBG_LOG
  15. #include <rtdbg.h>
  16. #define LOG_TAG "drv.spi"
  17. #ifdef BSP_USING_SPI
  18. #if defined(BSP_USING_SPI0) || defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2)
  19. static struct mcu_drv_spi_config spi_config[] =
  20. {
  21. #ifdef BSP_USING_SPI0
  22. MCU_SPI0_CONFIG,
  23. #endif
  24. #ifdef BSP_USING_SPI1
  25. MCU_SPI1_CONFIG,
  26. #endif
  27. };
  28. static struct mcu_drv_spi spi_bus_obj[sizeof(spi_config) / sizeof(spi_config[0])];
  29. /**
  30. * @brief This function config spi bus
  31. * @param device
  32. * @param configuration
  33. * @retval RT_EOK / RT_ERROR
  34. */
  35. static rt_err_t spi_configure(struct rt_spi_device *device,
  36. struct rt_spi_configuration *configuration)
  37. {
  38. RT_ASSERT(device != RT_NULL);
  39. RT_ASSERT(device->bus != RT_NULL);
  40. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  41. RT_ASSERT(configuration != RT_NULL);
  42. struct mcu_drv_spi *tmp_spi;
  43. tmp_spi = rt_container_of(device->bus, struct mcu_drv_spi, spi_bus);
  44. int mode;
  45. ///init
  46. switch (configuration->mode & RT_SPI_MODE_3)
  47. {
  48. case RT_SPI_MODE_0/* RT_SPI_CPOL:0 , RT_SPI_CPHA:0 */:
  49. case RT_SPI_MODE_1/* RT_SPI_CPOL:0 , RT_SPI_CPHA:1 */:
  50. case RT_SPI_MODE_2/* RT_SPI_CPOL:1 , RT_SPI_CPHA:0 */:
  51. case RT_SPI_MODE_3/* RT_SPI_CPOL:1 , RT_SPI_CPHA:1 */:
  52. mode = configuration->mode & RT_SPI_MODE_3;
  53. break;
  54. default:
  55. LOG_E("spi_configure mode error %x\n", configuration->mode);
  56. return RT_ERROR;
  57. }
  58. tmp_spi->spixfer_req.width = SPI17Y_WIDTH_1;
  59. tmp_spi->spixfer_req.bits = configuration->data_width;
  60. tmp_spi->spixfer_req.ssel = 0;
  61. tmp_spi->spixfer_req.deass = 1;
  62. tmp_spi->spixfer_req.tx_num = 0;
  63. tmp_spi->spixfer_req.rx_num = 0;
  64. tmp_spi->spixfer_req.callback = NULL;
  65. LOG_D("spi init mode:%d, rate:%d", mode, configuration->max_hz);
  66. if (SPI_Init(tmp_spi->spi_instance, mode, configuration->max_hz) != 0)
  67. {
  68. LOG_E("Error configuring SPI\n");
  69. while (1) {}
  70. }
  71. //init
  72. return RT_EOK;
  73. }
  74. static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  75. {
  76. RT_ASSERT(device != RT_NULL);
  77. RT_ASSERT(device->bus != RT_NULL);
  78. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  79. int ret = 0;
  80. struct mcu_drv_spi *tmp_spi;
  81. tmp_spi = rt_container_of(device->bus, struct mcu_drv_spi, spi_bus);
  82. tmp_spi->spixfer_req.tx_data = message->send_buf;
  83. tmp_spi->spixfer_req.rx_data = message->recv_buf;
  84. tmp_spi->spixfer_req.len = message->length;
  85. ret = SPI_MasterTrans(tmp_spi->spi_instance, &tmp_spi->spixfer_req);
  86. if (ret == E_NO_ERROR)
  87. {
  88. return message->length;
  89. }
  90. else
  91. {
  92. LOG_E("spixfer faild, ret %d", ret);
  93. return 0;
  94. }
  95. }
  96. /* spi bus callback function */
  97. static const struct rt_spi_ops nrfx_spi_ops =
  98. {
  99. .configure = spi_configure,
  100. .xfer = spixfer,
  101. };
  102. /*spi bus init*/
  103. static int rt_hw_spi_bus_init(void)
  104. {
  105. rt_err_t result = RT_ERROR;
  106. for (int i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
  107. {
  108. spi_bus_obj[i].spi_instance = spi_config[i].spi_instance;
  109. spi_bus_obj[i].spi_bus.parent.user_data = &spi_config[i]; //SPI INSTANCE
  110. result = rt_spi_bus_register(&spi_bus_obj[i].spi_bus, spi_config[i].bus_name, &nrfx_spi_ops);
  111. RT_ASSERT(result == RT_EOK);
  112. }
  113. return result;
  114. }
  115. int rt_hw_spi_init(void)
  116. {
  117. return rt_hw_spi_bus_init();
  118. }
  119. INIT_BOARD_EXPORT(rt_hw_spi_init);
  120. /**
  121. * Attach the spi device to SPI bus, this function must be used after initialization.
  122. */
  123. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t cs_pin)
  124. {
  125. RT_ASSERT(bus_name != RT_NULL);
  126. RT_ASSERT(device_name != RT_NULL);
  127. RT_ASSERT(cs_pin != RT_NULL);
  128. rt_err_t result;
  129. struct rt_spi_device *spi_device;
  130. /* attach the device to spi bus*/
  131. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  132. RT_ASSERT(spi_device != RT_NULL);
  133. /* initialize the cs pin */
  134. result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  135. if (result != RT_EOK)
  136. {
  137. LOG_E("%s attach to %s faild, %d", device_name, bus_name, result);
  138. result = RT_ERROR;
  139. }
  140. /* TODO: SET THE GPIO */
  141. RT_ASSERT(result == RT_EOK);
  142. return result;
  143. }
  144. #endif /* BSP_USING_SPI0 || BSP_USING_SPI1 || BSP_USING_SPI2 */
  145. #endif /*BSP_USING_SPI*/