drv_soft_i2c.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. /*
  44. * This function sets the sda pin.
  45. *
  46. * @param Ch32 config class.
  47. * @param The sda pin state.
  48. */
  49. static void ch32_set_sda(void *data, rt_int32_t state)
  50. {
  51. struct ch32_soft_i2c_config* cfg = (struct ch32_soft_i2c_config*)data;
  52. if (state)
  53. {
  54. rt_pin_write(cfg->sda, PIN_HIGH);
  55. }
  56. else
  57. {
  58. rt_pin_write(cfg->sda, PIN_LOW);
  59. }
  60. }
  61. /*
  62. * This function sets the scl pin.
  63. *
  64. * @param Ch32 config class.
  65. * @param The scl pin state.
  66. */
  67. static void ch32_set_scl(void *data, rt_int32_t state)
  68. {
  69. struct ch32_soft_i2c_config* cfg = (struct ch32_soft_i2c_config*)data;
  70. if (state)
  71. {
  72. rt_pin_write(cfg->scl, PIN_HIGH);
  73. }
  74. else
  75. {
  76. rt_pin_write(cfg->scl, PIN_LOW);
  77. }
  78. }
  79. /*
  80. * This function gets the sda pin state.
  81. *
  82. * @param The sda pin state.
  83. */
  84. static rt_int32_t ch32_get_sda(void *data)
  85. {
  86. struct ch32_soft_i2c_config* cfg = (struct ch32_soft_i2c_config*)data;
  87. return rt_pin_read(cfg->sda);
  88. }
  89. /*
  90. * This function gets the scl pin state.
  91. *
  92. * @param The scl pin state.
  93. */
  94. static rt_int32_t ch32_get_scl(void *data)
  95. {
  96. struct ch32_soft_i2c_config* cfg = (struct ch32_soft_i2c_config*)data;
  97. return rt_pin_read(cfg->scl);
  98. }
  99. /*
  100. * The time delay function.
  101. *
  102. * @param microseconds.
  103. */
  104. static void ch32_udelay(rt_uint32_t us)
  105. {
  106. rt_uint32_t ticks;
  107. rt_uint32_t told, tnow, tcnt = 0;
  108. rt_uint32_t reload = SysTick->CMP;
  109. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  110. told = SysTick->CNT;
  111. while (1)
  112. {
  113. tnow = SysTick->CNT;
  114. if (tnow != told)
  115. {
  116. if (tnow > told)
  117. {
  118. tcnt += tnow - told;
  119. }
  120. else
  121. {
  122. tcnt += reload - told + tnow;
  123. }
  124. told = tnow;
  125. if (tcnt >= ticks)
  126. {
  127. break;
  128. }
  129. }
  130. }
  131. }
  132. static const struct rt_i2c_bit_ops ch32_bit_ops_default =
  133. {
  134. .data = RT_NULL,
  135. .set_sda = ch32_set_sda,
  136. .set_scl = ch32_set_scl,
  137. .get_sda = ch32_get_sda,
  138. .get_scl = ch32_get_scl,
  139. .udelay = ch32_udelay,
  140. .delay_us = 1,
  141. .timeout = 100
  142. };
  143. /*
  144. * if i2c is locked, this function will unlock it
  145. *
  146. * @param ch32 config class
  147. *
  148. * @return RT_EOK indicates successful unlock.
  149. */
  150. static rt_err_t ch32_i2c_bus_unlock(const struct ch32_soft_i2c_config *cfg)
  151. {
  152. rt_int32_t i = 0;
  153. if (PIN_LOW == rt_pin_read(cfg->sda))
  154. {
  155. while (i++ < 9)
  156. {
  157. rt_pin_write(cfg->scl, PIN_HIGH);
  158. ch32_udelay(100);
  159. rt_pin_write(cfg->scl, PIN_LOW);
  160. ch32_udelay(100);
  161. }
  162. }
  163. if (PIN_LOW == rt_pin_read(cfg->sda))
  164. {
  165. return -RT_ERROR;
  166. }
  167. return RT_EOK;
  168. }
  169. /* I2C initialization function */
  170. int rt_hw_i2c_init(void)
  171. {
  172. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ch32_i2c);
  173. rt_err_t result;
  174. for (int i = 0; i < obj_num; i++)
  175. {
  176. i2c_obj[i].ops = ch32_bit_ops_default;
  177. i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
  178. i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
  179. ch32_i2c_gpio_init(&i2c_obj[i]);
  180. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
  181. RT_ASSERT(result == RT_EOK);
  182. ch32_i2c_bus_unlock(&soft_i2c_config[i]);
  183. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  184. soft_i2c_config[i].bus_name,
  185. soft_i2c_config[i].scl,
  186. soft_i2c_config[i].sda);
  187. }
  188. return RT_EOK;
  189. }
  190. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  191. #endif /* RT_USING_I2C */