Reputation: 59
I am trying to write a value to flash just to test any basic flash writing functionality. I was able to successfully write to memory, and it persists through resets, however I am encountering a very strange issue.
With the code below, I write a value of 0x42 to the FLASH address 0x080E0000, which is sector 11.
HAL_Init();
HAL_FLASH_Unlock();
FLASH_Erase_Sector(FLASH_SECTOR_11, VOLTAGE_RANGE_3);
HAL_FLASH_Program(TYPEPROGRAM_WORD, 0x080E0000, 0x42);
HAL_FLASH_Lock();
However, now when I change the 0x42 parameter to a 0x40, nothing is happening and the previous 0x42 is still written to that memory address.
Some very strange debugging information: I set a breakpoint at the following lines: Breakpoint 1:
HAL_Init();
Breakpoint 2:
HAL_FLASH_Program(TYPEPROGRAM_WORD, 0x080E0000, 0x42);
Breakpoint 3 is after HAL_Flash_Lock on another line in the function.
At breakpoint 1 it reads 0xff, at breakpoint 2 it reads ff, then at breakpoint 3 it reads 0x42. EVEN WHEN changing the data parameter in FLASH_Program to 0x40. This is weird because if it is reading ff at breakpoint 2 then I think that it is getting erased, so how is it possible that it 0x42 is getting read to it?
I have also allocated memory in my .lds file like so:
MEMORY
{
FLASH (RX) : ORIGIN = 0x08000000, LENGTH = 1536K-128K
DATA (RWX) : ORIGIN = 0x080E0000, LENGTH = 128K
SRAM (RWX) : ORIGIN = 0x20000000, LENGTH = 320K
}
_estack = 0x20050000;
SECTIONS
{
.user_data :
{
. = ALIGN(4);
KEEP(*(.user_data))
. = ALIGN(4);
} > DATA
The only time that I am able to write a new value to flash is when the I rebuild the project.
Thanks for the help.
Upvotes: 1
Views: 89
Reputation: 4297
You seem to be very confused about the relationship between your source code, object code, and the image running on your MCU.
Let's say you write some code, that writes 0x42
into the flash on your board. Let's call this Version-1 of your code. At this stage, it just exists in the editor in your IDE. There is no object code, and it is not yet running on the MCU.
Then you build this project. Now you have a Version-1 binary, probably a .elf
or .bin
file. It corresponds to your source code, but it is not yet running on the MCU.
Next, you run it on the MCU, perhaps using the debugger, so you can step through the code. Now you have Version-1 code in your editor, as a binary, and also running on the MCU. Debugging will be normal, and you will be able to step through and see that the value 0x42
has been written.
Let's imagine that you now modify the code in the editor to actually write 0x40
to the flash. Let's call this new version of the code Version-2. At this stage, you have Version-2 code in your editor, but you still have a Version-1 binary, and Version-1 code on the MCU. So the code will still be writing 0x42
into flash.
To get Version-2 code running on the MCU, you need to build your Version-2 source code, to get a Version-2 binary, then program that onto the MCU. Then it will behave as expected. Editing the source code, even during a debugging session, will not magically send that code onto the MCU.
Above and beyond all that - do not edit your code while in the middle of a debugging session and expect anything at all to work as expected. If you want to change the code, stop debugging, build the code, program it onto the board, and start debugging again.
Upvotes: 1
Reputation: 59
Not sure if this an obvious answer but changing code in the editor does nothing to the code being run. You will need to rebuild and relaunch/reprogram with the new code for the updates to be sent to the device.
Also, breakpoints seem like they've moved if the editor has a newer version of the file to what is being executed.
Upvotes: 1