#include "hw_memmap.h"#include "hw_types.h"#include "hw_flash.h"#include "flash.h"#include //Declare a 1 kB block in the flash aligned on 1024 bytes boundary.const unsigned char flashBlock[1024]__attribute__ ((aligned(1024))) = { 0x00, 0x00, 0x00, 0x00 };//Define a structure where you can put all the//variables you want to save in the flash.//The maximum structure size is 1024 byte.typedef struct tagPERMANENT_VAR{ int restartCount; int var1; int var2;} PERMANENT_VAR;//Create one instance of permanents variables.PERMANENT_VAR permanentVars;//This function writes the variables to the flash.void writePermanentsToFlash(){ FlashErase((unsigned long) flashBlock); //The length must be a multiple of 4 FlashProgram((unsigned long*) &permanentVars, (unsigned long) flashBlock, (sizeof(permanentVars) + 3) &~nbsp;3);}//This function reads the variables from the flash.void readPermanentsFromFlash(){ memcpy(&permanentVars, flashBlock, sizeof(permanentVars));}//Main function example.int main(void){ //Read the variables from the flash first. readPermanentsFromFlash(); //Store how many time the system restarted. permanentVars.restartCount++; //Write back the variables to the flash. writePermanentsToFlash(); //Continue your program. //... //... while(1);}
Hi,
I was trying do your code under IAR compiler.
But this declaration does not work in IAR compile:
//Declare a 1 kB block in the flash aligned on 1024 bytes boundary.const unsigned char flashBlock[1024]__attribute__ ((aligned(1024))) = { 0x00, 0x00, 0x00, 0x00 };Do you know how could do this to work under IAR compiler?
//Declare a 1 kB block in the flash aligned on 1024 bytes boundary.const unsigned char flashBlock[1024]__attribute__ ((aligned(1024))) = { 0x00, 0x00, 0x00, 0x00 };
ravazThis is an example on how to use the flash for permanents variables
It relies upon compiler-specific features - therefore you need to state what specific compiler (including version) it is for!
I would like save to flash memory only one variable type unsigned char. Is it my code right?
My code has not been saved here.