drv_soft_i2c.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. * 2024-06-25 Andeyqi the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rthw.h>
  12. #include <rtdevice.h>
  13. #include <board.h>
  14. #ifdef BSP_USING_SW_I2C
  15. #define LOG_TAG "drv.i2c"
  16. #include <rtdbg.h>
  17. /* MCXNXXX config class */
  18. struct mcxnxxx_soft_i2c_config
  19. {
  20. rt_uint8_t scl;
  21. rt_uint8_t sda;
  22. const char *bus_name;
  23. };
  24. /* MCXNXXX i2c dirver class */
  25. struct mcxnxxx_i2c
  26. {
  27. struct rt_i2c_bit_ops ops;
  28. struct rt_i2c_bus_device i2c2_bus;
  29. };
  30. #if defined(BSP_USING_SW_I2C0)
  31. #define SW_I2C0_BUS_CONFIG \
  32. { \
  33. .scl = BSP_SW_I2C0_SCL_PIN, \
  34. .sda = BSP_SW_I2C0_SDA_PIN, \
  35. .bus_name = "swi2c0", \
  36. }
  37. #endif
  38. static const struct mcxnxxx_soft_i2c_config soft_i2c_config[] =
  39. {
  40. #if defined(BSP_USING_SW_I2C0)
  41. SW_I2C0_BUS_CONFIG,
  42. #endif
  43. };
  44. static struct mcxnxxx_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
  45. /**
  46. * @brief This function initializes the i2c pin.
  47. * @param i2c
  48. * @retval None
  49. */
  50. static void mcxnxxx_i2c_gpio_init(struct mcxnxxx_i2c *i2c)
  51. {
  52. struct mcxnxxx_soft_i2c_config* cfg = (struct mcxnxxx_soft_i2c_config*)i2c->ops.data;
  53. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
  54. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  55. rt_pin_write(cfg->scl, PIN_HIGH);
  56. rt_pin_write(cfg->sda, PIN_HIGH);
  57. }
  58. /**
  59. * @brief This function sets the sda pin.
  60. * @param data, state
  61. * @retval None
  62. */
  63. static void mcxnxxx_set_sda(void *data, rt_int32_t state)
  64. {
  65. struct mcxnxxx_soft_i2c_config* cfg = (struct mcxnxxx_soft_i2c_config*)data;
  66. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  67. if (state)
  68. {
  69. rt_pin_write(cfg->sda, PIN_HIGH);
  70. }
  71. else
  72. {
  73. rt_pin_write(cfg->sda, PIN_LOW);
  74. }
  75. }
  76. /**
  77. * @brief This function sets the scl pin.
  78. * @param data, state
  79. * @retval None
  80. */
  81. static void mcxnxxx_set_scl(void *data, rt_int32_t state)
  82. {
  83. struct mcxnxxx_soft_i2c_config* cfg = (struct mcxnxxx_soft_i2c_config*)data;
  84. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
  85. if (state)
  86. {
  87. rt_pin_write(cfg->scl, PIN_HIGH);
  88. }
  89. else
  90. {
  91. rt_pin_write(cfg->scl, PIN_LOW);
  92. }
  93. }
  94. /**
  95. * @brief This function gets the sda pin state.
  96. * @param data
  97. * @retval None
  98. */
  99. static rt_int32_t mcxnxxx_get_sda(void *data)
  100. {
  101. struct mcxnxxx_soft_i2c_config* cfg = (struct mcxnxxx_soft_i2c_config*)data;
  102. rt_pin_mode(cfg->sda, PIN_MODE_INPUT);
  103. return rt_pin_read(cfg->sda);
  104. }
  105. /**
  106. * @brief This function gets the scl pin state.
  107. * @param data
  108. * @retval None
  109. */
  110. static rt_int32_t mcxnxxx_get_scl(void *data)
  111. {
  112. struct mcxnxxx_soft_i2c_config* cfg = (struct mcxnxxx_soft_i2c_config*)data;
  113. rt_pin_mode(cfg->scl,PIN_MODE_INPUT);
  114. return rt_pin_read(cfg->scl);
  115. }
  116. /**
  117. * @brief The time delay function.
  118. * @param us
  119. * @retval None
  120. */
  121. static void mcxnxxx_udelay(rt_uint32_t us)
  122. {
  123. rt_uint32_t frequency = CLOCK_GetCoreSysClkFreq();
  124. int i = (frequency/ 4000000 * us);
  125. while(i)
  126. {
  127. i--;
  128. }
  129. }
  130. static const struct rt_i2c_bit_ops mcxnxxx_bit_ops_default =
  131. {
  132. .data = RT_NULL,
  133. .set_sda = mcxnxxx_set_sda,
  134. .set_scl = mcxnxxx_set_scl,
  135. .get_sda = mcxnxxx_get_sda,
  136. .get_scl = mcxnxxx_get_scl,
  137. .udelay = mcxnxxx_udelay,
  138. .delay_us = 1,
  139. .timeout = 100
  140. };
  141. /**
  142. * @brief if i2c is locked, this function will unlock it
  143. * @param cfg
  144. * @retval RT_EOK indicates successful unlock.
  145. */
  146. static rt_err_t mcxnxxx_i2c_bus_unlock(const struct mcxnxxx_soft_i2c_config *cfg)
  147. {
  148. rt_int32_t i = 0;
  149. if (PIN_LOW == rt_pin_read(cfg->sda))
  150. {
  151. while (i++ < 9)
  152. {
  153. rt_pin_write(cfg->scl, PIN_HIGH);
  154. mcxnxxx_udelay(100);
  155. rt_pin_write(cfg->scl, PIN_LOW);
  156. mcxnxxx_udelay(100);
  157. }
  158. }
  159. if (PIN_LOW == rt_pin_read(cfg->sda))
  160. {
  161. return -RT_ERROR;
  162. }
  163. return RT_EOK;
  164. }
  165. /**
  166. * @brief I2C initialization function
  167. * @param None
  168. * @retval RT_EOK indicates successful initialization.
  169. */
  170. int rt_hw_soft_i2c_init(void)
  171. {
  172. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct mcxnxxx_i2c);
  173. rt_err_t result;
  174. for (int i = 0; i < obj_num; i++)
  175. {
  176. i2c_obj[i].ops = mcxnxxx_bit_ops_default;
  177. i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
  178. i2c_obj[i].i2c2_bus.priv = &i2c_obj[i].ops;
  179. mcxnxxx_i2c_gpio_init(&i2c_obj[i]);
  180. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c2_bus, soft_i2c_config[i].bus_name);
  181. RT_ASSERT(result == RT_EOK);
  182. mcxnxxx_i2c_bus_unlock(&soft_i2c_config[i]);
  183. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  184. soft_i2c_config[i].bus_name,
  185. soft_i2c_config[i].scl,
  186. soft_i2c_config[i].sda);
  187. }
  188. return RT_EOK;
  189. }
  190. INIT_APP_EXPORT(rt_hw_soft_i2c_init);
  191. #endif /* RT_USING_I2C */