i2c.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * File : i2c.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-04-25 weety first version
  13. */
  14. #ifndef __I2C_H__
  15. #define __I2C_H__
  16. #include <rtthread.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #ifndef RT_I2C_NAME_SIZE
  21. #define RT_I2C_NAME_SIZE 32
  22. #endif
  23. #define RT_I2C_WR 0x0000
  24. #define RT_I2C_RD (1u << 0)
  25. #define RT_I2C_ADDR_10BIT (1u << 2) /* this is a ten bit chip address */
  26. #define RT_I2C_NO_START (1u << 4)
  27. #define RT_I2C_IGNORE_NACK (1u << 5)
  28. #define RT_I2C_NO_READ_ACK (1u << 6) /* when I2C reading, we do not ACK */
  29. struct rt_i2c_msg {
  30. rt_uint16_t addr;
  31. rt_uint16_t flags;
  32. rt_uint16_t len;
  33. rt_uint8_t *buf;
  34. };
  35. struct rt_i2c_hardware_info {
  36. char name[RT_I2C_NAME_SIZE];
  37. rt_uint16_t flags;
  38. rt_uint16_t addr;
  39. rt_uint32_t bus_id;
  40. rt_list_t list;
  41. };
  42. #define RT_I2C_HARDWARE_INFO(name, flags, addr, bus_id) \
  43. name,flags,addr,bus_id,{RT_NULL,RT_NULL}
  44. struct rt_i2c_bus;
  45. struct rt_i2c_bus_ops {
  46. rt_size_t (*master_xfer) (struct rt_i2c_bus *bus, struct rt_i2c_msg *msgs, rt_uint32_t num);
  47. rt_size_t (*slave_xfer) (struct rt_i2c_bus *bus, struct rt_i2c_msg *msgs, rt_uint32_t num);
  48. rt_err_t (*i2c_bus_control) (struct rt_i2c_bus *bus, rt_uint32_t, rt_uint32_t);
  49. };
  50. /*for i2c bus driver*/
  51. struct rt_i2c_bus {
  52. struct rt_device *parent;
  53. char name[RT_I2C_NAME_SIZE];
  54. rt_uint32_t id;
  55. const struct rt_i2c_bus_ops *ops;
  56. struct rt_mutex lock;
  57. rt_list_t devices;
  58. rt_list_t list;
  59. rt_uint32_t timeout;
  60. rt_uint32_t retries;
  61. void *priv;
  62. };
  63. struct rt_i2c_device;
  64. struct rt_i2c_driver {
  65. char name[RT_I2C_NAME_SIZE];
  66. rt_err_t (*probe)(struct rt_i2c_device *device);
  67. rt_err_t (*remove)(struct rt_i2c_device *device);
  68. rt_list_t devices;
  69. };
  70. /*for i2c device driver*/
  71. struct rt_i2c_device {
  72. rt_uint32_t flags;
  73. rt_uint16_t addr;
  74. struct rt_i2c_bus *bus;
  75. struct rt_i2c_driver *driver;
  76. struct rt_device dev;
  77. rt_list_t drv_list;
  78. rt_list_t bus_list;
  79. };
  80. #ifdef RT_I2C_DEBUG
  81. #define i2c_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__)
  82. #else
  83. #define i2c_dbg(fmt, ...)
  84. #endif
  85. rt_err_t rt_i2c_bus_register(struct rt_i2c_bus *bus);
  86. rt_err_t rt_i2c_bus_unregister(struct rt_i2c_bus *bus);
  87. void rt_i2c_hw_info_register(struct rt_i2c_hardware_info *info, rt_uint32_t size);
  88. rt_err_t rt_i2c_bus_attach_driver(struct rt_i2c_driver *driver);
  89. rt_err_t rt_i2c_bus_detach_driver(struct rt_i2c_driver *driver);
  90. rt_size_t rt_i2c_transfer(struct rt_i2c_bus *bus,
  91. struct rt_i2c_msg *msgs, rt_uint32_t size);
  92. rt_size_t rt_i2c_master_send(struct rt_i2c_device *device,
  93. const rt_uint8_t *buf, rt_uint32_t size);
  94. rt_size_t rt_i2c_master_recv(struct rt_i2c_device *device,
  95. rt_uint8_t *buf ,rt_uint32_t size);
  96. rt_err_t rt_i2c_core_init();
  97. #ifdef __cplusplus
  98. }
  99. #endif
  100. #endif