drv_soft_i2c.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-05-29 QT-one first version
  9. */
  10. #include <rtdbg.h>
  11. #include "drv_soft_i2c.h"
  12. #ifdef RT_USING_I2C
  13. #if !defined(BSP_USING_I2C0_SW) && !defined(BSP_USING_I2C1_SW) && !defined(BSP_USING_I2C2_SW)
  14. #error "Please define at least one BSP_USING_I2Cx_SW"
  15. /* this driver can be disabled at menuconfig RT-Thread Components Device Drivers */
  16. #endif
  17. /* ht32 software i2c config class */
  18. struct ht32_soft_i2c_config
  19. {
  20. rt_uint8_t scl;
  21. rt_uint8_t sda;
  22. const char *bus_name;
  23. };
  24. /* ht32 software i2c dirver class */
  25. struct ht32_soft_i2c
  26. {
  27. struct rt_i2c_bit_ops ops;
  28. struct rt_i2c_bus_device i2c_bus;
  29. };
  30. static rt_uint8_t scl_rw_flag = 0;
  31. static rt_uint8_t sda_rw_flag = 0;
  32. static const struct ht32_soft_i2c_config soft_i2c_config[] =
  33. {
  34. #ifdef BSP_USING_I2C0_SW
  35. {
  36. .scl = BSP_I2C0_SLC_PIN,
  37. .sda = BSP_I2C0_SDA_PIN,
  38. .bus_name = BSP_USING_I2C0_SW_NAME,
  39. },
  40. #endif
  41. #ifdef BSP_USING_I2C1_SW
  42. {
  43. .scl = BSP_I2C1_SLC_PIN,
  44. .sda = BSP_I2C1_SDA_PIN,
  45. .bus_name = BSP_USING_I2C1_SW_NAME,
  46. },
  47. #endif
  48. #ifdef BSP_USING_I2C2_SW
  49. {
  50. .scl = BSP_I2C2_SLC_PIN,
  51. .sda = BSP_I2C2_SDA_PIN,
  52. .bus_name = BSP_USING_I2C2_SW_NAME,
  53. },
  54. #endif
  55. };
  56. static struct ht32_soft_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
  57. /* this function initializes the software i2c pin */
  58. static void ht32_soft_i2c_gpio_init(struct ht32_soft_i2c *i2c)
  59. {
  60. struct ht32_soft_i2c_config* cfg = (struct ht32_soft_i2c_config*)i2c->ops.data;
  61. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
  62. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  63. rt_pin_write(cfg->scl, PIN_HIGH);
  64. rt_pin_write(cfg->sda, PIN_HIGH);
  65. }
  66. /* this function sets the sda pin */
  67. void ht32_set_sda(void *data, rt_int32_t state)
  68. {
  69. struct ht32_soft_i2c_config* cfg = (struct ht32_soft_i2c_config*)data;
  70. if(sda_rw_flag != 0)
  71. {
  72. sda_rw_flag = 0;
  73. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  74. }
  75. if (state)
  76. {
  77. rt_pin_write(cfg->sda, PIN_HIGH);
  78. }
  79. else
  80. {
  81. rt_pin_write(cfg->sda, PIN_LOW);
  82. }
  83. }
  84. /* this function sets the scl pin */
  85. void ht32_set_scl(void *data, rt_int32_t state)
  86. {
  87. struct ht32_soft_i2c_config* cfg = (struct ht32_soft_i2c_config*)data;
  88. if(scl_rw_flag != 0)
  89. {
  90. scl_rw_flag = 0;
  91. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
  92. }
  93. if (state)
  94. {
  95. rt_pin_write(cfg->scl, PIN_HIGH);
  96. }
  97. else
  98. {
  99. rt_pin_write(cfg->scl, PIN_LOW);
  100. }
  101. }
  102. /* this function gets the sda pin state */
  103. rt_int32_t ht32_get_sda(void *data)
  104. {
  105. struct ht32_soft_i2c_config* cfg = (struct ht32_soft_i2c_config*)data;
  106. if(sda_rw_flag == 0)
  107. {
  108. sda_rw_flag = 1;
  109. rt_pin_mode(cfg->sda, PIN_MODE_INPUT);
  110. }
  111. return rt_pin_read(cfg->sda);
  112. }
  113. /* this function gets the scl pin state */
  114. rt_int32_t ht32_get_scl(void *data)
  115. {
  116. struct ht32_soft_i2c_config* cfg = (struct ht32_soft_i2c_config*)data;
  117. if(scl_rw_flag == 0)
  118. {
  119. scl_rw_flag = 1;
  120. rt_pin_mode(cfg->scl, PIN_MODE_INPUT);
  121. }
  122. return rt_pin_read(cfg->scl);
  123. }
  124. void ht32_udelay(rt_uint32_t us)
  125. {
  126. rt_uint32_t ticks;
  127. rt_uint32_t told, tnow, tcnt = 0;
  128. rt_uint32_t reload = SysTick->LOAD;
  129. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  130. told = SysTick->VAL;
  131. while (1)
  132. {
  133. tnow = SysTick->VAL;
  134. if (tnow != told)
  135. {
  136. if (tnow < told)
  137. {
  138. tcnt += told - tnow;
  139. }
  140. else
  141. {
  142. tcnt += reload - tnow + told;
  143. }
  144. told = tnow;
  145. if (tcnt >= ticks)
  146. {
  147. break;
  148. }
  149. }
  150. }
  151. }
  152. static const struct rt_i2c_bit_ops ht32_bit_ops_default =
  153. {
  154. .data = RT_NULL,
  155. .set_sda = ht32_set_sda,
  156. .set_scl = ht32_set_scl,
  157. .get_sda = ht32_get_sda,
  158. .get_scl = ht32_get_scl,
  159. .udelay = ht32_udelay,
  160. .delay_us = 1,
  161. .timeout = 100
  162. };
  163. /* if i2c is locked, this function will unlock it */
  164. static rt_err_t ht32_soft_i2c_bus_unlock(const struct ht32_soft_i2c_config *cfg)
  165. {
  166. rt_int32_t i = 0;
  167. rt_pin_mode(cfg->sda, PIN_MODE_INPUT_PULLUP);
  168. if (PIN_LOW == rt_pin_read(cfg->sda))
  169. {
  170. while (i++ < 9)
  171. {
  172. rt_pin_write(cfg->scl, PIN_HIGH);
  173. ht32_udelay(100);
  174. rt_pin_write(cfg->scl, PIN_LOW);
  175. ht32_udelay(100);
  176. }
  177. }
  178. if (PIN_LOW == rt_pin_read(cfg->sda))
  179. {
  180. return -RT_ERROR;
  181. }
  182. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  183. return RT_EOK;
  184. }
  185. /* i2c initialization function */
  186. int rt_sw_i2c_init(void)
  187. {
  188. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ht32_soft_i2c);
  189. rt_err_t result;
  190. for (int i = 0; i < obj_num; i++)
  191. {
  192. i2c_obj[i].ops = ht32_bit_ops_default;
  193. i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
  194. i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
  195. ht32_soft_i2c_gpio_init(&i2c_obj[i]);
  196. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
  197. RT_ASSERT(result == RT_EOK);
  198. ht32_soft_i2c_bus_unlock(&soft_i2c_config[i]);
  199. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  200. soft_i2c_config[i].bus_name,
  201. soft_i2c_config[i].scl,
  202. soft_i2c_config[i].sda);
  203. }
  204. return result;
  205. }
  206. INIT_BOARD_EXPORT(rt_sw_i2c_init);
  207. #endif /* RT_USING_I2C */