1
0

drv_soft_i2c.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-03-04 stevetong459 first version
  9. */
  10. #include "board.h"
  11. #include <sys/time.h>
  12. #ifdef RT_USING_I2C
  13. #define DBG_TAG "drv.i2c"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.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. #endif
  19. /* apm32 i2c config class */
  20. struct apm32_soft_i2c_config
  21. {
  22. rt_uint8_t scl_pin;
  23. rt_uint8_t sda_pin;
  24. const char *bus_name;
  25. };
  26. /* apm32 i2c dirver class */
  27. struct apm32_soft_i2c
  28. {
  29. struct rt_i2c_bit_ops ops;
  30. struct rt_i2c_bus_device i2c2_bus;
  31. };
  32. static const struct apm32_soft_i2c_config soft_i2c_config[] =
  33. {
  34. #ifdef BSP_USING_I2C1
  35. {
  36. BSP_I2C1_SCL_PIN,
  37. BSP_I2C1_SDA_PIN,
  38. "i2c1"
  39. },
  40. #endif
  41. #ifdef BSP_USING_I2C2
  42. {
  43. BSP_I2C2_SCL_PIN,
  44. BSP_I2C2_SDA_PIN,
  45. "i2c2"
  46. },
  47. #endif
  48. #ifdef BSP_USING_I2C3
  49. {
  50. BSP_I2C3_SCL_PIN,
  51. BSP_I2C3_SDA_PIN,
  52. "i2c3"
  53. },
  54. #endif
  55. #ifdef BSP_USING_I2C4
  56. {
  57. BSP_I2C4_SCL_PIN,
  58. BSP_I2C4_SDA_PIN,
  59. "i2c4"
  60. },
  61. #endif
  62. };
  63. static struct apm32_soft_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
  64. /**
  65. * @brief This function will config gpio of soft i2c.
  66. *
  67. * @param i2c is a pointer to the object of soft i2c.
  68. */
  69. static void apm32_soft_i2c_gpio_init(struct apm32_soft_i2c *i2c)
  70. {
  71. struct apm32_soft_i2c_config *cfg = (struct apm32_soft_i2c_config *)i2c->ops.data;
  72. rt_pin_mode(cfg->scl_pin, PIN_MODE_OUTPUT_OD);
  73. rt_pin_mode(cfg->sda_pin, PIN_MODE_OUTPUT_OD);
  74. rt_pin_write(cfg->scl_pin, PIN_HIGH);
  75. rt_pin_write(cfg->sda_pin, PIN_HIGH);
  76. }
  77. /**
  78. * @brief This function sets the sda pin.
  79. *
  80. * @param data is a pointer to the i2c config class.
  81. *
  82. * @param state is the level of sda pin.
  83. */
  84. static void apm32_soft_i2c_set_sda(void *data, rt_int32_t state)
  85. {
  86. struct apm32_soft_i2c_config *cfg = (struct apm32_soft_i2c_config *)data;
  87. rt_pin_write(cfg->sda_pin, state ? PIN_HIGH : PIN_LOW);
  88. }
  89. /**
  90. * @brief This function sets the scl pin.
  91. *
  92. * @param data is a pointer to the i2c config class.
  93. *
  94. * @param state is the level of scl pin.
  95. */
  96. static void apm32_soft_i2c_set_scl(void *data, rt_int32_t state)
  97. {
  98. struct apm32_soft_i2c_config *cfg = (struct apm32_soft_i2c_config *)data;
  99. rt_pin_write(cfg->scl_pin, state ? PIN_HIGH : PIN_LOW);
  100. }
  101. /**
  102. * @brief This function gets the sda pin state.
  103. *
  104. * @param data is a pointer to the i2c config class.
  105. */
  106. static rt_int32_t apm32_soft_i2c_get_sda(void *data)
  107. {
  108. struct apm32_soft_i2c_config *cfg = (struct apm32_soft_i2c_config *)data;
  109. return rt_pin_read(cfg->sda_pin);
  110. }
  111. /**
  112. * @brief This function gets the scl pin state.
  113. *
  114. * @param data is a pointer to the i2c config class.
  115. */
  116. static rt_int32_t apm32_soft_i2c_get_scl(void *data)
  117. {
  118. struct apm32_soft_i2c_config *cfg = (struct apm32_soft_i2c_config *)data;
  119. return rt_pin_read(cfg->scl_pin);
  120. }
  121. /**
  122. * @brief The time delay function in microseconds.
  123. *
  124. * @param us is the microseconds to delay.
  125. */
  126. static void apm32_soft_i2c_udelay(rt_uint32_t us)
  127. {
  128. rt_uint32_t count_old = SysTick->VAL;
  129. rt_uint32_t count_now;
  130. rt_uint32_t count = 0;
  131. rt_uint32_t reload = SysTick->LOAD;
  132. rt_uint32_t count_pre_us = (reload * RT_TICK_PER_SECOND) / 1000000;
  133. while (count_pre_us * us > count)
  134. {
  135. count_now = SysTick->VAL;
  136. if (count_now != count_old)
  137. {
  138. if (count_now < count_old)
  139. {
  140. count += count_old - count_now;
  141. }
  142. else
  143. {
  144. count += reload - count_now + count_old;
  145. }
  146. count_old = count_now;
  147. }
  148. }
  149. }
  150. /**
  151. * @brief This function will unlock i2c, if it is locked.
  152. *
  153. * @param cfg is a pointer to i2c config class.
  154. *
  155. * @return RT_EOK indicates successful unlock, other value indicates failed.
  156. */
  157. static rt_err_t apm32_i2c_bus_unlock(const struct apm32_soft_i2c_config *cfg)
  158. {
  159. rt_int32_t i = 0;
  160. if (PIN_LOW == rt_pin_read(cfg->sda_pin))
  161. {
  162. while (i++ < 9)
  163. {
  164. rt_pin_write(cfg->scl_pin, PIN_HIGH);
  165. apm32_soft_i2c_udelay(100);
  166. rt_pin_write(cfg->scl_pin, PIN_LOW);
  167. apm32_soft_i2c_udelay(100);
  168. }
  169. }
  170. if (PIN_LOW == rt_pin_read(cfg->sda_pin))
  171. {
  172. return -RT_ERROR;
  173. }
  174. return RT_EOK;
  175. }
  176. static const struct rt_i2c_bit_ops apm32_bit_ops_default =
  177. {
  178. .data = RT_NULL,
  179. .set_sda = apm32_soft_i2c_set_sda,
  180. .set_scl = apm32_soft_i2c_set_scl,
  181. .get_sda = apm32_soft_i2c_get_sda,
  182. .get_scl = apm32_soft_i2c_get_scl,
  183. .udelay = apm32_soft_i2c_udelay,
  184. .delay_us = 1,
  185. .timeout = 100
  186. };
  187. /**
  188. * @brief I2C initialization function.
  189. *
  190. * @return RT_EOK indicates successful initialization, other value indicates failed;
  191. */
  192. int rt_hw_i2c_init(void)
  193. {
  194. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct apm32_soft_i2c);
  195. rt_err_t result;
  196. for (int i = 0; i < obj_num; i++)
  197. {
  198. i2c_obj[i].ops = apm32_bit_ops_default;
  199. i2c_obj[i].ops.data = (void *)&soft_i2c_config[i];
  200. i2c_obj[i].i2c2_bus.priv = &i2c_obj[i].ops;
  201. apm32_soft_i2c_gpio_init(&i2c_obj[i]);
  202. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c2_bus, soft_i2c_config[i].bus_name);
  203. RT_ASSERT(result == RT_EOK);
  204. apm32_i2c_bus_unlock(&soft_i2c_config[i]);
  205. LOG_D("software simulation %s init done, pin scl: %d, pin sda: %d",
  206. soft_i2c_config[i].bus_name,
  207. soft_i2c_config[i].scl_pin,
  208. soft_i2c_config[i].sda_pin);
  209. }
  210. return RT_EOK;
  211. }
  212. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  213. #endif /* RT_USING_I2C */