drv_soft_i2c.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 i2c_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. static void apm32_i2c_pin_init(void)
  78. {
  79. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct apm32_soft_i2c);
  80. for(rt_size_t i = 0; i < obj_num; i++)
  81. {
  82. apm32_soft_i2c_gpio_init(&i2c_obj[i]);
  83. }
  84. }
  85. /**
  86. * @brief This function sets the sda pin.
  87. *
  88. * @param data is a pointer to the i2c config class.
  89. *
  90. * @param state is the level of sda pin.
  91. */
  92. static void apm32_soft_i2c_set_sda(void *data, rt_int32_t state)
  93. {
  94. struct apm32_soft_i2c_config *cfg = (struct apm32_soft_i2c_config *)data;
  95. rt_pin_write(cfg->sda_pin, state ? PIN_HIGH : PIN_LOW);
  96. }
  97. /**
  98. * @brief This function sets the scl pin.
  99. *
  100. * @param data is a pointer to the i2c config class.
  101. *
  102. * @param state is the level of scl pin.
  103. */
  104. static void apm32_soft_i2c_set_scl(void *data, rt_int32_t state)
  105. {
  106. struct apm32_soft_i2c_config *cfg = (struct apm32_soft_i2c_config *)data;
  107. rt_pin_write(cfg->scl_pin, state ? PIN_HIGH : PIN_LOW);
  108. }
  109. /**
  110. * @brief This function gets the sda pin state.
  111. *
  112. * @param data is a pointer to the i2c config class.
  113. */
  114. static rt_int32_t apm32_soft_i2c_get_sda(void *data)
  115. {
  116. struct apm32_soft_i2c_config *cfg = (struct apm32_soft_i2c_config *)data;
  117. return rt_pin_read(cfg->sda_pin);
  118. }
  119. /**
  120. * @brief This function gets the scl pin state.
  121. *
  122. * @param data is a pointer to the i2c config class.
  123. */
  124. static rt_int32_t apm32_soft_i2c_get_scl(void *data)
  125. {
  126. struct apm32_soft_i2c_config *cfg = (struct apm32_soft_i2c_config *)data;
  127. return rt_pin_read(cfg->scl_pin);
  128. }
  129. /**
  130. * @brief The time delay function in microseconds.
  131. *
  132. * @param us is the microseconds to delay.
  133. */
  134. static void apm32_soft_i2c_udelay(rt_uint32_t us)
  135. {
  136. rt_uint32_t count_old = SysTick->VAL;
  137. rt_uint32_t count_now;
  138. rt_uint32_t count = 0;
  139. rt_uint32_t reload = SysTick->LOAD;
  140. rt_uint32_t count_pre_us = (reload * RT_TICK_PER_SECOND) / 1000000;
  141. while (count_pre_us * us > count)
  142. {
  143. count_now = SysTick->VAL;
  144. if (count_now != count_old)
  145. {
  146. if (count_now < count_old)
  147. {
  148. count += count_old - count_now;
  149. }
  150. else
  151. {
  152. count += reload - count_now + count_old;
  153. }
  154. count_old = count_now;
  155. }
  156. }
  157. }
  158. /**
  159. * @brief This function will unlock i2c, if it is locked.
  160. *
  161. * @param cfg is a pointer to i2c config class.
  162. *
  163. * @return RT_EOK indicates successful unlock, other value indicates failed.
  164. */
  165. static rt_err_t apm32_i2c_bus_unlock(const struct apm32_soft_i2c_config *cfg)
  166. {
  167. rt_int32_t i = 0;
  168. if (PIN_LOW == rt_pin_read(cfg->sda_pin))
  169. {
  170. while (i++ < 9)
  171. {
  172. rt_pin_write(cfg->scl_pin, PIN_HIGH);
  173. apm32_soft_i2c_udelay(100);
  174. rt_pin_write(cfg->scl_pin, PIN_LOW);
  175. apm32_soft_i2c_udelay(100);
  176. }
  177. }
  178. if (PIN_LOW == rt_pin_read(cfg->sda_pin))
  179. {
  180. return -RT_ERROR;
  181. }
  182. return RT_EOK;
  183. }
  184. static const struct rt_i2c_bit_ops apm32_bit_ops_default =
  185. {
  186. .data = RT_NULL,
  187. .pin_init = apm32_i2c_pin_init,
  188. .set_sda = apm32_soft_i2c_set_sda,
  189. .set_scl = apm32_soft_i2c_set_scl,
  190. .get_sda = apm32_soft_i2c_get_sda,
  191. .get_scl = apm32_soft_i2c_get_scl,
  192. .udelay = apm32_soft_i2c_udelay,
  193. .delay_us = 1,
  194. .timeout = 100,
  195. .i2c_pin_init_flag = RT_FALSE
  196. };
  197. /**
  198. * @brief I2C initialization function.
  199. *
  200. * @return RT_EOK indicates successful initialization, other value indicates failed;
  201. */
  202. int rt_hw_i2c_init(void)
  203. {
  204. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct apm32_soft_i2c);
  205. rt_err_t result;
  206. for (rt_size_t i = 0; i < obj_num; i++)
  207. {
  208. i2c_obj[i].ops = apm32_bit_ops_default;
  209. i2c_obj[i].ops.data = (void *)&soft_i2c_config[i];
  210. i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
  211. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
  212. RT_ASSERT(result == RT_EOK);
  213. apm32_i2c_bus_unlock(&soft_i2c_config[i]);
  214. LOG_D("software simulation %s init done, pin scl: %d, pin sda: %d",
  215. soft_i2c_config[i].bus_name,
  216. soft_i2c_config[i].scl_pin,
  217. soft_i2c_config[i].sda_pin);
  218. }
  219. return RT_EOK;
  220. }
  221. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  222. #endif /* RT_USING_I2C */