drv_soft_i2c.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright (c) 2006-2022, Synwit Technology Co.,Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-07-01 lik first version
  9. */
  10. #include "drv_soft_i2c.h"
  11. #ifdef RT_USING_I2C
  12. #ifdef BSP_USING_I2C
  13. //#define DRV_DEBUG
  14. #define LOG_TAG "drv.i2c"
  15. #include <drv_log.h>
  16. #if !defined(BSP_USING_I2C0) && !defined(BSP_USING_I2C1)
  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. #ifdef BSP_USING_I2C0
  21. #define I2C0_BUS_CFG \
  22. { \
  23. .scl = BSP_I2C0_SCL_PIN, \
  24. .sda = BSP_I2C0_SDA_PIN, \
  25. .name = "i2c0", \
  26. }
  27. #endif
  28. #ifdef BSP_USING_I2C1
  29. #define I2C1_BUS_CFG \
  30. { \
  31. .scl = BSP_I2C1_SCL_PIN, \
  32. .sda = BSP_I2C1_SDA_PIN, \
  33. .name = "i2c1", \
  34. }
  35. #endif
  36. /* swm config class */
  37. struct swm_soft_i2c_cfg
  38. {
  39. rt_uint8_t scl;
  40. rt_uint8_t sda;
  41. const char *name;
  42. };
  43. /* swm i2c dirver class */
  44. struct swm_soft_i2c_device
  45. {
  46. struct rt_i2c_bit_ops ops;
  47. struct rt_i2c_bus_device i2c_bus;
  48. };
  49. static const struct swm_soft_i2c_cfg swm_soft_i2c_cfg[] =
  50. {
  51. #ifdef BSP_USING_I2C0
  52. I2C0_BUS_CFG,
  53. #endif
  54. #ifdef BSP_USING_I2C1
  55. I2C1_BUS_CFG,
  56. #endif
  57. };
  58. static struct swm_soft_i2c_device i2c_obj[sizeof(swm_soft_i2c_cfg) / sizeof(swm_soft_i2c_cfg[0])];
  59. /**
  60. * This function initializes the i2c pin.
  61. *
  62. * @param swm i2c dirver class.
  63. */
  64. static void swm_i2c_gpio_init(struct swm_soft_i2c_device *i2c)
  65. {
  66. struct swm_soft_i2c_cfg *soft_i2c_cfg = (struct swm_soft_i2c_cfg *)i2c->ops.data;
  67. rt_pin_mode(soft_i2c_cfg->scl, PIN_MODE_OUTPUT_OD);
  68. rt_pin_mode(soft_i2c_cfg->sda, PIN_MODE_OUTPUT_OD);
  69. rt_pin_write(soft_i2c_cfg->scl, PIN_HIGH);
  70. rt_pin_write(soft_i2c_cfg->sda, PIN_HIGH);
  71. }
  72. /**
  73. * This function sets the sda pin.
  74. *
  75. * @param swm config class.
  76. * @param The sda pin state.
  77. */
  78. static void swm_i2c_set_sda(void *data, rt_int32_t state)
  79. {
  80. struct swm_soft_i2c_cfg *soft_i2c_cfg = (struct swm_soft_i2c_cfg *)data;
  81. rt_pin_mode(soft_i2c_cfg->sda, PIN_MODE_OUTPUT_OD);
  82. if (state)
  83. {
  84. rt_pin_write(soft_i2c_cfg->sda, PIN_HIGH);
  85. }
  86. else
  87. {
  88. rt_pin_write(soft_i2c_cfg->sda, PIN_LOW);
  89. }
  90. }
  91. /**
  92. * This function sets the scl pin.
  93. *
  94. * @param swm config class.
  95. * @param The scl pin state.
  96. */
  97. static void swm_i2c_set_scl(void *data, rt_int32_t state)
  98. {
  99. struct swm_soft_i2c_cfg *soft_i2c_cfg = (struct swm_soft_i2c_cfg *)data;
  100. rt_pin_mode(soft_i2c_cfg->scl, PIN_MODE_OUTPUT_OD);
  101. if (state)
  102. {
  103. rt_pin_write(soft_i2c_cfg->scl, PIN_HIGH);
  104. }
  105. else
  106. {
  107. rt_pin_write(soft_i2c_cfg->scl, PIN_LOW);
  108. }
  109. }
  110. /**
  111. * This function gets the sda pin state.
  112. *
  113. * @param The sda pin state.
  114. */
  115. static rt_int32_t swm_i2c_get_sda(void *data)
  116. {
  117. struct swm_soft_i2c_cfg *soft_i2c_cfg = (struct swm_soft_i2c_cfg *)data;
  118. rt_pin_mode(soft_i2c_cfg->sda, PIN_MODE_INPUT_PULLDOWN);
  119. return rt_pin_read(soft_i2c_cfg->sda);
  120. }
  121. /**
  122. * This function gets the scl pin state.
  123. *
  124. * @param The scl pin state.
  125. */
  126. static rt_int32_t swm_i2c_get_scl(void *data)
  127. {
  128. struct swm_soft_i2c_cfg *soft_i2c_cfg = (struct swm_soft_i2c_cfg *)data;
  129. rt_pin_mode(soft_i2c_cfg->scl, PIN_MODE_INPUT_PULLDOWN);
  130. return rt_pin_read(soft_i2c_cfg->scl);
  131. }
  132. /**
  133. * The time delay function.
  134. *
  135. * @param microseconds.
  136. */
  137. static void swm_i2c_udelay(rt_uint32_t us)
  138. {
  139. rt_uint32_t ticks;
  140. rt_uint32_t told, tnow, tcnt = 0;
  141. rt_uint32_t reload = SysTick->LOAD;
  142. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  143. told = SysTick->VAL;
  144. while (1)
  145. {
  146. tnow = SysTick->VAL;
  147. if (tnow != told)
  148. {
  149. if (tnow < told)
  150. {
  151. tcnt += told - tnow;
  152. }
  153. else
  154. {
  155. tcnt += reload - tnow + told;
  156. }
  157. told = tnow;
  158. if (tcnt >= ticks)
  159. {
  160. break;
  161. }
  162. }
  163. }
  164. }
  165. static const struct rt_i2c_bit_ops swm_i2c_bit_ops =
  166. {
  167. .data = RT_NULL,
  168. .set_sda = swm_i2c_set_sda,
  169. .set_scl = swm_i2c_set_scl,
  170. .get_sda = swm_i2c_get_sda,
  171. .get_scl = swm_i2c_get_scl,
  172. .udelay = swm_i2c_udelay,
  173. .delay_us = 1,
  174. .timeout = 100};
  175. /* I2C initialization function */
  176. int swm_i2c_init(void)
  177. {
  178. rt_err_t result;
  179. for (int i = 0; i < sizeof(i2c_obj) / sizeof(struct swm_soft_i2c_device); i++)
  180. {
  181. i2c_obj[i].ops = swm_i2c_bit_ops;
  182. i2c_obj[i].ops.data = (void *)&swm_soft_i2c_cfg[i];
  183. i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
  184. swm_i2c_gpio_init(&i2c_obj[i]);
  185. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, swm_soft_i2c_cfg[i].name);
  186. RT_ASSERT(result == RT_EOK);
  187. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  188. swm_soft_i2c_cfg[i].name,
  189. swm_soft_i2c_cfg[i].scl,
  190. swm_soft_i2c_cfg[i].sda);
  191. }
  192. return RT_EOK;
  193. }
  194. INIT_DEVICE_EXPORT(swm_i2c_init);
  195. #endif /* BSP_USING_I2C */
  196. #endif /* RT_USING_I2C */