drv_soft_i2c.c 4.8 KB

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