drv_i2c.c 3.7 KB

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