i2c.h 2.9 KB

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