Rework block read support among i2cget and i2cdump
cover Rework block read support among i2cget and i2cdump
Hi all,
This is my attempt to improve the block read support in i2c-tools, specifically i2cget and i2cdump. The base of this series is the submission by Crestez Dan Leonard 5 years ago. Sorry for the delay ;-)
First we add support of both I2C and SMBus block read to i2cget:
[PATCH 1/7] i2cget: Add support for I2C block data [PATCH 2/7] i2cget: Document the support of I2C block reads [PATCH 3/7] i2cget: Add support for SMBus block read
Then we add support for range selection in I2C block mode to i2cdump:
[PATCH 4/7] i2cdump: Remove dead code
[PATCH 5/7] i2cdump: Add range support with mode i (I2C block)
Lastly we deprecate and eventually remove support for SMBus block mode from i2cdump:
[PATCH 6/7] i2cdump: Deprecate SMBus block mode [PATCH 7/7] i2cdump: Remove support for SMBus block mode
The idea would be to get the first 6 patches in the upcoming i2c-tools v4.3, and apply the 7th patch "later" (either immediately after that release, or some time later, I'm not sure).
Signature · 2 lines
Jean Delvare SUSE L3 Support
PATCH 1/7 i2cget: Add support for I2C block data
From: Crestez Dan Leonard <[email protected]>
This adds mode 'i' for I2C_SMBUS_I2C_BLOCK_DATA. This is the same mode letter from i2cdump.
Length is optional and defaults to 32 (maximum).
The intended use is debugging i2c devices with shell commands.
[JD: Fix the build (wrong variable name)
Ensure PEC isn't used in I2C block mode
Don't print the lenth (doesn't add value and could be mistaken as
an offset
Various cleanups]
--- tools/i2cget.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 10 deletions(-)
tools/i2cget.c +55 -10
@@ -41,14 +41,16 @@ static void help(void) __attribute__ ((nstatic void help (void ) {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" " ADDRESS is an integer (0x08 - 0x77, or 0x00 - 0x7f if -a is given)\n" " MODE is one of:\n" " b (read byte data, default)\n" " w (read word data)\n" " c (write byte/read byte)\n" -" Append p for SMBus PEC\n" ); +" i (read I2C block data)\n" +" Append p for SMBus PEC\n" +" LENGTH is the I2C block data length\n" );exit (1 ); }
@@ -89,6 +91,13 @@ static int check_funcs(int file, int sizreturn -1 ; }break ; + +case I2C_SMBUS_I2C_BLOCK_DATA : +if (!(funcs &I2C_FUNC_SMBUS_READ_I2C_BLOCK )) { +fprintf (stderr,MISSING_FUNC_FMT ,"SMBus read I2C block data" ); +return -1 ; + } +break ; } if (pec
@@ -101,7 +110,7 @@ static int check_funcs(int file, int siz }static int confirm (const char *filename,int address,int size,int daddress, -int pec) +int length,int pec) {int dont =0 ;
@@ -132,11 +141,15 @@ static int confirm(const char *filename,fprintf (stderr,"current data\naddress" ); else fprintf(stderr, "data address\n0x%02x", daddress); -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" : -"read word data" ); +if (size ==I2C_SMBUS_I2C_BLOCK_DATA ) +fprintf (stderr,", %d %s using read I2C block data.\n" , + length, length >1 ?"bytes" :"byte" ); +else +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" : +"read word data" );if (pec)fprintf (stderr,"PEC checking enabled.\n" );
@@ -159,6 +172,8 @@ int main(int argc, char *argv[])int pec =0 ;int flags =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 */ while (1 +flags < argc && argv[1 +flags][0 ] =='-' ) {
@@ -209,11 +224,30 @@ int main(int argc, char *argv[])case 'b' : size =I2C_SMBUS_BYTE_DATA ;break ;case 'w' : size =I2C_SMBUS_WORD_DATA ;break ;case 'c' : size =I2C_SMBUS_BYTE ;break ; +case 'i' : size =I2C_SMBUS_I2C_BLOCK_DATA ;break ;default :fprintf (stderr,"Error: Invalid mode!\n" );help (); } pec = argv[flags+4 ][1 ] =='p' ; +if (size ==I2C_SMBUS_I2C_BLOCK_DATA && pec) { +fprintf (stderr,"Error: PEC not supported for I2C block data!\n" ); +help (); + } + } + +if (argc > flags +5 ) { +if (size !=I2C_SMBUS_I2C_BLOCK_DATA ) { +fprintf (stderr,"Error: Length only valid for I2C block data!\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 );
@@ -222,7 +256,7 @@ int main(int argc, char *argv[]) || set_slave_addr(file, address, force))exit (1 ); -if (!yes && !confirm (filename, address, size, daddress, pec)) +if (!yes && !confirm (filename, address, size, daddress, length, pec))exit (0 );if (pec &&ioctl (file,I2C_PEC ,1 ) <0 ) {
@@ -244,6 +278,9 @@ int main(int argc, char *argv[])case I2C_SMBUS_WORD_DATA : res =i2c_smbus_read_word_data (file, daddress);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 */ res =i2c_smbus_read_byte_data (file, daddress); }
@@ -254,7 +291,15 @@ int main(int argc, char *argv[])exit (2 ); } -printf ("0x%0*x\n" , size ==I2C_SMBUS_WORD_DATA ?4 :2 , res); +if (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 ); }
Signature · 2 lines
Jean Delvare SUSE L3 Support
Re: [PATCH 1/7] i2cget: Add support for I2C block data
tools/i2cget.c static void help(void) __attribute__ ((n · line 53
Quoted 2 lines
tools/i2cget.c +1
-0+ " LENGTH is the I2C block data length\n");
I think it would be good to state the allowed length values. Same for patch 2.
Re: [PATCH 1/7] i2cget: Add support for I2C block data
Quoted 4 lines · Wolfram Sang
tools/i2cget.c static void help(void) __attribute__ ((n · line 53
Quoted 1 line
tools/i2cget.c +1
-0+ " LENGTH is the I2C block data length\n");I think it would be good to state the allowed length values. Same for patch 2.
Good idea. Done, thanks.
Signature · 2 lines
Jean Delvare SUSE L3 Support
PATCH 2/7 i2cget: Document the support of I2C block reads
In reply to Jean DelvareMention I2C block read support in the i2cget(8) manual page, together with an example.
--- tools/i2cget.8 | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-)
tools/i2cget.8 +15 -5
@@ -1,4 +1,4 @@ -.TH I2CGET 8 "October 2017" +.TH I2CGET 8 "June 2021" .SH "NAME" i2cget \- read from I2C/SMBus chip registers
@@ -9,7 +9,7 @@ i2cget \- read from I2C/SMBus chip regis .RB [ -a ] .I i2cbus .I chip-address -.RI [ "data-address " [ mode ]] +.RI [ "data-address " [ "mode " [ length ]]] .br .B i2cget .B -V
@@ -49,9 +49,11 @@ an integer between 0x00 and 0xFF. If omi will be read (if that makes sense for the considered chip). .PP The \fImode\fR parameter, if specified, is one of the letters \fBb\fP, -\fBw\fP or \fBc\fP, corresponding to a read byte data, a read word data or a -write byte/read byte transaction, respectively. A \fBp\fP can also be appended -to the \fImode\fR parameter to enable PEC. If the \fImode\fR parameter is omitted, +\fBw\fP, \fBc\fP, or \fBi\fP, corresponding to a read byte data, a read +word data, a write byte/read byte, or a read I2C block transaction, +respectively. A \fBp\fP can also be appended to the \fImode\fR parameter to +enable PEC, except for I2C block transactions. If the \fImode\fR +parameter is omitted, i2cget defaults to a read byte data transaction, unless \fIdata-address\fR is also omitted, in which case the default (and only valid) transaction is a single read byte.
@@ -112,6 +114,14 @@ concerns raised above still stand, howev equivalent, so this is the only way to read data from a large EEPROM if your master isn't fully I2C capable. With a fully I2C capable master, you would use \fIi2ctransfer\fR to achieve the same in a safe and faster way. +.PP +Read the first 8 bytes of an EEPROM device at 7-bit address 0x50 +on bus 4 (i2c-4): +.nf +.RS +# i2cget -y 4 0x50 0x00 i 8 +.RE +.fi .SH BUGS To report bugs or send fixes, please write to the Linux I2C mailing list
Signature · 2 lines
Jean Delvare SUSE L3 Support
PATCH 3/7 i2cget: Add support for SMBus block read
In reply to Jean DelvareNow that i2cget supports I2C block read, adding support for SMBus block read is trivial. This restores the symmetry between i2cset and i2cget, and paves the road for the removal of SMBus block read support from i2cdump.
--- tools/i2cget.8 | 6 +++--- tools/i2cget.c | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-)
tools/i2cget.c +16 -2
@@ -1,6 +1,6 @@/* i2cget.c - A user-space program to read an I2C register. -Copyright (C) 2005-2012 Jean Delvare <[email protected]> +Copyright (C) 2005-2021 Jean Delvare <[email protected]> Based on i2cset.c: Copyright (C) 2001-2003 Frodo Looijaard <[email protected]>, and
@@ -48,6 +48,7 @@ static void help(void)" b (read byte data, default)\n" " w (read word data)\n" " c (write byte/read byte)\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\n");
@@ -92,6 +93,13 @@ static int check_funcs(int file, int siz }break ; +case I2C_SMBUS_BLOCK_DATA : +if (!(funcs &I2C_FUNC_SMBUS_READ_BLOCK_DATA )) { +fprintf (stderr,MISSING_FUNC_FMT ,"SMBus read block data" ); +return -1 ; + } +break ; +case I2C_SMBUS_I2C_BLOCK_DATA :if (!(funcs &I2C_FUNC_SMBUS_READ_I2C_BLOCK )) {fprintf (stderr,MISSING_FUNC_FMT ,"SMBus read I2C block data" );
@@ -149,6 +157,7 @@ static int confirm(const char *filename, 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)fprintf (stderr,"PEC checking enabled.\n" );
@@ -224,6 +233,7 @@ int main(int argc, char *argv[])case 'b' : size =I2C_SMBUS_BYTE_DATA ;break ;case 'w' : size =I2C_SMBUS_WORD_DATA ;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 :fprintf (stderr,"Error: Invalid mode!\n" );
@@ -278,6 +288,9 @@ int main(int argc, char *argv[])case I2C_SMBUS_WORD_DATA : res =i2c_smbus_read_word_data (file, daddress);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 ;
@@ -291,7 +304,8 @@ int main(int argc, char *argv[])exit (2 ); } -if (size ==I2C_SMBUS_I2C_BLOCK_DATA ) { +if (size ==I2C_SMBUS_BLOCK_DATA || + size ==I2C_SMBUS_I2C_BLOCK_DATA ) {int i;for (i =0 ; i < res -1 ; ++i)
tools/i2cget.8 +3 -3
@@ -50,9 +50,9 @@ will be read (if that makes sense for th .PP The \fImode\fR parameter, if specified, is one of the letters \fBb\fP, \fBw\fP, \fBc\fP, or \fBi\fP, corresponding to a read byte data, a read -word data, a write byte/read byte, or a read I2C block transaction, -respectively. A \fBp\fP can also be appended to the \fImode\fR parameter to -enable PEC, except for I2C block transactions. If the \fImode\fR +word data, a write byte/read byte, a read SMBus block, or a read I2C block +transaction, respectively. A \fBp\fP can also be appended to the \fImode\fR +parameter to enable PEC, except for I2C block transactions. If the \fImode\fR parameter is omitted, i2cget defaults to a read byte data transaction, unless \fIdata-address\fR is also omitted, in which case the default (and only valid) transaction is a
Signature · 2 lines
Jean Delvare SUSE L3 Support
PATCH 4/7 i2cdump: Remove dead code
In reply to Jean DelvareConsidering that we exit immediately if an error happens during a block read, the code filling the remaining bytes with -1 so that they will be displayed as "XX" is effectively dead code, so let's remove it.
We might want to revisit the whole logic later, as there's in fact little reason why block read errors would be fatal when all other read errors are not. But I'd rather remove SMBus block support from the tool before attempting to clean up the code.
--- tools/i2cdump.c | 3 --- 1 file changed, 3 deletions(-)
tools/i2cdump.c +0 -3
@@ -360,9 +360,6 @@ int main(int argc, char *argv[]) res =256 ;for (i =0 ; i < res; i++) block[i] = cblock[i]; -if (size !=I2C_SMBUS_BLOCK_DATA ) -for (i = res; i <256 ; i++) - block[i] =-1 ; }if (size ==I2C_SMBUS_BYTE ) {
Signature · 2 lines
Jean Delvare SUSE L3 Support
PATCH 5/7 i2cdump: Add range support with mode i (I2C block)
In reply to Jean DelvareImplementing range support for I2C block reads (mode i) isn't particularly difficult so let's just do that.
--- tools/i2cdump.8 | 6 +++--- tools/i2cdump.c | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-)
tools/i2cdump.c +5 -5
@@ -2,7 +2,7 @@ i2cdump.c - a user-space program to dump I2C registers Copyright (C) 2002-2003 Frodo Looijaard <[email protected]>, and Mark D. Studebaker <[email protected]> - Copyright (C) 2004-2012 Jean Delvare <[email protected]> + Copyright (C) 2004-2021 Jean Delvare <[email protected]> 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
@@ -251,6 +251,7 @@ int main(int argc, char *argv[])switch (size) {case I2C_SMBUS_BYTE :case I2C_SMBUS_BYTE_DATA : +case I2C_SMBUS_I2C_BLOCK_DATA :break ;case I2C_SMBUS_WORD_DATA :if (!even || (!(first%2 ) && last%2 ))
@@ -341,8 +342,9 @@ int main(int argc, char *argv[])/* Remember returned block length for a nicer display later */ s_length = res; + last = res -1 ; }else { -for (res =0 ; res <256 ; res += i) { +for (res = first; res <= last; res += i) { i =i2c_smbus_read_i2c_block_data (file, res,32 , cblock + res);if (i <=0 ) {
@@ -356,9 +358,7 @@ int main(int argc, char *argv[]) "return code %d\n", res);exit (1 ); } -if (res >=256 ) - res =256 ; -for (i =0 ; i < res; i++) +for (i = first; i <= last; i++) block[i] = cblock[i]; }
tools/i2cdump.8 +3 -3
@@ -1,4 +1,4 @@ -.TH I2CDUMP 8 "October 2017" +.TH I2CDUMP 8 "June 2021" .SH NAME i2cdump \- examine I2C registers
@@ -32,8 +32,8 @@ kernel driver in question. It can also c results. So use at your own risk and only if you know what you're doing. .TP .B -r first-last -Limit the range of registers being accessed. This option is only available -with modes \fBb\fP, \fBw\fP, \fBc\fP and \fBW\fP. For mode \fBW\fP, +Limit the range of registers being accessed. This option is not available +with mode \fBs\fP. For mode \fBW\fP, \fBfirst\fR must be even and \fBlast\fR must be odd. .TP .B -y
Signature · 2 lines
Jean Delvare SUSE L3 Support
PATCH 6/7 i2cdump: Deprecate SMBus block mode
In reply to Jean Delvarei2cget is a better fit for this mode. Having it in i2cdump requires special-casing in various places, which makes the code harder to read and maintain.
--- tools/i2cdump.8 | 13 ++++--------- tools/i2cdump.c | 4 +++- 2 files changed, 7 insertions(+), 10 deletions(-)
tools/i2cdump.8 +4 -9
@@ -106,21 +106,16 @@ bus 1 (i2c-1), using the default read me # i2cdump -r 0x00-0x3f 1 0x2d .RE .fi -.PP -Dump the registers of the SMBus device at address 0x69 on bus 0 (i2c-0), -using one SMBus block read transaction with error checking enabled, after -user confirmation: -.nf -.RS -# i2cdump 0 0x69 sp -.RE -.fi .SH BUGS To report bugs or send fixes, please write to the Linux I2C mailing list <[email protected]> with Cc to the current maintainer: Jean Delvare <[email protected]>. +SMBus block mode is deprecated and will be removed in a future version +of this tool. +Please use \fIi2cget\fR instead. + .SH SEE ALSO i2cdetect(8), i2cget(8), i2cset(8), i2ctransfer(8), isadump(8)
tools/i2cdump.c +3 -1
@@ -43,7 +43,7 @@ static void help(void)" b (byte, default)\n" " w (word)\n" " W (word on even register addresses)\n" -" s (SMBus block)\n" +" s (SMBus block, deprecated)\n" " i (I2C block)\n" " c (consecutive byte)\n" " Append p for SMBus PEC\n");
@@ -181,6 +181,8 @@ int main(int argc, char *argv[]) even =1 ; }else if (!strncmp (argv[flags+3 ],"s" ,1 )) { size =I2C_SMBUS_BLOCK_DATA ; +fprintf (stderr, +"SMBus Block mode is deprecated, please use i2cget instead\n" ); pec = argv[flags+3 ][1 ] =='p' ; }else if (!strncmp (argv[flags+3 ],"c" ,1 )) { size =I2C_SMBUS_BYTE ;
Signature · 2 lines
Jean Delvare SUSE L3 Support
PATCH 7/7 i2cdump: Remove support for SMBus block mode
In reply to Jean DelvareUsers can turn to i2cget for this feature.
--- tools/i2cdump.8 | 7 ++-- tools/i2cdump.c | 81 +++++++++++--------------------------------------------- 2 files changed, 20 insertions(+), 68 deletions(-)
tools/i2cdump.8 +3 -4
@@ -51,8 +51,8 @@ of the busses listed by \fIi2cdetect -l\ address to be scanned on that bus, and is an integer between 0x08 and 0x77. .PP The \fImode\fR parameter, if specified, is one of the letters \fBb\fP, \fBw\fP, -\fBs\fP, or \fBi\fP, corresponding to a read size of a single byte, a 16-bit -word, an SMBus block, an I2C block, respectively. The \fBc\fP mode is a +or \fBi\fP, corresponding to a read size of a single byte, a 16-bit +word, an I2C block, respectively. The \fBc\fP mode is a little different, it reads all bytes consecutively, and is useful for chips that have an address auto-increment feature, such as EEPROMs. The \fBW\fP mode is also special, it is similar to \fBw\fP except that a read command will only
@@ -112,8 +112,7 @@ To report bugs or send fixes, please wri <[email protected]> with Cc to the current maintainer: Jean Delvare <[email protected]>. -SMBus block mode is deprecated and will be removed in a future version -of this tool. +SMBus block mode used to be supported by this tool. Please use \fIi2cget\fR instead. .SH SEE ALSO
tools/i2cdump.c +17 -64
171 lines not on this page · raw message
Signature · 2 lines
Jean Delvare SUSE L3 Support
Hi Jean,
Quoted 3 lines
The idea would be to get the first 6 patches in the upcoming i2c-tools v4.3, and apply the 7th patch "later" (either immediately after that release, or some time later, I'm not sure).
I agree with this approach.
I had a glimpse at the patches and think they look good so far. I would have squashed patches 1+2, but this minor, of course. I'll try to test them this weekend, too. Let's see...
Hi Wolfram,
Quoted 8 lines · Wolfram Sang
Quoted 3 lines
The idea would be to get the first 6 patches in the upcoming i2c-tools v4.3, and apply the 7th patch "later" (either immediately after that release, or some time later, I'm not sure).
I agree with this approach.
I had a glimpse at the patches and think they look good so far. I would have squashed patches 1+2, but this minor, of course.
I would have too but patch 1 was submitted by somebody else. I didn't want to put more changes in it than was required for committing acceptance, so as to respect the original submission.
Signature · 2 lines
Jean Delvare SUSE L3 Support
Quoted 4 lines
I would have too but patch 1 was submitted by somebody else. I didn't want to put more changes in it than was required for committing acceptance, so as to respect the original submission.
I see. This is fine, of course. Glad to see the patches upstream!
Quoted 4 lines
The idea would be to get the first 6 patches in the upcoming i2c-tools v4.3, and apply the 7th patch "later" (either immediately after that release, or some time later, I'm not sure).
So, my tests so far went nicely and looking more at the patches didn't reveal any major discussion point. So:
I'd vote also for an early removal. The code cleanup is too nice and i2cget is, in deed, the better place for mode 's'.
Hi Wolfram,
Quoted 4 lines · Wolfram Sang
Quoted 3 lines
The idea would be to get the first 6 patches in the upcoming i2c-tools v4.3, and apply the 7th patch "later" (either immediately after that release, or some time later, I'm not sure).
Quoted 8 lines
So, my tests so far went nicely and looking more at the patches didn't reveal any major discussion point. So:
Reviewed-by Wolfram Sang <[email protected]>Tested-by Wolfram Sang <[email protected]>I'd vote also for an early removal. The code cleanup is too nice and i2cget is, in deed, the better place for mode 's'.
Thanks for the review and testing, very much appreciated. I have committed the first 6 patches, with cosmetic fixes after a last self-review.
With this, we are ready to release i2c-tools 4.3.
Signature · 2 lines
Jean Delvare SUSE L3 Support