Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix various bugs in eeprom handling #46

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions 24c16.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ int update_checksum(t_eeprom_data* data)

p_data = (char*)data;

for (size_t i=0; i<sizeof(*data); i++)
for (size_t i=0; i < (sizeof(*data) - sizeof(data->checksum)); i++)
{
checksum += *p_data;
p_data++;
}

//fprintf(stderr, "Checksum: %x\n", checksum);
data->checksum = checksum;

return (0);
}

Expand Down Expand Up @@ -56,7 +57,7 @@ char verify_checksum(t_eeprom_data* data)

p_data = (char*)data;

for (size_t i=0; i<sizeof(*data); i++)
for (size_t i=0; i < (sizeof(*data) - sizeof(data->checksum)); i++)
{
checksum += *p_data;
p_data++;
Expand Down
7 changes: 6 additions & 1 deletion 24c16.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#define EEPROM_DATA_VERSION 1

#define SN_LENGTH 8

// define struct for MS5611 sensor
typedef struct {
int fd;
Expand All @@ -11,11 +13,14 @@ typedef struct {
typedef struct {
char header[3];
char data_version;
char serial[6];
float zero_offset;
char serial[SN_LENGTH];
char padding[3];
char checksum;
} t_eeprom_data;



// prototypes
int eeprom_open(t_24c16 *, unsigned char);
char eeprom_write(t_24c16 *, char *, unsigned char, unsigned char);
Expand Down
13 changes: 7 additions & 6 deletions sensorcal.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ int main (int argc, char **argv) {
int result;
int c;
int i;
char sn[7];
char sn[SN_LENGTH];
char zero[1]={0x00};


Expand Down Expand Up @@ -128,8 +128,9 @@ int main (int argc, char **argv) {
}
strcpy(data.header, "OV");
data.data_version = EEPROM_DATA_VERSION;
memset(data.serial,'0',6);
memset(data.serial,'0',SN_LENGTH);
data.zero_offset=0.0;
memset(data.padding,'0',3);
update_checksum(&data);
printf("Writing data to EEPROM ...\n");
result = eeprom_write(&eeprom, (char*)&data, 0x00, sizeof(data));
Expand Down Expand Up @@ -171,7 +172,7 @@ int main (int argc, char **argv) {
printf("Reading EEPROM values ...\n\n");
if( eeprom_read_data(&eeprom, &data) == 0)
{
memcpy(sn,data.serial,6);
memcpy(sn,data.serial,SN_LENGTH);
printf("Actual EEPROM values:\n");
printf("---------------------\n");
printf("Serial: \t\t\t%s\n", sn);
Expand All @@ -189,12 +190,12 @@ int main (int argc, char **argv) {
break;

case 's':
if( strlen(optarg) == 6)
if( strlen(optarg) == SN_LENGTH)
{
// read actual EEPROM values
if( eeprom_read_data(&eeprom, &data) == 0)
{
for(i=0; i<6;i++)
for(i=0; i<SN_LENGTH;i++)
{
sn[i]=data.serial[i]=*optarg;
optarg++;
Expand All @@ -214,7 +215,7 @@ int main (int argc, char **argv) {
}
else
{
printf("ERROR: Serialnumber has to have exactly 6 characters !!\n");
printf("ERROR: Serialnumber has to have exactly 8 characters !!\n");
exit_code=1;
break;
}
Expand Down