• AVR Freaks

Store variable in flash memory DSPIC33

New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2017/12/03 08:45:19
  • Location: 0
  • Status: offline
2020/06/01 09:41:24 (permalink)

Store variable in flash memory DSPIC33

Hi everyone,
I'm not completely sure about a few things about storing variables in flash. Probably you can help me easily. 
I have a DSPIC33EV32GM104 in a board running a program that must store some configuration and that configuration can change a few times (let's say 100).
I'm using the attached code to do so and it works nicely. However, I have a few doubts about if the code will always work OK or if a bug could arise. 
1 - I don't specify an address in flash memory space, so it's kind of arbitrary. As long as the var is always allocated in the same address the program will work. I suppose that unless I reprogram the device, the address won't change so it will work OK forever (until the flash reach it last erasing cycle).
2 - To program a multipage system so I erase a different page every time, I have to declare more variables like "dat".
3 - To control in which page is the current information, I can use a counter variable and read only the page with the higher number in it. 
I'm especially concern about point 1. I don't want to store the information somewhere and then try to read it from a different location (that would be disastrous).
Thanks a lot for your help.

//Var for allocating flash memory space
unsigned int __attribute__((space(prog),aligned(_FLASH_PAGE*2))) dat[_FLASH_PAGE];

int ReadFlashConfiguration(int *UserData)
_prog_addressT p;
_init_prog_address(p, dat); /* get address in program space */


//Return Magic Word in first byte
return UserData[0];
int WriteFlashConfiguration(int *UserData)
_prog_addressT p;
int i;
_init_prog_address(p, dat); /* get address in program space */
_erase_flash(p); /* erase a page */
for (i=0;i<sizeof(t_UserConfiguration);i=i+2)
_write_flash_word32( p+2*i, UserData[i], UserData[i+1]);

return 1;

Jump to:
© 2020 APG vNext Commercial Version 4.5