drv_soft_spi.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. * 2023-04-14 Wangyuqiang the first version
  9. */
  10. #include "board.h"
  11. #include "drv_soft_spi.h"
  12. #if defined BSP_USING_SOFT_SPI
  13. #define LOG_TAG "drv.soft_spi"
  14. #include <drv_log.h>
  15. static struct lpc_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 lpc_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_softspi_device_attach(const char *bus_name, const char *device_name, rt_base_t cs_pin)
  29. {
  30. rt_err_t result;
  31. struct rt_spi_device *spi_device;
  32. /* attach the device to soft spi bus*/
  33. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  34. RT_ASSERT(spi_device != RT_NULL);
  35. result = rt_spi_bus_attach_device_cspin(spi_device, device_name, bus_name, cs_pin, RT_NULL);
  36. return result;
  37. }
  38. static void lpc_spi_gpio_init(struct lpc_soft_spi *spi)
  39. {
  40. struct lpc_soft_spi_config *cfg = (struct lpc_soft_spi_config *)spi->cfg;
  41. rt_pin_mode(cfg->sck, PIN_MODE_OUTPUT);
  42. rt_pin_mode(cfg->miso, PIN_MODE_INPUT);
  43. rt_pin_mode(cfg->mosi, PIN_MODE_OUTPUT);
  44. rt_pin_write(cfg->miso, PIN_HIGH);
  45. rt_pin_write(cfg->sck, PIN_HIGH);
  46. rt_pin_write(cfg->mosi, PIN_HIGH);
  47. }
  48. static void lpc_tog_sclk(void *data)
  49. {
  50. struct lpc_soft_spi_config* cfg = (struct lpc_soft_spi_config*)data;
  51. if(rt_pin_read(cfg->sck) == PIN_HIGH)
  52. {
  53. rt_pin_write(cfg->sck, PIN_LOW);
  54. }
  55. else
  56. {
  57. rt_pin_write(cfg->sck, PIN_HIGH);
  58. }
  59. }
  60. static void lpc_set_sclk(void *data, rt_int32_t state)
  61. {
  62. struct lpc_soft_spi_config* cfg = (struct lpc_soft_spi_config*)data;
  63. if (state)
  64. {
  65. rt_pin_write(cfg->sck, PIN_HIGH);
  66. }
  67. else
  68. {
  69. rt_pin_write(cfg->sck, PIN_LOW);
  70. }
  71. }
  72. static void lpc_set_mosi(void *data, rt_int32_t state)
  73. {
  74. struct lpc_soft_spi_config* cfg = (struct lpc_soft_spi_config*)data;
  75. if (state)
  76. {
  77. rt_pin_write(cfg->mosi, PIN_HIGH);
  78. }
  79. else
  80. {
  81. rt_pin_write(cfg->mosi, PIN_LOW);
  82. }
  83. }
  84. static void lpc_set_miso(void *data, rt_int32_t state)
  85. {
  86. struct lpc_soft_spi_config* cfg = (struct lpc_soft_spi_config*)data;
  87. if (state)
  88. {
  89. rt_pin_write(cfg->miso, PIN_HIGH);
  90. }
  91. else
  92. {
  93. rt_pin_write(cfg->miso, PIN_LOW);
  94. }
  95. }
  96. static rt_int32_t lpc_get_sclk(void *data)
  97. {
  98. struct lpc_soft_spi_config* cfg = (struct lpc_soft_spi_config*)data;
  99. return rt_pin_read(cfg->sck);
  100. }
  101. static rt_int32_t lpc_get_mosi(void *data)
  102. {
  103. struct lpc_soft_spi_config* cfg = (struct lpc_soft_spi_config*)data;
  104. return rt_pin_read(cfg->mosi);
  105. }
  106. static rt_int32_t lpc_get_miso(void *data)
  107. {
  108. struct lpc_soft_spi_config* cfg = (struct lpc_soft_spi_config*)data;
  109. return rt_pin_read(cfg->miso);
  110. }
  111. static void lpc_dir_mosi(void *data, rt_int32_t state)
  112. {
  113. struct lpc_soft_spi_config* cfg = (struct lpc_soft_spi_config*)data;
  114. if (state)
  115. {
  116. rt_pin_mode(cfg->mosi, PIN_MODE_INPUT);
  117. }
  118. else
  119. {
  120. rt_pin_mode(cfg->mosi, PIN_MODE_OUTPUT);
  121. }
  122. }
  123. static void lpc_dir_miso(void *data, rt_int32_t state)
  124. {
  125. struct lpc_soft_spi_config* cfg = (struct lpc_soft_spi_config*)data;
  126. if (state)
  127. {
  128. rt_pin_mode(cfg->miso, PIN_MODE_INPUT);
  129. }
  130. else
  131. {
  132. rt_pin_mode(cfg->miso, PIN_MODE_OUTPUT);
  133. }
  134. }
  135. static void lpc_udelay(rt_uint32_t us)
  136. {
  137. rt_uint32_t ticks;
  138. rt_uint32_t told, tnow, tcnt = 0;
  139. rt_uint32_t reload = SysTick->LOAD;
  140. ticks = us * reload / (1000000UL / RT_TICK_PER_SECOND);
  141. told = SysTick->VAL;
  142. while (1)
  143. {
  144. tnow = SysTick->VAL;
  145. if (tnow != told)
  146. {
  147. if (tnow < told)
  148. {
  149. tcnt += told - tnow;
  150. }
  151. else
  152. {
  153. tcnt += reload - tnow + told;
  154. }
  155. told = tnow;
  156. if (tcnt >= ticks)
  157. {
  158. break;
  159. }
  160. }
  161. }
  162. }
  163. static void lpc_pin_init(void)
  164. {
  165. rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct lpc_soft_spi);
  166. for(rt_size_t i = 0; i < obj_num; i++)
  167. {
  168. lpc_spi_gpio_init(&spi_obj[i]);
  169. }
  170. }
  171. static struct rt_spi_bit_ops lpc_soft_spi_ops =
  172. {
  173. .data = RT_NULL,
  174. .pin_init = lpc_pin_init,
  175. .tog_sclk = lpc_tog_sclk,
  176. .set_sclk = lpc_set_sclk,
  177. .set_mosi = lpc_set_mosi,
  178. .set_miso = lpc_set_miso,
  179. .get_sclk = lpc_get_sclk,
  180. .get_mosi = lpc_get_mosi,
  181. .get_miso = lpc_get_miso,
  182. .dir_mosi = lpc_dir_mosi,
  183. .dir_miso = lpc_dir_miso,
  184. .udelay = lpc_udelay,
  185. .delay_us = 1,
  186. };
  187. /* Soft SPI initialization function */
  188. int rt_hw_softspi_init(void)
  189. {
  190. rt_size_t obj_num = sizeof(spi_obj) / sizeof(struct lpc_soft_spi);
  191. rt_err_t result;
  192. for (rt_size_t i = 0; i < obj_num; i++)
  193. {
  194. lpc_soft_spi_ops.data = (void *)&soft_spi_config[i];
  195. spi_obj[i].spi.ops = &lpc_soft_spi_ops;
  196. spi_obj[i].cfg = (void *)&soft_spi_config[i];
  197. result = rt_spi_bit_add_bus(&spi_obj[i].spi, soft_spi_config[i].bus_name, &lpc_soft_spi_ops);
  198. RT_ASSERT(result == RT_EOK);
  199. }
  200. return RT_EOK;
  201. }
  202. INIT_BOARD_EXPORT(rt_hw_softspi_init);
  203. #endif /* BSP_USING_SOFT_SPI */