"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "tools/i2cget.c" between
i2c-tools-4.2.tar.gz and i2c-tools-4.3.tar.gz

About: i2c-tools are an heterogeneous set of I2C tools for Linux (originally part of the lm-sensors package).

i2cget.c  (i2c-tools-4.2):i2cget.c  (i2c-tools-4.3)
/* /*
i2cget.c - A user-space program to read an I2C register. i2cget.c - A user-space program to read an I2C register.
Copyright (C) 2005-2012 Jean Delvare <jdelvare@suse.de> Copyright (C) 2005-2021 Jean Delvare <jdelvare@suse.de>
Based on i2cset.c: Based on i2cset.c:
Copyright (C) 2001-2003 Frodo Looijaard <frodol@dds.nl>, and Copyright (C) 2001-2003 Frodo Looijaard <frodol@dds.nl>, and
Mark D. Studebaker <mdsxyz123@yahoo.com> Mark D. Studebaker <mdsxyz123@yahoo.com>
Copyright (C) 2004-2005 Jean Delvare Copyright (C) 2004-2005 Jean Delvare
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or the Free Software Foundation; either version 2 of the License, or
(at your option) any later version. (at your option) any later version.
skipping to change at line 44 skipping to change at line 44
#include <i2c/smbus.h> #include <i2c/smbus.h>
#include "i2cbusses.h" #include "i2cbusses.h"
#include "util.h" #include "util.h"
#include "../version.h" #include "../version.h"
static void help(void) __attribute__ ((noreturn)); static void help(void) __attribute__ ((noreturn));
static void help(void) static void help(void)
{ {
fprintf(stderr, fprintf(stderr,
"Usage: i2cget [-f] [-y] [-a] I2CBUS CHIP-ADDRESS [DATA-ADDRESS [ MODE]]\n" "Usage: i2cget [-f] [-y] [-a] I2CBUS CHIP-ADDRESS [DATA-ADDRESS [ MODE [LENGTH]]]\n"
" I2CBUS is an integer or an I2C bus name\n" " I2CBUS is an integer or an I2C bus name\n"
" ADDRESS is an integer (0x08 - 0x77, or 0x00 - 0x7f if -a is gi ven)\n" " ADDRESS is an integer (0x08 - 0x77, or 0x00 - 0x7f if -a is gi ven)\n"
" MODE is one of:\n" " MODE is one of:\n"
" b (read byte data, default)\n" " b (read byte data, default)\n"
" w (read word data)\n" " w (read word data)\n"
" c (write byte/read byte)\n" " c (write byte/read byte)\n"
" Append p for SMBus PEC\n"); " s (read SMBus block data)\n"
" i (read I2C block data)\n"
" Append p for SMBus PEC\n"
" LENGTH is the I2C block data length (between 1 and %d, default
%d)\n",
I2C_SMBUS_BLOCK_MAX, I2C_SMBUS_BLOCK_MAX);
exit(1); exit(1);
} }
static int check_funcs(int file, int size, int daddress, int pec) static int check_funcs(int file, int size, int daddress, int pec)
{ {
unsigned long funcs; unsigned long funcs;
/* check adapter functionality */ /* check adapter functionality */
if (ioctl(file, I2C_FUNCS, &funcs) < 0) { if (ioctl(file, I2C_FUNCS, &funcs) < 0) {
fprintf(stderr, "Error: Could not get the adapter " fprintf(stderr, "Error: Could not get the adapter "
skipping to change at line 92 skipping to change at line 96
return -1; return -1;
} }
break; break;
case I2C_SMBUS_WORD_DATA: case I2C_SMBUS_WORD_DATA:
if (!(funcs & I2C_FUNC_SMBUS_READ_WORD_DATA)) { if (!(funcs & I2C_FUNC_SMBUS_READ_WORD_DATA)) {
fprintf(stderr, MISSING_FUNC_FMT, "SMBus read word"); fprintf(stderr, MISSING_FUNC_FMT, "SMBus read word");
return -1; return -1;
} }
break; break;
case I2C_SMBUS_BLOCK_DATA:
if (!(funcs & I2C_FUNC_SMBUS_READ_BLOCK_DATA)) {
fprintf(stderr, MISSING_FUNC_FMT, "SMBus block read");
return -1;
}
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
if (!(funcs & I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
fprintf(stderr, MISSING_FUNC_FMT, "I2C block read");
return -1;
}
break;
} }
if (pec if (pec
&& !(funcs & (I2C_FUNC_SMBUS_PEC | I2C_FUNC_I2C))) { && !(funcs & (I2C_FUNC_SMBUS_PEC | I2C_FUNC_I2C))) {
fprintf(stderr, "Warning: Adapter does " fprintf(stderr, "Warning: Adapter does "
"not seem to support PEC\n"); "not seem to support PEC\n");
} }
return 0; return 0;
} }
static int confirm(const char *filename, int address, int size, int daddress, static int confirm(const char *filename, int address, int size, int daddress,
int pec) int length, int pec)
{ {
int dont = 0; int dont = 0;
fprintf(stderr, "WARNING! This program can confuse your I2C " fprintf(stderr, "WARNING! This program can confuse your I2C "
"bus, cause data loss and worse!\n"); "bus, cause data loss and worse!\n");
/* Don't let the user break his/her EEPROMs */ /* Don't let the user break his/her EEPROMs */
if (address >= 0x50 && address <= 0x57 && pec) { if (address >= 0x50 && address <= 0x57 && pec) {
fprintf(stderr, "STOP! EEPROMs are I2C devices, not " fprintf(stderr, "STOP! EEPROMs are I2C devices, not "
"SMBus devices. Using PEC\non I2C devices may " "SMBus devices. Using PEC\non I2C devices may "
skipping to change at line 135 skipping to change at line 153
"value into a register!\n"); "value into a register!\n");
dont++; dont++;
} }
fprintf(stderr, "I will read from device file %s, chip " fprintf(stderr, "I will read from device file %s, chip "
"address 0x%02x, ", filename, address); "address 0x%02x, ", filename, address);
if (daddress < 0) if (daddress < 0)
fprintf(stderr, "current data\naddress"); fprintf(stderr, "current data\naddress");
else else
fprintf(stderr, "data address\n0x%02x", daddress); fprintf(stderr, "data address\n0x%02x", daddress);
fprintf(stderr, ", using %s.\n", if (size == I2C_SMBUS_I2C_BLOCK_DATA)
size == I2C_SMBUS_BYTE ? (daddress < 0 ? fprintf(stderr, ", %d %s using read I2C block data.\n",
"read byte" : "write byte/read byte") : length, length > 1 ? "bytes" : "byte");
size == I2C_SMBUS_BYTE_DATA ? "read byte data" : else
"read word data"); fprintf(stderr, ", using %s.\n",
size == I2C_SMBUS_BYTE ? (daddress < 0 ?
"read byte" : "write byte/read byte") :
size == I2C_SMBUS_BYTE_DATA ? "read byte data" :
size == I2C_SMBUS_BLOCK_DATA ? "read SMBus block data" :
"read word data");
if (pec) if (pec)
fprintf(stderr, "PEC checking enabled.\n"); fprintf(stderr, "PEC checking enabled.\n");
fprintf(stderr, "Continue? [%s] ", dont ? "y/N" : "Y/n"); fprintf(stderr, "Continue? [%s] ", dont ? "y/N" : "Y/n");
fflush(stderr); fflush(stderr);
if (!user_ack(!dont)) { if (!user_ack(!dont)) {
fprintf(stderr, "Aborting on user request.\n"); fprintf(stderr, "Aborting on user request.\n");
return 0; return 0;
} }
skipping to change at line 162 skipping to change at line 185
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char *end; char *end;
int res, i2cbus, address, size, file; int res, i2cbus, address, size, file;
int daddress; int daddress;
char filename[20]; char filename[20];
int pec = 0; int pec = 0;
int flags = 0; int flags = 0;
int force = 0, yes = 0, version = 0, all_addrs = 0; int force = 0, yes = 0, version = 0, all_addrs = 0;
int length;
unsigned char block_data[I2C_SMBUS_BLOCK_MAX];
/* handle (optional) flags first */ /* handle (optional) flags first */
while (1+flags < argc && argv[1+flags][0] == '-') { while (1+flags < argc && argv[1+flags][0] == '-') {
switch (argv[1+flags][1]) { switch (argv[1+flags][1]) {
case 'V': version = 1; break; case 'V': version = 1; break;
case 'f': force = 1; break; case 'f': force = 1; break;
case 'y': yes = 1; break; case 'y': yes = 1; break;
case 'a': all_addrs = 1; break; case 'a': all_addrs = 1; break;
default: default:
fprintf(stderr, "Error: Unsupported option " fprintf(stderr, "Error: Unsupported option "
skipping to change at line 212 skipping to change at line 237
} else { } else {
size = I2C_SMBUS_BYTE; size = I2C_SMBUS_BYTE;
daddress = -1; daddress = -1;
} }
if (argc > flags + 4) { if (argc > flags + 4) {
switch (argv[flags+4][0]) { switch (argv[flags+4][0]) {
case 'b': size = I2C_SMBUS_BYTE_DATA; break; case 'b': size = I2C_SMBUS_BYTE_DATA; break;
case 'w': size = I2C_SMBUS_WORD_DATA; break; case 'w': size = I2C_SMBUS_WORD_DATA; break;
case 'c': size = I2C_SMBUS_BYTE; break; case 'c': size = I2C_SMBUS_BYTE; break;
case 's': size = I2C_SMBUS_BLOCK_DATA; break;
case 'i': size = I2C_SMBUS_I2C_BLOCK_DATA; break;
default: default:
fprintf(stderr, "Error: Invalid mode!\n"); fprintf(stderr, "Error: Invalid mode!\n");
help(); help();
} }
pec = argv[flags+4][1] == 'p'; pec = argv[flags+4][1] == 'p';
if (size == I2C_SMBUS_I2C_BLOCK_DATA && pec) {
fprintf(stderr, "Error: PEC not supported for I2C block d
ata!\n");
help();
}
}
if (argc > flags + 5) {
if (size != I2C_SMBUS_I2C_BLOCK_DATA) {
fprintf(stderr, "Error: Length only valid for I2C block d
ata!\n");
help();
}
length = strtol(argv[flags+5], &end, 0);
if (*end || length < 1 || length > I2C_SMBUS_BLOCK_MAX) {
fprintf(stderr, "Error: Length invalid!\n");
help();
}
} else {
length = I2C_SMBUS_BLOCK_MAX;
} }
file = open_i2c_dev(i2cbus, filename, sizeof(filename), 0); file = open_i2c_dev(i2cbus, filename, sizeof(filename), 0);
if (file < 0 if (file < 0
|| check_funcs(file, size, daddress, pec) || check_funcs(file, size, daddress, pec)
|| set_slave_addr(file, address, force)) || set_slave_addr(file, address, force))
exit(1); exit(1);
if (!yes && !confirm(filename, address, size, daddress, pec)) if (!yes && !confirm(filename, address, size, daddress, length, pec))
exit(0); exit(0);
if (pec && ioctl(file, I2C_PEC, 1) < 0) { if (pec && ioctl(file, I2C_PEC, 1) < 0) {
fprintf(stderr, "Error: Could not set PEC: %s\n", fprintf(stderr, "Error: Could not set PEC: %s\n",
strerror(errno)); strerror(errno));
close(file); close(file);
exit(1); exit(1);
} }
switch (size) { switch (size) {
skipping to change at line 247 skipping to change at line 292
if (daddress >= 0) { if (daddress >= 0) {
res = i2c_smbus_write_byte(file, daddress); res = i2c_smbus_write_byte(file, daddress);
if (res < 0) if (res < 0)
fprintf(stderr, "Warning - write failed\n"); fprintf(stderr, "Warning - write failed\n");
} }
res = i2c_smbus_read_byte(file); res = i2c_smbus_read_byte(file);
break; break;
case I2C_SMBUS_WORD_DATA: case I2C_SMBUS_WORD_DATA:
res = i2c_smbus_read_word_data(file, daddress); res = i2c_smbus_read_word_data(file, daddress);
break; break;
case I2C_SMBUS_BLOCK_DATA:
res = i2c_smbus_read_block_data(file, daddress, block_data);
break;
case I2C_SMBUS_I2C_BLOCK_DATA:
res = i2c_smbus_read_i2c_block_data(file, daddress, length, block
_data);
break;
default: /* I2C_SMBUS_BYTE_DATA */ default: /* I2C_SMBUS_BYTE_DATA */
res = i2c_smbus_read_byte_data(file, daddress); res = i2c_smbus_read_byte_data(file, daddress);
} }
close(file); close(file);
if (res < 0) { if (res < 0) {
fprintf(stderr, "Error: Read failed\n"); fprintf(stderr, "Error: Read failed\n");
exit(2); exit(2);
} }
printf("0x%0*x\n", size == I2C_SMBUS_WORD_DATA ? 4 : 2, res); if (size == I2C_SMBUS_BLOCK_DATA ||
size == I2C_SMBUS_I2C_BLOCK_DATA) {
int i;
for (i = 0; i < res - 1; ++i)
printf("0x%02hhx ", block_data[i]);
printf("0x%02hhx\n", block_data[res - 1]);
} else {
printf("0x%0*x\n", size == I2C_SMBUS_WORD_DATA ? 4 : 2, res);
}
exit(0); exit(0);
} }
 End of changes. 12 change blocks. 
11 lines changed or deleted 75 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)