drv_soft_i2c.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-02-22 airm2m 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 air32_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 air32_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
  36. /**
  37. * This function initializes the i2c pin.
  38. *
  39. * @param air32 i2c dirver class.
  40. */
  41. static void air32_i2c_gpio_init(struct air32_i2c *i2c)
  42. {
  43. struct air32_soft_i2c_config* cfg = (struct air32_soft_i2c_config*)i2c->ops.data;
  44. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
  45. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  46. rt_pin_write(cfg->scl, PIN_HIGH);
  47. rt_pin_write(cfg->sda, PIN_HIGH);
  48. }
  49. static void air32_i2c_pin_init(void)
  50. {
  51. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct air32_i2c);
  52. for(rt_size_t i = 0; i < obj_num; i++)
  53. {
  54. air32_i2c_gpio_init(&i2c_obj[i]);
  55. }
  56. }
  57. /**
  58. * This function sets the sda pin.
  59. *
  60. * @param air32 config class.
  61. * @param The sda pin state.
  62. */
  63. static void air32_set_sda(void *data, rt_int32_t state)
  64. {
  65. struct air32_soft_i2c_config* cfg = (struct air32_soft_i2c_config*)data;
  66. if (state)
  67. {
  68. rt_pin_write(cfg->sda, PIN_HIGH);
  69. }
  70. else
  71. {
  72. rt_pin_write(cfg->sda, PIN_LOW);
  73. }
  74. }
  75. /**
  76. * This function sets the scl pin.
  77. *
  78. * @param air32 config class.
  79. * @param The scl pin state.
  80. */
  81. static void air32_set_scl(void *data, rt_int32_t state)
  82. {
  83. struct air32_soft_i2c_config* cfg = (struct air32_soft_i2c_config*)data;
  84. if (state)
  85. {
  86. rt_pin_write(cfg->scl, PIN_HIGH);
  87. }
  88. else
  89. {
  90. rt_pin_write(cfg->scl, PIN_LOW);
  91. }
  92. }
  93. /**
  94. * This function gets the sda pin state.
  95. *
  96. * @param The sda pin state.
  97. */
  98. static rt_int32_t air32_get_sda(void *data)
  99. {
  100. struct air32_soft_i2c_config* cfg = (struct air32_soft_i2c_config*)data;
  101. return rt_pin_read(cfg->sda);
  102. }
  103. /**
  104. * This function gets the scl pin state.
  105. *
  106. * @param The scl pin state.
  107. */
  108. static rt_int32_t air32_get_scl(void *data)
  109. {
  110. struct air32_soft_i2c_config* cfg = (struct air32_soft_i2c_config*)data;
  111. return rt_pin_read(cfg->scl);
  112. }
  113. /**
  114. * The time delay function.
  115. *
  116. * @param microseconds.
  117. */
  118. static void air32_udelay(rt_uint32_t us)
  119. {
  120. rt_uint32_t ticks;
  121. rt_uint32_t told, tnow, tcnt = 0;
  122. rt_uint32_t reload = SysTick->LOAD;
  123. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  124. told = SysTick->VAL;
  125. while (1)
  126. {
  127. tnow = SysTick->VAL;
  128. if (tnow != told)
  129. {
  130. if (tnow < told)
  131. {
  132. tcnt += told - tnow;
  133. }
  134. else
  135. {
  136. tcnt += reload - tnow + told;
  137. }
  138. told = tnow;
  139. if (tcnt >= ticks)
  140. {
  141. break;
  142. }
  143. }
  144. }
  145. }
  146. static const struct rt_i2c_bit_ops air32_bit_ops_default =
  147. {
  148. .data = RT_NULL,
  149. .pin_init = air32_i2c_pin_init,
  150. .set_sda = air32_set_sda,
  151. .set_scl = air32_set_scl,
  152. .get_sda = air32_get_sda,
  153. .get_scl = air32_get_scl,
  154. .udelay = air32_udelay,
  155. .delay_us = 1,
  156. .timeout = 100,
  157. .i2c_pin_init_flag = RT_FALSE
  158. };
  159. /**
  160. * if i2c is locked, this function will unlock it
  161. *
  162. * @param air32 config class
  163. *
  164. * @return RT_EOK indicates successful unlock.
  165. */
  166. static rt_err_t air32_i2c_bus_unlock(const struct air32_soft_i2c_config *cfg)
  167. {
  168. rt_int32_t i = 0;
  169. if (PIN_LOW == rt_pin_read(cfg->sda))
  170. {
  171. while (i++ < 9)
  172. {
  173. rt_pin_write(cfg->scl, PIN_HIGH);
  174. air32_udelay(100);
  175. rt_pin_write(cfg->scl, PIN_LOW);
  176. air32_udelay(100);
  177. }
  178. }
  179. if (PIN_LOW == rt_pin_read(cfg->sda))
  180. {
  181. return -RT_ERROR;
  182. }
  183. return RT_EOK;
  184. }
  185. /* I2C initialization function */
  186. int rt_sw_i2c_init(void)
  187. {
  188. rt_err_t result;
  189. for (rt_size_t i = 0; i < sizeof(i2c_obj) / sizeof(struct air32_i2c); i++)
  190. {
  191. i2c_obj[i].ops = air32_bit_ops_default;
  192. i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
  193. i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
  194. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
  195. RT_ASSERT(result == RT_EOK);
  196. air32_i2c_bus_unlock(&soft_i2c_config[i]);
  197. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  198. soft_i2c_config[i].bus_name,
  199. soft_i2c_config[i].scl,
  200. soft_i2c_config[i].sda);
  201. }
  202. return RT_EOK;
  203. }
  204. INIT_BOARD_EXPORT(rt_sw_i2c_init);
  205. #endif /* RT_USING_I2C */