Skip to content

Commit 03b87f0

Browse files
committed
Change eeprom_bytes from unit16_t to uint32_t
1 parent f621281 commit 03b87f0

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

at24c.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,17 @@ static int i2c_write_2b(struct eeprom *e, __s8 i2c_addr, __u8 buf[2])
4242
int r;
4343
if( ( r = ioctl(e->fd, I2C_SLAVE, i2c_addr)) < 0)
4444
{
45+
fprintf(stderr, "ioctl r=%d\n", r);
4546
fprintf(stderr, "Error i2c_write_2b: %s\n", strerror(errno));
4647
return r;
4748
}
4849

4950
// we must simulate a plain I2C byte write with SMBus functions
5051
r = i2c_smbus_write_byte_data(e->fd, buf[0], buf[1]);
51-
if(r < 0)
52+
if(r < 0) {
53+
fprintf(stderr, "i2c_smbus_write_word_data r=%d\n", r);
5254
fprintf(stderr, "Error i2c_write_2b: %s\n", strerror(errno));
55+
}
5356
usleep(10);
5457
return r;
5558
}
@@ -60,15 +63,18 @@ static int i2c_write_3b(struct eeprom *e, __s8 i2c_addr, __u8 buf[3])
6063
int r;
6164
if( ( r = ioctl(e->fd, I2C_SLAVE, i2c_addr)) < 0)
6265
{
66+
fprintf(stderr, "ioctl r=%d\n", r);
6367
fprintf(stderr, "Error i2c_write_3b: %s\n", strerror(errno));
6468
return r;
6569
}
6670

6771
// we must simulate a plain I2C byte write with SMBus functions
6872
// the __u16 data field will be byte swapped by the SMBus protocol
6973
r = i2c_smbus_write_word_data(e->fd, buf[0], buf[2] << 8 | buf[1]);
70-
if(r < 0)
74+
if(r < 0) {
75+
fprintf(stderr, "i2c_smbus_write_word_data r=%d\n", r);
7176
fprintf(stderr, "Error i2c_write_3b: %s\n", strerror(errno));
77+
}
7278
usleep(10);
7379
return r;
7480
}
@@ -112,7 +118,7 @@ int eeprom_open(char *dev_fqn, int i2c_addr, int bits, int write_cycle_time, str
112118

113119
#if 0
114120
// set working device
115-
if( ( r = ioctl(fd, I2C_SLAVE, addr)) < 0)
121+
if( ( r = ioctl(fd, I2C_SLAVE, i2c_addr)) < 0)
116122
{
117123
fprintf(stderr, "Error eeprom_open: %s\n", strerror(errno));
118124
return -1;
@@ -124,7 +130,7 @@ int eeprom_open(char *dev_fqn, int i2c_addr, int bits, int write_cycle_time, str
124130
e->bits = bits;
125131
e->type = EEPROM_TYPE_8BIT_ADDR;
126132
if (bits > 16) e->type = EEPROM_TYPE_16BIT_ADDR;
127-
e->bytes = 128 * bits;
133+
e->bytes = (__u32)128 * (__u32)bits;
128134
e->write_cycle_time = write_cycle_time;
129135
//printf("bits=%d bytes=%d type=%d\n",e->bits, e->bytes, e->type);
130136
return 0;
@@ -139,7 +145,7 @@ int eeprom_close(struct eeprom *e)
139145
return 0;
140146
}
141147

142-
__u16 getEEPROMbytes(struct eeprom* e)
148+
__u32 getEEPROMbytes(struct eeprom* e)
143149
{
144150
return(e->bytes);
145151
}

at24c.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ struct eeprom
2222
int type; // eeprom register size(8bit/16bit)
2323
int write_cycle_time;
2424
int bits; // eeprom memory size(Kbits)
25-
__u16 bytes; // eeprom memory size(byte)
25+
//__u16 bytes; // eeprom memory size(byte)
26+
__u32 bytes; // eeprom memory size(byte)
2627
};
2728

2829
/*
@@ -39,7 +40,7 @@ int eeprom_close(struct eeprom *e);
3940
/*
4041
* get EEPROM bytes
4142
*/
42-
__u16 getEEPROMbytes(struct eeprom* e);
43+
__u32 getEEPROMbytes(struct eeprom* e);
4344
/*
4445
* read and returns the eeprom byte at memory address [mem_addr]
4546
* Note: eeprom must have been selected by ioctl(fd,I2C_SLAVE,address)

main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ int main(int argc, char *argv[])
140140

141141

142142
// get EEPROM size(byte)
143-
uint16_t eeprom_bytes = getEEPROMbytes(&e);
143+
uint32_t eeprom_bytes = getEEPROMbytes(&e);
144144
printf("EEPROM chip=24C%.02d bytes=%dByte\n",eeprom_bits,eeprom_bytes);
145145

146146
uint16_t mem_addr;

0 commit comments

Comments
 (0)