drv_i2c.c 3.9 KB

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