drv_soft_spi.c 5.2 KB

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