dev_i2c.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 __DEV_I2C_H__
  12. #define __DEV_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. #define RT_I2C_DEV_CTRL_10BIT (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x01)
  25. #define RT_I2C_DEV_CTRL_ADDR (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x02)
  26. #define RT_I2C_DEV_CTRL_TIMEOUT (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x03)
  27. #define RT_I2C_DEV_CTRL_RW (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x04)
  28. #define RT_I2C_DEV_CTRL_CLK (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x05)
  29. #define RT_I2C_DEV_CTRL_UNLOCK (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x06)
  30. #define RT_I2C_DEV_CTRL_GET_STATE (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x07)
  31. #define RT_I2C_DEV_CTRL_GET_MODE (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x08)
  32. #define RT_I2C_DEV_CTRL_GET_ERROR (RT_DEVICE_CTRL_BASE(I2CBUS) + 0x09)
  33. struct rt_i2c_priv_data
  34. {
  35. struct rt_i2c_msg *msgs;
  36. rt_size_t number;
  37. };
  38. struct rt_i2c_msg
  39. {
  40. rt_uint16_t addr;
  41. rt_uint16_t flags;
  42. rt_uint16_t len;
  43. rt_uint8_t *buf;
  44. };
  45. struct rt_i2c_bus_device;
  46. struct rt_i2c_bus_device_ops
  47. {
  48. rt_ssize_t (*master_xfer)(struct rt_i2c_bus_device *bus,
  49. struct rt_i2c_msg msgs[],
  50. rt_uint32_t num);
  51. rt_ssize_t (*slave_xfer)(struct rt_i2c_bus_device *bus,
  52. struct rt_i2c_msg msgs[],
  53. rt_uint32_t num);
  54. rt_err_t (*i2c_bus_control)(struct rt_i2c_bus_device *bus,
  55. int cmd,
  56. void *args);
  57. };
  58. /*for i2c bus driver*/
  59. struct rt_i2c_bus_device
  60. {
  61. struct rt_device parent;
  62. const struct rt_i2c_bus_device_ops *ops;
  63. rt_uint16_t flags;
  64. struct rt_mutex lock;
  65. rt_uint32_t timeout;
  66. rt_uint32_t retries;
  67. void *priv;
  68. };
  69. struct rt_i2c_client
  70. {
  71. #ifdef RT_USING_DM
  72. struct rt_device parent;
  73. const char *name;
  74. const struct rt_i2c_device_id *id;
  75. const struct rt_ofw_node_id *ofw_id;
  76. #endif
  77. struct rt_i2c_bus_device *bus;
  78. rt_uint16_t client_addr;
  79. };
  80. #ifdef RT_USING_DM
  81. struct rt_i2c_device_id
  82. {
  83. char name[20];
  84. void *data;
  85. };
  86. struct rt_i2c_driver
  87. {
  88. struct rt_driver parent;
  89. const struct rt_i2c_device_id *ids;
  90. const struct rt_ofw_node_id *ofw_ids;
  91. rt_err_t (*probe)(struct rt_i2c_client *client);
  92. rt_err_t (*remove)(struct rt_i2c_client *client);
  93. rt_err_t (*shutdown)(struct rt_i2c_client *client);
  94. };
  95. rt_err_t rt_i2c_driver_register(struct rt_i2c_driver *driver);
  96. rt_err_t rt_i2c_device_register(struct rt_i2c_client *client);
  97. #define RT_I2C_DRIVER_EXPORT(driver) RT_DRIVER_EXPORT(driver, i2c, BUILIN)
  98. #endif /* RT_USING_DM */
  99. rt_err_t rt_i2c_bus_device_device_init(struct rt_i2c_bus_device *bus,
  100. const char *name);
  101. rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
  102. const char *bus_name);
  103. struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name);
  104. rt_ssize_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
  105. struct rt_i2c_msg msgs[],
  106. rt_uint32_t num);
  107. rt_err_t rt_i2c_control(struct rt_i2c_bus_device *bus,
  108. int cmd,
  109. void *args);
  110. rt_ssize_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
  111. rt_uint16_t addr,
  112. rt_uint16_t flags,
  113. const rt_uint8_t *buf,
  114. rt_uint32_t count);
  115. rt_ssize_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus,
  116. rt_uint16_t addr,
  117. rt_uint16_t flags,
  118. rt_uint8_t *buf,
  119. rt_uint32_t count);
  120. rt_inline rt_err_t rt_i2c_bus_lock(struct rt_i2c_bus_device *bus, rt_tick_t timeout)
  121. {
  122. return rt_mutex_take(&bus->lock, timeout);
  123. }
  124. rt_inline rt_err_t rt_i2c_bus_unlock(struct rt_i2c_bus_device *bus)
  125. {
  126. return rt_mutex_release(&bus->lock);
  127. }
  128. #ifdef __cplusplus
  129. }
  130. #endif
  131. #endif