drv_soft_i2c.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-11-08 balanceTWK first version
  9. * 2023-06-27 Meco Man replace stm32_udelay as rt_hw_us_delay
  10. */
  11. #include "drv_soft_i2c.h"
  12. #include "drv_config.h"
  13. #if defined(BSP_USING_I2C1) || defined(BSP_USING_I2C2) || defined(BSP_USING_I2C3) || defined(BSP_USING_I2C4) || defined(BSP_USING_I2C5)
  14. //#define DRV_DEBUG
  15. #define LOG_TAG "drv.i2c.sw"
  16. #include <drv_log.h>
  17. static const struct stm32_soft_i2c_config soft_i2c_config[] =
  18. {
  19. #ifdef BSP_USING_I2C1
  20. I2C1_BUS_CONFIG,
  21. #endif
  22. #ifdef BSP_USING_I2C2
  23. I2C2_BUS_CONFIG,
  24. #endif
  25. #ifdef BSP_USING_I2C3
  26. I2C3_BUS_CONFIG,
  27. #endif
  28. #ifdef BSP_USING_I2C4
  29. I2C4_BUS_CONFIG,
  30. #endif
  31. #ifdef BSP_USING_I2C5
  32. I2C5_BUS_CONFIG,
  33. #endif
  34. };
  35. static struct stm32_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
  36. /**
  37. * This function initializes the i2c pin.
  38. *
  39. * @param Stm32 i2c dirver class.
  40. */
  41. static void stm32_i2c_gpio_init(struct stm32_i2c *i2c)
  42. {
  43. struct stm32_soft_i2c_config* cfg = (struct stm32_soft_i2c_config*)i2c->ops.data;
  44. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
  45. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  46. rt_pin_write(cfg->scl, PIN_HIGH);
  47. rt_pin_write(cfg->sda, PIN_HIGH);
  48. }
  49. static void stm32_i2c_pin_init(void)
  50. {
  51. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct stm32_i2c);
  52. for(rt_size_t i = 0; i < obj_num; i++)
  53. {
  54. stm32_i2c_gpio_init(&i2c_obj[i]);
  55. }
  56. }
  57. /**
  58. * This function sets the sda pin.
  59. *
  60. * @param Stm32 config class.
  61. * @param The sda pin state.
  62. */
  63. static void stm32_set_sda(void *data, rt_int32_t state)
  64. {
  65. struct stm32_soft_i2c_config* cfg = (struct stm32_soft_i2c_config*)data;
  66. if (state)
  67. {
  68. rt_pin_write(cfg->sda, PIN_HIGH);
  69. }
  70. else
  71. {
  72. rt_pin_write(cfg->sda, PIN_LOW);
  73. }
  74. }
  75. /**
  76. * This function sets the scl pin.
  77. *
  78. * @param Stm32 config class.
  79. * @param The scl pin state.
  80. */
  81. static void stm32_set_scl(void *data, rt_int32_t state)
  82. {
  83. struct stm32_soft_i2c_config* cfg = (struct stm32_soft_i2c_config*)data;
  84. if (state)
  85. {
  86. rt_pin_write(cfg->scl, PIN_HIGH);
  87. }
  88. else
  89. {
  90. rt_pin_write(cfg->scl, PIN_LOW);
  91. }
  92. }
  93. /**
  94. * This function gets the sda pin state.
  95. *
  96. * @param The sda pin state.
  97. */
  98. static rt_int32_t stm32_get_sda(void *data)
  99. {
  100. struct stm32_soft_i2c_config* cfg = (struct stm32_soft_i2c_config*)data;
  101. return rt_pin_read(cfg->sda);
  102. }
  103. /**
  104. * This function gets the scl pin state.
  105. *
  106. * @param The scl pin state.
  107. */
  108. static rt_int32_t stm32_get_scl(void *data)
  109. {
  110. struct stm32_soft_i2c_config* cfg = (struct stm32_soft_i2c_config*)data;
  111. return rt_pin_read(cfg->scl);
  112. }
  113. static const struct rt_i2c_bit_ops stm32_bit_ops_default =
  114. {
  115. .data = RT_NULL,
  116. .pin_init = stm32_i2c_pin_init,
  117. .set_sda = stm32_set_sda,
  118. .set_scl = stm32_set_scl,
  119. .get_sda = stm32_get_sda,
  120. .get_scl = stm32_get_scl,
  121. .udelay = rt_hw_us_delay,
  122. .delay_us = 1,
  123. .timeout = 100,
  124. .i2c_pin_init_flag = RT_FALSE
  125. };
  126. /**
  127. * if i2c is locked, this function will unlock it
  128. *
  129. * @param stm32 config class
  130. *
  131. * @return RT_EOK indicates successful unlock.
  132. */
  133. static rt_err_t stm32_i2c_bus_unlock(const struct stm32_soft_i2c_config *cfg)
  134. {
  135. rt_int32_t i = 0;
  136. if (PIN_LOW == rt_pin_read(cfg->sda))
  137. {
  138. while (i++ < 9)
  139. {
  140. rt_pin_write(cfg->scl, PIN_HIGH);
  141. rt_hw_us_delay(100);
  142. rt_pin_write(cfg->scl, PIN_LOW);
  143. rt_hw_us_delay(100);
  144. }
  145. }
  146. if (PIN_LOW == rt_pin_read(cfg->sda))
  147. {
  148. return -RT_ERROR;
  149. }
  150. return RT_EOK;
  151. }
  152. /* I2C initialization function */
  153. int rt_hw_i2c_init(void)
  154. {
  155. rt_err_t result;
  156. for (rt_size_t i = 0; i < sizeof(i2c_obj) / sizeof(struct stm32_i2c); i++)
  157. {
  158. i2c_obj[i].ops = stm32_bit_ops_default;
  159. i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
  160. i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
  161. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
  162. RT_ASSERT(result == RT_EOK);
  163. stm32_i2c_bus_unlock(&soft_i2c_config[i]);
  164. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  165. soft_i2c_config[i].bus_name,
  166. soft_i2c_config[i].scl,
  167. soft_i2c_config[i].sda);
  168. }
  169. return RT_EOK;
  170. }
  171. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  172. #endif /* defined(BSP_USING_I2C1) || defined(BSP_USING_I2C2) || defined(BSP_USING_I2C3) || defined(BSP_USING_I2C4) */