drv_soft_i2c.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2006-2018, 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 change to new framework
  9. */
  10. #include <board.h>
  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)
  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. };
  33. static struct stm32_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
  34. /**
  35. * This function initializes the i2c pin.
  36. *
  37. * @param Stm32 i2c dirver class.
  38. */
  39. static void stm32_i2c_gpio_init(struct stm32_i2c *i2c)
  40. {
  41. struct stm32_soft_i2c_config* cfg = (struct stm32_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. * This function sets the sda pin.
  49. *
  50. * @param Stm32 config class.
  51. * @param The sda pin state.
  52. */
  53. static void stm32_set_sda(void *data, rt_int32_t state)
  54. {
  55. struct stm32_soft_i2c_config* cfg = (struct stm32_soft_i2c_config*)data;
  56. if (state)
  57. {
  58. rt_pin_write(cfg->sda, PIN_HIGH);
  59. }
  60. else
  61. {
  62. rt_pin_write(cfg->sda, PIN_LOW);
  63. }
  64. }
  65. /**
  66. * This function sets the scl pin.
  67. *
  68. * @param Stm32 config class.
  69. * @param The scl pin state.
  70. */
  71. static void stm32_set_scl(void *data, rt_int32_t state)
  72. {
  73. struct stm32_soft_i2c_config* cfg = (struct stm32_soft_i2c_config*)data;
  74. if (state)
  75. {
  76. rt_pin_write(cfg->scl, PIN_HIGH);
  77. }
  78. else
  79. {
  80. rt_pin_write(cfg->scl, PIN_LOW);
  81. }
  82. }
  83. /**
  84. * This function gets the sda pin state.
  85. *
  86. * @param The sda pin state.
  87. */
  88. static rt_int32_t stm32_get_sda(void *data)
  89. {
  90. struct stm32_soft_i2c_config* cfg = (struct stm32_soft_i2c_config*)data;
  91. return rt_pin_read(cfg->sda);
  92. }
  93. /**
  94. * This function gets the scl pin state.
  95. *
  96. * @param The scl pin state.
  97. */
  98. static rt_int32_t stm32_get_scl(void *data)
  99. {
  100. struct stm32_soft_i2c_config* cfg = (struct stm32_soft_i2c_config*)data;
  101. return rt_pin_read(cfg->scl);
  102. }
  103. /**
  104. * The time delay function.
  105. *
  106. * @param microseconds.
  107. */
  108. static void stm32_udelay(rt_uint32_t us)
  109. {
  110. rt_uint32_t ticks;
  111. rt_uint32_t told, tnow, tcnt = 0;
  112. rt_uint32_t reload = SysTick->LOAD;
  113. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  114. told = SysTick->VAL;
  115. while (1)
  116. {
  117. tnow = SysTick->VAL;
  118. if (tnow != told)
  119. {
  120. if (tnow < told)
  121. {
  122. tcnt += told - tnow;
  123. }
  124. else
  125. {
  126. tcnt += reload - tnow + told;
  127. }
  128. told = tnow;
  129. if (tcnt >= ticks)
  130. {
  131. break;
  132. }
  133. }
  134. }
  135. }
  136. static const struct rt_i2c_bit_ops stm32_bit_ops_default =
  137. {
  138. .data = RT_NULL,
  139. .set_sda = stm32_set_sda,
  140. .set_scl = stm32_set_scl,
  141. .get_sda = stm32_get_sda,
  142. .get_scl = stm32_get_scl,
  143. .udelay = stm32_udelay,
  144. .delay_us = 1,
  145. .timeout = 100
  146. };
  147. /**
  148. * if i2c is locked, this function will unlock it
  149. *
  150. * @param stm32 config class
  151. *
  152. * @return RT_EOK indicates successful unlock.
  153. */
  154. static rt_err_t stm32_i2c_bus_unlock(const struct stm32_soft_i2c_config *cfg)
  155. {
  156. rt_int32_t i = 0;
  157. if (PIN_LOW == rt_pin_read(cfg->sda))
  158. {
  159. while (i++ < 9)
  160. {
  161. rt_pin_write(cfg->scl, PIN_HIGH);
  162. stm32_udelay(100);
  163. rt_pin_write(cfg->scl, PIN_LOW);
  164. stm32_udelay(100);
  165. }
  166. }
  167. if (PIN_LOW == rt_pin_read(cfg->sda))
  168. {
  169. return -RT_ERROR;
  170. }
  171. return RT_EOK;
  172. }
  173. /* I2C initialization function */
  174. int rt_hw_i2c_init(void)
  175. {
  176. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct stm32_i2c);
  177. rt_err_t result;
  178. for (int i = 0; i < obj_num; i++)
  179. {
  180. i2c_obj[i].ops = stm32_bit_ops_default;
  181. i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
  182. i2c_obj[i].i2c2_bus.priv = &i2c_obj[i].ops;
  183. stm32_i2c_gpio_init(&i2c_obj[i]);
  184. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c2_bus, soft_i2c_config[i].bus_name);
  185. RT_ASSERT(result == RT_EOK);
  186. stm32_i2c_bus_unlock(&soft_i2c_config[i]);
  187. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  188. soft_i2c_config[i].bus_name,
  189. soft_i2c_config[i].scl,
  190. soft_i2c_config[i].sda);
  191. }
  192. return RT_EOK;
  193. }
  194. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  195. #endif /* RT_USING_I2C */