Saving & retreiving a structure in eeprom
Royce Pereira <[email protected]> Tue, 9 Jun 2020 18:48:45 +0530
| Newsgroups | gmane.comp.hardware.avr.gcc |
|---|---|
| Message-ID | <CACzHknj9hyZq0++gz_CbPgyNfREquVzDr9G0RLspm_ZnVZD6Eg@mail.gmail.com> |
Hi,
The MPU is ATMega8. AVR-GCC version is 9.1.0
I have a structure that I'm trying to save (& retrieve in eeprom).
Furthermore, it is to be saved in an indexed location in the eeprom from 0
to 29.
The structure is 4 bytes in size. So total 127 bytes are used in EEPROM
//-----------------------------------------
#define EEP_START 7 //offset in EEPROM where my structure storage
starts.
typedef struct
{
int temp ;
unsigned int time ;
} _pgm ;
#define STRU_SIZE (sizeof(_pgm))
_pgm setPgm ; //create struct in RAM.
uint16_t eepLoc ; //holds address of EEPROM
uint8_t index ; //index to read from in EEPROM...
//----------------------------------------------------------
//save struct from RAM to an indexed location in EEPROM...
void savPgm(char idx)
{
eepLoc = EEP_START + ((uint16_t)idx * STRU_SIZE) ;
eeprom_write_block((const void *)&setPgm, (void *)eepLoc, STRU_SIZE) ;
return ;
}
//-------------------------------------------------------
//Retreive struct from an indexed location in EEPROM....
void getPgm(char idx)
{
eepLoc = EEP_START + ((uint16_t)idx * STRU_SIZE) ;
eeprom_read_block((void *)&setPgm, (const void *)eepLoc, num) ;
return ;
}
//------------------------------------------
int main(void)
{
index = 5 ;
setPgm.temp = 50 ;
setPgm.time = 10 ;
savPgm(index) ; //example .. save in 6th location in EEPROM;
//Now read the saved struct back from eeprom...
// this reads back correctly.:-
setPgm.temp = eeprom_read_word((const uint16_t *)eepLoc) ; //eepLoc was set
before.
setPgm.time = eeprom_read_word((const uint16_t *)eepLoc + 1) ;
//But this reads 0xFFFF for both elementss. (Why?):-
getPgm(index) ; //uses block-read function
return 0 ;
}
//---------------------------------------------------------------
The getPgm function uses the read block api but retreives garbage(0xFFFF);
What am I doing wrong ?
Thank you!
--
Best Regards,
-- Royce Pereira
--
Best Regards,
-- Royce Pereira