drv_soft_i2c.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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-09-19 hg0720 the first version which add from wch
  9. */
  10. #include <board.h>
  11. #include "drv_soft_i2c.h"
  12. #ifdef BSP_USING_SOFT_I2C
  13. //#define DRV_DEBUG
  14. #define LOG_TAG "drv.i2c"
  15. #include <drv_log.h>
  16. #if !defined(BSP_USING_I2C1) && !defined(BSP_USING_I2C2)
  17. #error "Please define at least one BSP_USING_I2Cx"
  18. /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
  19. #endif
  20. static const struct ch32_soft_i2c_config soft_i2c_config[] =
  21. {
  22. #ifdef BSP_USING_I2C1
  23. I2C1_BUS_CONFIG,
  24. #endif
  25. #ifdef BSP_USING_I2C2
  26. I2C2_BUS_CONFIG,
  27. #endif
  28. };
  29. static struct ch32_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
  30. /*
  31. * This function initializes the i2c pin.
  32. *
  33. * @param ch32 i2c dirver class.
  34. */
  35. static void ch32_i2c_gpio_init(struct ch32_i2c *i2c)
  36. {
  37. struct ch32_soft_i2c_config* cfg = (struct ch32_soft_i2c_config*)i2c->ops.data;
  38. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
  39. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  40. rt_pin_write(cfg->scl, PIN_HIGH);
  41. rt_pin_write(cfg->sda, PIN_HIGH);
  42. }
  43. static void ch32_i2c_pin_init(void)
  44. {
  45. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ch32_i2c);
  46. for(rt_size_t i = 0; i < obj_num; i++)
  47. {
  48. ch32_i2c_gpio_init(&i2c_obj[i]);
  49. }
  50. }
  51. /*
  52. * This function sets the sda pin.
  53. *
  54. * @param Ch32 config class.
  55. * @param The sda pin state.
  56. */
  57. static void ch32_set_sda(void *data, rt_int32_t state)
  58. {
  59. struct ch32_soft_i2c_config* cfg = (struct ch32_soft_i2c_config*)data;
  60. if (state)
  61. {
  62. rt_pin_write(cfg->sda, PIN_HIGH);
  63. }
  64. else
  65. {
  66. rt_pin_write(cfg->sda, PIN_LOW);
  67. }
  68. }
  69. /*
  70. * This function sets the scl pin.
  71. *
  72. * @param Ch32 config class.
  73. * @param The scl pin state.
  74. */
  75. static void ch32_set_scl(void *data, rt_int32_t state)
  76. {
  77. struct ch32_soft_i2c_config* cfg = (struct ch32_soft_i2c_config*)data;
  78. if (state)
  79. {
  80. rt_pin_write(cfg->scl, PIN_HIGH);
  81. }
  82. else
  83. {
  84. rt_pin_write(cfg->scl, PIN_LOW);
  85. }
  86. }
  87. /*
  88. * This function gets the sda pin state.
  89. *
  90. * @param The sda pin state.
  91. */
  92. static rt_int32_t ch32_get_sda(void *data)
  93. {
  94. struct ch32_soft_i2c_config* cfg = (struct ch32_soft_i2c_config*)data;
  95. return rt_pin_read(cfg->sda);
  96. }
  97. /*
  98. * This function gets the scl pin state.
  99. *
  100. * @param The scl pin state.
  101. */
  102. static rt_int32_t ch32_get_scl(void *data)
  103. {
  104. struct ch32_soft_i2c_config* cfg = (struct ch32_soft_i2c_config*)data;
  105. return rt_pin_read(cfg->scl);
  106. }
  107. /*
  108. * The time delay function.
  109. *
  110. * @param microseconds.
  111. */
  112. static void ch32_udelay(rt_uint32_t us)
  113. {
  114. rt_uint32_t ticks;
  115. rt_uint32_t told, tnow, tcnt = 0;
  116. rt_uint32_t reload = SysTick->CMP;
  117. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  118. told = SysTick->CNT;
  119. while (1)
  120. {
  121. tnow = SysTick->CNT;
  122. if (tnow != told)
  123. {
  124. if (tnow > told)
  125. {
  126. tcnt += tnow - told;
  127. }
  128. else
  129. {
  130. tcnt += reload - told + tnow;
  131. }
  132. told = tnow;
  133. if (tcnt >= ticks)
  134. {
  135. break;
  136. }
  137. }
  138. }
  139. }
  140. static const struct rt_i2c_bit_ops ch32_bit_ops_default =
  141. {
  142. .data = RT_NULL,
  143. .pin_init = ch32_i2c_pin_init,
  144. .set_sda = ch32_set_sda,
  145. .set_scl = ch32_set_scl,
  146. .get_sda = ch32_get_sda,
  147. .get_scl = ch32_get_scl,
  148. .udelay = ch32_udelay,
  149. .delay_us = 1,
  150. .timeout = 100,
  151. .i2c_pin_init_flag = RT_FALSE
  152. };
  153. /*
  154. * if i2c is locked, this function will unlock it
  155. *
  156. * @param ch32 config class
  157. *
  158. * @return RT_EOK indicates successful unlock.
  159. */
  160. static rt_err_t ch32_i2c_bus_unlock(const struct ch32_soft_i2c_config *cfg)
  161. {
  162. rt_int32_t i = 0;
  163. if (PIN_LOW == rt_pin_read(cfg->sda))
  164. {
  165. while (i++ < 9)
  166. {
  167. rt_pin_write(cfg->scl, PIN_HIGH);
  168. ch32_udelay(100);
  169. rt_pin_write(cfg->scl, PIN_LOW);
  170. ch32_udelay(100);
  171. }
  172. }
  173. if (PIN_LOW == rt_pin_read(cfg->sda))
  174. {
  175. return -RT_ERROR;
  176. }
  177. return RT_EOK;
  178. }
  179. /* I2C initialization function */
  180. int rt_hw_i2c_init(void)
  181. {
  182. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ch32_i2c);
  183. rt_err_t result;
  184. for (rt_size_t i = 0; i < obj_num; i++)
  185. {
  186. i2c_obj[i].ops = ch32_bit_ops_default;
  187. i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
  188. i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
  189. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
  190. RT_ASSERT(result == RT_EOK);
  191. ch32_i2c_bus_unlock(&soft_i2c_config[i]);
  192. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  193. soft_i2c_config[i].bus_name,
  194. soft_i2c_config[i].scl,
  195. soft_i2c_config[i].sda);
  196. }
  197. return RT_EOK;
  198. }
  199. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  200. #endif /* RT_USING_I2C */