drv_soft_i2c.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. #ifdef RT_USING_I2C
  14. //#define DRV_DEBUG
  15. #define LOG_TAG "drv.i2c"
  16. #include <drv_log.h>
  17. #if !defined(BSP_USING_I2C1) && !defined(BSP_USING_I2C2) && !defined(BSP_USING_I2C3) && !defined(BSP_USING_I2C4)
  18. #error "Please define at least one BSP_USING_I2Cx"
  19. /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
  20. #endif
  21. static const struct stm32_soft_i2c_config soft_i2c_config[] =
  22. {
  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. #ifdef BSP_USING_I2C4
  33. I2C4_BUS_CONFIG,
  34. #endif
  35. };
  36. static struct stm32_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
  37. /**
  38. * This function initializes the i2c pin.
  39. *
  40. * @param Stm32 i2c dirver class.
  41. */
  42. static void stm32_i2c_gpio_init(struct stm32_i2c *i2c)
  43. {
  44. struct stm32_soft_i2c_config* cfg = (struct stm32_soft_i2c_config*)i2c->ops.data;
  45. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
  46. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  47. rt_pin_write(cfg->scl, PIN_HIGH);
  48. rt_pin_write(cfg->sda, PIN_HIGH);
  49. }
  50. /**
  51. * This function sets the sda pin.
  52. *
  53. * @param Stm32 config class.
  54. * @param The sda pin state.
  55. */
  56. static void stm32_set_sda(void *data, rt_int32_t state)
  57. {
  58. struct stm32_soft_i2c_config* cfg = (struct stm32_soft_i2c_config*)data;
  59. if (state)
  60. {
  61. rt_pin_write(cfg->sda, PIN_HIGH);
  62. }
  63. else
  64. {
  65. rt_pin_write(cfg->sda, PIN_LOW);
  66. }
  67. }
  68. /**
  69. * This function sets the scl pin.
  70. *
  71. * @param Stm32 config class.
  72. * @param The scl pin state.
  73. */
  74. static void stm32_set_scl(void *data, rt_int32_t state)
  75. {
  76. struct stm32_soft_i2c_config* cfg = (struct stm32_soft_i2c_config*)data;
  77. if (state)
  78. {
  79. rt_pin_write(cfg->scl, PIN_HIGH);
  80. }
  81. else
  82. {
  83. rt_pin_write(cfg->scl, PIN_LOW);
  84. }
  85. }
  86. /**
  87. * This function gets the sda pin state.
  88. *
  89. * @param The sda pin state.
  90. */
  91. static rt_int32_t stm32_get_sda(void *data)
  92. {
  93. struct stm32_soft_i2c_config* cfg = (struct stm32_soft_i2c_config*)data;
  94. return rt_pin_read(cfg->sda);
  95. }
  96. /**
  97. * This function gets the scl pin state.
  98. *
  99. * @param The scl pin state.
  100. */
  101. static rt_int32_t stm32_get_scl(void *data)
  102. {
  103. struct stm32_soft_i2c_config* cfg = (struct stm32_soft_i2c_config*)data;
  104. return rt_pin_read(cfg->scl);
  105. }
  106. static const struct rt_i2c_bit_ops stm32_bit_ops_default =
  107. {
  108. .data = RT_NULL,
  109. .set_sda = stm32_set_sda,
  110. .set_scl = stm32_set_scl,
  111. .get_sda = stm32_get_sda,
  112. .get_scl = stm32_get_scl,
  113. .udelay = rt_hw_us_delay,
  114. .delay_us = 1,
  115. .timeout = 100
  116. };
  117. /**
  118. * if i2c is locked, this function will unlock it
  119. *
  120. * @param stm32 config class
  121. *
  122. * @return RT_EOK indicates successful unlock.
  123. */
  124. static rt_err_t stm32_i2c_bus_unlock(const struct stm32_soft_i2c_config *cfg)
  125. {
  126. rt_int32_t i = 0;
  127. if (PIN_LOW == rt_pin_read(cfg->sda))
  128. {
  129. while (i++ < 9)
  130. {
  131. rt_pin_write(cfg->scl, PIN_HIGH);
  132. rt_hw_us_delay(100);
  133. rt_pin_write(cfg->scl, PIN_LOW);
  134. rt_hw_us_delay(100);
  135. }
  136. }
  137. if (PIN_LOW == rt_pin_read(cfg->sda))
  138. {
  139. return -RT_ERROR;
  140. }
  141. return RT_EOK;
  142. }
  143. /* I2C initialization function */
  144. int rt_hw_i2c_init(void)
  145. {
  146. rt_err_t result;
  147. for (rt_size_t i = 0; i < sizeof(i2c_obj) / sizeof(struct stm32_i2c); i++)
  148. {
  149. i2c_obj[i].ops = stm32_bit_ops_default;
  150. i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
  151. i2c_obj[i].i2c2_bus.priv = &i2c_obj[i].ops;
  152. stm32_i2c_gpio_init(&i2c_obj[i]);
  153. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c2_bus, soft_i2c_config[i].bus_name);
  154. RT_ASSERT(result == RT_EOK);
  155. stm32_i2c_bus_unlock(&soft_i2c_config[i]);
  156. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  157. soft_i2c_config[i].bus_name,
  158. soft_i2c_config[i].scl,
  159. soft_i2c_config[i].sda);
  160. }
  161. return RT_EOK;
  162. }
  163. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  164. #endif /* RT_USING_I2C */