简体   繁体   中英

STM32F7- Strange behaviour when writing to FLASH memory

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".

The write does not change anything in the flash memory. The debugger is caching memory accesses and thinks that it is RAM. It is giving you false information.

To write flash it is not enough to unlock it. You need also to erase the whole sector. So assignment to this kind of variable does exactly nothing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM