drv_soft_i2c.c 4.9 KB

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