I have created a variable placed in flash-memory with the __attribute__(at())
command (ARM compiler v5, Keil uVision 5):
uint8_t myVar __attribute__((at(0x081C0008)));
Checking the placement with &myVar
gives the correct value. To write to areas in the flash it is required to unlock the flash. But when I tried to write directly to the variable with myVar = someValue
, the variable actually changed its value. In Keil uVision 5 Memory-Window I checked address 0x081C0008 of the variable and the value had changed. But when I reset the MCU the value at 0x081C0008 gets reset to its old value.
So my question is what happens when i write to myVar
. Is it stored somewhere else in the RAM or how can it change during runtime and after reset hold the original value which was stored in the flash.
EDIT:
Here is the code used to change the value of the variable placed in flash and the corresponding assembly code.
C:
myVar = !myVar;
someVar = myVar;
Assembler:
0x0800F196 4888 LDR r0,[pc,#544] ; @0x0800F3B8 (Memory location of the value 0x081C0008)
0x0800F198 7800 LDRB r0,[r0,#0x00]
0x0800F19A B908 CBNZ r0,0x0800F1A0
0x0800F19C 2001 MOVS r0,#0x01
0x0800F19E E000 B 0x0800F1A2
0x0800F1A0 2000 MOVS r0,#0x00
0x0800F1A2 4985 LDR r1,[pc,#532] ; @0x0800F3B8
0x0800F1A4 7008 STRB r0,[r1,#0x00]
0x0800F1A6 4608 MOV r0,r1
0x0800F1A8 7800 LDRB r0,[r0,#0x00]
0x0800F1AA 4984 LDR r1,[pc,#528] ; @0x0800F3BC
0x0800F1AC 7008 STRB r0,[r1,#0x00]
If I watch the registers during debug the variable "myVar" gets actually loaded from the flash address, then it is changed and stored back at the flash address. When "myVar" is assigned to someVar, it reads the actual flash address and writes it to someVar. At the end someVar holds the changed value of "myVar".