drv_i2c.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * 2019-03-19 wangyq the first version
  9. * 2019-11-01 wangyq update libraries
  10. * 2020-12-15 liuhy update libraries
  11. */
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #include <rtdbg.h>
  15. #include "board.h"
  16. #include "drv_i2c.h"
  17. #include <ald_i2c.h>
  18. #include <ald_gpio.h>
  19. #ifdef RT_USING_I2C
  20. #define TIMEOUT 0x0FFF
  21. /* I2C struct definition */
  22. static i2c_handle_t _h_i2c0, _h_i2c1;
  23. static void _i2c_init(void)
  24. {
  25. gpio_init_t gpio_instruct;
  26. /* Initialize I2C Pin */
  27. gpio_instruct.mode = GPIO_MODE_OUTPUT;
  28. gpio_instruct.odos = GPIO_OPEN_DRAIN;
  29. gpio_instruct.pupd = GPIO_PUSH_UP;
  30. gpio_instruct.odrv = GPIO_OUT_DRIVE_NORMAL;
  31. gpio_instruct.flt = GPIO_FILTER_DISABLE;
  32. gpio_instruct.type = GPIO_TYPE_CMOS;
  33. gpio_instruct.func = GPIO_FUNC_5;
  34. #ifdef BSP_USING_I2C0
  35. /* Initialize I2C Function */
  36. _h_i2c0.perh = I2C0;
  37. _h_i2c0.init.clk_speed = 100000;
  38. _h_i2c0.init.duty = I2C_DUTYCYCLE_2;
  39. _h_i2c0.init.own_addr1 = 0x0A;
  40. _h_i2c0.init.addr_mode = I2C_ADDR_7BIT;
  41. _h_i2c0.init.general_call = I2C_GENERALCALL_DISABLE;
  42. _h_i2c0.init.no_stretch = I2C_NOSTRETCH_ENABLE;
  43. ald_i2c_reset(&_h_i2c0);
  44. ald_i2c_init(&_h_i2c0);
  45. /* I2C0_SCL->PB8, I2C0_SDA->PB9 */
  46. ald_gpio_init(GPIOB, GPIO_PIN_8 | GPIO_PIN_9, &gpio_instruct);
  47. #endif
  48. #ifdef BSP_USING_I2C1
  49. /* Initialize i2c function */
  50. _h_i2c1.perh = I2C1;
  51. _h_i2c1.init.clk_speed = 100000;
  52. _h_i2c1.init.duty = I2C_DUTYCYCLE_2;
  53. _h_i2c1.init.own_addr1 = 0xA0;
  54. _h_i2c1.init.addr_mode = I2C_ADDR_7BIT;
  55. _h_i2c1.init.general_call = I2C_GENERALCALL_DISABLE;
  56. _h_i2c1.init.no_stretch = I2C_NOSTRETCH_ENABLE;
  57. ald_i2c_reset(&_h_i2c1);
  58. ald_i2c_init(&_h_i2c1);
  59. /* I2C1_SCL->PB10, I2C1_SDA->PB11 */
  60. ald_gpio_init(GPIOB, GPIO_PIN_10 | GPIO_PIN_11, &gpio_instruct);
  61. #endif
  62. }
  63. static rt_size_t es32f0_master_xfer(struct rt_i2c_bus_device *bus,
  64. struct rt_i2c_msg msgs[],
  65. rt_uint32_t num)
  66. {
  67. struct rt_i2c_msg *msg;
  68. rt_uint32_t i;
  69. rt_err_t ret = RT_ERROR;
  70. for (i = 0; i < num; i++)
  71. {
  72. msg = &msgs[i];
  73. if (msg->flags & RT_I2C_RD)
  74. {
  75. if (ald_i2c_master_recv(bus->priv, msg->addr << 1, msg->buf, msg->len, TIMEOUT) != 0)
  76. {
  77. LOG_E("i2c bus write failed,i2c bus stop!\n");
  78. goto out;
  79. }
  80. }
  81. else
  82. {
  83. if (ald_i2c_master_send(bus->priv, msg->addr << 1, msg->buf, msg->len, TIMEOUT) != 0)
  84. {
  85. LOG_E("i2c bus write failed,i2c bus stop!\n");
  86. goto out;
  87. }
  88. }
  89. }
  90. ret = i;
  91. out:
  92. LOG_E("send stop condition\n");
  93. return ret;
  94. }
  95. const struct rt_i2c_bus_device_ops es32f0_i2c_ops =
  96. {
  97. es32f0_master_xfer,
  98. RT_NULL,
  99. RT_NULL,
  100. };
  101. int rt_hw_i2c_init(void)
  102. {
  103. _i2c_init();
  104. #ifdef BSP_USING_I2C0
  105. /* define i2c Instance */
  106. static struct rt_i2c_bus_device _i2c_device0;
  107. rt_memset((void *)&_i2c_device0, 0, sizeof(struct rt_i2c_bus_device));
  108. _i2c_device0.ops = &es32f0_i2c_ops;
  109. _i2c_device0.priv = &_h_i2c0;
  110. rt_i2c_bus_device_register(&_i2c_device0, "i2c0");
  111. #endif
  112. #ifdef BSP_USING_I2C1
  113. /* define i2c Instance */
  114. static struct rt_i2c_bus_device _i2c_device1;
  115. rt_memset((void *)&_i2c_device1, 0, sizeof(struct rt_i2c_bus_device));
  116. _i2c_device1.ops = &es32f0_i2c_ops;
  117. _i2c_device1.priv = &_h_i2c1;
  118. rt_i2c_bus_device_register(&_i2c_device1, "i2c1");
  119. #endif
  120. return RT_EOK;
  121. }
  122. INIT_DEVICE_EXPORT(rt_hw_i2c_init);
  123. #endif