1 /* 2 * Copyright (C) 2020-2021 Alibaba Group Holding Limited 3 */ 4 5 #ifndef AOS_I2C_H 6 #define AOS_I2C_H 7 8 #include <aos/device.h> 9 10 #define AOS_I2C_MCFG_TX ((uint32_t)0x0 << 0) 11 #define AOS_I2C_MCFG_RX ((uint32_t)0x1 << 0) 12 #define AOS_I2C_MCFG_ADDR_7 ((uint32_t)0x0 << 1) 13 #define AOS_I2C_MCFG_ADDR_10 ((uint32_t)0x1 << 1) 14 15 typedef aos_dev_ref_t aos_i2c_ref_t; 16 17 typedef struct { 18 uint32_t cfg; 19 uint16_t addr; 20 size_t count; 21 void *buf; 22 } aos_i2c_msg_t; 23 24 #define AOS_I2C_MSG_INIT_VAL \ 25 { \ 26 .cfg = 0, \ 27 .addr = 0, \ 28 .count = 0, \ 29 .buf = NULL, \ 30 } 31 32 #define aos_i2c_msg_init(x) do { *(x) = (aos_i2c_msg_t)AOS_I2C_MSG_INIT_VAL; } while (0) 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif 37 38 /** 39 * @brief Get a I2C device. 40 * @param[out] ref I2C ref to operate 41 * @param[in] id I2C device ID 42 * @return 0: on success; < 0: on failure 43 */ 44 aos_status_t aos_i2c_get(aos_i2c_ref_t *ref, uint32_t id); 45 /** 46 * @brief Release a I2C device. 47 * @param[in] ref I2C ref to operate 48 * @return None 49 */ 50 void aos_i2c_put(aos_i2c_ref_t *ref); 51 /** 52 * @brief Synchronous I2C data transfer. 53 * @param[in] ref I2C ref to operate 54 * @param[in] msgs array of messages 55 * @param[in] num_msgs number of messages 56 * @return 0: on success; < 0: on failure 57 */ 58 aos_status_t aos_i2c_transfer(aos_i2c_ref_t *ref, const aos_i2c_msg_t *msgs, size_t num_msgs); 59 60 #ifdef __cplusplus 61 } 62 #endif 63 64 #endif /* AOS_I2C_H */ 65