i2c.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-04-25 weety first version
  23. */
  24. #ifndef __I2C_H__
  25. #define __I2C_H__
  26. #include <rtthread.h>
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #define RT_I2C_WR 0x0000
  31. #define RT_I2C_RD (1u << 0)
  32. #define RT_I2C_ADDR_10BIT (1u << 2) /* this is a ten bit chip address */
  33. #define RT_I2C_NO_START (1u << 4)
  34. #define RT_I2C_IGNORE_NACK (1u << 5)
  35. #define RT_I2C_NO_READ_ACK (1u << 6) /* when I2C reading, we do not ACK */
  36. struct rt_i2c_msg
  37. {
  38. rt_uint16_t addr;
  39. rt_uint16_t flags;
  40. rt_uint16_t len;
  41. rt_uint8_t *buf;
  42. };
  43. struct rt_i2c_bus_device;
  44. struct rt_i2c_bus_device_ops
  45. {
  46. rt_size_t (*master_xfer)(struct rt_i2c_bus_device *bus,
  47. struct rt_i2c_msg msgs[],
  48. rt_uint32_t num);
  49. rt_size_t (*slave_xfer)(struct rt_i2c_bus_device *bus,
  50. struct rt_i2c_msg msgs[],
  51. rt_uint32_t num);
  52. rt_err_t (*i2c_bus_control)(struct rt_i2c_bus_device *bus,
  53. rt_uint32_t,
  54. rt_uint32_t);
  55. };
  56. /*for i2c bus driver*/
  57. struct rt_i2c_bus_device
  58. {
  59. struct rt_device parent;
  60. const struct rt_i2c_bus_device_ops *ops;
  61. rt_uint16_t flags;
  62. rt_uint16_t addr;
  63. struct rt_mutex lock;
  64. rt_uint32_t timeout;
  65. rt_uint32_t retries;
  66. void *priv;
  67. };
  68. #ifdef RT_I2C_DEBUG
  69. #define i2c_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__)
  70. #else
  71. #define i2c_dbg(fmt, ...)
  72. #endif
  73. rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
  74. const char *bus_name);
  75. struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name);
  76. rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
  77. struct rt_i2c_msg msgs[],
  78. rt_uint32_t num);
  79. rt_size_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
  80. rt_uint16_t addr,
  81. rt_uint16_t flags,
  82. const rt_uint8_t *buf,
  83. rt_uint32_t count);
  84. rt_size_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus,
  85. rt_uint16_t addr,
  86. rt_uint16_t flags,
  87. rt_uint8_t *buf,
  88. rt_uint32_t count);
  89. int rt_i2c_core_init(void);
  90. #ifdef __cplusplus
  91. }
  92. #endif
  93. #endif