avr-gcc-list
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Saving & retreiving a structure in eeprom


From: Dave Hansen
Subject: Re: Saving & retreiving a structure in eeprom
Date: Tue, 9 Jun 2020 18:57:26 +0000

You wrote
>>>   eeprom_read_block((void *)&setPgm, (const void *)eepLoc, num) ;

What is num?

   -=Dave

From: AVR-GCC-list <avr-gcc-list-bounces+iddw=hotmail.com@nongnu.org> on behalf of Royce Pereira <roycejp@gmail.com>
Sent: Tuesday, June 9, 2020 8:18 AM
To: avr-gcc-list@nongnu.org <avr-gcc-list@nongnu.org>
Subject: Saving & retreiving a structure in eeprom
 
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

reply via email to

[Prev in Thread] Current Thread [Next in Thread]