1 /* 2 * Copyright (C) 2020-2021 Alibaba Group Holding Limited 3 */ 4 5 #ifndef AOS_I2C_CORE_H 6 #define AOS_I2C_CORE_H 7 8 #include <aos/device_core.h> 9 #include <aos/i2c.h> 10 11 #define AOS_I2C_F_ADDR_10 ((uint32_t)0x1 << 0) 12 13 #define AOS_I2C_XF_MSG_HEAD ((uint32_t)0x1 << 0) 14 #define AOS_I2C_XF_MSG_TAIL ((uint32_t)0x1 << 1) 15 #define AOS_I2C_XF_SEQ_HEAD ((uint32_t)0x1 << 2) 16 #define AOS_I2C_XF_SEQ_TAIL ((uint32_t)0x1 << 3) 17 #define AOS_I2C_XF_BUF_IDX ((uint32_t)0x1 << 4) 18 19 #ifndef AOS_I2C_BUF_SIZE 20 #define AOS_I2C_BUF_SIZE 256 21 #endif 22 23 struct aos_i2c; 24 25 typedef struct { 26 void (*unregister)(struct aos_i2c *); 27 aos_status_t (*startup)(struct aos_i2c *); 28 void (*shutdown)(struct aos_i2c *); 29 aos_status_t (*start_xfer)(struct aos_i2c *); 30 void (*finish_xfer)(struct aos_i2c *); 31 void (*abort_xfer)(struct aos_i2c *); 32 } aos_i2c_ops_t; 33 34 typedef struct aos_i2c { 35 aos_dev_t dev; 36 37 /* must be initialized before registration */ 38 const aos_i2c_ops_t *ops; 39 uint32_t flags; 40 uint32_t hz; 41 42 aos_spinlock_t lock; 43 aos_event_t event; 44 struct { 45 uint32_t timeout; 46 uint32_t flags; 47 uint32_t cfg; 48 uint16_t addr; 49 size_t len; 50 size_t pos[2]; 51 } x; 52 uint8_t buf[2][AOS_I2C_BUF_SIZE]; 53 } aos_i2c_t; 54 55 #ifdef __cplusplus 56 extern "C" { 57 #endif 58 59 aos_status_t aos_i2c_register(aos_i2c_t *i2c); 60 aos_status_t aos_i2c_unregister(uint32_t id); 61 62 size_t aos_i2c_hard_push(aos_i2c_t *i2c, void *tx_buf, size_t count); 63 bool aos_i2c_hard_pull(aos_i2c_t *i2c, const void *rx_buf, size_t count); 64 void aos_i2c_hard_fail(aos_i2c_t *i2c); 65 aos_i2c_hard_remain(aos_i2c_t * i2c)66static inline size_t aos_i2c_hard_remain(aos_i2c_t *i2c) 67 { 68 return i2c->x.pos[0] - i2c->x.pos[1]; 69 } 70 aos_i2c_xfer_remain(aos_i2c_t * i2c)71static inline size_t aos_i2c_xfer_remain(aos_i2c_t *i2c) 72 { 73 return i2c->x.len - i2c->x.pos[0]; 74 } 75 76 #ifdef __cplusplus 77 } 78 #endif 79 80 #endif /* AOS_I2C_CORE_H */ 81