drv_softi2c.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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);
  105. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT);
  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. rt_pin_mode(cfg->sda, PIN_MODE_INPUT);
  120. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT);
  121. if (PIN_LOW == rt_pin_read(cfg->sda))
  122. {
  123. while (i++ < 9)
  124. {
  125. rt_pin_write(cfg->scl, PIN_HIGH);
  126. nu_soft_i2c_udelay(100);
  127. rt_pin_write(cfg->scl, PIN_LOW);
  128. nu_soft_i2c_udelay(100);
  129. }
  130. }
  131. if (PIN_LOW == rt_pin_read(cfg->sda))
  132. {
  133. return -RT_ERROR;
  134. }
  135. return RT_EOK;
  136. }
  137. /**
  138. * This function sets the sda pin.
  139. *
  140. * @param soft i2c config class.
  141. * @param The sda pin state.
  142. */
  143. static void nu_soft_i2c_set_sda(void *data, rt_int32_t state)
  144. {
  145. struct nu_soft_i2c_config *cfg = (struct nu_soft_i2c_config *)data;
  146. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT);
  147. rt_pin_write(cfg->sda, state ? PIN_HIGH : PIN_LOW);
  148. }
  149. /**
  150. * This function sets the scl pin.
  151. *
  152. * @param soft i2c config class.
  153. * @param The scl pin state.
  154. */
  155. static void nu_soft_i2c_set_scl(void *data, rt_int32_t state)
  156. {
  157. struct nu_soft_i2c_config *cfg = (struct nu_soft_i2c_config *)data;
  158. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT);
  159. rt_pin_write(cfg->scl, state ? PIN_HIGH : PIN_LOW);
  160. }
  161. /**
  162. * This function gets the sda pin state.
  163. *
  164. * @param The sda pin state.
  165. */
  166. static rt_int32_t nu_soft_i2c_get_sda(void *data)
  167. {
  168. struct nu_soft_i2c_config *cfg = (struct nu_soft_i2c_config *)data;
  169. rt_pin_mode(cfg->sda, PIN_MODE_INPUT);
  170. return rt_pin_read(cfg->sda);
  171. }
  172. /**
  173. * This function gets the scl pin state.
  174. *
  175. * @param The scl pin state.
  176. */
  177. static rt_int32_t nu_soft_i2c_get_scl(void *data)
  178. {
  179. struct nu_soft_i2c_config *cfg = (struct nu_soft_i2c_config *)data;
  180. rt_pin_mode(cfg->scl, PIN_MODE_INPUT);
  181. return rt_pin_read(cfg->scl);
  182. }
  183. /* Soft I2C initialization function */
  184. int rt_soft_i2c_init(void)
  185. {
  186. rt_size_t obj_num = sizeof(nu_soft_i2c_obj) / sizeof(struct nu_soft_i2c);
  187. rt_err_t result;
  188. for (int i = 0; i < obj_num; i++)
  189. {
  190. nu_soft_i2c_obj[i].ops = nu_soft_i2c_bit_ops;
  191. nu_soft_i2c_obj[i].ops.data = (void *)&nu_soft_i2c_cfg[i];
  192. nu_soft_i2c_obj[i].soft_i2c_bus.priv = &nu_soft_i2c_obj[i].ops;
  193. nu_soft_i2c_gpio_init(&nu_soft_i2c_cfg[i]);
  194. result = rt_i2c_bit_add_bus(&nu_soft_i2c_obj[i].soft_i2c_bus, nu_soft_i2c_cfg[i].bus_name);
  195. RT_ASSERT(result == RT_EOK);
  196. nu_soft_i2c_bus_unlock(&nu_soft_i2c_cfg[i]);
  197. LOG_I("software simulation %s init done, pin scl: %d, pin sda %d",
  198. nu_soft_i2c_cfg[i].bus_name,
  199. nu_soft_i2c_cfg[i].scl,
  200. nu_soft_i2c_cfg[i].sda);
  201. }
  202. return 0;
  203. }
  204. INIT_DEVICE_EXPORT(rt_soft_i2c_init);
  205. #endif //#if (defined(BSP_USING_SOFT_I2C) && defined(RT_USING_I2C_BITOPS) && defined(RT_USING_I2C) && defined(RT_USING_PIN))