drv_soft_i2c.c 4.8 KB

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