drv_i2c.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023/05/10 flyingcys first version
  9. */
  10. #include <rthw.h>
  11. #include <rtdevice.h>
  12. #ifdef BSP_USING_I2C
  13. #include "drv_i2c.h"
  14. #define DBG_TAG "DRV.I2C"
  15. #define DBG_LVL DBG_WARNING
  16. #include <rtdbg.h>
  17. struct bl_i2c_bus
  18. {
  19. struct rt_i2c_bus_device parent;
  20. struct bflb_device_s *i2c;
  21. struct rt_mutex lock;
  22. };
  23. static rt_ssize_t _i2c_master_xfer(struct rt_i2c_bus_device *bus, struct rt_i2c_msg *msgs, rt_uint32_t num)
  24. {
  25. struct bl_i2c_bus *_i2c_bus = (struct bl_i2c_bus *)bus;
  26. struct rt_i2c_msg *msg;
  27. struct bflb_i2c_msg_s *bflb_i2c_msg;
  28. int i;
  29. int ret;
  30. bflb_i2c_msg = rt_malloc(sizeof(struct bflb_i2c_msg_s) * num);
  31. if (!bflb_i2c_msg)
  32. {
  33. LOG_E("i2c xfer malloc(%d) failure\n", sizeof(struct bflb_i2c_msg_s) * num);
  34. return -RT_ENOMEM;
  35. }
  36. rt_memset(bflb_i2c_msg, 0, sizeof(struct bflb_i2c_msg_s) * num);
  37. for (i = 0; i < num; i++)
  38. {
  39. msg = &msgs[i];
  40. if (msg->flags & RT_I2C_RD)
  41. {
  42. bflb_i2c_msg[i].flags = I2C_M_READ;
  43. }
  44. else
  45. {
  46. bflb_i2c_msg[i].flags = 0;
  47. }
  48. if (_i2c_bus->parent.flags & RT_I2C_DEV_CTRL_10BIT)
  49. {
  50. bflb_i2c_msg[i].flags |= I2C_M_TEN;
  51. }
  52. bflb_i2c_msg[i].addr = msg->addr;
  53. bflb_i2c_msg[i].length = msg->len;
  54. bflb_i2c_msg[i].buffer = msg->buf;
  55. }
  56. rt_mutex_take(&_i2c_bus->lock, RT_WAITING_FOREVER);
  57. ret = bflb_i2c_transfer(_i2c_bus->i2c, bflb_i2c_msg, num);
  58. rt_mutex_release(&_i2c_bus->lock);
  59. rt_free(bflb_i2c_msg);
  60. bflb_i2c_msg = RT_NULL;
  61. if (ret < 0)
  62. {
  63. i = 0;
  64. LOG_E("i2c xfer failure %d\n", ret);
  65. }
  66. return i;
  67. }
  68. static const struct rt_i2c_bus_device_ops _i2c_ops =
  69. {
  70. _i2c_master_xfer,
  71. RT_NULL,
  72. RT_NULL
  73. };
  74. int rt_hw_i2c_init(void)
  75. {
  76. struct bflb_device_s *gpio;
  77. gpio = bflb_device_get_by_name("gpio");
  78. #ifdef BSP_USING_I2C0
  79. static struct bl_i2c_bus i2c_bus0;
  80. /* I2C0_SCL */
  81. bflb_gpio_init(gpio, I2C0_GPIO_SCL, GPIO_FUNC_I2C0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  82. /* I2C0_SDA */
  83. bflb_gpio_init(gpio, I2C0_GPIO_SDA, GPIO_FUNC_I2C0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  84. /* init i2c bus device */
  85. rt_mutex_init(&i2c_bus0.lock, "i2c0_mutex", RT_IPC_FLAG_PRIO);
  86. i2c_bus0.parent.ops = &_i2c_ops;
  87. i2c_bus0.i2c = bflb_device_get_by_name("i2c0");
  88. bflb_i2c_init(i2c_bus0.i2c, I2C0_FREQUENCY);
  89. if (rt_i2c_bus_device_register(&i2c_bus0.parent, "i2c0") != RT_EOK)
  90. LOG_E("i2c bus register:%s failure\n", "i2c0");
  91. #endif
  92. #ifdef BSP_USING_I2C1
  93. static struct bl_i2c_bus i2c_bus1;
  94. /* I2C1_SCL */
  95. bflb_gpio_init(gpio, I2C1_GPIO_SCL, GPIO_FUNC_I2C1 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  96. /* I2C1_SDA */
  97. bflb_gpio_init(gpio, I2C1_GPIO_SDA, GPIO_FUNC_I2C1 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  98. /* init i2c bus device */
  99. rt_mutex_init(&i2c_bus1.lock, "i2c1_mutex", RT_IPC_FLAG_PRIO);
  100. i2c_bus1.parent.ops = &_i2c_ops;
  101. i2c_bus1.i2c = bflb_device_get_by_name("i2c1");
  102. bflb_i2c_init(i2c_bus1.i2c, I2C1_FREQUENCY);
  103. if (rt_i2c_bus_device_register(&i2c_bus1.parent, "i2c1") != RT_EOK)
  104. LOG_E("i2c bus register:%s failure\n", "i2c1");
  105. #endif
  106. #ifdef BSP_USING_I2C2
  107. static struct bl_i2c_bus i2c_bus2;
  108. /* I2C2_SCL */
  109. bflb_gpio_init(gpio, I2C2_GPIO_SCL, GPIO_FUNC_I2C2 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  110. /* I2C2_SDA */
  111. bflb_gpio_init(gpio, I2C2_GPIO_SDA, GPIO_FUNC_I2C2 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  112. /* init i2c bus device */
  113. rt_mutex_init(&i2c_bus2.lock, "i2c2_mutex", RT_IPC_FLAG_PRIO);
  114. i2c_bus2.parent.ops = &_i2c_ops;
  115. i2c_bus2.i2c = bflb_device_get_by_name("i2c2");
  116. bflb_i2c_init(i2c_bus2.i2c, I2C2_FREQUENCY);
  117. if (rt_i2c_bus_device_register(&i2c_bus2.parent, "i2c2") != RT_EOK)
  118. LOG_E("i2c bus register:%s failure\n", "i2c2");
  119. #endif
  120. #ifdef BSP_USING_I2C3
  121. static struct bl_i2c_bus i2c_bus3;
  122. /* I2C3_SCL */
  123. bflb_gpio_init(gpio, I2C3_GPIO_SCL, GPIO_FUNC_I2C3 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  124. /* I2C3_SDA */
  125. bflb_gpio_init(gpio, I2C3_GPIO_SDA, GPIO_FUNC_I2C3 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
  126. /* init i2c bus device */
  127. rt_mutex_init(&i2c_bus3.lock, "i2c3_mutex", RT_IPC_FLAG_PRIO);
  128. i2c_bus3.parent.ops = &_i2c_ops;
  129. i2c_bus3.i2c = bflb_device_get_by_name("i2c3");
  130. bflb_i2c_init(i2c_bus3.i2c, I2C3_FREQUENCY);
  131. if (rt_i2c_bus_device_register(&i2c_bus3.parent, "i2c3") != RT_EOK)
  132. LOG_E("i2c bus register:%s failure\n", "i2c3");
  133. #endif
  134. return 0;
  135. }
  136. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  137. #endif /* BSP_USING_I2C */