drv_i2c.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023/01/17 chushicheng first version
  9. */
  10. #include <board.h>
  11. #include "drv_i2c.h"
  12. #include "bl808_common.h"
  13. #ifdef RT_USING_I2C
  14. #define DBG_TAG "drv.i2c"
  15. #define DBG_LVL DBG_INFO
  16. #include <rtdbg.h>
  17. #if !defined(BSP_USING_I2C1)
  18. #error "Please define at least one BSP_USING_I2Cx"
  19. /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
  20. #endif
  21. static const struct bl808_soft_i2c_config soft_i2c_config[] =
  22. {
  23. #ifdef BSP_USING_I2C1
  24. I2C1_BUS_CONFIG,
  25. #endif
  26. };
  27. static struct bl808_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
  28. /**
  29. * This function initializes the i2c pin.
  30. *
  31. * @param bl808 i2c dirver class.
  32. */
  33. static void bl808_i2c_gpio_init(struct bl808_i2c *i2c)
  34. {
  35. struct bl808_soft_i2c_config* cfg = (struct bl808_soft_i2c_config*)i2c->ops.data;
  36. rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
  37. rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
  38. rt_pin_write(cfg->scl, PIN_HIGH);
  39. rt_pin_write(cfg->sda, PIN_HIGH);
  40. }
  41. /**
  42. * This function sets the sda pin.
  43. *
  44. * @param bl808 config class.
  45. * @param The sda pin state.
  46. */
  47. static void bl808_set_sda(void *data, rt_int32_t state)
  48. {
  49. struct bl808_soft_i2c_config* cfg = (struct bl808_soft_i2c_config*)data;
  50. if (state)
  51. {
  52. rt_pin_write(cfg->sda, PIN_HIGH);
  53. }
  54. else
  55. {
  56. rt_pin_write(cfg->sda, PIN_LOW);
  57. }
  58. }
  59. /**
  60. * This function sets the scl pin.
  61. *
  62. * @param bl808 config class.
  63. * @param The scl pin state.
  64. */
  65. static void bl808_set_scl(void *data, rt_int32_t state)
  66. {
  67. struct bl808_soft_i2c_config* cfg = (struct bl808_soft_i2c_config*)data;
  68. if (state)
  69. {
  70. rt_pin_write(cfg->scl, PIN_HIGH);
  71. }
  72. else
  73. {
  74. rt_pin_write(cfg->scl, PIN_LOW);
  75. }
  76. }
  77. /**
  78. * This function gets the sda pin state.
  79. *
  80. * @param The sda pin state.
  81. */
  82. static rt_int32_t bl808_get_sda(void *data)
  83. {
  84. struct bl808_soft_i2c_config* cfg = (struct bl808_soft_i2c_config*)data;
  85. return rt_pin_read(cfg->sda);
  86. }
  87. /**
  88. * This function gets the scl pin state.
  89. *
  90. * @param The scl pin state.
  91. */
  92. static rt_int32_t bl808_get_scl(void *data)
  93. {
  94. struct bl808_soft_i2c_config* cfg = (struct bl808_soft_i2c_config*)data;
  95. return rt_pin_read(cfg->scl);
  96. }
  97. /**
  98. * The time delay function.
  99. *
  100. * @param microseconds.
  101. */
  102. static void bl808_udelay(rt_uint32_t us)
  103. {
  104. arch_delay_us(us);
  105. }
  106. static const struct rt_i2c_bit_ops bl808_bit_ops_default =
  107. {
  108. .data = RT_NULL,
  109. .set_sda = bl808_set_sda,
  110. .set_scl = bl808_set_scl,
  111. .get_sda = bl808_get_sda,
  112. .get_scl = bl808_get_scl,
  113. .udelay = bl808_udelay,
  114. .delay_us = 1,
  115. .timeout = 100
  116. };
  117. /**
  118. * if i2c is locked, this function will unlock it
  119. *
  120. * @param bl808 config class
  121. *
  122. * @return RT_EOK indicates successful unlock.
  123. */
  124. static rt_err_t bl808_i2c_bus_unlock(const struct bl808_soft_i2c_config *cfg)
  125. {
  126. rt_int32_t i = 0;
  127. if (PIN_LOW == rt_pin_read(cfg->sda))
  128. {
  129. while (i++ < 9)
  130. {
  131. rt_pin_write(cfg->scl, PIN_HIGH);
  132. bl808_udelay(100);
  133. rt_pin_write(cfg->scl, PIN_LOW);
  134. bl808_udelay(100);
  135. }
  136. }
  137. if (PIN_LOW == rt_pin_read(cfg->sda))
  138. {
  139. return -RT_ERROR;
  140. }
  141. return RT_EOK;
  142. }
  143. /* I2C initialization function */
  144. int rt_hw_i2c_init(void)
  145. {
  146. rt_err_t result;
  147. for (rt_size_t i = 0; i < sizeof(i2c_obj) / sizeof(struct bl808_i2c); i++)
  148. {
  149. i2c_obj[i].ops = bl808_bit_ops_default;
  150. i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
  151. i2c_obj[i].i2c2_bus.priv = &i2c_obj[i].ops;
  152. bl808_i2c_gpio_init(&i2c_obj[i]);
  153. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c2_bus, soft_i2c_config[i].bus_name);
  154. RT_ASSERT(result == RT_EOK);
  155. bl808_i2c_bus_unlock(&soft_i2c_config[i]);
  156. LOG_D("software simulation %s init done, pin scl: %d, pin sda %d",
  157. soft_i2c_config[i].bus_name,
  158. soft_i2c_config[i].scl,
  159. soft_i2c_config[i].sda);
  160. }
  161. return RT_EOK;
  162. }
  163. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  164. #endif /* RT_USING_I2C */