/* * Copyright (c) Hisilicon Technologies Co., Ltd.. 2014-2019. All rights reserved. * Description:i2c write data sample */ #include #include #include "uapi_i2c.h" #define soc_info_i2c(format, arg...) printf(format, ##arg) #define EXT_I2C_MAX_LENGTH 2048 static td_s32 i2c_write_param_prepare(td_s32 argc, td_char **argv, td_u32 *i2c_num, td_u32 *write_number, uapi_i2c_addr_info *i2c_addr_info) { if (argc < 6) { /* para number 6 */ soc_info_i2c("Usage: i2c_write i2c_channel device_addr register_addr "); soc_info_i2c("register_addr_len write_bytes_number byte0 [... byten]\n"); return -1; } if (argv == TD_NULL) { soc_info_i2c("para argv is NULL.\n"); return -1; } if (i2c_num == TD_NULL || write_number == TD_NULL || i2c_addr_info == TD_NULL) { soc_info_i2c("null pointer 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 invalid.\n"); return -1; } *write_number = (td_u32)strtol(argv[5], NULL, 0); /* 5 is fifth param */ if ((*write_number > EXT_I2C_MAX_LENGTH) || (*write_number == 0)) { soc_info_i2c("write_number:%u unexpected!\n", *write_number); return -1; } if (*write_number + 6 > (td_u32)argc) { /* para number 6 */ soc_info_i2c("input error!\n"); soc_info_i2c("Usage: i2c_write i2c_channel device_addr register_addr "); soc_info_i2c("register_addr_len write_bytes_number byte0 [... byten]\n"); return -1; } return TD_SUCCESS; } td_s32 main(td_s32 argc, td_char **argv) { td_s32 ret; td_u8 *data = TD_NULL; td_u32 i2c_num; td_u32 write_number; td_u32 loop; uapi_i2c_addr_info i2c_addr_info; ret = i2c_write_param_prepare(argc, argv, &i2c_num, &write_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 *)malloc(write_number); if (data == TD_NULL) { soc_info_i2c("\n malloc() error!\n"); uapi_i2c_deinit(); return TD_FAILURE; } for (loop = 0; loop < write_number; loop++) { data[loop] = (td_u8)strtol(argv[loop + 6], NULL, 0); /* 6 para */ } /* write data to device */ ret = uapi_i2c_write(i2c_num, &i2c_addr_info, data, write_number); if (ret != TD_SUCCESS) { soc_info_i2c("i2c write failed!\n"); } else { soc_info_i2c("i2c write success!\n"); } free(data); uapi_i2c_deinit(); return ret; }