Re: Saving & retreiving a structure in eeprom

2020-06-10 Thread Royce Pereira
Hi,

Sorry, num should be STRU_SIZE.

But that's not the error I'm getting, It's just a typo while pasting a
sample code here.

In my actual code I did use STRU_SIZE (not num).



On Wed, 10 Jun 2020 at 00:27, Dave Hansen  wrote:

> You wrote
> >>>   eeprom_read_block((void *), (const void *)eepLoc, num) ;
>
> What is num?
>
>-=Dave
> --
> *From:* AVR-GCC-list 
> on behalf of Royce Pereira 
> *Sent:* Tuesday, June 9, 2020 8:18 AM
> *To:* 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_START7 //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_teepLoc ;   //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 *), (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 *), (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 0x for both elementss. (Why?):-
> getPgm(index) ;  //uses block-read function
>
> return 0 ;
> }
> //---
> The getPgm function uses the read block api but retreives garbage(0x);
> What am I doing wrong ?
>
> Thank you!
> --
> Best Regards,
>
> -- Royce Pereira
>
> --
> Best Regards,
>
> -- Royce Pereira
>


-- 
Best Regards,

-- Royce Pereira


Re: Saving & retreiving a structure in eeprom

2020-06-09 Thread Dave Hansen
You wrote
>>>   eeprom_read_block((void *), (const void *)eepLoc, num) ;

What is num?

   -=Dave

From: AVR-GCC-list  on behalf 
of Royce Pereira 
Sent: Tuesday, June 9, 2020 8:18 AM
To: 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_START7 //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_teepLoc ;   //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 *), (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 *), (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 0x for both elementss. (Why?):-
getPgm(index) ;  //uses block-read function

return 0 ;
}
//---
The getPgm function uses the read block api but retreives garbage(0x);
What am I doing wrong ?

Thank you!
--
Best Regards,

-- Royce Pereira

--
Best Regards,

-- Royce Pereira