1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include <securec.h>
10 #include "hdf_log.h"
11 #include "input_i2c_ops.h"
12 
13 #define I2C_READ_MSG_NUM   2
14 #define I2C_WRITE_MSG_NUM  1
15 
InputI2cRead(const InputI2cClient * client,uint8_t * writeBuf,uint32_t writeLen,uint8_t * readBuf,uint32_t readLen)16 int32_t InputI2cRead(const InputI2cClient *client, uint8_t *writeBuf, uint32_t writeLen, uint8_t *readBuf,
17     uint32_t readLen)
18 {
19     struct I2cMsg msg[I2C_READ_MSG_NUM];
20     (void)memset_s(msg, sizeof(msg), 0, sizeof(msg));
21 
22     msg[0].addr = client->i2cCfg.addr;
23     msg[0].flags = 0;
24     msg[0].len = writeLen;
25     msg[0].buf = writeBuf;
26 
27     msg[1].addr = client->i2cCfg.addr;
28     msg[1].flags = I2C_FLAG_READ;
29     msg[1].len = readLen;
30     msg[1].buf = readBuf;
31 
32     if (I2cTransfer(client->i2cHandle, msg, I2C_READ_MSG_NUM) != I2C_READ_MSG_NUM) {
33         HDF_LOGE("%s: i2c read err", __func__);
34         return HDF_FAILURE;
35     }
36     return HDF_SUCCESS;
37 }
38 
InputI2cWrite(const InputI2cClient * client,uint8_t * writeBuf,uint32_t len)39 int32_t InputI2cWrite(const InputI2cClient *client, uint8_t *writeBuf, uint32_t len)
40 {
41     struct I2cMsg msg[I2C_WRITE_MSG_NUM];
42     (void)memset_s(msg, sizeof(msg), 0, sizeof(msg));
43 
44     msg[0].addr = client->i2cCfg.addr;
45     msg[0].flags = 0;
46     msg[0].len = len;
47     msg[0].buf = writeBuf;
48 
49     if (I2cTransfer(client->i2cHandle, msg, I2C_WRITE_MSG_NUM) != I2C_WRITE_MSG_NUM) {
50         HDF_LOGE("%s: i2c write err", __func__);
51         return HDF_FAILURE;
52     }
53     return HDF_SUCCESS;
54 }
55