drv_soft_i2c.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. /**
  48. * @brief This function sets the sda pin.
  49. * @param data, state
  50. * @retval None
  51. */
  52. static void gd32_set_sda(void *data, rt_int32_t state)
  53. {
  54. struct gd32_soft_i2c_config* cfg = (struct gd32_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. * @brief This function sets the scl pin.
  66. * @param data, state
  67. * @retval None
  68. */
  69. static void gd32_set_scl(void *data, rt_int32_t state)
  70. {
  71. struct gd32_soft_i2c_config* cfg = (struct gd32_soft_i2c_config*)data;
  72. if (state)
  73. {
  74. rt_pin_write(cfg->scl, PIN_HIGH);
  75. }
  76. else
  77. {
  78. rt_pin_write(cfg->scl, PIN_LOW);
  79. }
  80. }
  81. /**
  82. * @brief This function gets the sda pin state.
  83. * @param data
  84. * @retval None
  85. */
  86. static rt_int32_t gd32_get_sda(void *data)
  87. {
  88. struct gd32_soft_i2c_config* cfg = (struct gd32_soft_i2c_config*)data;
  89. return rt_pin_read(cfg->sda);
  90. }
  91. /**
  92. * @brief This function gets the scl pin state.
  93. * @param data
  94. * @retval None
  95. */
  96. static rt_int32_t gd32_get_scl(void *data)
  97. {
  98. struct gd32_soft_i2c_config* cfg = (struct gd32_soft_i2c_config*)data;
  99. return rt_pin_read(cfg->scl);
  100. }
  101. /**
  102. * @brief The time delay function.
  103. * @param us
  104. * @retval None
  105. */
  106. static void gd32_udelay(rt_uint32_t us)
  107. {
  108. int i = ( rcu_clock_freq_get(CK_SYS) / 4000000 * us);
  109. while(i)
  110. {
  111. i--;
  112. }
  113. }
  114. static const struct rt_i2c_bit_ops gd32_bit_ops_default =
  115. {
  116. .data = RT_NULL,
  117. .set_sda = gd32_set_sda,
  118. .set_scl = gd32_set_scl,
  119. .get_sda = gd32_get_sda,
  120. .get_scl = gd32_get_scl,
  121. .udelay = gd32_udelay,
  122. .delay_us = 1,
  123. .timeout = 100
  124. };
  125. /**
  126. * @brief if i2c is locked, this function will unlock it
  127. * @param cfg
  128. * @retval RT_EOK indicates successful unlock.
  129. */
  130. static rt_err_t gd32_i2c_bus_unlock(const struct gd32_soft_i2c_config *cfg)
  131. {
  132. rt_int32_t i = 0;
  133. if (PIN_LOW == rt_pin_read(cfg->sda))
  134. {
  135. while (i++ < 9)
  136. {
  137. rt_pin_write(cfg->scl, PIN_HIGH);
  138. gd32_udelay(100);
  139. rt_pin_write(cfg->scl, PIN_LOW);
  140. gd32_udelay(100);
  141. }
  142. }
  143. if (PIN_LOW == rt_pin_read(cfg->sda))
  144. {
  145. return -RT_ERROR;
  146. }
  147. return RT_EOK;
  148. }
  149. /**
  150. * @brief I2C initialization function
  151. * @param None
  152. * @retval RT_EOK indicates successful initialization.
  153. */
  154. int rt_hw_i2c_init(void)
  155. {
  156. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct gd32_i2c);
  157. rt_err_t result;
  158. for (int i = 0; i < obj_num; i++)
  159. {
  160. i2c_obj[i].ops = gd32_bit_ops_default;
  161. i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
  162. i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
  163. gd32_i2c_gpio_init(&i2c_obj[i]);
  164. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
  165. RT_ASSERT(result == RT_EOK);
  166. gd32_i2c_bus_unlock(&soft_i2c_config[i]);
  167. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  168. soft_i2c_config[i].bus_name,
  169. soft_i2c_config[i].scl,
  170. soft_i2c_config[i].sda);
  171. }
  172. return RT_EOK;
  173. }
  174. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  175. #endif /* RT_USING_I2C */