drv_soft_i2c.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. * 2021-08-15 Jonas 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 hk32_soft_i2c_config soft_i2c_config[] =
  20. {
  21. #ifdef BSP_USING_I2C1
  22. I2C1_BUS_CONFIG,
  23. #endif
  24. #ifdef BSP_USING_I2C2
  25. I2C2_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 hk32_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
  35. /**
  36. * This function initializes the i2c pin.
  37. *
  38. * @param hk32 i2c dirver class.
  39. */
  40. static void hk32_i2c_gpio_init(struct hk32_i2c *i2c)
  41. {
  42. struct hk32_soft_i2c_config *cfg = (struct hk32_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. static void hk32_i2c_pin_init(void)
  49. {
  50. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct hk32_i2c);
  51. for(rt_size_t i = 0; i < obj_num; i++)
  52. {
  53. hk32_i2c_gpio_init(&i2c_obj[i]);
  54. }
  55. }
  56. /**
  57. * This function sets the sda pin.
  58. *
  59. * @param hk32 config class.
  60. * @param The sda pin state.
  61. */
  62. static void hk32_set_sda(void *data, rt_int32_t state)
  63. {
  64. struct hk32_soft_i2c_config *cfg = (struct hk32_soft_i2c_config *)data;
  65. if (state)
  66. {
  67. rt_pin_write(cfg->sda, PIN_HIGH);
  68. }
  69. else
  70. {
  71. rt_pin_write(cfg->sda, PIN_LOW);
  72. }
  73. }
  74. /**
  75. * This function sets the scl pin.
  76. *
  77. * @param hk32 config class.
  78. * @param The scl pin state.
  79. */
  80. static void hk32_set_scl(void *data, rt_int32_t state)
  81. {
  82. struct hk32_soft_i2c_config *cfg = (struct hk32_soft_i2c_config *)data;
  83. if (state)
  84. {
  85. rt_pin_write(cfg->scl, PIN_HIGH);
  86. }
  87. else
  88. {
  89. rt_pin_write(cfg->scl, PIN_LOW);
  90. }
  91. }
  92. /**
  93. * This function gets the sda pin state.
  94. *
  95. * @param The sda pin state.
  96. */
  97. static rt_int32_t hk32_get_sda(void *data)
  98. {
  99. struct hk32_soft_i2c_config *cfg = (struct hk32_soft_i2c_config *)data;
  100. return rt_pin_read(cfg->sda);
  101. }
  102. /**
  103. * This function gets the scl pin state.
  104. *
  105. * @param The scl pin state.
  106. */
  107. static rt_int32_t hk32_get_scl(void *data)
  108. {
  109. struct hk32_soft_i2c_config *cfg = (struct hk32_soft_i2c_config *)data;
  110. return rt_pin_read(cfg->scl);
  111. }
  112. /**
  113. * The time delay function.
  114. *
  115. * @param microseconds.
  116. */
  117. static void hk32_udelay(rt_uint32_t us)
  118. {
  119. rt_uint32_t ticks;
  120. rt_uint32_t told, tnow, tcnt = 0;
  121. rt_uint32_t reload = SysTick->LOAD;
  122. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  123. told = SysTick->VAL;
  124. while (1)
  125. {
  126. tnow = SysTick->VAL;
  127. if (tnow != told)
  128. {
  129. if (tnow < told)
  130. {
  131. tcnt += told - tnow;
  132. }
  133. else
  134. {
  135. tcnt += reload - tnow + told;
  136. }
  137. told = tnow;
  138. if (tcnt >= ticks)
  139. {
  140. break;
  141. }
  142. }
  143. }
  144. }
  145. static const struct rt_i2c_bit_ops hk32_bit_ops_default =
  146. {
  147. .data = RT_NULL,
  148. .pin_init = hk32_i2c_pin_init,
  149. .set_sda = hk32_set_sda,
  150. .set_scl = hk32_set_scl,
  151. .get_sda = hk32_get_sda,
  152. .get_scl = hk32_get_scl,
  153. .udelay = hk32_udelay,
  154. .delay_us = 1,
  155. .timeout = 100,
  156. .i2c_pin_init_flag = RT_FALSE
  157. };
  158. /**
  159. * if i2c is locked, this function will unlock it
  160. *
  161. * @param hk32 config class
  162. *
  163. * @return RT_EOK indicates successful unlock.
  164. */
  165. static rt_err_t hk32_i2c_bus_unlock(const struct hk32_soft_i2c_config *cfg)
  166. {
  167. rt_int32_t i = 0;
  168. if (PIN_LOW == rt_pin_read(cfg->sda))
  169. {
  170. while (i++ < 9)
  171. {
  172. rt_pin_write(cfg->scl, PIN_HIGH);
  173. hk32_udelay(100);
  174. rt_pin_write(cfg->scl, PIN_LOW);
  175. hk32_udelay(100);
  176. }
  177. }
  178. if (PIN_LOW == rt_pin_read(cfg->sda))
  179. {
  180. return -RT_ERROR;
  181. }
  182. return RT_EOK;
  183. }
  184. /* I2C initialization function */
  185. int rt_hw_i2c_init(void)
  186. {
  187. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct hk32_i2c);
  188. rt_err_t result;
  189. for (int i = 0; i < obj_num; i++)
  190. {
  191. i2c_obj[i].ops = hk32_bit_ops_default;
  192. i2c_obj[i].ops.data = (void *)&soft_i2c_config[i];
  193. i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
  194. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
  195. RT_ASSERT(result == RT_EOK);
  196. hk32_i2c_bus_unlock(&soft_i2c_config[i]);
  197. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  198. soft_i2c_config[i].bus_name,
  199. soft_i2c_config[i].scl,
  200. soft_i2c_config[i].sda);
  201. }
  202. return RT_EOK;
  203. }
  204. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  205. #endif /* RT_USING_I2C */