i2c.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #define RT_I2C_NO_STOP (1u << 7)
  23. struct rt_i2c_msg
  24. {
  25. rt_uint16_t addr;
  26. rt_uint16_t flags;
  27. rt_uint16_t len;
  28. rt_uint8_t *buf;
  29. };
  30. struct rt_i2c_bus_device;
  31. struct rt_i2c_bus_device_ops
  32. {
  33. rt_size_t (*master_xfer)(struct rt_i2c_bus_device *bus,
  34. struct rt_i2c_msg msgs[],
  35. rt_uint32_t num);
  36. rt_size_t (*slave_xfer)(struct rt_i2c_bus_device *bus,
  37. struct rt_i2c_msg msgs[],
  38. rt_uint32_t num);
  39. rt_err_t (*i2c_bus_control)(struct rt_i2c_bus_device *bus,
  40. rt_uint32_t,
  41. rt_uint32_t);
  42. };
  43. /*for i2c bus driver*/
  44. struct rt_i2c_bus_device
  45. {
  46. struct rt_device parent;
  47. const struct rt_i2c_bus_device_ops *ops;
  48. rt_uint16_t flags;
  49. rt_uint16_t addr;
  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_device parent;
  58. struct rt_i2c_bus_device *bus;
  59. rt_uint16_t client_addr;
  60. };
  61. rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
  62. const char *bus_name);
  63. struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name);
  64. rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
  65. struct rt_i2c_msg msgs[],
  66. rt_uint32_t num);
  67. rt_size_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
  68. rt_uint16_t addr,
  69. rt_uint16_t flags,
  70. const rt_uint8_t *buf,
  71. rt_uint32_t count);
  72. rt_size_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus,
  73. rt_uint16_t addr,
  74. rt_uint16_t flags,
  75. rt_uint8_t *buf,
  76. rt_uint32_t count);
  77. rt_inline rt_err_t rt_i2c_bus_lock(struct rt_i2c_bus_device *bus, rt_tick_t timeout)
  78. {
  79. return rt_mutex_take(&bus->lock, timeout);
  80. }
  81. rt_inline rt_err_t rt_i2c_bus_unlock(struct rt_i2c_bus_device *bus)
  82. {
  83. return rt_mutex_release(&bus->lock);
  84. }
  85. int rt_i2c_core_init(void);
  86. #ifdef __cplusplus
  87. }
  88. #endif
  89. #endif