drv_soft_i2c.c 5.7 KB

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