drv_i2c.c 3.6 KB

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