drv_spi.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2021 hpm
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #include <rtthread.h>
  8. #ifdef BSP_USING_SPI
  9. #include <rtdevice.h>
  10. #include "board.h"
  11. #include "drv_spi.h"
  12. #include "hpm_spi_drv.h"
  13. #include "hpm_sysctl_drv.h"
  14. struct hpm_spi
  15. {
  16. uint32_t instance;
  17. char *bus_name;
  18. SPI_Type *spi_base;
  19. spi_control_config_t control_config;
  20. struct rt_spi_bus spi_bus;
  21. rt_sem_t xfer_sem;
  22. /* TODO: add DMA support later */
  23. };
  24. static rt_err_t hpm_spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg);
  25. static rt_uint32_t hpm_spi_xfer(struct rt_spi_device *device, struct rt_spi_message *msg);
  26. static struct hpm_spi hpm_spis[] =
  27. {
  28. #if defined(BSP_USING_SPI0)
  29. {
  30. .bus_name = "spi0",
  31. .spi_base = HPM_SPI0,
  32. },
  33. #endif
  34. #if defined(BSP_USING_SPI1)
  35. {
  36. .bus_name = "spi1",
  37. .spi_base = HPM_SPI1,
  38. },
  39. #endif
  40. #if defined(BSP_USING_SPI2)
  41. {
  42. .bus_name = "spi2",
  43. .spi_base = HPM_SPI2,
  44. },
  45. #endif
  46. #if defined(BSP_USING_SPI3)
  47. {
  48. .bus_name = "spi3",
  49. .spi_base = HPM_SPI3,
  50. },
  51. #endif
  52. };
  53. static struct rt_spi_ops hpm_spi_ops =
  54. {
  55. .configure = hpm_spi_configure,
  56. .xfer = hpm_spi_xfer,
  57. };
  58. static rt_err_t hpm_spi_configure(struct rt_spi_device *device, struct rt_spi_configuration *cfg)
  59. {
  60. spi_timing_config_t timing_config = { 0 };
  61. spi_format_config_t format_config = { 0 };
  62. struct hpm_spi *spi = RT_NULL;
  63. spi = (struct hpm_spi *) (device->bus->parent.user_data);
  64. RT_ASSERT(spi != RT_NULL);
  65. if (cfg->data_width != 8 && cfg->data_width != 16 && cfg->data_width != 32)
  66. {
  67. return RT_EINVAL;
  68. }
  69. spi_master_get_default_timing_config(&timing_config);
  70. spi_master_get_default_format_config(&format_config);
  71. init_spi_pins(spi->spi_base);
  72. timing_config.master_config.clk_src_freq_in_hz = board_init_spi_clock(spi->spi_base);
  73. format_config.common_config.data_len_in_bits = cfg->data_width;
  74. format_config.common_config.cpha = cfg->mode & RT_SPI_CPHA ? 1 : 0;
  75. format_config.common_config.cpol = cfg->mode & RT_SPI_CPOL ? 1 : 0;
  76. format_config.common_config.lsb = cfg->mode & RT_SPI_MSB ? false : true;
  77. format_config.common_config.mosi_bidir = cfg->mode & RT_SPI_3WIRE ? true : false;
  78. spi_format_init(spi->spi_base, &format_config);
  79. if (cfg->max_hz > timing_config.master_config.clk_src_freq_in_hz)
  80. {
  81. cfg->max_hz = timing_config.master_config.clk_src_freq_in_hz;
  82. }
  83. timing_config.master_config.sclk_freq_in_hz = cfg->max_hz;
  84. spi_master_timing_init(spi->spi_base, &timing_config);
  85. spi_master_get_default_control_config(&spi->control_config);
  86. spi->control_config.master_config.addr_enable = false;
  87. spi->control_config.master_config.cmd_enable = false;
  88. spi->control_config.master_config.token_enable = false;
  89. spi->control_config.common_config.trans_mode = spi_trans_write_read_together;
  90. return RT_EOK;
  91. }
  92. static rt_uint32_t hpm_spi_xfer(struct rt_spi_device *device, struct rt_spi_message *msg)
  93. {
  94. RT_ASSERT(device != RT_NULL);
  95. RT_ASSERT(msg != RT_NULL);
  96. RT_ASSERT(device->bus != RT_NULL);
  97. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  98. cs_ctrl_callback_t cs_pin_control = (cs_ctrl_callback_t) device->parent.user_data;
  99. struct hpm_spi *spi = (struct hpm_spi *) (device->bus->parent.user_data);
  100. hpm_stat_t spi_stat = status_success;
  101. if ((cs_pin_control != NULL) && msg->cs_take)
  102. {
  103. cs_pin_control(SPI_CS_TAKE);
  104. }
  105. uint32_t remaining_size = msg->length;
  106. uint32_t transfer_len;
  107. uint8_t *tx_buf = (uint8_t*) msg->send_buf;
  108. uint8_t *rx_buf = (uint8_t*) msg->recv_buf;
  109. while (remaining_size > 0)
  110. {
  111. transfer_len = MIN(512, remaining_size);
  112. if (msg->send_buf != NULL && msg->recv_buf != NULL)
  113. {
  114. spi->control_config.common_config.trans_mode = spi_trans_write_read_together;
  115. spi_stat = spi_transfer(spi->spi_base,
  116. &spi->control_config,
  117. NULL,
  118. NULL,
  119. tx_buf, transfer_len,
  120. rx_buf, transfer_len);
  121. }
  122. else if (msg->send_buf != NULL)
  123. {
  124. spi->control_config.common_config.trans_mode = spi_trans_write_only;
  125. spi_stat = spi_transfer(spi->spi_base, &spi->control_config,
  126. NULL,
  127. NULL,
  128. (uint8_t*) tx_buf, transfer_len,
  129. NULL, 0);
  130. }
  131. else
  132. {
  133. spi->control_config.common_config.trans_mode = spi_trans_read_only;
  134. spi_stat = spi_transfer(spi->spi_base, &spi->control_config,
  135. NULL,
  136. NULL,
  137. NULL, 0,
  138. rx_buf, transfer_len);
  139. }
  140. if (spi_stat != status_success)
  141. {
  142. break;
  143. }
  144. if (tx_buf != NULL)
  145. {
  146. tx_buf += transfer_len;
  147. }
  148. if (rx_buf != NULL)
  149. {
  150. rx_buf += transfer_len;
  151. }
  152. remaining_size -= transfer_len;
  153. }
  154. if (spi_stat != status_success)
  155. {
  156. msg->length = 0;
  157. }
  158. if ((cs_pin_control != NULL) && msg->cs_release)
  159. {
  160. cs_pin_control(SPI_CS_RELEASE);
  161. }
  162. return msg->length;
  163. }
  164. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, cs_ctrl_callback_t callback)
  165. {
  166. RT_ASSERT(bus_name != RT_NULL);
  167. RT_ASSERT(device_name != RT_NULL);
  168. rt_err_t result;
  169. struct rt_spi_device *spi_device;
  170. /* attach the device to spi bus*/
  171. spi_device = (struct rt_spi_device *) rt_malloc(sizeof(struct rt_spi_device));
  172. RT_ASSERT(spi_device != RT_NULL);
  173. result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void*)callback);
  174. RT_ASSERT(result == RT_EOK);
  175. return result;
  176. }
  177. int rt_hw_spi_init(void)
  178. {
  179. rt_err_t ret = RT_EOK;
  180. for (uint32_t i = 0; i < sizeof(hpm_spis) / sizeof(hpm_spis[0]); i++)
  181. {
  182. hpm_spis[i].spi_bus.parent.user_data = &hpm_spis[i];
  183. ret = rt_spi_bus_register(&hpm_spis[i].spi_bus, hpm_spis[i].bus_name, &hpm_spi_ops);
  184. if (ret != RT_EOK)
  185. {
  186. break;
  187. }
  188. char sem_name[RT_NAME_MAX];
  189. rt_sprintf(sem_name, "%s_s", hpm_spis[i].bus_name);
  190. hpm_spis[i].xfer_sem = rt_sem_create(sem_name, 0, RT_IPC_FLAG_PRIO);
  191. }
  192. return ret;
  193. }
  194. INIT_BOARD_EXPORT(rt_hw_spi_init);
  195. #endif /*BSP_USING_SPI*/