drv_soft_i2c.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-06-04 BruceOu the first version
  9. */
  10. #include "drv_soft_i2c.h"
  11. #ifdef RT_USING_I2C
  12. #define LOG_TAG "drv.i2c"
  13. #include <rtdbg.h>
  14. #if !defined(BSP_USING_I2C0) && !defined(BSP_USING_I2C1) && !defined(BSP_USING_I2C2) && !defined(BSP_USING_I2C3)
  15. #error "Please define at least one BSP_USING_I2Cx"
  16. /* this driver can be disabled at menuconfig → RT-Thread Components → Device Drivers */
  17. #endif
  18. static const struct gd32_soft_i2c_config soft_i2c_config[] =
  19. {
  20. #ifdef BSP_USING_I2C0
  21. I2C0_BUS_CONFIG,
  22. #endif
  23. #ifdef BSP_USING_I2C1
  24. I2C1_BUS_CONFIG,
  25. #endif
  26. #ifdef BSP_USING_I2C2
  27. I2C2_BUS_CONFIG,
  28. #endif
  29. #ifdef BSP_USING_I2C3
  30. I2C3_BUS_CONFIG,
  31. #endif
  32. };
  33. static struct gd32_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
  34. /**
  35. * @brief This function initializes the i2c pin.
  36. * @param i2c
  37. * @retval None
  38. */
  39. static void gd32_i2c_gpio_init(struct gd32_i2c *i2c)
  40. {
  41. struct gd32_soft_i2c_config* cfg = (struct gd32_soft_i2c_config*)i2c->ops.data;
  42. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
  43. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  44. rt_pin_write(cfg->scl, PIN_HIGH);
  45. rt_pin_write(cfg->sda, PIN_HIGH);
  46. }
  47. static void gd32_i2c_pin_init(void)
  48. {
  49. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct gd32_i2c);
  50. for(rt_size_t i = 0; i < obj_num; i++)
  51. {
  52. gd32_i2c_gpio_init(&i2c_obj[i]);
  53. }
  54. }
  55. /**
  56. * @brief This function sets the sda pin.
  57. * @param data, state
  58. * @retval None
  59. */
  60. static void gd32_set_sda(void *data, rt_int32_t state)
  61. {
  62. struct gd32_soft_i2c_config* cfg = (struct gd32_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. * @brief This function sets the scl pin.
  74. * @param data, state
  75. * @retval None
  76. */
  77. static void gd32_set_scl(void *data, rt_int32_t state)
  78. {
  79. struct gd32_soft_i2c_config* cfg = (struct gd32_soft_i2c_config*)data;
  80. if (state)
  81. {
  82. rt_pin_write(cfg->scl, PIN_HIGH);
  83. }
  84. else
  85. {
  86. rt_pin_write(cfg->scl, PIN_LOW);
  87. }
  88. }
  89. /**
  90. * @brief This function gets the sda pin state.
  91. * @param data
  92. * @retval None
  93. */
  94. static rt_int32_t gd32_get_sda(void *data)
  95. {
  96. struct gd32_soft_i2c_config* cfg = (struct gd32_soft_i2c_config*)data;
  97. return rt_pin_read(cfg->sda);
  98. }
  99. /**
  100. * @brief This function gets the scl pin state.
  101. * @param data
  102. * @retval None
  103. */
  104. static rt_int32_t gd32_get_scl(void *data)
  105. {
  106. struct gd32_soft_i2c_config* cfg = (struct gd32_soft_i2c_config*)data;
  107. return rt_pin_read(cfg->scl);
  108. }
  109. /**
  110. * @brief The time delay function.
  111. * @param us
  112. * @retval None
  113. */
  114. static void gd32_udelay(rt_uint32_t us)
  115. {
  116. int i = ( rcu_clock_freq_get(CK_SYS) / 4000000 * us);
  117. while(i)
  118. {
  119. i--;
  120. }
  121. }
  122. static const struct rt_i2c_bit_ops gd32_bit_ops_default =
  123. {
  124. .data = RT_NULL,
  125. .pin_init = gd32_i2c_pin_init,
  126. .set_sda = gd32_set_sda,
  127. .set_scl = gd32_set_scl,
  128. .get_sda = gd32_get_sda,
  129. .get_scl = gd32_get_scl,
  130. .udelay = gd32_udelay,
  131. .delay_us = 1,
  132. .timeout = 100,
  133. .i2c_pin_init_flag = RT_FALSE
  134. };
  135. /**
  136. * @brief if i2c is locked, this function will unlock it
  137. * @param cfg
  138. * @retval RT_EOK indicates successful unlock.
  139. */
  140. static rt_err_t gd32_i2c_bus_unlock(const struct gd32_soft_i2c_config *cfg)
  141. {
  142. rt_int32_t i = 0;
  143. if (PIN_LOW == rt_pin_read(cfg->sda))
  144. {
  145. while (i++ < 9)
  146. {
  147. rt_pin_write(cfg->scl, PIN_HIGH);
  148. gd32_udelay(100);
  149. rt_pin_write(cfg->scl, PIN_LOW);
  150. gd32_udelay(100);
  151. }
  152. }
  153. if (PIN_LOW == rt_pin_read(cfg->sda))
  154. {
  155. return -RT_ERROR;
  156. }
  157. return RT_EOK;
  158. }
  159. /**
  160. * @brief I2C initialization function
  161. * @param None
  162. * @retval RT_EOK indicates successful initialization.
  163. */
  164. int rt_hw_i2c_init(void)
  165. {
  166. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct gd32_i2c);
  167. rt_err_t result;
  168. for (rt_size_t = 0; i < obj_num; i++)
  169. {
  170. i2c_obj[i].ops = gd32_bit_ops_default;
  171. i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
  172. i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
  173. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
  174. RT_ASSERT(result == RT_EOK);
  175. gd32_i2c_bus_unlock(&soft_i2c_config[i]);
  176. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  177. soft_i2c_config[i].bus_name,
  178. soft_i2c_config[i].scl,
  179. soft_i2c_config[i].sda);
  180. }
  181. return RT_EOK;
  182. }
  183. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  184. #endif /* RT_USING_I2C */