i2c.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * 2012-04-25 weety first version
  9. * 2021-04-20 RiceChen added support for bus control api
  10. */
  11. #ifndef __I2C_H__
  12. #define __I2C_H__
  13. #include <rtthread.h>
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #define RT_I2C_WR 0x0000
  18. #define RT_I2C_RD (1u << 0)
  19. #define RT_I2C_ADDR_10BIT (1u << 2) /* this is a ten bit chip address */
  20. #define RT_I2C_NO_START (1u << 4)
  21. #define RT_I2C_IGNORE_NACK (1u << 5)
  22. #define RT_I2C_NO_READ_ACK (1u << 6) /* when I2C reading, we do not ACK */
  23. #define RT_I2C_NO_STOP (1u << 7)
  24. struct rt_i2c_msg
  25. {
  26. rt_uint16_t addr;
  27. rt_uint16_t flags;
  28. rt_uint16_t len;
  29. rt_uint8_t *buf;
  30. };
  31. struct rt_i2c_bus_device;
  32. struct rt_i2c_bus_device_ops
  33. {
  34. rt_ssize_t (*master_xfer)(struct rt_i2c_bus_device *bus,
  35. struct rt_i2c_msg msgs[],
  36. rt_uint32_t num);
  37. rt_ssize_t (*slave_xfer)(struct rt_i2c_bus_device *bus,
  38. struct rt_i2c_msg msgs[],
  39. rt_uint32_t num);
  40. rt_err_t (*i2c_bus_control)(struct rt_i2c_bus_device *bus,
  41. int cmd,
  42. void *args);
  43. };
  44. /*for i2c bus driver*/
  45. struct rt_i2c_bus_device
  46. {
  47. struct rt_device parent;
  48. const struct rt_i2c_bus_device_ops *ops;
  49. rt_uint16_t flags;
  50. struct rt_mutex lock;
  51. rt_uint32_t timeout;
  52. rt_uint32_t retries;
  53. void *priv;
  54. };
  55. struct rt_i2c_client
  56. {
  57. #ifdef RT_USING_DM
  58. struct rt_device parent;
  59. const char *name;
  60. const struct rt_i2c_device_id *id;
  61. const struct rt_ofw_node_id *ofw_id;
  62. #endif
  63. struct rt_i2c_bus_device *bus;
  64. rt_uint16_t client_addr;
  65. };
  66. #ifdef RT_USING_DM
  67. struct rt_i2c_device_id
  68. {
  69. char name[20];
  70. void *data;
  71. };
  72. struct rt_i2c_driver
  73. {
  74. struct rt_driver parent;
  75. const struct rt_i2c_device_id *ids;
  76. const struct rt_ofw_node_id *ofw_ids;
  77. rt_err_t (*probe)(struct rt_i2c_client *client);
  78. rt_err_t (*remove)(struct rt_i2c_client *client);
  79. rt_err_t (*shutdown)(struct rt_i2c_client *client);
  80. };
  81. rt_err_t rt_i2c_driver_register(struct rt_i2c_driver *driver);
  82. rt_err_t rt_i2c_device_register(struct rt_i2c_client *client);
  83. #define RT_I2C_DRIVER_EXPORT(driver) RT_DRIVER_EXPORT(driver, i2c, BUILIN)
  84. #endif /* RT_USING_DM */
  85. rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
  86. const char *bus_name);
  87. struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name);
  88. rt_ssize_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
  89. struct rt_i2c_msg msgs[],
  90. rt_uint32_t num);
  91. rt_err_t rt_i2c_control(struct rt_i2c_bus_device *bus,
  92. int cmd,
  93. void *args);
  94. rt_ssize_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
  95. rt_uint16_t addr,
  96. rt_uint16_t flags,
  97. const rt_uint8_t *buf,
  98. rt_uint32_t count);
  99. rt_ssize_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus,
  100. rt_uint16_t addr,
  101. rt_uint16_t flags,
  102. rt_uint8_t *buf,
  103. rt_uint32_t count);
  104. rt_inline rt_err_t rt_i2c_bus_lock(struct rt_i2c_bus_device *bus, rt_tick_t timeout)
  105. {
  106. return rt_mutex_take(&bus->lock, timeout);
  107. }
  108. rt_inline rt_err_t rt_i2c_bus_unlock(struct rt_i2c_bus_device *bus)
  109. {
  110. return rt_mutex_release(&bus->lock);
  111. }
  112. #ifdef __cplusplus
  113. }
  114. #endif
  115. #endif