drv_spi.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-07-18 Rbb666 first version
  9. */
  10. #include <drv_spi.h>
  11. #ifdef RT_USING_SPI
  12. //#define DRV_DEBUG
  13. #define DBG_TAG "drv.spi"
  14. #ifdef DRV_DEBUG
  15. #define DBG_LVL DBG_LOG
  16. #else
  17. #define DBG_LVL DBG_INFO
  18. #endif /* DRV_DEBUG */
  19. #include <rtdbg.h>
  20. struct ifx_sw_spi_cs
  21. {
  22. rt_uint32_t pin;
  23. };
  24. #ifdef BSP_USING_SPI3
  25. static struct rt_spi_bus spi_bus3;
  26. #endif
  27. static struct ifx_spi spi_bus_obj[] =
  28. {
  29. #ifdef BSP_USING_SPI3
  30. {
  31. .bus_name = "spi3",
  32. .spi_bus = &spi_bus3,
  33. .sck_pin = GET_PIN(6, 2),
  34. .miso_pin = GET_PIN(6, 1),
  35. .mosi_pin = GET_PIN(6, 0),
  36. },
  37. #endif
  38. };
  39. /* private rt-thread spi ops function */
  40. static rt_err_t spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
  41. static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message);
  42. static struct rt_spi_ops ifx_spi_ops =
  43. {
  44. .configure = spi_configure,
  45. .xfer = spixfer,
  46. };
  47. static void ifx_spi_init(struct ifx_spi *ifx_spi)
  48. {
  49. int result = RT_EOK;
  50. result = cyhal_spi_init(ifx_spi->spi_obj, ifx_spi->mosi_pin, ifx_spi->miso_pin, ifx_spi->sck_pin,
  51. NC, NULL, ifx_spi->spi_obj->data_bits, ifx_spi->spi_obj->mode, false);
  52. RT_ASSERT(result != RT_ERROR);
  53. rt_kprintf("[%s] Freq:[%d]HZ\n", ifx_spi->bus_name, ifx_spi->freq);
  54. result = cyhal_spi_set_frequency(ifx_spi->spi_obj, ifx_spi->freq);
  55. RT_ASSERT(result != CYHAL_SPI_RSLT_CLOCK_ERROR);
  56. }
  57. static rt_err_t spi_configure(struct rt_spi_device *device,
  58. struct rt_spi_configuration *configuration)
  59. {
  60. struct rt_spi_bus *spi_bus = (struct rt_spi_bus *)device->bus;
  61. struct ifx_spi *spi_device = (struct ifx_spi *)spi_bus->parent.user_data;
  62. RT_ASSERT(device != RT_NULL);
  63. RT_ASSERT(configuration != RT_NULL);
  64. /* data_width */
  65. if (configuration->data_width <= 8)
  66. {
  67. spi_device->spi_obj->data_bits = 8;
  68. }
  69. else if (configuration->data_width <= 16)
  70. {
  71. spi_device->spi_obj->data_bits = 16;
  72. }
  73. else
  74. {
  75. return RT_EIO;
  76. }
  77. uint32_t max_hz;
  78. max_hz = configuration->max_hz;
  79. spi_device->freq = max_hz;
  80. /* MSB or LSB */
  81. switch (configuration->mode & RT_SPI_MODE_3)
  82. {
  83. case RT_SPI_MODE_0:
  84. spi_device->spi_obj->mode = CYHAL_SPI_MODE_00_MSB;
  85. break;
  86. case RT_SPI_MODE_1:
  87. spi_device->spi_obj->mode = CYHAL_SPI_MODE_01_MSB;
  88. break;
  89. case RT_SPI_MODE_2:
  90. spi_device->spi_obj->mode = CYHAL_SPI_MODE_10_MSB;
  91. break;
  92. case RT_SPI_MODE_3:
  93. spi_device->spi_obj->mode = CYHAL_SPI_MODE_11_MSB;
  94. break;
  95. }
  96. ifx_spi_init(spi_device);
  97. return RT_EOK;
  98. }
  99. static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  100. {
  101. RT_ASSERT(device != NULL);
  102. RT_ASSERT(message != NULL);
  103. struct rt_spi_bus *spi_bus = (struct rt_spi_bus *)device->bus;
  104. struct ifx_spi *spi_device = (struct ifx_spi *)spi_bus->parent.user_data;
  105. struct rt_spi_configuration *config = &device->config;
  106. struct ifx_sw_spi_cs *cs = device->parent.user_data;
  107. /* take CS */
  108. if (message->cs_take)
  109. {
  110. cyhal_gpio_write(cs->pin, PIN_LOW);
  111. LOG_D("spi take cs\n");
  112. }
  113. int result = RT_EOK;
  114. if (message->length > 0)
  115. {
  116. if (message->send_buf == RT_NULL && message->recv_buf != RT_NULL)
  117. {
  118. /**< receive message */
  119. result = cyhal_spi_transfer(spi_device->spi_obj, RT_NULL, 0x00, message->recv_buf, message->length, 0x00);
  120. }
  121. else if (message->send_buf != RT_NULL && message->recv_buf == RT_NULL)
  122. {
  123. /**< send message */
  124. result = cyhal_spi_transfer(spi_device->spi_obj, message->send_buf, message->length, RT_NULL, 0x00, 0x00);
  125. }
  126. else if (message->send_buf != RT_NULL && message->recv_buf != RT_NULL)
  127. {
  128. /**< send and receive message */
  129. result = cyhal_spi_transfer(spi_device->spi_obj, message->send_buf, message->length, message->recv_buf, message->length, 0x00);
  130. }
  131. }
  132. if (message->cs_release && !(device->config.mode & RT_SPI_NO_CS))
  133. {
  134. if (device->config.mode & RT_SPI_CS_HIGH)
  135. cyhal_gpio_write(cs->pin, PIN_LOW);
  136. else
  137. cyhal_gpio_write(cs->pin, PIN_HIGH);
  138. }
  139. return message->length;
  140. }
  141. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, uint16_t cs_gpio_pin)
  142. {
  143. RT_ASSERT(bus_name != RT_NULL);
  144. RT_ASSERT(device_name != RT_NULL);
  145. rt_err_t result;
  146. struct rt_spi_device *spi_device;
  147. /* attach the device to spi bus */
  148. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  149. RT_ASSERT(spi_device != RT_NULL);
  150. struct ifx_sw_spi_cs *cs_pin = (struct ifx_sw_spi_cs *)rt_malloc(sizeof(struct ifx_sw_spi_cs));
  151. RT_ASSERT(cs_pin != RT_NULL);
  152. cs_pin->pin = cs_gpio_pin;
  153. if (cs_pin->pin != 0x00)
  154. {
  155. /* initialize the cs pin & select the slave*/
  156. cyhal_gpio_init(cs_pin->pin, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, 1);
  157. cyhal_gpio_write(cs_pin->pin, PIN_HIGH);
  158. }
  159. result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void *)cs_pin);
  160. RT_ASSERT(spi_device != RT_NULL);
  161. return result;
  162. }
  163. int rt_hw_spi_init(void)
  164. {
  165. int result = RT_EOK;
  166. for (int i = 0; i < sizeof(spi_bus_obj) / sizeof(spi_bus_obj[0]); i++)
  167. {
  168. spi_bus_obj[i].spi_obj = rt_malloc(sizeof(cyhal_spi_t));
  169. RT_ASSERT(spi_bus_obj[i].spi_obj != RT_NULL);
  170. spi_bus_obj[i].spi_bus->parent.user_data = (void *)&spi_bus_obj[i];
  171. result = rt_spi_bus_register(spi_bus_obj[i].spi_bus, spi_bus_obj[i].bus_name, &ifx_spi_ops);
  172. RT_ASSERT(result == RT_EOK);
  173. LOG_D("%s bus init done", spi_bus_obj[i].bus_name);
  174. LOG_D("MOSI PIN:[%d], MISO PIN[%d], CLK PIN[%d]\n",
  175. spi_bus_obj[i].mosi_pin, spi_bus_obj[i].miso_pin,
  176. spi_bus_obj[i].sck_pin);
  177. }
  178. return result;
  179. }
  180. INIT_BOARD_EXPORT(rt_hw_spi_init);
  181. #endif /* RT_USING_SPI */