drv_soft_i2c.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-07-29 KyleChan first version
  9. */
  10. #include "board.h"
  11. #include "drv_soft_i2c.h"
  12. #include "drv_config.h"
  13. #ifdef BSP_USING_SOFT_I2C
  14. #define DBG_TAG "drv.i2c"
  15. #ifdef DRV_DEBUG
  16. #define DBG_LVL DBG_LOG
  17. #else
  18. #define DBG_LVL DBG_INFO
  19. #endif /* DRV_DEBUG */
  20. #if !defined(BSP_USING_I2C0) && !defined(BSP_USING_I2C1) && !defined(BSP_USING_I2C2)
  21. #error "Please define at least one BSP_USING_I2Cx"
  22. /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
  23. #endif
  24. static const struct ra_soft_i2c_config soft_i2c_config[] =
  25. {
  26. #ifdef BSP_USING_I2C0
  27. I2C0_BUS_CONFIG,
  28. #endif
  29. #ifdef BSP_USING_I2C1
  30. I2C1_BUS_CONFIG,
  31. #endif
  32. #ifdef BSP_USING_I2C2
  33. I2C2_BUS_CONFIG,
  34. #endif
  35. };
  36. static struct ra_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
  37. /**
  38. * This function initializes the i2c pin.
  39. *
  40. * @param ra i2c dirver class.
  41. */
  42. static void ra_i2c_gpio_init(struct ra_i2c *i2c)
  43. {
  44. struct ra_soft_i2c_config *cfg = (struct ra_soft_i2c_config *)i2c->ops.data;
  45. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
  46. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  47. rt_pin_write(cfg->scl, PIN_HIGH);
  48. rt_pin_write(cfg->sda, PIN_HIGH);
  49. }
  50. static void ra_i2c_pin_init(void)
  51. {
  52. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ra_i2c);
  53. for(rt_size_t i = 0; i < obj_num; i++)
  54. {
  55. ra_i2c_gpio_init(&i2c_obj[i]);
  56. }
  57. }
  58. /**
  59. * This function sets the sda pin.
  60. *
  61. * @param ra config class.
  62. * @param The sda pin state.
  63. */
  64. static void ra_set_sda(void *data, rt_int32_t state)
  65. {
  66. struct ra_soft_i2c_config *cfg = (struct ra_soft_i2c_config *)data;
  67. if (state)
  68. {
  69. rt_pin_write(cfg->sda, PIN_HIGH);
  70. }
  71. else
  72. {
  73. rt_pin_write(cfg->sda, PIN_LOW);
  74. }
  75. }
  76. /**
  77. * This function sets the scl pin.
  78. *
  79. * @param ra config class.
  80. * @param The scl pin state.
  81. */
  82. static void ra_set_scl(void *data, rt_int32_t state)
  83. {
  84. struct ra_soft_i2c_config *cfg = (struct ra_soft_i2c_config *)data;
  85. if (state)
  86. {
  87. rt_pin_write(cfg->scl, PIN_HIGH);
  88. }
  89. else
  90. {
  91. rt_pin_write(cfg->scl, PIN_LOW);
  92. }
  93. }
  94. /**
  95. * This function gets the sda pin state.
  96. *
  97. * @param The sda pin state.
  98. */
  99. static rt_int32_t ra_get_sda(void *data)
  100. {
  101. struct ra_soft_i2c_config *cfg = (struct ra_soft_i2c_config *)data;
  102. return rt_pin_read(cfg->sda);
  103. }
  104. /**
  105. * This function gets the scl pin state.
  106. *
  107. * @param The scl pin state.
  108. */
  109. static rt_int32_t ra_get_scl(void *data)
  110. {
  111. struct ra_soft_i2c_config *cfg = (struct ra_soft_i2c_config *)data;
  112. return rt_pin_read(cfg->scl);
  113. }
  114. /**
  115. * The time delay function.
  116. *
  117. * @param microseconds.
  118. */
  119. static void ra_udelay(rt_uint32_t us)
  120. {
  121. rt_uint32_t ticks;
  122. rt_uint32_t told, tnow, tcnt = 0;
  123. rt_uint32_t reload = SysTick->LOAD;
  124. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  125. told = SysTick->VAL;
  126. while (1)
  127. {
  128. tnow = SysTick->VAL;
  129. if (tnow != told)
  130. {
  131. if (tnow < told)
  132. {
  133. tcnt += told - tnow;
  134. }
  135. else
  136. {
  137. tcnt += reload - tnow + told;
  138. }
  139. told = tnow;
  140. if (tcnt >= ticks)
  141. {
  142. break;
  143. }
  144. }
  145. }
  146. }
  147. static const struct rt_i2c_bit_ops ra_bit_ops_default =
  148. {
  149. .data = RT_NULL,
  150. .pin_init = ra_i2c_pin_init,
  151. .set_sda = ra_set_sda,
  152. .set_scl = ra_set_scl,
  153. .get_sda = ra_get_sda,
  154. .get_scl = ra_get_scl,
  155. .udelay = ra_udelay,
  156. .delay_us = 1,
  157. .timeout = 100,
  158. .i2c_pin_init_flag = RT_FALSE
  159. };
  160. /**
  161. * if i2c is locked, this function will unlock it
  162. *
  163. * @param ra config class
  164. *
  165. * @return RT_EOK indicates successful unlock.
  166. */
  167. static rt_err_t ra_i2c_bus_unlock(const struct ra_soft_i2c_config *cfg)
  168. {
  169. rt_int32_t i = 0;
  170. if (PIN_LOW == rt_pin_read(cfg->sda))
  171. {
  172. while (i++ < 9)
  173. {
  174. rt_pin_write(cfg->scl, PIN_HIGH);
  175. ra_udelay(100);
  176. rt_pin_write(cfg->scl, PIN_LOW);
  177. ra_udelay(100);
  178. }
  179. }
  180. if (PIN_LOW == rt_pin_read(cfg->sda))
  181. {
  182. return -RT_ERROR;
  183. }
  184. return RT_EOK;
  185. }
  186. /* I2C initialization function */
  187. int rt_hw_i2c_init(void)
  188. {
  189. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ra_i2c);
  190. rt_err_t result;
  191. for (rt_size_t i = 0; i < obj_num; i++)
  192. {
  193. i2c_obj[i].ops = ra_bit_ops_default;
  194. i2c_obj[i].ops.data = (void *)&soft_i2c_config[i];
  195. i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
  196. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
  197. RT_ASSERT(result == RT_EOK);
  198. ra_i2c_bus_unlock(&soft_i2c_config[i]);
  199. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  200. soft_i2c_config[i].bus_name,
  201. soft_i2c_config[i].scl,
  202. soft_i2c_config[i].sda);
  203. }
  204. return RT_EOK;
  205. }
  206. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  207. #endif /* BSP_USING_SOFT_I2C */