Reading an EEPROM from within C

Hi,

...may be I am totally wrong here, but...

When doing a

    hexdump /sys/devices/platform/omap/omap_i2c.3/i2c-3/3-0057/eeprom

I got an hexdump of the eeproms contents.

When I do an

    fh = fopen( /sys/devices/platform/omap/omap_i2c.3/i2c-3/3-0057/eeprom, "r" );
    fread( fh, .....)
    fclose( ...);

I cannot read a single byte.

I am sure its my fault...but where?

Best regards,
mcc

maybe the lseek?

Have a look here how I'm doing it: https://github.com/modmaker/BeBoPr/blob/master/eeprom.c

-- Bas

This works for me…

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define sizeEEPROM 244
unsigned char eeprom[sizeEEPROM+100]; // Give a little extra for no good reason

int main(int argc, char* argv[])
{
// read data to file
char *file = “/sys/bus/i2c/drivers/at24/3-0054/eeprom”;
FILE *p = NULL;
p = fopen(file, “r”);
if (p== NULL) {
printf(“Error in opening a file…”, file);
return(1);
}
fread(eeprom, sizeEEPROM, 1, p);
fclose§;

int x;
for (x=0; x<sizeEEPROM; x++) {
if (!(x % 16))
printf("\n");
printf(" %2x", eeprom[x]);
}

printf("\n\n");
return 0;
}

  • Ken -

K.Keller <Ken@AZKeller.com> [12-10-22 04:28]:

>
> Hi,
>
> ...may be I am totally wrong here, but...
>
> When doing a
>
> hexdump /sys/devices/platform/omap/omap_i2c.3/i2c-3/3-0057/eeprom
>
> I got an hexdump of the eeproms contents.
>
> When I do an
>
> fh = fopen( /sys/devices/platform/omap/omap_i2c.3/i2c-3/3-0057/eeprom,
> "r" );
> fread( fh, .....)
> fclose( ...);
>
> I cannot read a single byte.
>
> I am sure its my fault...but where?
>
> Best regards,
> mcc
>
>
This works for me...

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define sizeEEPROM 244
unsigned char eeprom[sizeEEPROM+100]; // Give a little extra for no good
reason

int main(int argc, char* argv[])
{
    // read data to file
    char *file = "/sys/bus/i2c/drivers/at24/3-0054/eeprom";
    FILE *p = NULL;
    p = fopen(file, "r");
    if (p== NULL) {
        printf("Error in opening a file..", file);
        return(1);
    }
    fread(eeprom, sizeEEPROM, 1, p);
    fclose(p);

    int x;
    for (x=0; x<sizeEEPROM; x++) {
        if (!(x % 16))
            printf("\n");
        printf(" %2x", eeprom[x]);
    }

    printf("\n\n");
    return 0;
}

- Ken -

--

Hi Bas, hi Ken

thank you for the C-codes! :slight_smile:

best regards,
mcc