drv_i2c.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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-07-27 Chushicheng the first version
  9. */
  10. #include "drv_i2c.h"
  11. #include "pico/binary_info.h"
  12. #include "hardware/gpio.h"
  13. #include "hardware/i2c.h"
  14. #ifdef BSP_USING_I2C
  15. #define DBG_TAG "drv.i2c"
  16. #define DBG_LVL DBG_INFO
  17. #include <rtdbg.h>
  18. struct pico_i2c_bus
  19. {
  20. struct rt_i2c_bus_device parent;
  21. i2c_inst_t *handle;
  22. rt_uint8_t i2c_sda_pin;
  23. rt_uint8_t i2c_scl_pin;
  24. rt_uint32_t baud;
  25. char *device_name;
  26. };
  27. static struct pico_i2c_bus pico_i2c_obj[] =
  28. {
  29. #ifdef BSP_USING_I2C0
  30. {
  31. .handle = i2c0,
  32. .i2c_sda_pin = BSP_I2C0_SDA_PIN,
  33. .i2c_scl_pin = BSP_I2C0_SCL_PIN,
  34. .baud = 400000U,
  35. .device_name = "i2c0",
  36. },
  37. #endif
  38. #ifdef BSP_USING_I2C1
  39. {
  40. .handle = i2c1,
  41. .i2c_sda_pin = BSP_I2C1_SDA_PIN,
  42. .i2c_scl_pin = BSP_I2C1_SCL_PIN,
  43. .baud = 400000U,
  44. .device_name = "i2c1",
  45. },
  46. #endif
  47. };
  48. rt_ssize_t pico_i2c_xfer(struct rt_i2c_bus_device *bus,
  49. struct rt_i2c_msg msgs[],
  50. rt_uint32_t num)
  51. {
  52. struct rt_i2c_msg *msg;
  53. rt_uint32_t i;
  54. rt_err_t ret = -RT_ERROR;
  55. struct pico_i2c_bus *pico_i2c = (struct pico_i2c_bus *)bus;
  56. for (i = 0; i < num; i++)
  57. {
  58. msg = &msgs[i];
  59. if (msg->flags & RT_I2C_RD)
  60. {
  61. i2c_read_blocking(pico_i2c->handle, msg->addr, msg->buf, msg->len, false);
  62. }
  63. else
  64. {
  65. i2c_write_blocking(pico_i2c->handle, msg->addr, msg->buf, msg->len, false);
  66. }
  67. }
  68. return ret;
  69. }
  70. static const struct rt_i2c_bus_device_ops i2c_ops =
  71. {
  72. .master_xfer = pico_i2c_xfer,
  73. .slave_xfer = RT_NULL,
  74. .i2c_bus_control = RT_NULL
  75. };
  76. int rt_hw_i2c_init(void)
  77. {
  78. int result = RT_EOK;
  79. for (rt_size_t i = 0; i < sizeof(pico_i2c_obj) / sizeof(struct pico_i2c_bus); i++)
  80. {
  81. i2c_init(pico_i2c_obj[i].handle, pico_i2c_obj[i].baud);
  82. gpio_set_function(pico_i2c_obj[i].i2c_sda_pin, GPIO_FUNC_I2C);
  83. gpio_set_function(pico_i2c_obj[i].i2c_scl_pin, GPIO_FUNC_I2C);
  84. gpio_pull_up(pico_i2c_obj[i].i2c_sda_pin);
  85. gpio_pull_up(pico_i2c_obj[i].i2c_scl_pin);
  86. // Make the I2C pins available to picotool
  87. bi_decl(bi_2pins_with_func(pico_i2c_obj[i].i2c_sda_pin, pico_i2c_obj[i].i2c_scl_pin, GPIO_FUNC_I2C));
  88. pico_i2c_obj[i].parent.ops = &i2c_ops;
  89. /* register i2c device */
  90. if (rt_i2c_bus_device_register(&pico_i2c_obj[i].parent, pico_i2c_obj[i].device_name) == RT_EOK)
  91. {
  92. LOG_D("%s init success", pico_i2c_obj[i].device_name);
  93. }
  94. else
  95. {
  96. LOG_E("%s register failed", pico_i2c_obj[i].device_name);
  97. result = -RT_ERROR;
  98. }
  99. }
  100. return result;
  101. }
  102. INIT_BOARD_EXPORT(rt_hw_i2c_init);
  103. #endif /* BSP_USING_I2C */