Skip to main content
RBharol
Associate III
April 1, 2021
Question

Need help running STM32F4 from internal RAM

  • April 1, 2021
  • 5 replies
  • 1479 views

Hi Experts,

I need to run my code (it is a small code) from internal RAM. It currently runs from the first sector in the flash. For some reason, I need to change a few bytes in the same sector and it seems I can't write to the same sector from where the code is running (I wish I could) so I plan it running from RAM so that I can change those bytes.

I would appreciate it if I could get any help (linker file, startup code examples) to make it work.

Thanks

This topic has been closed for replies.

5 replies

Bowman32
ST Employee
April 1, 2021

Hi @RBharol​ 

I suspect the write protection is active. You can use STM32Cube Programmer to check the flash memory sector and disable the write protection as shown in the attached figure.

Also, for more details about option bytes and write protection you can refer to STM32F4 reference manual, section "3.4 Flash option bytes" with focus on "3.4.4 Write protections".

Bouraoui

0693W000008yXO8QAM.png

Tesla DeLorean
Guru
April 1, 2021

The F4 has the ART cache running against the FLASH memory, so watch that.

Also I'd strongly advise not modifying the first flash sector as it makes the part very vulnerable to failure. Put a loader in the first sector (16KB), and put other data you want to change in the second, third or fourth sector.

So what's stopping you memcpy()ing the code block to RAM, and jumping via a function pointer? Or doing so with simpler assembler code? The latter being easy to contain and understand the call tree and literal pool dependencies on.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
RBharol
RBharolAuthor
Associate III
April 1, 2021

I am able to execute code from RAM. At the startup, Reset_Handler function copies entire flash code to RAM and executes from there but I am still not able to write to the first sector (from where the code was copied to RAM). I can write to other sectors. Why should that be the case?