You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
112 lines
3.1 KiB
112 lines
3.1 KiB
/*
|
|
* Copyright (c) Hisilicon Technologies Co., Ltd.. 2014-2019. All rights reserved.
|
|
* Description: i2c read data sample
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include "uapi_i2c.h"
|
|
|
|
#define soc_info_i2c(format, arg...) printf(format, ##arg)
|
|
#define EXT_I2C_MAX_LENGTH 2048
|
|
|
|
static td_s32 i2c_read_param_prepare(td_s32 argc, td_char **argv, td_u32 *i2c_num, td_u32 *read_number,
|
|
uapi_i2c_addr_info *i2c_addr_info)
|
|
{
|
|
if (argc != 6) { /* para number 6 */
|
|
soc_info_i2c("Usage: i2c_read i2c_channel device_addr register_addr ");
|
|
soc_info_i2c("register_addr_len read_bytes_number\n");
|
|
|
|
return -1;
|
|
}
|
|
|
|
if (argv == TD_NULL) {
|
|
soc_info_i2c("para argv is NULL.\n");
|
|
return -1;
|
|
}
|
|
|
|
if (i2c_num == TD_NULL || read_number == TD_NULL || i2c_addr_info == TD_NULL) {
|
|
soc_info_i2c("nullptr error.\n");
|
|
return -1;
|
|
}
|
|
|
|
*i2c_num = (td_u32)strtol(argv[1], NULL, 0);
|
|
i2c_addr_info->dev_address = (td_u8)strtol(argv[2], NULL, 0); /* 2 is second param */
|
|
i2c_addr_info->reg_addr = (td_u32)strtol(argv[3], NULL, 0); /* 3 is 3rd param */
|
|
i2c_addr_info->reg_addr_count = (td_u32)strtol(argv[4], NULL, 0); /* 4 is fourth param */
|
|
if (i2c_addr_info->reg_addr_count > 4) { /* 4 byte */
|
|
soc_info_i2c("register address length is error!\n");
|
|
return -1;
|
|
}
|
|
|
|
*read_number = (td_u32)strtol(argv[5], NULL, 0); /* 5 is fifth param */
|
|
if ((*read_number > EXT_I2C_MAX_LENGTH) || (*read_number == 0)) {
|
|
soc_info_i2c("read_number:%u unexpected!\n", *read_number);
|
|
return -1;
|
|
}
|
|
|
|
return TD_SUCCESS;
|
|
}
|
|
|
|
static td_void i2c_print_read_data(const td_u8 *data, td_u32 data_len)
|
|
{
|
|
td_u32 loop;
|
|
|
|
if (data == TD_NULL) {
|
|
soc_info_i2c("para data is NULL.\n");
|
|
return;
|
|
}
|
|
|
|
soc_info_i2c("\ndata read:\n");
|
|
|
|
for (loop = 0; loop < data_len; loop++) {
|
|
soc_info_i2c("0x%02x ", data[loop]);
|
|
}
|
|
|
|
soc_info_i2c("\n\n");
|
|
}
|
|
|
|
td_s32 main(td_s32 argc, td_char **argv)
|
|
{
|
|
td_s32 ret;
|
|
td_u8 *data = TD_NULL;
|
|
td_u32 i2c_num;
|
|
td_u32 read_number;
|
|
uapi_i2c_addr_info i2c_addr_info;
|
|
|
|
ret = i2c_read_param_prepare(argc, argv, &i2c_num, &read_number, &i2c_addr_info);
|
|
if (ret != TD_SUCCESS) {
|
|
return ret;
|
|
}
|
|
|
|
ret = uapi_i2c_init();
|
|
if (ret != TD_SUCCESS) {
|
|
soc_info_i2c("%s: %d ErrorCode=0x%x\n", __FILE__, __LINE__, ret);
|
|
return ret;
|
|
}
|
|
|
|
data = (td_u8 *)calloc(1, read_number); /* 1:nmemb */
|
|
if (data == TD_NULL) {
|
|
soc_info_i2c("\n pReadData malloc() error!\n");
|
|
uapi_i2c_deinit();
|
|
|
|
return TD_FAILURE;
|
|
}
|
|
|
|
/* Read data from Device */
|
|
ret = uapi_i2c_read(i2c_num, &i2c_addr_info, data, read_number);
|
|
if (ret != TD_SUCCESS) {
|
|
soc_info_i2c("call uapi_i2c_read failed.\n");
|
|
free(data);
|
|
uapi_i2c_deinit();
|
|
return ret;
|
|
}
|
|
|
|
i2c_print_read_data(data, read_number);
|
|
|
|
free(data);
|
|
uapi_i2c_deinit();
|
|
|
|
return TD_SUCCESS;
|
|
}
|