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 11 #ifndef __I2C_H__ 12 #define __I2C_H__ 13 14 #include <rtthread.h> 15 16 #ifdef __cplusplus 17 extern "C" { 18 #endif 19 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 27 struct rt_i2c_msg 28 { 29 rt_uint16_t addr; 30 rt_uint16_t flags; 31 rt_uint16_t len; 32 rt_uint8_t *buf; 33 }; 34 35 struct rt_i2c_bus_device; 36 37 struct rt_i2c_bus_device_ops 38 { 39 rt_size_t (*master_xfer)(struct rt_i2c_bus_device *bus, 40 struct rt_i2c_msg msgs[], 41 rt_uint32_t num); 42 rt_size_t (*slave_xfer)(struct rt_i2c_bus_device *bus, 43 struct rt_i2c_msg msgs[], 44 rt_uint32_t num); 45 rt_err_t (*i2c_bus_control)(struct rt_i2c_bus_device *bus, 46 rt_uint32_t, 47 rt_uint32_t); 48 }; 49 50 /*for i2c bus driver*/ 51 struct rt_i2c_bus_device 52 { 53 struct rt_device parent; 54 const struct rt_i2c_bus_device_ops *ops; 55 rt_uint16_t flags; 56 rt_uint16_t addr; 57 struct rt_mutex lock; 58 rt_uint32_t timeout; 59 rt_uint32_t retries; 60 void *priv; 61 }; 62 63 #ifdef RT_I2C_DEBUG 64 #define i2c_dbg(fmt, ...) rt_kprintf(fmt, ##__VA_ARGS__) 65 #else 66 #define i2c_dbg(fmt, ...) 67 #endif 68 69 rt_err_t rt_i2c_bus_device_register(struct rt_i2c_bus_device *bus, 70 const char *bus_name); 71 struct rt_i2c_bus_device *rt_i2c_bus_device_find(const char *bus_name); 72 rt_size_t rt_i2c_transfer(struct rt_i2c_bus_device *bus, 73 struct rt_i2c_msg msgs[], 74 rt_uint32_t num); 75 rt_size_t rt_i2c_master_send(struct rt_i2c_bus_device *bus, 76 rt_uint16_t addr, 77 rt_uint16_t flags, 78 const rt_uint8_t *buf, 79 rt_uint32_t count); 80 rt_size_t rt_i2c_master_recv(struct rt_i2c_bus_device *bus, 81 rt_uint16_t addr, 82 rt_uint16_t flags, 83 rt_uint8_t *buf, 84 rt_uint32_t count); 85 int rt_i2c_core_init(void); 86 87 #ifdef __cplusplus 88 } 89 #endif 90 91 #endif 92