drv_i2c.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (C) 2018 Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-01-14 wangyq the first version
  9. * 2019-11-01 wangyq update libraries
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #include "board.h"
  15. #include "drv_i2c.h"
  16. #include <ald_i2c.h>
  17. #include <ald_gpio.h>
  18. #include <rtdbg.h>
  19. #ifdef RT_USING_I2C
  20. #define TIMEOUT 0x0FFF
  21. /* I2C struct definition */
  22. #ifdef BSP_USING_I2C0
  23. static i2c_handle_t _h_i2c0;
  24. #endif
  25. #ifdef BSP_USING_I2C1
  26. static i2c_handle_t _h_i2c1;
  27. #endif
  28. static void _i2c_init(void)
  29. {
  30. gpio_init_t gpio_instruct;
  31. /* Initialize I2C Pin */
  32. gpio_instruct.mode = GPIO_MODE_OUTPUT;
  33. gpio_instruct.odos = GPIO_PUSH_PULL;
  34. gpio_instruct.pupd = GPIO_PUSH_UP;
  35. gpio_instruct.podrv = GPIO_OUT_DRIVE_1;
  36. gpio_instruct.nodrv = GPIO_OUT_DRIVE_0_1;
  37. gpio_instruct.flt = GPIO_FILTER_DISABLE;
  38. gpio_instruct.type = GPIO_TYPE_CMOS;
  39. gpio_instruct.func = GPIO_FUNC_5;
  40. #ifdef BSP_USING_I2C0
  41. /* Initialize I2C Function */
  42. _h_i2c0.perh = I2C0;
  43. _h_i2c0.init.clk_speed = 100000;
  44. _h_i2c0.init.own_addr1 = 0x0A;
  45. _h_i2c0.init.addr_mode = I2C_ADDR_7BIT;
  46. _h_i2c0.init.general_call = I2C_GENERALCALL_DISABLE;
  47. _h_i2c0.init.no_stretch = I2C_NOSTRETCH_ENABLE;
  48. ald_i2c_reset(&_h_i2c0);
  49. ald_i2c_init(&_h_i2c0);
  50. /* PB06->I2C0_SCL, PB07->I2C0_SDA */
  51. ald_gpio_init(GPIOB, GPIO_PIN_6 | GPIO_PIN_7, &gpio_instruct);
  52. #endif
  53. #ifdef BSP_USING_I2C1
  54. /* Initialize i2c function */
  55. _h_i2c1.perh = I2C1;
  56. _h_i2c1.init.clk_speed = 100000;
  57. _h_i2c1.init.own_addr1 = 0xA0;
  58. _h_i2c1.init.addr_mode = I2C_ADDR_7BIT;
  59. _h_i2c1.init.general_call = I2C_GENERALCALL_DISABLE;
  60. _h_i2c1.init.no_stretch = I2C_NOSTRETCH_ENABLE;
  61. ald_i2c_reset(&_h_i2c1);
  62. ald_i2c_init(&_h_i2c1);
  63. /* PA05->I2C1_SCL, PA06->I2C1_SDA */
  64. ald_gpio_init(GPIOA, GPIO_PIN_5 | GPIO_PIN_6, &gpio_instruct);
  65. #endif
  66. }
  67. static rt_size_t es32f3_master_xfer(struct rt_i2c_bus_device *bus,
  68. struct rt_i2c_msg msgs[],
  69. rt_uint32_t num)
  70. {
  71. struct rt_i2c_msg *msg;
  72. rt_uint32_t i;
  73. rt_err_t ret = RT_ERROR;
  74. for (i = 0; i < num; i++)
  75. {
  76. msg = &msgs[i];
  77. if (msg->flags & RT_I2C_RD)
  78. {
  79. if (ald_i2c_master_recv(bus->priv, msg->addr << 1, msg->buf, msg->len, TIMEOUT) != 0)
  80. {
  81. LOG_E("i2c bus write failed,i2c bus stop!\n");
  82. goto out;
  83. }
  84. }
  85. else
  86. {
  87. if (ald_i2c_master_send(bus->priv, msg->addr << 1, msg->buf, msg->len, TIMEOUT) != 0)
  88. {
  89. LOG_E("i2c bus write failed,i2c bus stop!\n");
  90. goto out;
  91. }
  92. }
  93. }
  94. ret = i;
  95. out:
  96. LOG_E("send stop condition\n");
  97. return ret;
  98. }
  99. const struct rt_i2c_bus_device_ops es32f3_i2c_ops =
  100. {
  101. es32f3_master_xfer,
  102. RT_NULL,
  103. RT_NULL,
  104. };
  105. int rt_hw_i2c_init(void)
  106. {
  107. int result = RT_EOK;
  108. _i2c_init();
  109. #ifdef BSP_USING_I2C0
  110. /* define i2c Instance */
  111. static struct rt_i2c_bus_device _i2c_device0;
  112. rt_memset((void *)&_i2c_device0, 0, sizeof(struct rt_i2c_bus_device));
  113. _i2c_device0.ops = &es32f3_i2c_ops;
  114. _i2c_device0.priv = &_h_i2c0;
  115. result = rt_i2c_bus_device_register(&_i2c_device0, "i2c0");
  116. if (result != RT_EOK)
  117. {
  118. return result;
  119. }
  120. #endif
  121. #ifdef BSP_USING_I2C1
  122. /* define i2c Instance */
  123. static struct rt_i2c_bus_device _i2c_device1;
  124. rt_memset((void *)&_i2c_device1, 0, sizeof(struct rt_i2c_bus_device));
  125. _i2c_device1.ops = &es32f3_i2c_ops;
  126. _i2c_device1.priv = &_h_i2c1;
  127. rt_i2c_bus_device_register(&_i2c_device1, "i2c1");
  128. if (result != RT_EOK)
  129. {
  130. return result;
  131. }
  132. #endif
  133. return RT_EOK;
  134. }
  135. INIT_DEVICE_EXPORT(rt_hw_i2c_init);
  136. #endif