drv_soft_spi.c 5.6 KB

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