drv_soft_spi.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. * 2022-6-14 solar first version
  9. */
  10. #include <board.h>
  11. #include "drv_soft_spi.h"
  12. #include "drv_config.h"
  13. #if defined(RT_USING_SPI) && defined(RT_USING_SPI_BITOPS) && defined(RT_USING_PIN)
  14. //#define DRV_DEBUG
  15. #define LOG_TAG "drv.soft_spi"
  16. #include <drv_log.h>
  17. static struct stm32_soft_spi_config soft_spi_config[] =
  18. {
  19. #ifdef BSP_USING_SOFT_SPI1
  20. SOFT_SPI1_BUS_CONFIG,
  21. #endif
  22. #ifdef BSP_USING_SOFT_SPI2
  23. SOFT_SPI2_BUS_CONFIG,
  24. #endif
  25. };
  26. /**
  27. * Attach the spi device to soft SPI bus, this function must be used after initialization.
  28. */
  29. rt_err_t rt_hw_softspi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin)
  30. {
  31. rt_err_t result;
  32. struct rt_spi_device *spi_device;
  33. /* initialize the cs pin && select the slave*/
  34. if(cs_pin != PIN_NONE)
  35. {
  36. rt_pin_mode(cs_pin,PIN_MODE_OUTPUT);
  37. rt_pin_write(cs_pin,PIN_HIGH);
  38. }
  39. /* attach the device to soft spi bus*/
  40. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  41. RT_ASSERT(spi_device != RT_NULL);
  42. spi_device->cs_pin = cs_pin;
  43. result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, RT_NULL);
  44. return result;
  45. }
  46. static void stm32_spi_gpio_init(struct stm32_soft_spi *spi)
  47. {
  48. struct stm32_soft_spi_config *cfg = (struct stm32_soft_spi_config *)spi->cfg;
  49. rt_pin_mode(cfg->sck, PIN_MODE_OUTPUT);
  50. rt_pin_mode(cfg->miso, PIN_MODE_INPUT);
  51. rt_pin_mode(cfg->mosi, PIN_MODE_OUTPUT);
  52. rt_pin_write(cfg->miso, PIN_HIGH);
  53. rt_pin_write(cfg->sck, PIN_HIGH);
  54. rt_pin_write(cfg->mosi, PIN_HIGH);
  55. }
  56. void stm32_tog_sclk(void *data)
  57. {
  58. struct stm32_soft_spi_config* cfg = (struct stm32_soft_spi_config*)data;
  59. if(rt_pin_read(cfg->sck) == PIN_HIGH)
  60. {
  61. rt_pin_write(cfg->sck, PIN_LOW);
  62. }
  63. else
  64. {
  65. rt_pin_write(cfg->sck, PIN_HIGH);
  66. }
  67. }
  68. void stm32_set_sclk(void *data, rt_int32_t state)
  69. {
  70. struct stm32_soft_spi_config* cfg = (struct stm32_soft_spi_config*)data;
  71. if (state)
  72. {
  73. rt_pin_write(cfg->sck, PIN_HIGH);
  74. }
  75. else
  76. {
  77. rt_pin_write(cfg->sck, PIN_LOW);
  78. }
  79. }
  80. void stm32_set_mosi(void *data, rt_int32_t state)
  81. {
  82. struct stm32_soft_spi_config* cfg = (struct stm32_soft_spi_config*)data;
  83. if (state)
  84. {
  85. rt_pin_write(cfg->mosi, PIN_HIGH);
  86. }
  87. else
  88. {
  89. rt_pin_write(cfg->mosi, PIN_LOW);
  90. }
  91. }
  92. void stm32_set_miso(void *data, rt_int32_t state)
  93. {
  94. struct stm32_soft_spi_config* cfg = (struct stm32_soft_spi_config*)data;
  95. if (state)
  96. {
  97. rt_pin_write(cfg->miso, PIN_HIGH);
  98. }
  99. else
  100. {
  101. rt_pin_write(cfg->miso, PIN_LOW);
  102. }
  103. }
  104. rt_int32_t stm32_get_sclk(void *data)
  105. {
  106. struct stm32_soft_spi_config* cfg = (struct stm32_soft_spi_config*)data;
  107. return rt_pin_read(cfg->sck);
  108. }
  109. rt_int32_t stm32_get_mosi(void *data)
  110. {
  111. struct stm32_soft_spi_config* cfg = (struct stm32_soft_spi_config*)data;
  112. return rt_pin_read(cfg->mosi);
  113. }
  114. rt_int32_t stm32_get_miso(void *data)
  115. {
  116. struct stm32_soft_spi_config* cfg = (struct stm32_soft_spi_config*)data;
  117. return rt_pin_read(cfg->miso);
  118. }
  119. void stm32_dir_mosi(void *data, rt_int32_t state)
  120. {
  121. struct stm32_soft_spi_config* cfg = (struct stm32_soft_spi_config*)data;
  122. if (state)
  123. {
  124. rt_pin_mode(cfg->mosi, PIN_MODE_INPUT);
  125. }
  126. else
  127. {
  128. rt_pin_mode(cfg->mosi, PIN_MODE_OUTPUT);
  129. }
  130. }
  131. void stm32_dir_miso(void *data, rt_int32_t state)
  132. {
  133. struct stm32_soft_spi_config* cfg = (struct stm32_soft_spi_config*)data;
  134. if (state)
  135. {
  136. rt_pin_mode(cfg->miso, PIN_MODE_INPUT);
  137. }
  138. else
  139. {
  140. rt_pin_mode(cfg->miso, PIN_MODE_OUTPUT);
  141. }
  142. }
  143. static void stm32_udelay(rt_uint32_t us)
  144. {
  145. rt_uint32_t ticks;
  146. rt_uint32_t told, tnow, tcnt = 0;
  147. rt_uint32_t reload = SysTick->LOAD;
  148. ticks = us * reload / (1000000UL / RT_TICK_PER_SECOND);
  149. told = SysTick->VAL;
  150. while (1)
  151. {
  152. tnow = SysTick->VAL;
  153. if (tnow != told)
  154. {
  155. if (tnow < told)
  156. {
  157. tcnt += told - tnow;
  158. }
  159. else
  160. {
  161. tcnt += reload - tnow + told;
  162. }
  163. told = tnow;
  164. if (tcnt >= ticks)
  165. {
  166. break;
  167. }
  168. }
  169. }
  170. }
  171. static struct rt_spi_bit_ops stm32_soft_spi_ops =
  172. {
  173. .data = RT_NULL,
  174. .tog_sclk = stm32_tog_sclk,
  175. .set_sclk = stm32_set_sclk,
  176. .set_mosi = stm32_set_mosi,
  177. .set_miso = stm32_set_miso,
  178. .get_sclk = stm32_get_sclk,
  179. .get_mosi = stm32_get_mosi,
  180. .get_miso = stm32_get_miso,
  181. .dir_mosi = stm32_dir_mosi,
  182. .dir_miso = stm32_dir_miso,
  183. .udelay = stm32_udelay,
  184. .delay_us = 1,
  185. };
  186. static struct stm32_soft_spi spi_obj[sizeof(soft_spi_config) / sizeof(soft_spi_config[0])];
  187. /* Soft SPI initialization function */
  188. int rt_hw_softspi_init(void)
  189. {
  190. rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct stm32_soft_spi);
  191. rt_err_t result;
  192. for (int i = 0; i < obj_num; i++)
  193. {
  194. stm32_soft_spi_ops.data = (void *)&soft_spi_config[i];
  195. spi_obj[i].spi.ops = &stm32_soft_spi_ops;
  196. spi_obj[i].cfg = (void *)&soft_spi_config[i];
  197. stm32_spi_gpio_init(&spi_obj[i]);
  198. result = rt_spi_bit_add_bus(&spi_obj[i].spi, soft_spi_config[i].bus_name, &stm32_soft_spi_ops);
  199. RT_ASSERT(result == RT_EOK);
  200. }
  201. return RT_EOK;
  202. }
  203. INIT_BOARD_EXPORT(rt_hw_softspi_init);
  204. #endif /* defined(RT_USING_SPI) && defined(RT_USING_SPI_BITOPS) && defined(RT_USING_PIN) */