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
master
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_I2C_FUNC( funcs, I2C_FUNC_SMBUS_QUICK );
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_READ_BYTE_DATA );
@ -187,3 +188,16 @@ int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data)
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_
#define _24CXX_H_
#include "i2c-dev.h"
#include <linux/i2c-dev.h>
#define EEPROM_TYPE_UNKNOWN 0
#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)
*/
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

View File

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