i2c.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. struct rt_i2c_bus_device *bus;
  58. rt_uint16_t client_addr;
  59. };
  60. rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
  61. const char *bus_name);
  62. struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name);
  63. rt_ssize_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
  64. struct rt_i2c_msg msgs[],
  65. rt_uint32_t num);
  66. rt_err_t rt_i2c_control(struct rt_i2c_bus_device *bus,
  67. int cmd,
  68. void *args);
  69. rt_ssize_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
  70. rt_uint16_t addr,
  71. rt_uint16_t flags,
  72. const rt_uint8_t *buf,
  73. rt_uint32_t count);
  74. rt_ssize_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus,
  75. rt_uint16_t addr,
  76. rt_uint16_t flags,
  77. rt_uint8_t *buf,
  78. rt_uint32_t count);
  79. rt_inline rt_err_t rt_i2c_bus_lock(struct rt_i2c_bus_device *bus, rt_tick_t timeout)
  80. {
  81. return rt_mutex_take(&bus->lock, timeout);
  82. }
  83. rt_inline rt_err_t rt_i2c_bus_unlock(struct rt_i2c_bus_device *bus)
  84. {
  85. return rt_mutex_release(&bus->lock);
  86. }
  87. #ifdef __cplusplus
  88. }
  89. #endif
  90. #endif