drv_soft_i2c.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. /**
  124. * The time delay function.
  125. *
  126. * @param us microseconds.
  127. */
  128. static void ab32_udelay(rt_uint32_t us)
  129. {
  130. rt_uint32_t ticks;
  131. rt_uint32_t told, tnow, tcnt = 0;
  132. rt_uint32_t reload = TMR0PR;
  133. ticks = us * reload / (1000 / RT_TICK_PER_SECOND);
  134. told = TMR0CNT;
  135. while (1)
  136. {
  137. tnow = TMR0CNT;
  138. if (tnow != told)
  139. {
  140. if (tnow < told)
  141. {
  142. tcnt += told - tnow;
  143. }
  144. else
  145. {
  146. tcnt += reload - tnow + told;
  147. }
  148. told = tnow;
  149. if (tcnt >= ticks)
  150. {
  151. break;
  152. }
  153. }
  154. }
  155. }
  156. static const struct rt_i2c_bit_ops ab32_bit_ops_default =
  157. {
  158. .data = RT_NULL,
  159. .set_sda = ab32_set_sda,
  160. .set_scl = ab32_set_scl,
  161. .get_sda = ab32_get_sda,
  162. .get_scl = ab32_get_scl,
  163. .udelay = ab32_udelay,
  164. .delay_us = 1,
  165. .timeout = 100
  166. };
  167. /**
  168. * if i2c is locked, this function will unlock it
  169. *
  170. * @param cfg ab32 config class
  171. *
  172. * @return RT_EOK indicates successful unlock.
  173. */
  174. static rt_err_t ab32_i2c_bus_unlock(const struct ab32_soft_i2c_config *cfg)
  175. {
  176. rt_int32_t i = 0;
  177. if (PIN_LOW == rt_pin_read(cfg->sda))
  178. {
  179. while (i++ < 9)
  180. {
  181. rt_pin_write(cfg->scl, PIN_HIGH);
  182. ab32_udelay(100);
  183. rt_pin_write(cfg->scl, PIN_LOW);
  184. ab32_udelay(100);
  185. }
  186. }
  187. if (PIN_LOW == rt_pin_read(cfg->sda))
  188. {
  189. return -RT_ERROR;
  190. }
  191. return RT_EOK;
  192. }
  193. /* I2C initialization function */
  194. int rt_hw_i2c_init(void)
  195. {
  196. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ab32_i2c);
  197. rt_err_t result;
  198. for (int i = 0; i < obj_num; i++)
  199. {
  200. i2c_obj[i].ops = ab32_bit_ops_default;
  201. i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
  202. i2c_obj[i].i2c2_bus.priv = &i2c_obj[i].ops;
  203. ab32_i2c_gpio_init(&i2c_obj[i]);
  204. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c2_bus, soft_i2c_config[i].bus_name);
  205. RT_ASSERT(result == RT_EOK);
  206. ab32_i2c_bus_unlock(&soft_i2c_config[i]);
  207. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  208. soft_i2c_config[i].bus_name,
  209. soft_i2c_config[i].scl,
  210. soft_i2c_config[i].sda);
  211. }
  212. return RT_EOK;
  213. }
  214. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  215. #endif