drv_softi2c.c 6.3 KB

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