i2c.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. struct rt_i2c_client
  55. {
  56. struct rt_device parent;
  57. struct rt_i2c_bus_device *bus;
  58. rt_uint16_t client_addr;
  59. };
  60. #ifdef RT_I2C_DEBUG
  61. #define i2c_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__)
  62. #else
  63. #define i2c_dbg(fmt, ...)
  64. #endif
  65. rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
  66. const char *bus_name);
  67. struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name);
  68. rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
  69. struct rt_i2c_msg msgs[],
  70. rt_uint32_t num);
  71. rt_size_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
  72. rt_uint16_t addr,
  73. rt_uint16_t flags,
  74. const rt_uint8_t *buf,
  75. rt_uint32_t count);
  76. rt_size_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus,
  77. rt_uint16_t addr,
  78. rt_uint16_t flags,
  79. rt_uint8_t *buf,
  80. rt_uint32_t count);
  81. int rt_i2c_core_init(void);
  82. #ifdef __cplusplus
  83. }
  84. #endif
  85. #endif