drv_soft_i2c.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-07-20 Rbb666 first version
  9. */
  10. #include <board.h>
  11. #include "drv_soft_i2c.h"
  12. #ifdef RT_USING_I2C
  13. //#define DRV_DEBUG
  14. #define LOG_TAG "drv.i2c"
  15. #include <drv_log.h>
  16. #if !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. static const struct ifx_soft_i2c_config soft_i2c_config[] =
  21. {
  22. #ifdef BSP_USING_I2C1
  23. I2C1_BUS_CONFIG,
  24. #endif
  25. };
  26. static struct ifx_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
  27. /**
  28. * This function initializes the i2c pin.
  29. *
  30. * @param ifx i2c dirver class.
  31. */
  32. static void ifx_i2c_gpio_init(struct ifx_i2c *i2c)
  33. {
  34. struct ifx_soft_i2c_config *cfg = (struct ifx_soft_i2c_config *)i2c->ops.data;
  35. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
  36. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  37. rt_pin_write(cfg->scl, PIN_HIGH);
  38. rt_pin_write(cfg->sda, PIN_HIGH);
  39. }
  40. /**
  41. * This function sets the sda pin.
  42. *
  43. * @param ifx config class.
  44. * @param The sda pin state.
  45. */
  46. static void ifx_set_sda(void *data, rt_int32_t state)
  47. {
  48. struct ifx_soft_i2c_config *cfg = (struct ifx_soft_i2c_config *)data;
  49. if (state)
  50. {
  51. rt_pin_write(cfg->sda, PIN_HIGH);
  52. }
  53. else
  54. {
  55. rt_pin_write(cfg->sda, PIN_LOW);
  56. }
  57. }
  58. /**
  59. * This function sets the scl pin.
  60. *
  61. * @param ifx config class.
  62. * @param The scl pin state.
  63. */
  64. static void ifx_set_scl(void *data, rt_int32_t state)
  65. {
  66. struct ifx_soft_i2c_config *cfg = (struct ifx_soft_i2c_config *)data;
  67. if (state)
  68. {
  69. rt_pin_write(cfg->scl, PIN_HIGH);
  70. }
  71. else
  72. {
  73. rt_pin_write(cfg->scl, PIN_LOW);
  74. }
  75. }
  76. /**
  77. * This function gets the sda pin state.
  78. *
  79. * @param The sda pin state.
  80. */
  81. static rt_int32_t ifx_get_sda(void *data)
  82. {
  83. struct ifx_soft_i2c_config *cfg = (struct ifx_soft_i2c_config *)data;
  84. return rt_pin_read(cfg->sda);
  85. }
  86. /**
  87. * This function gets the scl pin state.
  88. *
  89. * @param The scl pin state.
  90. */
  91. static rt_int32_t ifx_get_scl(void *data)
  92. {
  93. struct ifx_soft_i2c_config *cfg = (struct ifx_soft_i2c_config *)data;
  94. return rt_pin_read(cfg->scl);
  95. }
  96. static const struct rt_i2c_bit_ops ifx_bit_ops_default =
  97. {
  98. .data = RT_NULL,
  99. .set_sda = ifx_set_sda,
  100. .set_scl = ifx_set_scl,
  101. .get_sda = ifx_get_sda,
  102. .get_scl = ifx_get_scl,
  103. .udelay = rt_hw_us_delay,
  104. .delay_us = 1,
  105. .timeout = 100
  106. };
  107. /**
  108. * if i2c is locked, this function will unlock it
  109. *
  110. * @param ifx config class
  111. *
  112. * @return RT_EOK indicates successful unlock.
  113. */
  114. static rt_err_t ifx_i2c_bus_unlock(const struct ifx_soft_i2c_config *cfg)
  115. {
  116. rt_int32_t i = 0;
  117. if (PIN_LOW == rt_pin_read(cfg->sda))
  118. {
  119. while (i++ < 9)
  120. {
  121. rt_pin_write(cfg->scl, PIN_HIGH);
  122. rt_hw_us_delay(100);
  123. rt_pin_write(cfg->scl, PIN_LOW);
  124. rt_hw_us_delay(100);
  125. }
  126. }
  127. if (PIN_LOW == rt_pin_read(cfg->sda))
  128. {
  129. return -RT_ERROR;
  130. }
  131. return RT_EOK;
  132. }
  133. /* I2C initialization function */
  134. int rt_hw_i2c_init(void)
  135. {
  136. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ifx_i2c);
  137. rt_err_t result;
  138. for (int i = 0; i < obj_num; i++)
  139. {
  140. i2c_obj[i].ops = ifx_bit_ops_default;
  141. i2c_obj[i].ops.data = (void *)&soft_i2c_config[i];
  142. i2c_obj[i].i2c2_bus.priv = &i2c_obj[i].ops;
  143. ifx_i2c_gpio_init(&i2c_obj[i]);
  144. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c2_bus, soft_i2c_config[i].bus_name);
  145. RT_ASSERT(result == RT_EOK);
  146. ifx_i2c_bus_unlock(&soft_i2c_config[i]);
  147. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  148. soft_i2c_config[i].bus_name,
  149. soft_i2c_config[i].scl,
  150. soft_i2c_config[i].sda);
  151. }
  152. return RT_EOK;
  153. }
  154. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  155. #endif /* RT_USING_I2C */