drv_soft_spi.c 5.7 KB

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