drv_soft_i2c.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright (c) 2006-2024 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. static void ifx_i2c_pin_init(void)
  41. {
  42. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ifx_i2c);
  43. for(rt_size_t i = 0; i < obj_num; i++)
  44. {
  45. ifx_i2c_gpio_init(&i2c_obj[i]);
  46. }
  47. }
  48. /**
  49. * This function sets the sda pin.
  50. *
  51. * @param ifx config class.
  52. * @param The sda pin state.
  53. */
  54. static void ifx_set_sda(void *data, rt_int32_t state)
  55. {
  56. struct ifx_soft_i2c_config *cfg = (struct ifx_soft_i2c_config *)data;
  57. if (state)
  58. {
  59. rt_pin_write(cfg->sda, PIN_HIGH);
  60. }
  61. else
  62. {
  63. rt_pin_write(cfg->sda, PIN_LOW);
  64. }
  65. }
  66. /**
  67. * This function sets the scl pin.
  68. *
  69. * @param ifx config class.
  70. * @param The scl pin state.
  71. */
  72. static void ifx_set_scl(void *data, rt_int32_t state)
  73. {
  74. struct ifx_soft_i2c_config *cfg = (struct ifx_soft_i2c_config *)data;
  75. if (state)
  76. {
  77. rt_pin_write(cfg->scl, PIN_HIGH);
  78. }
  79. else
  80. {
  81. rt_pin_write(cfg->scl, PIN_LOW);
  82. }
  83. }
  84. /**
  85. * This function gets the sda pin state.
  86. *
  87. * @param The sda pin state.
  88. */
  89. static rt_int32_t ifx_get_sda(void *data)
  90. {
  91. struct ifx_soft_i2c_config *cfg = (struct ifx_soft_i2c_config *)data;
  92. return rt_pin_read(cfg->sda);
  93. }
  94. /**
  95. * This function gets the scl pin state.
  96. *
  97. * @param The scl pin state.
  98. */
  99. static rt_int32_t ifx_get_scl(void *data)
  100. {
  101. struct ifx_soft_i2c_config *cfg = (struct ifx_soft_i2c_config *)data;
  102. return rt_pin_read(cfg->scl);
  103. }
  104. static const struct rt_i2c_bit_ops ifx_bit_ops_default =
  105. {
  106. .data = RT_NULL,
  107. .pin_init = ifx_i2c_pin_init,
  108. .set_sda = ifx_set_sda,
  109. .set_scl = ifx_set_scl,
  110. .get_sda = ifx_get_sda,
  111. .get_scl = ifx_get_scl,
  112. .udelay = rt_hw_us_delay,
  113. .delay_us = 1,
  114. .timeout = 100,
  115. .i2c_pin_init_flag = RT_FALSE
  116. };
  117. /**
  118. * if i2c is locked, this function will unlock it
  119. *
  120. * @param ifx config class
  121. *
  122. * @return RT_EOK indicates successful unlock.
  123. */
  124. static rt_err_t ifx_i2c_bus_unlock(const struct ifx_soft_i2c_config *cfg)
  125. {
  126. rt_int32_t i = 0;
  127. if (PIN_LOW == rt_pin_read(cfg->sda))
  128. {
  129. while (i++ < 9)
  130. {
  131. rt_pin_write(cfg->scl, PIN_HIGH);
  132. rt_hw_us_delay(100);
  133. rt_pin_write(cfg->scl, PIN_LOW);
  134. rt_hw_us_delay(100);
  135. }
  136. }
  137. if (PIN_LOW == rt_pin_read(cfg->sda))
  138. {
  139. return -RT_ERROR;
  140. }
  141. return RT_EOK;
  142. }
  143. /* I2C initialization function */
  144. int rt_hw_i2c_init(void)
  145. {
  146. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct ifx_i2c);
  147. rt_err_t result;
  148. for (int i = 0; i < obj_num; i++)
  149. {
  150. i2c_obj[i].ops = ifx_bit_ops_default;
  151. i2c_obj[i].ops.data = (void *)&soft_i2c_config[i];
  152. i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
  153. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
  154. RT_ASSERT(result == RT_EOK);
  155. ifx_i2c_bus_unlock(&soft_i2c_config[i]);
  156. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  157. soft_i2c_config[i].bus_name,
  158. soft_i2c_config[i].scl,
  159. soft_i2c_config[i].sda);
  160. }
  161. return RT_EOK;
  162. }
  163. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  164. #endif /* RT_USING_I2C */