Variable in flash memory - how to write?
-
Hi, I read that i can set a variable in flash memory like this (PIC32):
uint8 Gain_factor_Den_17[128] __attribute__((space(prog), section(".table_flash_control")));
But what happens when i change that variable, flash has some requirements (e.g. need to erase page before writing etc). Who will handle that?
-
Hi, I read that i can set a variable in flash memory like this (PIC32):
uint8 Gain_factor_Den_17[128] __attribute__((space(prog), section(".table_flash_control")));
But what happens when i change that variable, flash has some requirements (e.g. need to erase page before writing etc). Who will handle that?
The variable is read only for your program. It is up to you to handle writing to it. See the Microchip article Data EEPROM Emulation for PIC18, PIC24, dsPIC, PIC32[^]. There is also a library that emulates an EEPROM using the program flash memory. But you should take care of the limited write endurance of your PIC device. If you need to change the variable frequently, you should use an external EEPROM.
-
The variable is read only for your program. It is up to you to handle writing to it. See the Microchip article Data EEPROM Emulation for PIC18, PIC24, dsPIC, PIC32[^]. There is also a library that emulates an EEPROM using the program flash memory. But you should take care of the limited write endurance of your PIC device. If you need to change the variable frequently, you should use an external EEPROM.
Thanks for the response, but i am still a little confused. Just theoretically, what happens if i were to write:
uint8 Table[128] __attribute__((space(prog), section(".table_flash_control"))); // Create variable in flash memory
...
Table[0] = 13; // Is it written to flash now?Unfortunately i cannot try it, just trying to understand the logic. I am aware that the flash memory has only some certain write lifespan.
-
Thanks for the response, but i am still a little confused. Just theoretically, what happens if i were to write:
uint8 Table[128] __attribute__((space(prog), section(".table_flash_control"))); // Create variable in flash memory
...
Table[0] = 13; // Is it written to flash now?Unfortunately i cannot try it, just trying to understand the logic. I am aware that the flash memory has only some certain write lifespan.
The flash memory of the PIC controllers is the program memory where code is stored. You can initialize the variable at compile time and it will be written together with the program code. To write to the program memory by the running program you need to execute special instructions. For the PIC 32 see section 5. Flash Programming[^ PDF] of the PIC32 Family Reference Manual. A compiler or assembler may provide functions or macros to perform the writing or compilers may have built-in support for writing. So the answer if the value is written depends on the used compiler. But if the compiler does not support writing to flash memory, you will get an error at compile time.
-
The flash memory of the PIC controllers is the program memory where code is stored. You can initialize the variable at compile time and it will be written together with the program code. To write to the program memory by the running program you need to execute special instructions. For the PIC 32 see section 5. Flash Programming[^ PDF] of the PIC32 Family Reference Manual. A compiler or assembler may provide functions or macros to perform the writing or compilers may have built-in support for writing. So the answer if the value is written depends on the used compiler. But if the compiler does not support writing to flash memory, you will get an error at compile time.