cancel
Showing results for 
Search instead for 
Did you mean: 

Hardfault during SRAM Code execution

n.serina
Associate III
Posted on October 14, 2017 at 16:36

Hello, 

I am using stm32f407 discovery board and when i tried to execute the code stored in the SRAM i am getting hardfault exception . the instructions are indeed valid. 

i have attached the screenshot which shows the PC, SP, and disassembly code. please help me solve this issue. 

In this window the processor is ready to execute the PUSH instruction  , and when i hit ''step in'' button i goes to hardfault handler. 

0690X00000608c7QAA.png

#stm32f4 #stm32f429zit6-discovery
6 REPLIES 6
AvaTar
Lead
Posted on October 14, 2017 at 18:05

Supposedly normal SRAM.

How do you jump to your RAM code ?

Are you aware of the ARM/Thumb code difference, and how the core distinguishes both ?

Posted on October 14, 2017 at 19:14

The branches to subroutines look like they will be problematic..

Also jump_addr of 0x20000004 will be a problem, should be 0x20000005, as the core can't execute 32-bit code, and WILL fault.

It will help significantly working at this level of processor functionality to read and understand the core's documentation.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 15, 2017 at 04:57

Thanks for your replay . if i understood correctly cortex M4 uses Thumb2 and its a mix of ARM 32 bit and Thumb 16 bit instruction set. so when it tries to execute the first instruction ,, do you mean it could not able to recognize the instruction ? why would that happen ? because its a thumb 16 bit instruction right ? please correct me if i am wrong 

Posted on October 15, 2017 at 05:03

Hello Clive, 

Thanks for your reply . my requirement is this . 

I have to execute the a program from SRAM. 

the code should be stored in the serial flash connected over SPI. 

when the MCU runs , the code which is there in the internal flash copies the program code in the serial flash to the SRAM . and it jumps to the sram to execute that code. 

i have interfaced the serial flash and i tested the reading and writing some data. 

Now i have to compile my program to generate code which is valid to execute from the SRAM. how can i do it ?

once i get the stream of code , then i will think about how to store that in the serial flash.

Thanks 

Posted on October 15, 2017 at 11:27

As clive wrote :

Also jump_addr of 0x20000004 will be a problem, should be 0x20000005, as the core can't execute 32-bit code, and WILL fault.

This means, the LSB of the jump address must always been set on a Cortex M, this bit serves to distinguish between ARM and thumb code.

The Cortex M does not support ARM code, and will hardfault on it.

Usually, the compiler does this for you.

Posted on October 15, 2017 at 15:28

The code needs to be built for 0x20000000 if that's the final destination, you'll also need to move the RAM used by the statics deeper into memory, ie 0x20002000 if you other code takes 8KB

You can do this via a Scatter file, or by changing the target IROM/IRAM settings for the code you want to load into RAM.

The Cortex-Mx series can only execute Thumb code, that cannot execute 32-bit ARM code. The jump addresses used for function pointers will be ODD

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..