11bd4fe43Sopenharmony_ci/** 21bd4fe43Sopenharmony_ci * @file hi_i2c.h 31bd4fe43Sopenharmony_ci * 41bd4fe43Sopenharmony_ci * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 51bd4fe43Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 61bd4fe43Sopenharmony_ci * you may not use this file except in compliance with the License. 71bd4fe43Sopenharmony_ci * You may obtain a copy of the License at 81bd4fe43Sopenharmony_ci * 91bd4fe43Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 101bd4fe43Sopenharmony_ci * 111bd4fe43Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 121bd4fe43Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 131bd4fe43Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 141bd4fe43Sopenharmony_ci * See the License for the specific language governing permissions and 151bd4fe43Sopenharmony_ci * limitations under the License. 161bd4fe43Sopenharmony_ci */ 171bd4fe43Sopenharmony_ci 181bd4fe43Sopenharmony_ci/** @defgroup iot_i2c I2C 191bd4fe43Sopenharmony_ci * @ingroup drivers 201bd4fe43Sopenharmony_ci */ 211bd4fe43Sopenharmony_ci#ifndef __HI_I2C_H__ 221bd4fe43Sopenharmony_ci#define __HI_I2C_H__ 231bd4fe43Sopenharmony_ci 241bd4fe43Sopenharmony_ci#include <hi_types_base.h> 251bd4fe43Sopenharmony_ci 261bd4fe43Sopenharmony_ci/* 271bd4fe43Sopenharmony_ci * I2C Interface 281bd4fe43Sopenharmony_ci */ 291bd4fe43Sopenharmony_ci#define I2C_RATE_DEFAULT 100000 301bd4fe43Sopenharmony_ci 311bd4fe43Sopenharmony_citypedef hi_void (*i2c_reset_func)(hi_void); 321bd4fe43Sopenharmony_citypedef hi_void (*i2c_prepare_func)(hi_void); 331bd4fe43Sopenharmony_citypedef hi_void (*i2c_restore_func)(hi_void); 341bd4fe43Sopenharmony_ci 351bd4fe43Sopenharmony_ci/** 361bd4fe43Sopenharmony_ci * @ingroup iot_i2c 371bd4fe43Sopenharmony_ci * 381bd4fe43Sopenharmony_ci * I2C callback function. CNcomment:I2C回调函数。CNend 391bd4fe43Sopenharmony_ci */ 401bd4fe43Sopenharmony_citypedef struct { 411bd4fe43Sopenharmony_ci i2c_reset_func reset_func; /**< This function is called back when the communication with the slave device 421bd4fe43Sopenharmony_ci is abnormal.CNcomment:I2C从异常处理函数CNend */ 431bd4fe43Sopenharmony_ci i2c_prepare_func prepare_func; /**< This function is called back before the I2C read/write operation to implement 441bd4fe43Sopenharmony_ci the preparations before the I2C operation. 451bd4fe43Sopenharmony_ci CNcomment:I2C操作前准备函数CNend */ 461bd4fe43Sopenharmony_ci i2c_restore_func restore_func; /**< After the I2C read/write operation is performed, this function is 471bd4fe43Sopenharmony_ci called back to implement the recovery after the I2C operation. 481bd4fe43Sopenharmony_ci CNcomment:I2C操作后恢复函数CNend */ 491bd4fe43Sopenharmony_ci} hi_i2c_func; 501bd4fe43Sopenharmony_ci 511bd4fe43Sopenharmony_ci/** 521bd4fe43Sopenharmony_ci * @ingroup iot_i2c 531bd4fe43Sopenharmony_ci * 541bd4fe43Sopenharmony_ci * I2C TX/RX data descriptor. CNcomment:I2C发送/接收数据描述符。CNend 551bd4fe43Sopenharmony_ci */ 561bd4fe43Sopenharmony_citypedef struct { 571bd4fe43Sopenharmony_ci hi_u8* send_buf; /**< Data TX pointer. The user needs to ensure that no null pointer is transferred. 581bd4fe43Sopenharmony_ci CNcomment:数据发送指针CNend */ 591bd4fe43Sopenharmony_ci hi_u32 send_len; /**< Length of sent data (unit: byte). 601bd4fe43Sopenharmony_ci CNcomment:发送数据长度(单位:byte)CNend */ 611bd4fe43Sopenharmony_ci hi_u8* receive_buf; /**< Data RX pointer. CNcomment:数据接收指针CNend */ 621bd4fe43Sopenharmony_ci hi_u32 receive_len; /**< Length of received data (unit: byte). 631bd4fe43Sopenharmony_ci CNcomment:接收数据长度(单位:byte)CNend */ 641bd4fe43Sopenharmony_ci} hi_i2c_data; 651bd4fe43Sopenharmony_ci 661bd4fe43Sopenharmony_ci/** 671bd4fe43Sopenharmony_ci * @ingroup iot_i2c 681bd4fe43Sopenharmony_ci * 691bd4fe43Sopenharmony_ci * I2C hardware index. CNComment:I2C硬件设备枚举。CNend 701bd4fe43Sopenharmony_ci */ 711bd4fe43Sopenharmony_citypedef enum { 721bd4fe43Sopenharmony_ci HI_I2C_IDX_0, 731bd4fe43Sopenharmony_ci HI_I2C_IDX_1, 741bd4fe43Sopenharmony_ci} hi_i2c_idx; 751bd4fe43Sopenharmony_ci 761bd4fe43Sopenharmony_ci/** 771bd4fe43Sopenharmony_ci* @ingroup iot_i2c 781bd4fe43Sopenharmony_ci* @brief Set I2C baudrate. CNcomment:I2C设置波特率。CNend 791bd4fe43Sopenharmony_ci* 801bd4fe43Sopenharmony_ci* @par 描述: 811bd4fe43Sopenharmony_ci* Set I2C baudrate. CNcomment:I2C设置波特率。CNend 821bd4fe43Sopenharmony_ci* 831bd4fe43Sopenharmony_ci* @attention Multiple tasks are not protected (multiple tasks are not supported). CNcomment:未作 841bd4fe43Sopenharmony_ci多任务保护(不支持多任务)。CNend 851bd4fe43Sopenharmony_ci* @param id [IN] type #hi_i2c_idx,I2C hardware selection. CNcomment:I2C硬件设备选择。CNend 861bd4fe43Sopenharmony_ci* @param baudrate [IN] type #hi_u32,I2C baudrate. CNcomment:I2C波特率。CNend 871bd4fe43Sopenharmony_ci* 881bd4fe43Sopenharmony_ci* @retval #0 Success. 891bd4fe43Sopenharmony_ci* @retval #Other Failure. For details, see hi_errno.h. 901bd4fe43Sopenharmony_ci* @par 依赖: 911bd4fe43Sopenharmony_ci* @li hi_i2c.h:Declares the API.CNcomment:该接口声明所在的头文件。CNend 921bd4fe43Sopenharmony_ci* @see hi_i2c_write|hi_i2c_receive。 931bd4fe43Sopenharmony_ci*/ 941bd4fe43Sopenharmony_cihi_u32 hi_i2c_set_baudrate(hi_i2c_idx id, hi_u32 baudrate); 951bd4fe43Sopenharmony_ci 961bd4fe43Sopenharmony_ci/** 971bd4fe43Sopenharmony_ci* @ingroup iot_i2c 981bd4fe43Sopenharmony_ci* @brief I2C data TX and RX.CNcomment:I2C发送与接收数据。CNend 991bd4fe43Sopenharmony_ci* 1001bd4fe43Sopenharmony_ci* @par 描述: 1011bd4fe43Sopenharmony_ci* The I2C sends data to the slave device and then receives data from the slave device. 1021bd4fe43Sopenharmony_ciCNcomment:I2C向从机发送数据,然后接收从机数据。CNend 1031bd4fe43Sopenharmony_ci* 1041bd4fe43Sopenharmony_ci* @attention Multi-tasking is not supported. CNcomment:未作多任务保护(不支持多任务)。CNend 1051bd4fe43Sopenharmony_ci* @param id [IN] type #hi_i2c_idx,I2C hardware selection. CNcomment:I2C硬件设备选择。CNend 1061bd4fe43Sopenharmony_ci* @param device_addr [IN] type #hi_u16,The device ID. High three bits of offset address of the I2C device on chipset. 1071bd4fe43Sopenharmony_ciCNcomment:设备号及设备片内偏移地址高3位(从设备哪个地方开始读)。CNend 1081bd4fe43Sopenharmony_ci* @param i2c_data [IN/OUT] type #const hi_i2c_data*,The data descriptor to be received. 1091bd4fe43Sopenharmony_ci* The structure member data sending pointer and data receiving pointer cannot be null. 1101bd4fe43Sopenharmony_ciCNcomment:待接收数据描述符,结构体成员数据发送指针和数据接收指针都不为空。CNend 1111bd4fe43Sopenharmony_ci* 1121bd4fe43Sopenharmony_ci* @retval #0 Success. 1131bd4fe43Sopenharmony_ci* @retval #Other Failure. For details, see hi_errno.h. 1141bd4fe43Sopenharmony_ci* @par 依赖: 1151bd4fe43Sopenharmony_ci* @li hi_i2c.h:Declares the API.CNcomment:该接口声明所在的头文件。CNend 1161bd4fe43Sopenharmony_ci* @see hi_i2c_write|hi_i2c_receive。 1171bd4fe43Sopenharmony_ci*/ 1181bd4fe43Sopenharmony_cihi_u32 hi_i2c_writeread(hi_i2c_idx id, hi_u16 device_addr, const hi_i2c_data *i2c_data); 1191bd4fe43Sopenharmony_ci 1201bd4fe43Sopenharmony_ci/** 1211bd4fe43Sopenharmony_ci* @ingroup iot_i2c 1221bd4fe43Sopenharmony_ci* @brief I2C data TX. CNcomment:I2C发送数据。CNend 1231bd4fe43Sopenharmony_ci* 1241bd4fe43Sopenharmony_ci* @par 描述: 1251bd4fe43Sopenharmony_ci* I2C data TX. CNcomment:I2C发送数据。CNend 1261bd4fe43Sopenharmony_ci* 1271bd4fe43Sopenharmony_ci* @attention Multiple tasks are not protected (multiple tasks are not supported). CNcomment:未作 1281bd4fe43Sopenharmony_ci多任务保护(不支持多任务)。CNend 1291bd4fe43Sopenharmony_ci* @param id [IN] type #hi_i2c_idx,I2C hardware selection. CNcomment:I2C硬件设备选择。CNend 1301bd4fe43Sopenharmony_ci* @param device_addr [IN] type #hi_u16,The device ID. High three bits of offset address of the I2C device on chipset. 1311bd4fe43Sopenharmony_ciCNcomment:设备号及设备片内偏移地址高3位(从设备哪个地方开始读)。CNend 1321bd4fe43Sopenharmony_ci* @param i2c_data [IN] type #const hi_i2c_data*,The data descriptor to be received. The structure member data sending 1331bd4fe43Sopenharmony_ci* pointer and data receiving pointer cannot be null. 1341bd4fe43Sopenharmony_ciCNcomment:待接收数据描述符,结构体成员数据发送指针和数据接收指针都不为空。CNend 1351bd4fe43Sopenharmony_ci* 1361bd4fe43Sopenharmony_ci* @retval #0 Success. 1371bd4fe43Sopenharmony_ci* @retval #Other Failure. For details, see hi_errno.h. 1381bd4fe43Sopenharmony_ci* @par 依赖: 1391bd4fe43Sopenharmony_ci* @li hi_i2c.h:Declares the API.CNcomment:该接口声明所在的头文件。CNend 1401bd4fe43Sopenharmony_ci* @see hi_i2c_writeread|hi_i2c_receive。 1411bd4fe43Sopenharmony_ci*/ 1421bd4fe43Sopenharmony_cihi_u32 hi_i2c_write(hi_i2c_idx id, hi_u16 device_addr, const hi_i2c_data *i2c_data); 1431bd4fe43Sopenharmony_ci 1441bd4fe43Sopenharmony_ci/** 1451bd4fe43Sopenharmony_ci* @ingroup iot_i2c 1461bd4fe43Sopenharmony_ci* @brief I2C data RX. CNcomment:I2C接收数据。CNend 1471bd4fe43Sopenharmony_ci* 1481bd4fe43Sopenharmony_ci* @par 描述: 1491bd4fe43Sopenharmony_ci* I2C data RX. CNcomment:I2C接收数据。CNend 1501bd4fe43Sopenharmony_ci* 1511bd4fe43Sopenharmony_ci* @attention Multi-tasking is not supported. CNcomment:未作多任务保护(不支持多任务)。CNend 1521bd4fe43Sopenharmony_ci* @param id [IN] type #hi_i2c_idx,I2C hardware selection. CNcomment:I2C硬件设备选择。CNend 1531bd4fe43Sopenharmony_ci* @param device_addr [IN] type #hi_u16,The device ID. High three bits of offset address of the I2C device on chipset. 1541bd4fe43Sopenharmony_ciCNcomment:设备号及设备片内偏移地址高3位(从设备哪个地方开始读)。CNend 1551bd4fe43Sopenharmony_ci* @param i2c_data [OUT] type #const hi_i2c_data*,The data descriptor to be received. The structure member data sending 1561bd4fe43Sopenharmony_ci* pointer and data receiving pointer cannot be null. 1571bd4fe43Sopenharmony_ciCNcomment:待接收数据描述符,结构体成员数据发送指针和数据接收指针都不为空。CNend 1581bd4fe43Sopenharmony_ci* 1591bd4fe43Sopenharmony_ci* @retval #0 Success. 1601bd4fe43Sopenharmony_ci* @retval #Other Failure. For details, see hi_errno.h. 1611bd4fe43Sopenharmony_ci* @par 依赖: 1621bd4fe43Sopenharmony_ci* @li hi_i2c.h:Declares the API.CNcomment:该接口声明所在的头文件。CNend 1631bd4fe43Sopenharmony_ci* @see hi_i2c_write|hi_i2c_sendreceive。 1641bd4fe43Sopenharmony_ci*/ 1651bd4fe43Sopenharmony_cihi_u32 hi_i2c_read(hi_i2c_idx id, hi_u16 device_addr, const hi_i2c_data *i2c_data); 1661bd4fe43Sopenharmony_ci 1671bd4fe43Sopenharmony_ci/** 1681bd4fe43Sopenharmony_ci* @ingroup iot_i2c 1691bd4fe43Sopenharmony_ci* @brief Initializes the I2C controller. CNcomment:I2C初始化。CNend 1701bd4fe43Sopenharmony_ci* 1711bd4fe43Sopenharmony_ci* @par 描述: 1721bd4fe43Sopenharmony_ci* Initializes the I2C controller. CNcomment:I2C初始化。CNend 1731bd4fe43Sopenharmony_ci* 1741bd4fe43Sopenharmony_ci* @attention None 1751bd4fe43Sopenharmony_ci* @param id [IN] type #hi_i2c_idx,I2C hardware selection. CNcomment:I2C硬件设备选择。CNend 1761bd4fe43Sopenharmony_ci* @param baudrate [IN] type #hi_u32,I2C baudrate. CNcomment:I2C波特率。CNend 1771bd4fe43Sopenharmony_ci* 1781bd4fe43Sopenharmony_ci* @retval #0 Success. 1791bd4fe43Sopenharmony_ci* @retval #Other Failure. For details, see hi_errno.h. 1801bd4fe43Sopenharmony_ci* @par 依赖: 1811bd4fe43Sopenharmony_ci* @li hi_i2c.h:Declares the API.CNcomment:该接口声明所在的头文件。CNend 1821bd4fe43Sopenharmony_ci* @see hi_i2c_deinit。 1831bd4fe43Sopenharmony_ci*/ 1841bd4fe43Sopenharmony_cihi_u32 hi_i2c_init(hi_i2c_idx id, hi_u32 baudrate); 1851bd4fe43Sopenharmony_ci 1861bd4fe43Sopenharmony_ci/** 1871bd4fe43Sopenharmony_ci* @ingroup iot_i2c 1881bd4fe43Sopenharmony_ci* @brief Exits the I2C module.CNcomment:退出I2C模块。CNend 1891bd4fe43Sopenharmony_ci* 1901bd4fe43Sopenharmony_ci* @par 描述: 1911bd4fe43Sopenharmony_ci* Exits the I2C module. CNcomment:退出I2C模块。CNend 1921bd4fe43Sopenharmony_ci* 1931bd4fe43Sopenharmony_ci* @attention This API is called after hi_i2c_init is called. CNcomment:hi_i2c_init调用后再使用。CNend 1941bd4fe43Sopenharmony_ci* @param id [IN] type #hi_i2c_idx,I2C hardware selection. CNcomment:I2C硬件设备选择。CNend 1951bd4fe43Sopenharmony_ci* 1961bd4fe43Sopenharmony_ci* @retval #0 Success. 1971bd4fe43Sopenharmony_ci* @retval #Other Failure. For details, see hi_errno.h. 1981bd4fe43Sopenharmony_ci* @par 依赖: 1991bd4fe43Sopenharmony_ci* @li hi_i2c.h:Declares the API.CNcomment:该接口声明所在的头文件。CNend 2001bd4fe43Sopenharmony_ci* @see hi_i2c_init。 2011bd4fe43Sopenharmony_ci*/ 2021bd4fe43Sopenharmony_cihi_u32 hi_i2c_deinit(hi_i2c_idx id); 2031bd4fe43Sopenharmony_ci 2041bd4fe43Sopenharmony_ci/** 2051bd4fe43Sopenharmony_ci* @ingroup iot_i2c 2061bd4fe43Sopenharmony_ci* @brief Registers the I2C callback function.CNcomment:注册I2C回调函数。CNend 2071bd4fe43Sopenharmony_ci* 2081bd4fe43Sopenharmony_ci* @par 描述: 2091bd4fe43Sopenharmony_ci* Registers the I2C callback function, for extension.CNcomment:注册I2C回调函数,用于扩展。CNend 2101bd4fe43Sopenharmony_ci* 2111bd4fe43Sopenharmony_ci* @attention None 2121bd4fe43Sopenharmony_ci* @param id [IN] type #hi_i2c_idx,I2C hardware selection. CNcomment:I2C硬件设备选择。CNend 2131bd4fe43Sopenharmony_ci* @param pfn [IN] type #hi_i2c_func,Callback function. CNcomment:回调函数。CNend 2141bd4fe43Sopenharmony_ci* 2151bd4fe43Sopenharmony_ci* @retval #0 Success. 2161bd4fe43Sopenharmony_ci* @retval #Other Failure. For details, see hi_errno.h. 2171bd4fe43Sopenharmony_ci* @par 依赖: 2181bd4fe43Sopenharmony_ci* @li hi_i2c.h:Declares the API.CNcomment:该接口声明所在的头文件。CNend 2191bd4fe43Sopenharmony_ci* @see None 2201bd4fe43Sopenharmony_ci*/ 2211bd4fe43Sopenharmony_cihi_void hi_i2c_register_reset_bus_func(hi_i2c_idx id, hi_i2c_func pfn); 2221bd4fe43Sopenharmony_ci 2231bd4fe43Sopenharmony_ci#endif 224