Electronics > Microcontrollers

Programming flash memory STM32 bare metal

(1/2) > >>

Etesla:
Hi,

I'm using an stm32f072rbt6. I'm trying to write to flash memory without using the HAL libraries for the sake of the learning.

The datasheet of interest is this one called reference manual here:
https://www.st.com/en/microcontrollers-microprocessors/stm32f072rb.html#documentation
The pages of interest are probably pages 56, 59 and 927

My first goal is just to write a value to a single flash memory address. I'm having an issue where I get stuck in some hard fault looking situation whenever I try to do so.
[attachimg=1]

The code that gets me here looks like this.
[attachimg=2]

If I use the debugger than my code gets into that fault thing after trying to execute line 96. That is my problem and I can't seem to fix it.

There is mention in the datasheet that the MCU will enter the hard fault interrupt if you try to write more than a half word to a memory address, but I'm pretty sure that I am explicitly writing a half word in the code I have now... The flash SR register also doesn't give me any hints.
[attachimg=3]

Let me know what you guys think. Thanks in advance!

ataradov:
You are writing a word. The line must read "*(volatile uint16_t *)0x0801f000 = toWrite".

timenutgoblin:

--- Quote from: Etesla on October 23, 2021, 05:00:08 am ---The datasheet of interest is this one:
file:///C:/Users/Erik%20Bothe/Downloads/rm0091-stm32f0x1stm32f0x2stm32f0x8-advanced-armbased-32bit-mcus-stmicroelectronics%20(2).pdf
The pages of interest are probably pages 56, 59 and 927

--- End quote ---

The URL that you've linked to appears to be an address located on your device (local c: drive).

Here is the link that I think you're referring to:

https://www.st.com/resource/en/reference_manual/dm00031936-stm32f0x1stm32f0x2stm32f0x8-advanced-armbased-32bit-mcus-stmicroelectronics.pdf

Etesla:
ataradov that worked great. Thanks!

SiliconWizard:

--- Quote from: Etesla on October 23, 2021, 05:00:08 am ---There is mention in the datasheet that the MCU will enter the hard fault interrupt if you try to write more than a half word to a memory address, but I'm pretty sure that I am explicitly writing a half word in the code I have now...

--- End quote ---

As ataradov showed you, you were not. Well, at least you probably got confused by what "writing a half word" means. The right-hand value of the assignment was indeed 16-bit (half-word), but that doesn't matter here. What matters is that your write access was 32-bit due to dereferencing a pointer to a 32-bit integer. Maybe this is obvious to you and you just made a silly mistake (that happens to everybody!), but otherwise, I suggest learning a bit more about this if you're doing embedded programming.

As to the "volatile" qualifier, this is good practice to add it here, because such an access could get optimized out by the compiler. This is a common issue, and talked about in numerous other threads, so you can look it up. Note that from tests I made with recent versions of GCC, dereferencing a pointer converted from an absolute address (integer) such as here actually does NOT get optimized out even without the volatile qualifier, but there's no guarantee about this from a standard POV, so you should use it.

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version