drv_soft_i2c.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (C) 2020, Huada Semiconductor Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-10-30 CDT first version
  9. * 2021-01-18 CDT modify i2c gpio init
  10. */
  11. /*******************************************************************************
  12. * Include files
  13. ******************************************************************************/
  14. #include "drv_soft_i2c.h"
  15. #include "board.h"
  16. #if defined RT_USING_I2C
  17. #if !defined(BSP_USING_I2C1) && !defined(BSP_USING_I2C2) && \
  18. !defined(BSP_USING_I2C3) && !defined(BSP_USING_I2C4) && \
  19. !defined(BSP_USING_I2C5) && !defined(BSP_USING_I2C6)
  20. #error "Please define at least one BSP_USING_I2Cx"
  21. #endif
  22. /*******************************************************************************
  23. * Local type definitions ('typedef')
  24. ******************************************************************************/
  25. /*******************************************************************************
  26. * Local pre-processor symbols/macros ('#define')
  27. ******************************************************************************/
  28. /*******************************************************************************
  29. * Global variable definitions (declared in header file with 'extern')
  30. ******************************************************************************/
  31. /*******************************************************************************
  32. * Local function prototypes ('static')
  33. ******************************************************************************/
  34. /*******************************************************************************
  35. * Local variable definitions ('static')
  36. ******************************************************************************/
  37. static const struct hc32_soft_i2c_config soft_i2c_config[] =
  38. {
  39. #ifdef BSP_USING_I2C1
  40. I2C1_BUS_CONFIG,
  41. #endif
  42. #ifdef BSP_USING_I2C2
  43. I2C2_BUS_CONFIG,
  44. #endif
  45. #ifdef BSP_USING_I2C3
  46. I2C3_BUS_CONFIG,
  47. #endif
  48. #ifdef BSP_USING_I2C4
  49. I2C4_BUS_CONFIG,
  50. #endif
  51. #ifdef BSP_USING_I2C5
  52. I2C5_BUS_CONFIG,
  53. #endif
  54. #ifdef BSP_USING_I2C6
  55. I2C6_BUS_CONFIG,
  56. #endif
  57. };
  58. static struct hc32_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
  59. /*******************************************************************************
  60. * Function implementation - global ('extern') and local ('static')
  61. ******************************************************************************/
  62. /**
  63. * This function initializes the i2c pin.
  64. *
  65. * @param Hc32 i2c dirver class.
  66. */
  67. static void hc32_i2c_gpio_init(struct hc32_i2c *i2c)
  68. {
  69. struct hc32_soft_i2c_config* cfg = (struct hc32_soft_i2c_config*)i2c->ops.data;
  70. rt_pin_mode(cfg->scl_pin, PIN_MODE_OUTPUT_OD);
  71. rt_pin_mode(cfg->sda_pin, PIN_MODE_OUTPUT_OD);
  72. rt_pin_write(cfg->scl_pin, PIN_HIGH);
  73. rt_pin_write(cfg->sda_pin, PIN_HIGH);
  74. }
  75. /**
  76. * This function sets the sda pin.
  77. *
  78. * @param Hc32 config class.
  79. * @param The sda pin state.
  80. */
  81. static void hc32_set_sda(void *data, rt_int32_t state)
  82. {
  83. struct hc32_soft_i2c_config* cfg = (struct hc32_soft_i2c_config*)data;
  84. if (state)
  85. rt_pin_write(cfg->sda_pin, PIN_HIGH);
  86. else
  87. rt_pin_write(cfg->sda_pin, PIN_LOW);
  88. }
  89. /**
  90. * This function sets the scl pin.
  91. *
  92. * @param Hc32 config class.
  93. * @param The scl pin state.
  94. */
  95. static void hc32_set_scl(void *data, rt_int32_t state)
  96. {
  97. struct hc32_soft_i2c_config* cfg = (struct hc32_soft_i2c_config*)data;
  98. if (state)
  99. rt_pin_write(cfg->scl_pin, PIN_HIGH);
  100. else
  101. rt_pin_write(cfg->scl_pin, PIN_LOW);
  102. }
  103. /**
  104. * This function gets the sda pin state.
  105. *
  106. * @param The sda pin state.
  107. */
  108. static rt_int32_t hc32_get_sda(void *data)
  109. {
  110. struct hc32_soft_i2c_config* cfg = (struct hc32_soft_i2c_config*)data;
  111. return rt_pin_read(cfg->sda_pin);
  112. }
  113. /**
  114. * This function gets the scl pin state.
  115. *
  116. * @param The scl pin state.
  117. */
  118. static rt_int32_t hc32_get_scl(void *data)
  119. {
  120. struct hc32_soft_i2c_config* cfg = (struct hc32_soft_i2c_config*)data;
  121. return rt_pin_read(cfg->scl_pin);
  122. }
  123. static void hc32_udelay(rt_uint32_t us)
  124. {
  125. rt_hw_us_delay(us);
  126. }
  127. static const struct rt_i2c_bit_ops hc32_bit_ops =
  128. {
  129. .data = RT_NULL,
  130. .set_sda = hc32_set_sda,
  131. .set_scl = hc32_set_scl,
  132. .get_sda = hc32_get_sda,
  133. .get_scl = hc32_get_scl,
  134. .udelay = hc32_udelay,
  135. .delay_us = 1,
  136. .timeout = 100
  137. };
  138. static rt_err_t hc32_i2c_bus_unlock(const struct hc32_soft_i2c_config *cfg)
  139. {
  140. rt_uint32_t i = 0;
  141. if (PIN_LOW == rt_pin_read(cfg->sda_pin))
  142. {
  143. while (i++ < 9)
  144. {
  145. rt_pin_write(cfg->scl_pin, PIN_HIGH);
  146. //HC32_udelay(100);
  147. rt_pin_write(cfg->scl_pin, PIN_LOW);
  148. //hc32_udelay(100);
  149. }
  150. }
  151. if(PIN_LOW == rt_pin_read(cfg->sda_pin))
  152. return RT_ERROR;
  153. return RT_EOK;
  154. }
  155. /* I2C initialization function */
  156. int hc32_hw_i2c_init(void)
  157. {
  158. rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct hc32_i2c);
  159. rt_err_t result;
  160. for (int i = 0; i < obj_num; i++)
  161. {
  162. i2c_obj[i].ops = hc32_bit_ops;
  163. i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
  164. i2c_obj[i].i2c1_bus.priv = &i2c_obj[i].ops;
  165. hc32_i2c_gpio_init(&i2c_obj[i]);
  166. result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c1_bus, soft_i2c_config[i].bus_name);
  167. RT_ASSERT(result == RT_EOK);
  168. hc32_i2c_bus_unlock(&soft_i2c_config[i]);
  169. }
  170. return RT_EOK;
  171. }
  172. INIT_BOARD_EXPORT(hc32_hw_i2c_init);
  173. #endif /* RT_USING_I2C */
  174. /*******************************************************************************
  175. * EOF (not truncated)
  176. ******************************************************************************/