i2c.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2006-2018, 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. */
  10. #ifndef __I2C_H__
  11. #define __I2C_H__
  12. #include <rtthread.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #define RT_I2C_WR 0x0000
  17. #define RT_I2C_RD (1u << 0)
  18. #define RT_I2C_ADDR_10BIT (1u << 2) /* this is a ten bit chip address */
  19. #define RT_I2C_NO_START (1u << 4)
  20. #define RT_I2C_IGNORE_NACK (1u << 5)
  21. #define RT_I2C_NO_READ_ACK (1u << 6) /* when I2C reading, we do not ACK */
  22. struct rt_i2c_msg
  23. {
  24. rt_uint16_t addr;
  25. rt_uint16_t flags;
  26. rt_uint16_t len;
  27. rt_uint8_t *buf;
  28. };
  29. struct rt_i2c_bus_device;
  30. struct rt_i2c_bus_device_ops
  31. {
  32. rt_size_t (*master_xfer)(struct rt_i2c_bus_device *bus,
  33. struct rt_i2c_msg msgs[],
  34. rt_uint32_t num);
  35. rt_size_t (*slave_xfer)(struct rt_i2c_bus_device *bus,
  36. struct rt_i2c_msg msgs[],
  37. rt_uint32_t num);
  38. rt_err_t (*i2c_bus_control)(struct rt_i2c_bus_device *bus,
  39. rt_uint32_t,
  40. rt_uint32_t);
  41. };
  42. /*for i2c bus driver*/
  43. struct rt_i2c_bus_device
  44. {
  45. struct rt_device parent;
  46. const struct rt_i2c_bus_device_ops *ops;
  47. rt_uint16_t flags;
  48. rt_uint16_t addr;
  49. struct rt_mutex lock;
  50. rt_uint32_t timeout;
  51. rt_uint32_t retries;
  52. void *priv;
  53. };
  54. #ifdef RT_I2C_DEBUG
  55. #define i2c_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__)
  56. #else
  57. #define i2c_dbg(fmt, ...)
  58. #endif
  59. rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
  60. const char *bus_name);
  61. struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name);
  62. rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
  63. struct rt_i2c_msg msgs[],
  64. rt_uint32_t num);
  65. rt_size_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
  66. rt_uint16_t addr,
  67. rt_uint16_t flags,
  68. const rt_uint8_t *buf,
  69. rt_uint32_t count);
  70. rt_size_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus,
  71. rt_uint16_t addr,
  72. rt_uint16_t flags,
  73. rt_uint8_t *buf,
  74. rt_uint32_t count);
  75. int rt_i2c_core_init(void);
  76. #ifdef __cplusplus
  77. }
  78. #endif
  79. #endif