drv_softi2c.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. * 2020-5-28 YCHuang12 First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if (defined(BSP_USING_SOFT_I2C) && defined(BSP_USING_GPIO) && 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_set_sda(void *data, rt_int32_t state);
  60. static void nu_soft_i2c_set_scl(void *data, rt_int32_t state);
  61. static rt_int32_t nu_soft_i2c_get_sda(void *data);
  62. static rt_int32_t nu_soft_i2c_get_scl(void *data);
  63. /* Private variables ------------------------------------------------------------*/
  64. static const struct nu_soft_i2c_config nu_soft_i2c_cfg[] =
  65. {
  66. #ifdef BSP_USING_SOFT_I2C0
  67. NU_SOFT_I2C0_BUS_CONFIG,
  68. #endif
  69. #ifdef BSP_USING_SOFT_I2C1
  70. NU_SOFT_I2C1_BUS_CONFIG,
  71. #endif
  72. };
  73. static struct nu_soft_i2c nu_soft_i2c_obj[sizeof(nu_soft_i2c_cfg) / sizeof(nu_soft_i2c_cfg[0])];
  74. static const struct rt_i2c_bit_ops nu_soft_i2c_bit_ops =
  75. {
  76. .data = RT_NULL,
  77. .set_sda = nu_soft_i2c_set_sda,
  78. .set_scl = nu_soft_i2c_set_scl,
  79. .get_sda = nu_soft_i2c_get_sda,
  80. .get_scl = nu_soft_i2c_get_scl,
  81. .udelay = rt_hw_us_delay,
  82. .delay_us = 1,
  83. .timeout = 100
  84. };
  85. /* Functions define ------------------------------------------------------------*/
  86. /**
  87. * This function initializes the soft i2c pin.
  88. *
  89. * @param soft i2c config class.
  90. */
  91. static void nu_soft_i2c_gpio_init(const struct nu_soft_i2c_config *cfg)
  92. {
  93. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
  94. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  95. rt_pin_write(cfg->scl, PIN_HIGH);
  96. rt_pin_write(cfg->sda, PIN_HIGH);
  97. }
  98. /**
  99. * if i2c is locked, this function will unlock it
  100. *
  101. * @param soft i2c config class
  102. *
  103. * @return RT_EOK indicates successful unlock.
  104. */
  105. static rt_err_t nu_soft_i2c_bus_unlock(const struct nu_soft_i2c_config *cfg)
  106. {
  107. rt_int32_t i = 0;
  108. if (PIN_LOW == rt_pin_read(cfg->sda))
  109. {
  110. while (i++ < 9)
  111. {
  112. rt_pin_write(cfg->scl, PIN_HIGH);
  113. rt_hw_us_delay(100);
  114. rt_pin_write(cfg->scl, PIN_LOW);
  115. rt_hw_us_delay(100);
  116. }
  117. }
  118. if (PIN_LOW == rt_pin_read(cfg->sda))
  119. {
  120. return -RT_ERROR;
  121. }
  122. return RT_EOK;
  123. }
  124. /**
  125. * This function sets the sda pin.
  126. *
  127. * @param soft i2c config class.
  128. * @param The sda pin state.
  129. */
  130. static void nu_soft_i2c_set_sda(void *data, rt_int32_t state)
  131. {
  132. struct nu_soft_i2c_config *cfg = (struct nu_soft_i2c_config *)data;
  133. rt_pin_write(cfg->sda, state ? PIN_HIGH : PIN_LOW);
  134. }
  135. /**
  136. * This function sets the scl pin.
  137. *
  138. * @param soft i2c config class.
  139. * @param The scl pin state.
  140. */
  141. static void nu_soft_i2c_set_scl(void *data, rt_int32_t state)
  142. {
  143. struct nu_soft_i2c_config *cfg = (struct nu_soft_i2c_config *)data;
  144. rt_pin_write(cfg->scl, state ? PIN_HIGH : PIN_LOW);
  145. }
  146. /**
  147. * This function gets the sda pin state.
  148. *
  149. * @param The sda pin state.
  150. */
  151. static rt_int32_t nu_soft_i2c_get_sda(void *data)
  152. {
  153. struct nu_soft_i2c_config *cfg = (struct nu_soft_i2c_config *)data;
  154. return rt_pin_read(cfg->sda);
  155. }
  156. /**
  157. * This function gets the scl pin state.
  158. *
  159. * @param The scl pin state.
  160. */
  161. static rt_int32_t nu_soft_i2c_get_scl(void *data)
  162. {
  163. struct nu_soft_i2c_config *cfg = (struct nu_soft_i2c_config *)data;
  164. return rt_pin_read(cfg->scl);
  165. }
  166. /* Soft I2C initialization function */
  167. int rt_soft_i2c_init(void)
  168. {
  169. rt_size_t obj_num = sizeof(nu_soft_i2c_obj) / sizeof(struct nu_soft_i2c);
  170. rt_err_t result;
  171. for (int i = 0; i < obj_num; i++)
  172. {
  173. nu_soft_i2c_obj[i].ops = nu_soft_i2c_bit_ops;
  174. nu_soft_i2c_obj[i].ops.data = (void *)&nu_soft_i2c_cfg[i];
  175. nu_soft_i2c_obj[i].soft_i2c_bus.priv = &nu_soft_i2c_obj[i].ops;
  176. nu_soft_i2c_gpio_init(&nu_soft_i2c_cfg[i]);
  177. result = rt_i2c_bit_add_bus(&nu_soft_i2c_obj[i].soft_i2c_bus, nu_soft_i2c_cfg[i].bus_name);
  178. RT_ASSERT(result == RT_EOK);
  179. nu_soft_i2c_bus_unlock(&nu_soft_i2c_cfg[i]);
  180. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  181. nu_soft_i2c_cfg[i].bus_name,
  182. nu_soft_i2c_cfg[i].scl,
  183. nu_soft_i2c_cfg[i].sda);
  184. }
  185. return 0;
  186. }
  187. INIT_DEVICE_EXPORT(rt_soft_i2c_init);
  188. #endif //#if (defined(BSP_USING_SOFT_I2C) && defined(BSP_USING_GPIO) && defined(RT_USING_I2C_BITOPS) && defined(RT_USING_I2C) && defined(RT_USING_PIN))