add eeprom write completion waiting with timeout

- added new option "-t n" to specify write timeout
- wait after each byte write for the eeprom to accept new commands
This commit is contained in:
Axel Schwenke 2020-12-10 15:49:18 +01:00
parent 22216e6f62
commit ac41ae75eb
3 changed files with 56 additions and 32 deletions

14
24cXX.c
View file

@ -89,6 +89,7 @@ int eeprom_open(char *dev_fqn, int addr, int type, struct eeprom* e)
// check for req funcs // check for req funcs
CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_QUICK );
CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE ); CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE );
CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE ); CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_WRITE_BYTE );
CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA ); CHECK_I2C_FUNC( funcs, I2C_FUNC_SMBUS_READ_BYTE_DATA );
@ -187,3 +188,16 @@ int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data)
return -1; return -1;
} }
int eeprom_wait_ready(struct eeprom *e, int max_ms_to_wait)
{
while (max_ms_to_wait > 0) {
int r = i2c_smbus_write_quick(e->fd, 0);
if (r == 0) {
return r;
}
usleep(1000);
--max_ms_to_wait;
}
return -1;
}

View file

@ -15,7 +15,7 @@
***************************************************************************/ ***************************************************************************/
#ifndef _24CXX_H_ #ifndef _24CXX_H_
#define _24CXX_H_ #define _24CXX_H_
#include "i2c-dev.h" #include <linux/i2c-dev.h>
#define EEPROM_TYPE_UNKNOWN 0 #define EEPROM_TYPE_UNKNOWN 0
#define EEPROM_TYPE_8BIT_ADDR 1 #define EEPROM_TYPE_8BIT_ADDR 1
@ -53,6 +53,10 @@ int eeprom_read_current_byte(struct eeprom *e);
* Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address) * Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address)
*/ */
int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data); int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data);
/*
* wait for the eeprom to accept new commands, to be called after a write
*/
int eeprom_wait_ready(struct eeprom *e, int max_ms_to_wait);
#endif #endif

View file

@ -46,6 +46,7 @@ const static char *eeprog_usage =
" -r addr[:count] Read [count] (1 if omitted) bytes from [addr]\n" " -r addr[:count] Read [count] (1 if omitted) bytes from [addr]\n"
" and print them to the standard output\n" " and print them to the standard output\n"
" -w addr Write input (stdin) at address [addr] of the EEPROM\n" " -w addr Write input (stdin) at address [addr] of the EEPROM\n"
" -t n write timeout in ms (default 10)\n"
" -h Print this help\n" " -h Print this help\n"
" Options: \n" " Options: \n"
" -x Set hex output mode\n" " -x Set hex output mode\n"
@ -140,7 +141,7 @@ int read_from_eeprom(struct eeprom *e, int addr, int size, int hex)
return 0; return 0;
} }
int write_to_eeprom(struct eeprom *e, int addr) int write_to_eeprom(struct eeprom *e, int addr, int timeout)
{ {
int c; int c;
while((c = getchar()) != EOF) while((c = getchar()) != EOF)
@ -148,6 +149,7 @@ int write_to_eeprom(struct eeprom *e, int addr)
print_info("."); print_info(".");
fflush(stdout); fflush(stdout);
die_if(eeprom_write_byte(e, addr++, c), "write error"); die_if(eeprom_write_byte(e, addr++, c), "write error");
die_if(eeprom_wait_ready(e, timeout), "write timeout");
} }
print_info("\n\n"); print_info("\n\n");
return 0; return 0;
@ -157,6 +159,7 @@ int main(int argc, char** argv)
{ {
struct eeprom e; struct eeprom e;
int ret, op, i2c_addr, memaddr, size, want_hex, dummy, force, sixteen; int ret, op, i2c_addr, memaddr, size, want_hex, dummy, force, sixteen;
int timeout = 10;
char *device, *arg = 0, *i2c_addr_s; char *device, *arg = 0, *i2c_addr_s;
struct stat st; struct stat st;
int eeprom_type = 0; int eeprom_type = 0;
@ -192,6 +195,9 @@ int main(int argc, char** argv)
case 'h': case 'h':
usage_if(1); usage_if(1);
break; break;
case 't':
timeout = atoi(optarg);
break;
default: default:
die_if(op != 0, "Both read and write requested"); die_if(op != 0, "Both read and write requested");
arg = optarg; arg = optarg;
@ -257,9 +263,9 @@ int main(int argc, char** argv)
if(force == 0) if(force == 0)
confirm_action(); confirm_action();
parse_arg(arg, &memaddr, &size); parse_arg(arg, &memaddr, &size);
print_info(" Writing stdin starting at address 0x%x\n", print_info(" Writing stdin starting at address 0x%x with timeout %dms\n",
memaddr); memaddr, timeout);
write_to_eeprom(&e, memaddr); write_to_eeprom(&e, memaddr, timeout);
break; break;
default: default:
usage_if(1); usage_if(1);