cancel
Showing results for 
Search instead for 
Did you mean: 

Need help running STM32F4 from internal RAM

RBharol
Associate III

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

5 REPLIES 5
Bouraoui Chemli
ST Employee

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

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 Up vote any posts that you find helpful, it shows what's working..

I am taking care of unlocking the flash and locking it after writing. I can write to other sectors (from where I am NOT running). So writing to flash works just not in the sector I am running from. So I want to run from RAM and do the writes. I need help with some linker files and init code that does it.

Yes RAM is one selection, second is maybe create second copy function on other sector and run this from flash when write to 0.

You can place it over section pragmas simmilar way as to RAM or CCMRAM.

You dont specify used IDE, then link scripts and syntax is different for KEIL or CubeIDE...

RBharol
Associate III

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?