drv_soft_i2c.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. static void ab32_i2c_pin_init(void)
  52. {
  53. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ab32_i2c);
  54. for(rt_size_t i = 0; i < obj_num; i++)
  55. {
  56. ab32_i2c_gpio_init(&i2c_obj[i]);
  57. }
  58. }
  59. /**
  60. * This function sets the sda pin.
  61. *
  62. * @param data ab32 config class.
  63. * @param state The sda pin state.
  64. */
  65. static void ab32_set_sda(void *data, rt_int32_t state)
  66. {
  67. struct ab32_soft_i2c_config* cfg = (struct ab32_soft_i2c_config*)data;
  68. if (cfg->sda_mode == PIN_MODE_INPUT_PULLUP) {
  69. cfg->sda_mode = PIN_MODE_OUTPUT_OD;
  70. rt_pin_mode(cfg->sda, cfg->sda_mode);
  71. }
  72. if (state)
  73. {
  74. rt_pin_write(cfg->sda, PIN_HIGH);
  75. }
  76. else
  77. {
  78. rt_pin_write(cfg->sda, PIN_LOW);
  79. }
  80. }
  81. /**
  82. * This function sets the scl pin.
  83. *
  84. * @param data ab32 config class.
  85. * @param state The scl pin state.
  86. */
  87. static void ab32_set_scl(void *data, rt_int32_t state)
  88. {
  89. struct ab32_soft_i2c_config* cfg = (struct ab32_soft_i2c_config*)data;
  90. if (cfg->scl_mode == PIN_MODE_INPUT_PULLUP) {
  91. cfg->scl_mode = PIN_MODE_OUTPUT_OD;
  92. rt_pin_mode(cfg->scl, cfg->scl_mode);
  93. }
  94. if (state)
  95. {
  96. rt_pin_write(cfg->scl, PIN_HIGH);
  97. }
  98. else
  99. {
  100. rt_pin_write(cfg->scl, PIN_LOW);
  101. }
  102. }
  103. /**
  104. * This function gets the sda pin state.
  105. *
  106. * @param data The sda pin state.
  107. */
  108. static rt_int32_t ab32_get_sda(void *data)
  109. {
  110. struct ab32_soft_i2c_config* cfg = (struct ab32_soft_i2c_config*)data;
  111. if (cfg->sda_mode != PIN_MODE_INPUT_PULLUP) {
  112. cfg->sda_mode = PIN_MODE_INPUT_PULLUP;
  113. rt_pin_mode(cfg->sda, cfg->sda_mode);
  114. }
  115. return rt_pin_read(cfg->sda);
  116. }
  117. /**
  118. * This function gets the scl pin state.
  119. *
  120. * @param data The scl pin state.
  121. */
  122. static rt_int32_t ab32_get_scl(void *data)
  123. {
  124. struct ab32_soft_i2c_config* cfg = (struct ab32_soft_i2c_config*)data;
  125. if (cfg->scl_mode == PIN_MODE_INPUT_PULLUP) {
  126. cfg->scl_mode = PIN_MODE_INPUT_PULLUP;
  127. rt_pin_mode(cfg->scl, cfg->scl_mode);
  128. }
  129. return rt_pin_read(cfg->scl);
  130. }
  131. static const struct rt_i2c_bit_ops ab32_bit_ops_default =
  132. {
  133. .data = RT_NULL,
  134. .pin_init = ab32_i2c_pin_init,
  135. .set_sda = ab32_set_sda,
  136. .set_scl = ab32_set_scl,
  137. .get_sda = ab32_get_sda,
  138. .get_scl = ab32_get_scl,
  139. .udelay = rt_hw_us_delay,
  140. .delay_us = 1,
  141. .timeout = 100,
  142. .i2c_pin_init_flag = RT_FALSE
  143. };
  144. /**
  145. * if i2c is locked, this function will unlock it
  146. *
  147. * @param cfg ab32 config class
  148. *
  149. * @return RT_EOK indicates successful unlock.
  150. */
  151. static rt_err_t ab32_i2c_bus_unlock(const struct ab32_soft_i2c_config *cfg)
  152. {
  153. rt_int32_t i = 0;
  154. if (PIN_LOW == rt_pin_read(cfg->sda))
  155. {
  156. while (i++ < 9)
  157. {
  158. rt_pin_write(cfg->scl, PIN_HIGH);
  159. rt_hw_us_delay(100);
  160. rt_pin_write(cfg->scl, PIN_LOW);
  161. rt_hw_us_delay(100);
  162. }
  163. }
  164. if (PIN_LOW == rt_pin_read(cfg->sda))
  165. {
  166. return -RT_ERROR;
  167. }
  168. return RT_EOK;
  169. }
  170. /* I2C initialization function */
  171. int rt_hw_i2c_init(void)
  172. {
  173. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ab32_i2c);
  174. rt_err_t result;
  175. for (rt_size_t i = 0; i < obj_num; i++)
  176. {
  177. i2c_obj[i].ops = ab32_bit_ops_default;
  178. i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
  179. i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
  180. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
  181. RT_ASSERT(result == RT_EOK);
  182. ab32_i2c_bus_unlock(&soft_i2c_config[i]);
  183. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  184. soft_i2c_config[i].bus_name,
  185. soft_i2c_config[i].scl,
  186. soft_i2c_config[i].sda);
  187. }
  188. return RT_EOK;
  189. }
  190. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  191. #endif