drv_soft_i2c.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-05-16 shelton first version
  9. */
  10. #include "drv_common.h"
  11. #include "drv_soft_i2c.h"
  12. #ifdef RT_USING_I2C
  13. #define LOG_TAG "drv.i2c"
  14. #include <drv_log.h>
  15. #if !defined(BSP_USING_I2C1) && !defined(BSP_USING_I2C2) && \
  16. !defined(BSP_USING_I2C3)
  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 at32_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. #ifdef BSP_USING_I2C3
  29. I2C3_BUS_CONFIG,
  30. #endif
  31. };
  32. static struct at32_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
  33. /**
  34. * this function initializes the i2c pin.
  35. *
  36. * @param at32 i2c dirver class.
  37. */
  38. static void at32_i2c_gpio_init(struct at32_i2c *i2c)
  39. {
  40. struct at32_soft_i2c_config* cfg = (struct at32_soft_i2c_config*)i2c->ops.data;
  41. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
  42. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  43. rt_pin_write(cfg->scl, PIN_HIGH);
  44. rt_pin_write(cfg->sda, PIN_HIGH);
  45. }
  46. static void at32_i2c_pin_init(void)
  47. {
  48. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct at32_i2c);
  49. for(rt_size_t i = 0; i < obj_num; i++)
  50. {
  51. at32_i2c_gpio_init(&i2c_obj[i]);
  52. }
  53. }
  54. /**
  55. * this function sets the sda pin.
  56. *
  57. * @param at32 config class.
  58. * @param the sda pin state.
  59. */
  60. static void at32_set_sda(void *data, rt_int32_t state)
  61. {
  62. struct at32_soft_i2c_config* cfg = (struct at32_soft_i2c_config*)data;
  63. if (state)
  64. {
  65. rt_pin_write(cfg->sda, PIN_HIGH);
  66. }
  67. else
  68. {
  69. rt_pin_write(cfg->sda, PIN_LOW);
  70. }
  71. }
  72. /**
  73. * this function sets the scl pin.
  74. *
  75. * @param at32 config class.
  76. * @param the scl pin state.
  77. */
  78. static void at32_set_scl(void *data, rt_int32_t state)
  79. {
  80. struct at32_soft_i2c_config* cfg = (struct at32_soft_i2c_config*)data;
  81. if (state)
  82. {
  83. rt_pin_write(cfg->scl, PIN_HIGH);
  84. }
  85. else
  86. {
  87. rt_pin_write(cfg->scl, PIN_LOW);
  88. }
  89. }
  90. /**
  91. * this function gets the sda pin state.
  92. *
  93. * @param the sda pin state.
  94. */
  95. static rt_int32_t at32_get_sda(void *data)
  96. {
  97. struct at32_soft_i2c_config* cfg = (struct at32_soft_i2c_config*)data;
  98. return rt_pin_read(cfg->sda);
  99. }
  100. /**
  101. * this function gets the scl pin state.
  102. *
  103. * @param the scl pin state.
  104. */
  105. static rt_int32_t at32_get_scl(void *data)
  106. {
  107. struct at32_soft_i2c_config* cfg = (struct at32_soft_i2c_config*)data;
  108. return rt_pin_read(cfg->scl);
  109. }
  110. /**
  111. * the time delay function.
  112. *
  113. * @param microseconds.
  114. */
  115. static void at32_udelay(rt_uint32_t us)
  116. {
  117. rt_uint32_t ticks;
  118. rt_uint32_t told, tnow, tcnt = 0;
  119. rt_uint32_t reload = SysTick->LOAD;
  120. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  121. told = SysTick->VAL;
  122. while (1)
  123. {
  124. tnow = SysTick->VAL;
  125. if (tnow != told)
  126. {
  127. if (tnow < told)
  128. {
  129. tcnt += told - tnow;
  130. }
  131. else
  132. {
  133. tcnt += reload - tnow + told;
  134. }
  135. told = tnow;
  136. if (tcnt >= ticks)
  137. {
  138. break;
  139. }
  140. }
  141. }
  142. }
  143. static const struct rt_i2c_bit_ops at32_bit_ops_default =
  144. {
  145. .data = RT_NULL,
  146. .pin_init = at32_i2c_pin_init,
  147. .set_sda = at32_set_sda,
  148. .set_scl = at32_set_scl,
  149. .get_sda = at32_get_sda,
  150. .get_scl = at32_get_scl,
  151. .udelay = at32_udelay,
  152. .delay_us = 1,
  153. .timeout = 100,
  154. .i2c_pin_init_flag = RT_FALSE
  155. };
  156. /**
  157. * if i2c is locked, this function will unlock it
  158. *
  159. * @param at32 config class
  160. *
  161. * @return RT_EOK indicates successful unlock.
  162. */
  163. static rt_err_t at32_i2c_bus_unlock(const struct at32_soft_i2c_config *cfg)
  164. {
  165. rt_int32_t i = 0;
  166. if (PIN_LOW == rt_pin_read(cfg->sda))
  167. {
  168. while (i++ < 9)
  169. {
  170. rt_pin_write(cfg->scl, PIN_HIGH);
  171. at32_udelay(100);
  172. rt_pin_write(cfg->scl, PIN_LOW);
  173. at32_udelay(100);
  174. }
  175. }
  176. if (PIN_LOW == rt_pin_read(cfg->sda))
  177. {
  178. return -RT_ERROR;
  179. }
  180. return RT_EOK;
  181. }
  182. /* i2c initialization function */
  183. int rt_hw_i2c_init(void)
  184. {
  185. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct at32_i2c);
  186. rt_err_t result;
  187. for (rt_size_t i = 0; i < obj_num; i++)
  188. {
  189. i2c_obj[i].ops = at32_bit_ops_default;
  190. i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
  191. i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
  192. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
  193. RT_ASSERT(result == RT_EOK);
  194. at32_i2c_bus_unlock(&soft_i2c_config[i]);
  195. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  196. soft_i2c_config[i].bus_name,
  197. soft_i2c_config[i].scl,
  198. soft_i2c_config[i].sda);
  199. }
  200. return RT_EOK;
  201. }
  202. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  203. #endif /* RT_USING_I2C */