Commit 0127f233 authored by Raptor Engineering Development Team's avatar Raptor Engineering Development Team Committed by Timothy Pearson

Initial version of eeprog with 16 byte page write support

parent fd58aaae
......@@ -2,6 +2,8 @@
copyright : (C) by 2003-2004 Stefano Barbato
email : stefano@codesink.org
(C) 2021 Raptor Engineering, LLC
$Id: 24cXX.c,v 1.5 2004/02/29 11:05:28 tat Exp $
***************************************************************************/
......@@ -47,14 +49,13 @@ static int i2c_write_2b(struct eeprom *e, __u8 buf[2])
return r;
}
static int i2c_write_3b(struct eeprom *e, __u8 buf[3])
static int i2c_write_arbitrary(struct eeprom *e, __u8 *buf, int len)
{
int r;
// we must simulate a plain I2C byte write with SMBus functions
// the __u16 data field will be byte swapped by the SMBus protocol
r = i2c_smbus_write_word_data(e->fd, buf[0], buf[2] << 8 | buf[1]);
// we must simulate a plain I2C byte write with SMBus functions
r = i2c_smbus_write_i2c_block_data(e->fd, buf[0], len-1, buf+1);
if(r < 0)
fprintf(stderr, "Error i2c_write_3b: %s\n", strerror(errno));
fprintf(stderr, "Error i2c_write_arbitrary: %s\n", strerror(errno));
usleep(10);
return r;
}
......@@ -174,18 +175,29 @@ int eeprom_read_byte(struct eeprom* e, __u16 mem_addr)
return r;
}
int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8 data)
int eeprom_write_byte(struct eeprom *e, __u16 mem_addr, __u8* data, int data_len)
{
int ret = 0;
int offset = 0;
__u8 write_buffer[MAX_PAGE_SIZE+2];
if(e->type == EEPROM_TYPE_8BIT_ADDR) {
__u8 buf[2] = { mem_addr & 0x00ff, data };
return i2c_write_2b(e, buf);
} else if(e->type == EEPROM_TYPE_16BIT_ADDR) {
__u8 buf[3] =
{ (mem_addr >> 8) & 0x00ff, mem_addr & 0x00ff, data };
return i2c_write_3b(e, buf);
write_buffer[0] = mem_addr & 0x00ff;
offset = 1;
}
fprintf(stderr, "ERR: unknown eeprom type\n");
return -1;
else if(e->type == EEPROM_TYPE_16BIT_ADDR) {
write_buffer[0] = (mem_addr >> 8) & 0xff;
write_buffer[1] = mem_addr & 0xff;
offset = 2;
}
else {
fprintf(stderr, "ERR: unknown eeprom type\n");
return -1;
}
memcpy(write_buffer+offset, data, data_len);
ret = i2c_write_arbitrary(e, write_buffer, data_len+offset);
usleep(5000);
return ret;
}
int eeprom_wait_ready(struct eeprom *e, int max_ms_to_wait)
......
......@@ -2,6 +2,8 @@
copyright : (C) by 2003-2004 Stefano Barbato
email : stefano@codesink.org
(C) 2021 Raptor Engineering, LLC
$Id: 24cXX.h,v 1.6 2004/02/29 11:05:28 tat Exp $
***************************************************************************/
......@@ -21,6 +23,10 @@
#define EEPROM_TYPE_8BIT_ADDR 1
#define EEPROM_TYPE_16BIT_ADDR 2
// SMBus is limited to 32 bytes
// Unfortunately, the 16-bit addressing on the EEPROM chews up one of those bytes, and further downgrades the max transfer size to 16 bytes of data (since we have to stay power of 2 aligned)
#define MAX_PAGE_SIZE 16
struct eeprom
{
char *dev; // device file i.e. /dev/i2c-N
......@@ -52,7 +58,7 @@ int eeprom_read_current_byte(struct eeprom *e);
* writes [data] at memory address [mem_addr]
* 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, int data_len);
/*
* wait for the eeprom to accept new commands, to be called after a write
*/
......
CFLAGS=-g -I. -Wall -O2
CFLAGS=-g -I. -Wall -O2 -march=armv6
all: eeprog
......
......@@ -2,6 +2,8 @@
copyright : (C) by 2003-2004 Stefano Barbato
email : stefano@codesink.org
(C) 2021 Raptor Engineering, LLC
$Id: eeprog.c,v 1.28 2004/02/29 11:06:41 tat Exp $
***************************************************************************/
......@@ -143,13 +145,41 @@ int read_from_eeprom(struct eeprom *e, int addr, int size, int hex)
int write_to_eeprom(struct eeprom *e, int addr, int timeout)
{
int actual_page_size = MAX_PAGE_SIZE;
int buffer_index = 0;
int current_byte = 0;
__u8 buffer[MAX_PAGE_SIZE];
__u32 mask = 0;
mask = (1 << actual_page_size) - 1;
if (addr & mask) {
actual_page_size = 1;
}
print_info(" Using page size: %d\n", actual_page_size);
int c;
while((c = getchar()) != EOF)
{
print_info(".");
buffer[buffer_index] = c;
buffer_index++;
if (buffer_index >= actual_page_size)
{
print_info(".");
fflush(stdout);
die_if(eeprom_write_byte(e, addr, buffer, buffer_index), "write error");
die_if(eeprom_wait_ready(e, timeout), "write timeout");
buffer_index = 0;
addr = addr + actual_page_size;
}
}
// Flush any remaining bytes one by one
for (current_byte = 0; current_byte<buffer_index; current_byte++)
{
print_info("+");
fflush(stdout);
die_if(eeprom_write_byte(e, addr++, c), "write error");
die_if(eeprom_write_byte(e, addr++, buffer + current_byte, 1), "write error");
die_if(eeprom_wait_ready(e, timeout), "write timeout");
buffer_index = 0;
}
print_info("\n\n");
return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment