drv_soft_i2c.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. * 2023-04-11 linshire the first version
  9. */
  10. #include <board.h>
  11. #include "drv_soft_i2c.h"
  12. #ifdef RT_USING_I2C
  13. #define LOG_TAG "drv.i2c"
  14. #include <drv_log.h>
  15. #if !defined(BSP_USING_I2C1) && !defined(BSP_USING_I2C2) && !defined(BSP_USING_I2C3) && !defined(BSP_USING_I2C4)
  16. #error "Please define at least one BSP_USING_I2Cx"
  17. /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
  18. #endif
  19. static const struct lpc55s69_soft_i2c_config soft_i2c_config[] =
  20. {
  21. #ifdef BSP_USING_SOFT_I2C1
  22. SOFT_I2C1_BUS_CONFIG,
  23. #endif
  24. #ifdef BSP_USING_SOFT_I2C2
  25. SOFT_I2C2_BUS_CONFIG,
  26. #endif
  27. };
  28. static struct lpc55s69_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
  29. /**
  30. * This function initializes the i2c pin.
  31. *
  32. * @param lpc55s69 i2c dirver class.
  33. */
  34. static void lpc55s69_i2c_gpio_init(struct lpc55s69_i2c *i2c)
  35. {
  36. struct lpc55s69_soft_i2c_config* cfg = (struct lpc55s69_soft_i2c_config*)i2c->ops.data;
  37. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
  38. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  39. rt_pin_write(cfg->scl, PIN_HIGH);
  40. rt_pin_write(cfg->sda, PIN_HIGH);
  41. }
  42. /**
  43. * This function sets the sda pin.
  44. *
  45. * @param lpc55s69 config class.
  46. * @param The sda pin state.
  47. */
  48. static void lpc55s69_set_sda(void *data, rt_int32_t state)
  49. {
  50. struct lpc55s69_soft_i2c_config* cfg = (struct lpc55s69_soft_i2c_config*)data;
  51. if (state)
  52. {
  53. rt_pin_write(cfg->sda, PIN_HIGH);
  54. }
  55. else
  56. {
  57. rt_pin_write(cfg->sda, PIN_LOW);
  58. }
  59. }
  60. /**
  61. * This function sets the scl pin.
  62. *
  63. * @param lpc55s69 config class.
  64. * @param The scl pin state.
  65. */
  66. static void lpc55s69_set_scl(void *data, rt_int32_t state)
  67. {
  68. struct lpc55s69_soft_i2c_config* cfg = (struct lpc55s69_soft_i2c_config*)data;
  69. if (state)
  70. {
  71. rt_pin_write(cfg->scl, PIN_HIGH);
  72. }
  73. else
  74. {
  75. rt_pin_write(cfg->scl, PIN_LOW);
  76. }
  77. }
  78. /**
  79. * This function gets the sda pin state.
  80. *
  81. * @param The sda pin state.
  82. */
  83. static rt_int32_t lpc55s69_get_sda(void *data)
  84. {
  85. struct lpc55s69_soft_i2c_config* cfg = (struct lpc55s69_soft_i2c_config*)data;
  86. return rt_pin_read(cfg->sda);
  87. }
  88. /**
  89. * This function gets the scl pin state.
  90. *
  91. * @param The scl pin state.
  92. */
  93. static rt_int32_t lpc55s69_get_scl(void *data)
  94. {
  95. struct lpc55s69_soft_i2c_config* cfg = (struct lpc55s69_soft_i2c_config*)data;
  96. return rt_pin_read(cfg->scl);
  97. }
  98. /**
  99. * The time delay function.
  100. *
  101. * @param microseconds.
  102. */
  103. static void lpc55s69_udelay(rt_uint32_t us)
  104. {
  105. rt_uint32_t ticks;
  106. rt_uint32_t told, tnow, tcnt = 0;
  107. rt_uint32_t reload = SysTick->LOAD;
  108. ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
  109. told = SysTick->VAL;
  110. while (1)
  111. {
  112. tnow = SysTick->VAL;
  113. if (tnow != told)
  114. {
  115. if (tnow < told)
  116. {
  117. tcnt += told - tnow;
  118. }
  119. else
  120. {
  121. tcnt += reload - tnow + told;
  122. }
  123. told = tnow;
  124. if (tcnt >= ticks)
  125. {
  126. break;
  127. }
  128. }
  129. }
  130. }
  131. static const struct rt_i2c_bit_ops lpc55s69_bit_ops_default =
  132. {
  133. .data = RT_NULL,
  134. .set_sda = lpc55s69_set_sda,
  135. .set_scl = lpc55s69_set_scl,
  136. .get_sda = lpc55s69_get_sda,
  137. .get_scl = lpc55s69_get_scl,
  138. .udelay = lpc55s69_udelay,
  139. .delay_us = 1,
  140. .timeout = 100
  141. };
  142. /**
  143. * if i2c is locked, this function will unlock it
  144. *
  145. * @param lpc55s69 config class
  146. *
  147. * @return RT_EOK indicates successful unlock.
  148. */
  149. static rt_err_t lpc55s69_i2c_bus_unlock(const struct lpc55s69_soft_i2c_config *cfg)
  150. {
  151. rt_int32_t i = 0;
  152. if (PIN_LOW == rt_pin_read(cfg->sda))
  153. {
  154. while (i++ < 9)
  155. {
  156. rt_pin_write(cfg->scl, PIN_HIGH);
  157. lpc55s69_udelay(100);
  158. rt_pin_write(cfg->scl, PIN_LOW);
  159. lpc55s69_udelay(100);
  160. }
  161. }
  162. if (PIN_LOW == rt_pin_read(cfg->sda))
  163. {
  164. return -RT_ERROR;
  165. }
  166. return RT_EOK;
  167. }
  168. /* I2C initialization function */
  169. int rt_hw_i2c_init(void)
  170. {
  171. rt_err_t result;
  172. for (rt_size_t i = 0; i < sizeof(i2c_obj) / sizeof(struct lpc55s69_i2c); i++)
  173. {
  174. i2c_obj[i].ops = lpc55s69_bit_ops_default;
  175. i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
  176. i2c_obj[i].i2c2_bus.priv = &i2c_obj[i].ops;
  177. lpc55s69_i2c_gpio_init(&i2c_obj[i]);
  178. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c2_bus, soft_i2c_config[i].bus_name);
  179. RT_ASSERT(result == RT_EOK);
  180. lpc55s69_i2c_bus_unlock(&soft_i2c_config[i]);
  181. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  182. soft_i2c_config[i].bus_name,
  183. soft_i2c_config[i].scl,
  184. soft_i2c_config[i].sda);
  185. }
  186. return RT_EOK;
  187. }
  188. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  189. #endif /* RT_USING_I2C */