drv_softi2c.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2022-3-15 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if (defined(BSP_USING_SOFT_I2C) && defined(RT_USING_I2C_BITOPS) && defined(RT_USING_I2C) && defined(RT_USING_PIN))
  14. #include <rtthread.h>
  15. #include <rthw.h>
  16. #include <rtdevice.h>
  17. #include "NuMicro.h"
  18. /* Private define ---------------------------------------------------------------*/
  19. #define LOG_TAG "drv.softi2c"
  20. #define DBG_ENABLE
  21. #define DBG_SECTION_NAME LOG_TAG
  22. #define DBG_LEVEL DBG_INFO
  23. #include <rtdbg.h>
  24. #ifdef BSP_USING_SOFT_I2C0
  25. #define NU_SOFT_I2C0_BUS_CONFIG \
  26. { \
  27. .scl = BSP_SOFT_I2C0_SCL_PIN, \
  28. .sda = BSP_SOFT_I2C0_SDA_PIN, \
  29. .bus_name = "softi2c0", \
  30. }
  31. #endif
  32. #ifdef BSP_USING_SOFT_I2C1
  33. #define NU_SOFT_I2C1_BUS_CONFIG \
  34. { \
  35. .scl = BSP_SOFT_I2C1_SCL_PIN, \
  36. .sda = BSP_SOFT_I2C1_SDA_PIN, \
  37. .bus_name = "softi2c1", \
  38. }
  39. #endif
  40. #if (!defined(BSP_USING_SOFT_I2C0) && !defined(BSP_USING_SOFT_I2C1))
  41. #error "Please define at least one BSP_USING_SOFT_I2Cx"
  42. /* this driver can be disabled at menuconfig ? RT-Thread Components ? Device Drivers */
  43. #endif
  44. /* Private typedef --------------------------------------------------------------*/
  45. /* soft i2c config class */
  46. struct nu_soft_i2c_config
  47. {
  48. rt_uint8_t scl;
  49. rt_uint8_t sda;
  50. const char *bus_name;
  51. };
  52. /* soft i2c driver class */
  53. struct nu_soft_i2c
  54. {
  55. struct rt_i2c_bit_ops ops;
  56. struct rt_i2c_bus_device soft_i2c_bus;
  57. };
  58. /* Private functions ------------------------------------------------------------*/
  59. static void nu_soft_i2c_udelay(rt_uint32_t us);
  60. static void nu_soft_i2c_set_sda(void *data, rt_int32_t state);
  61. static void nu_soft_i2c_set_scl(void *data, rt_int32_t state);
  62. static rt_int32_t nu_soft_i2c_get_sda(void *data);
  63. static rt_int32_t nu_soft_i2c_get_scl(void *data);
  64. /* Private variables ------------------------------------------------------------*/
  65. static const struct nu_soft_i2c_config nu_soft_i2c_cfg[] =
  66. {
  67. #ifdef BSP_USING_SOFT_I2C0
  68. NU_SOFT_I2C0_BUS_CONFIG,
  69. #endif
  70. #ifdef BSP_USING_SOFT_I2C1
  71. NU_SOFT_I2C1_BUS_CONFIG,
  72. #endif
  73. };
  74. static struct nu_soft_i2c nu_soft_i2c_obj[sizeof(nu_soft_i2c_cfg) / sizeof(nu_soft_i2c_cfg[0])];
  75. static const struct rt_i2c_bit_ops nu_soft_i2c_bit_ops =
  76. {
  77. .data = RT_NULL,
  78. .set_sda = nu_soft_i2c_set_sda,
  79. .set_scl = nu_soft_i2c_set_scl,
  80. .get_sda = nu_soft_i2c_get_sda,
  81. .get_scl = nu_soft_i2c_get_scl,
  82. .udelay = nu_soft_i2c_udelay,
  83. .delay_us = 1,
  84. .timeout = 100
  85. };
  86. /* Functions define ------------------------------------------------------------*/
  87. /**
  88. * The time delay function.
  89. *
  90. * @param microseconds.
  91. */
  92. static void nu_soft_i2c_udelay(rt_uint32_t us)
  93. {
  94. rt_hw_us_delay(us);
  95. }
  96. /**
  97. * This function initializes the soft i2c pin.
  98. *
  99. * @param soft i2c config class.
  100. */
  101. static void nu_soft_i2c_gpio_init(const struct nu_soft_i2c_config *cfg)
  102. {
  103. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
  104. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  105. rt_pin_write(cfg->scl, PIN_HIGH);
  106. rt_pin_write(cfg->sda, PIN_HIGH);
  107. }
  108. /**
  109. * if i2c is locked, this function will unlock it
  110. *
  111. * @param soft i2c config class
  112. *
  113. * @return RT_EOK indicates successful unlock.
  114. */
  115. static rt_err_t nu_soft_i2c_bus_unlock(const struct nu_soft_i2c_config *cfg)
  116. {
  117. rt_int32_t i = 0;
  118. if (PIN_LOW == rt_pin_read(cfg->sda))
  119. {
  120. while (i++ < 9)
  121. {
  122. rt_pin_write(cfg->scl, PIN_HIGH);
  123. nu_soft_i2c_udelay(100);
  124. rt_pin_write(cfg->scl, PIN_LOW);
  125. nu_soft_i2c_udelay(100);
  126. }
  127. }
  128. if (PIN_LOW == rt_pin_read(cfg->sda))
  129. {
  130. return -RT_ERROR;
  131. }
  132. return RT_EOK;
  133. }
  134. /**
  135. * This function sets the sda pin.
  136. *
  137. * @param soft i2c config class.
  138. * @param The sda pin state.
  139. */
  140. static void nu_soft_i2c_set_sda(void *data, rt_int32_t state)
  141. {
  142. struct nu_soft_i2c_config *cfg = (struct nu_soft_i2c_config *)data;
  143. rt_pin_write(cfg->sda, state ? PIN_HIGH : PIN_LOW);
  144. }
  145. /**
  146. * This function sets the scl pin.
  147. *
  148. * @param soft i2c config class.
  149. * @param The scl pin state.
  150. */
  151. static void nu_soft_i2c_set_scl(void *data, rt_int32_t state)
  152. {
  153. struct nu_soft_i2c_config *cfg = (struct nu_soft_i2c_config *)data;
  154. rt_pin_write(cfg->scl, state ? PIN_HIGH : PIN_LOW);
  155. }
  156. /**
  157. * This function gets the sda pin state.
  158. *
  159. * @param The sda pin state.
  160. */
  161. static rt_int32_t nu_soft_i2c_get_sda(void *data)
  162. {
  163. struct nu_soft_i2c_config *cfg = (struct nu_soft_i2c_config *)data;
  164. return rt_pin_read(cfg->sda);
  165. }
  166. /**
  167. * This function gets the scl pin state.
  168. *
  169. * @param The scl pin state.
  170. */
  171. static rt_int32_t nu_soft_i2c_get_scl(void *data)
  172. {
  173. struct nu_soft_i2c_config *cfg = (struct nu_soft_i2c_config *)data;
  174. return rt_pin_read(cfg->scl);
  175. }
  176. /* Soft I2C initialization function */
  177. int rt_soft_i2c_init(void)
  178. {
  179. rt_size_t obj_num = sizeof(nu_soft_i2c_obj) / sizeof(struct nu_soft_i2c);
  180. rt_err_t result;
  181. for (int i = 0; i < obj_num; i++)
  182. {
  183. nu_soft_i2c_obj[i].ops = nu_soft_i2c_bit_ops;
  184. nu_soft_i2c_obj[i].ops.data = (void *)&nu_soft_i2c_cfg[i];
  185. nu_soft_i2c_obj[i].soft_i2c_bus.priv = &nu_soft_i2c_obj[i].ops;
  186. nu_soft_i2c_gpio_init(&nu_soft_i2c_cfg[i]);
  187. result = rt_i2c_bit_add_bus(&nu_soft_i2c_obj[i].soft_i2c_bus, nu_soft_i2c_cfg[i].bus_name);
  188. RT_ASSERT(result == RT_EOK);
  189. nu_soft_i2c_bus_unlock(&nu_soft_i2c_cfg[i]);
  190. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  191. nu_soft_i2c_cfg[i].bus_name,
  192. nu_soft_i2c_cfg[i].scl,
  193. nu_soft_i2c_cfg[i].sda);
  194. }
  195. return 0;
  196. }
  197. INIT_DEVICE_EXPORT(rt_soft_i2c_init);
  198. #endif //#if (defined(BSP_USING_SOFT_I2C) && defined(RT_USING_I2C_BITOPS) && defined(RT_USING_I2C) && defined(RT_USING_PIN))