drv_i2c.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. *
  3. * SPDX-License-Identifier: Apache-2.0
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the License); you may
  6. * not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * Change Logs:
  18. * Date Author Notes
  19. * 2019-01-24 wangyq the first version
  20. * 2019-11-01 wangyq update libraries
  21. * 2020-12-15 liuhy update libraries
  22. */
  23. #include <rthw.h>
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include "board.h"
  27. #include "drv_i2c.h"
  28. #ifdef RT_USING_I2C
  29. #define TIMEOUT 0x0FFF
  30. /* I2C struct definition */
  31. #ifdef BSP_USING_I2C0
  32. static i2c_handle_t _h_i2c0;
  33. #endif
  34. #ifdef BSP_USING_I2C1
  35. static i2c_handle_t _h_i2c1;
  36. #endif
  37. static void _i2c_init(void)
  38. {
  39. gpio_init_t gpio_instruct;
  40. /* Initialize I2C Pin */
  41. gpio_instruct.mode = GPIO_MODE_OUTPUT;
  42. gpio_instruct.odos = GPIO_OPEN_DRAIN;
  43. gpio_instruct.pupd = GPIO_PUSH_UP;
  44. gpio_instruct.odrv = GPIO_OUT_DRIVE_NORMAL;
  45. gpio_instruct.flt = GPIO_FILTER_DISABLE;
  46. gpio_instruct.type = GPIO_TYPE_CMOS;
  47. gpio_instruct.func = GPIO_FUNC_5;
  48. #ifdef BSP_USING_I2C0
  49. #if defined(ES_I2C0_SCL_GPIO_FUNC)&&defined(ES_I2C0_SCL_GPIO_PORT)&&defined(ES_I2C0_SCL_GPIO_PIN)
  50. gpio_instruct.func = ES_I2C0_SCL_GPIO_FUNC;
  51. ald_gpio_init(ES_I2C0_SCL_GPIO_PORT, ES_I2C0_SCL_GPIO_PIN, &gpio_instruct);
  52. #endif
  53. #if defined(ES_I2C0_SDA_GPIO_FUNC)&&defined(ES_I2C0_SDA_GPIO_PORT)&&defined(ES_I2C0_SDA_GPIO_PIN)
  54. gpio_instruct.func = ES_I2C0_SDA_GPIO_FUNC;
  55. ald_gpio_init(ES_I2C0_SDA_GPIO_PORT, ES_I2C0_SDA_GPIO_PIN, &gpio_instruct);
  56. #endif
  57. /* Initialize I2C Function */
  58. _h_i2c0.perh = I2C0;
  59. _h_i2c0.init.duty = I2C_DUTYCYCLE_2;
  60. _h_i2c0.init.clk_speed = ES_I2C0_CLK_SPEED;
  61. _h_i2c0.init.own_addr1 = ES_I2C0_OWN_ADDR1;
  62. _h_i2c0.init.addr_mode = ES_I2C0_ADDR_MODE;
  63. _h_i2c0.init.general_call = ES_I2C0_GENERAL_CALL;
  64. _h_i2c0.init.no_stretch = ES_I2C0_STRETCH;
  65. ald_i2c_reset(&_h_i2c0);
  66. ald_i2c_init(&_h_i2c0);
  67. #endif
  68. #ifdef BSP_USING_I2C1
  69. #if defined(ES_I2C1_SCL_GPIO_FUNC)&&defined(ES_I2C1_SCL_GPIO_PORT)&&defined(ES_I2C1_SCL_GPIO_PIN)
  70. gpio_instruct.func = ES_I2C1_SCL_GPIO_FUNC;
  71. ald_gpio_init(ES_I2C1_SCL_GPIO_PORT, ES_I2C1_SCL_GPIO_PIN, &gpio_instruct);
  72. #endif
  73. #if defined(ES_I2C1_SDA_GPIO_FUNC)&&defined(ES_I2C1_SDA_GPIO_PORT)&&defined(ES_I2C1_SDA_GPIO_PIN)
  74. gpio_instruct.func = ES_I2C1_SDA_GPIO_FUNC;
  75. ald_gpio_init(ES_I2C1_SDA_GPIO_PORT, ES_I2C1_SDA_GPIO_PIN, &gpio_instruct);
  76. #endif
  77. /* Initialize i2c function */
  78. _h_i2c1.perh = I2C1;
  79. _h_i2c1.init.duty = I2C_DUTYCYCLE_2;
  80. _h_i2c1.init.clk_speed = ES_I2C1_CLK_SPEED;
  81. _h_i2c1.init.own_addr1 = ES_I2C1_OWN_ADDR1;
  82. _h_i2c1.init.addr_mode = ES_I2C1_ADDR_MODE;
  83. _h_i2c1.init.general_call = ES_I2C1_GENERAL_CALL;
  84. _h_i2c1.init.no_stretch = ES_I2C1_STRETCH;
  85. ald_i2c_reset(&_h_i2c1);
  86. ald_i2c_init(&_h_i2c1);
  87. #endif
  88. }
  89. static rt_ssize_t es32f0_master_xfer(struct rt_i2c_bus_device *bus,
  90. struct rt_i2c_msg msgs[],
  91. rt_uint32_t num)
  92. {
  93. struct rt_i2c_msg *msg;
  94. rt_uint32_t i;
  95. rt_err_t ret = -RT_ERROR;
  96. for (i = 0; i < num; i++)
  97. {
  98. msg = &msgs[i];
  99. if (msg->flags & RT_I2C_RD)
  100. {
  101. if (ald_i2c_master_recv(bus->priv, msg->addr << 1, msg->buf, msg->len, TIMEOUT) != 0)
  102. {
  103. LOG_E("i2c bus write failed,i2c bus stop!\n");
  104. goto out;
  105. }
  106. }
  107. else
  108. {
  109. if (ald_i2c_master_send(bus->priv, msg->addr << 1, msg->buf, msg->len, TIMEOUT) != 0)
  110. {
  111. LOG_E("i2c bus write failed,i2c bus stop!\n");
  112. goto out;
  113. }
  114. }
  115. }
  116. ret = i;
  117. out:
  118. LOG_E("send stop condition\n");
  119. return ret;
  120. }
  121. const struct rt_i2c_bus_device_ops es32f0_i2c_ops =
  122. {
  123. es32f0_master_xfer,
  124. RT_NULL,
  125. RT_NULL,
  126. };
  127. int rt_hw_i2c_init(void)
  128. {
  129. int result = RT_EOK;
  130. _i2c_init();
  131. #ifdef BSP_USING_I2C0
  132. /* define i2c Instance */
  133. static struct rt_i2c_bus_device _i2c_device0;
  134. rt_memset((void *)&_i2c_device0, 0, sizeof(struct rt_i2c_bus_device));
  135. _i2c_device0.ops = &es32f0_i2c_ops;
  136. _i2c_device0.priv = &_h_i2c0;
  137. result = rt_i2c_bus_device_register(&_i2c_device0, ES_DEVICE_NAME_I2C0);
  138. if (result != RT_EOK)
  139. {
  140. return result;
  141. }
  142. #endif
  143. #ifdef BSP_USING_I2C1
  144. /* define i2c Instance */
  145. static struct rt_i2c_bus_device _i2c_device1;
  146. rt_memset((void *)&_i2c_device1, 0, sizeof(struct rt_i2c_bus_device));
  147. _i2c_device1.ops = &es32f0_i2c_ops;
  148. _i2c_device1.priv = &_h_i2c1;
  149. rt_i2c_bus_device_register(&_i2c_device1, ES_DEVICE_NAME_I2C1);
  150. if (result != RT_EOK)
  151. {
  152. return result;
  153. }
  154. #endif
  155. return RT_EOK;
  156. }
  157. INIT_DEVICE_EXPORT(rt_hw_i2c_init);
  158. #endif