i2c.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file i2c.c
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #ifndef __I2C_H__
  35. #define __I2C_H__
  36. #include <rtthread.h>
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. #define RT_I2C_WR 0x0000
  41. #define RT_I2C_RD (1u << 0)
  42. #define RT_I2C_ADDR_10BIT (1u << 2) /* this is a ten bit chip address */
  43. #define RT_I2C_NO_START (1u << 4)
  44. #define RT_I2C_IGNORE_NACK (1u << 5)
  45. #define RT_I2C_NO_READ_ACK (1u << 6) /* when I2C reading, we do not ACK */
  46. struct rt_i2c_msg
  47. {
  48. rt_uint16_t addr;
  49. rt_uint16_t flags;
  50. rt_uint16_t len;
  51. rt_uint8_t *buf;
  52. };
  53. struct rt_i2c_bus_device;
  54. struct rt_i2c_bus_device_ops
  55. {
  56. rt_size_t (*master_xfer)(struct rt_i2c_bus_device *bus,
  57. struct rt_i2c_msg msgs[],
  58. rt_uint32_t num);
  59. rt_size_t (*slave_xfer)(struct rt_i2c_bus_device *bus,
  60. struct rt_i2c_msg msgs[],
  61. rt_uint32_t num);
  62. rt_err_t (*i2c_bus_control)(struct rt_i2c_bus_device *bus,
  63. rt_uint32_t,
  64. rt_uint32_t);
  65. };
  66. /*for i2c bus driver*/
  67. struct rt_i2c_bus_device
  68. {
  69. struct rt_device parent;
  70. const struct rt_i2c_bus_device_ops *ops;
  71. rt_uint16_t flags;
  72. rt_uint16_t addr;
  73. struct rt_mutex lock;
  74. rt_uint32_t timeout;
  75. rt_uint32_t retries;
  76. void *priv;
  77. };
  78. struct rt_i2c_client
  79. {
  80. struct rt_device parent;
  81. struct rt_i2c_bus_device *bus;
  82. rt_uint16_t client_addr;
  83. };
  84. rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus,
  85. const char *bus_name);
  86. struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name);
  87. rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus,
  88. struct rt_i2c_msg msgs[],
  89. rt_uint32_t num);
  90. rt_size_t rt_i2c_master_send(struct rt_i2c_bus_device *bus,
  91. rt_uint16_t addr,
  92. rt_uint16_t flags,
  93. const rt_uint8_t *buf,
  94. rt_uint32_t count);
  95. rt_size_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus,
  96. rt_uint16_t addr,
  97. rt_uint16_t flags,
  98. rt_uint8_t *buf,
  99. rt_uint32_t count);
  100. int rt_i2c_core_init(void);
  101. #ifdef __cplusplus
  102. }
  103. #endif
  104. #endif