drv_spi.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-05-19 Sherman first version
  9. * 2023-01-28 Andrew add Nrf5340 support
  10. */
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #include <rthw.h>
  14. #ifndef __DRV_SPI_H_
  15. #define __DRV_SPI_H_
  16. #ifdef BSP_USING_SPI
  17. #if defined(SOC_NRF5340)
  18. #include "nrfx_spim.h"
  19. /**
  20. * @brief Attach the spi device to SPI bus, this function must be used after initialization.
  21. * @param bus_name spi bus name "spi0"/"spi1"/"spi2"
  22. * @param device_name spi device name "spi0x"/"spi1x"/"spi2x"
  23. * @param ss_pin spi ss pin number
  24. * @retval -RT_ERROR / RT_EOK
  25. */
  26. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t ss_pin);
  27. //SPI bus config
  28. #ifdef BSP_USING_SPI0
  29. #define NRFX_SPI0_CONFIG \
  30. { \
  31. .bus_name = "spi0", \
  32. .spi = NRFX_SPIM_INSTANCE(0) \
  33. }
  34. #endif
  35. #ifdef BSP_USING_SPI1
  36. #define NRFX_SPI1_CONFIG \
  37. { \
  38. .bus_name = "spi1", \
  39. .spi = NRFX_SPIM_INSTANCE(1) \
  40. }
  41. #endif
  42. #ifdef BSP_USING_SPI2
  43. #define NRFX_SPI2_CONFIG \
  44. { \
  45. .bus_name = "spi2", \
  46. .spi = NRFX_SPIM_INSTANCE(2) \
  47. }
  48. #endif
  49. #ifdef BSP_USING_SPI3
  50. #define NRFX_SPI3_CONFIG \
  51. { \
  52. .bus_name = "spi3", \
  53. .spi = NRFX_SPIM_INSTANCE(3) \
  54. }
  55. #endif
  56. #ifdef BSP_USING_SPI4
  57. #define NRFX_SPI4_CONFIG \
  58. { \
  59. .bus_name = "spi4", \
  60. .spi = NRFX_SPIM_INSTANCE(4) \
  61. }
  62. #endif
  63. struct nrfx_drv_spi_config
  64. {
  65. char *bus_name;
  66. nrfx_spim_t spi;
  67. };
  68. struct nrfx_drv_spi
  69. {
  70. nrfx_spim_t spi; /* nrfx spi driver instance. */
  71. nrfx_spim_config_t spi_config; /* nrfx spi config Configuration */
  72. struct rt_spi_configuration *cfg;
  73. struct rt_spi_bus spi_bus;
  74. };
  75. struct nrfx_drv_spi_pin_config
  76. {
  77. rt_uint8_t sck_pin;
  78. rt_uint8_t mosi_pin;
  79. rt_uint8_t miso_pin;
  80. rt_uint8_t ss_pin;
  81. };
  82. #else
  83. #include "nrfx_spi.h"
  84. /**
  85. * @brief Attach the spi device to SPI bus, this function must be used after initialization.
  86. * @param bus_name spi bus name "spi0"/"spi1"/"spi2"
  87. * @param device_name spi device name "spi0x"/"spi1x"/"spi2x"
  88. * @param ss_pin spi ss pin number
  89. * @retval -RT_ERROR / RT_EOK
  90. */
  91. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t ss_pin);
  92. //SPI bus config
  93. #ifdef BSP_USING_SPI0
  94. #define NRFX_SPI0_CONFIG \
  95. { \
  96. .bus_name = "spi0", \
  97. .spi = NRFX_SPI_INSTANCE(0) \
  98. }
  99. #endif
  100. #ifdef BSP_USING_SPI1
  101. #define NRFX_SPI1_CONFIG \
  102. { \
  103. .bus_name = "spi1", \
  104. .spi = NRFX_SPI_INSTANCE(1) \
  105. }
  106. #endif
  107. #ifdef BSP_USING_SPI2
  108. #define NRFX_SPI2_CONFIG \
  109. { \
  110. .bus_name = "spi2", \
  111. .spi = NRFX_SPI_INSTANCE(2) \
  112. }
  113. #endif
  114. struct nrfx_drv_spi_config
  115. {
  116. char *bus_name;
  117. nrfx_spi_t spi;
  118. };
  119. struct nrfx_drv_spi
  120. {
  121. nrfx_spi_t spi; /* nrfx spi driver instance. */
  122. nrfx_spi_config_t spi_config; /* nrfx spi config Configuration */
  123. struct rt_spi_configuration *cfg;
  124. struct rt_spi_bus spi_bus;
  125. };
  126. struct nrfx_drv_spi_pin_config
  127. {
  128. rt_uint8_t sck_pin;
  129. rt_uint8_t mosi_pin;
  130. rt_uint8_t miso_pin;
  131. rt_uint8_t ss_pin;
  132. };
  133. #endif
  134. #endif /* BSP_USING_SPI */
  135. #endif /*__DRV_SPI_H_*/