cancel
Showing results for 
Search instead for 
Did you mean: 

Executing code with stm32f469 from SDRAM

ari_v2
Senior

Hi,

My board consists of stm32f469 that interfaces SDRAM via FMC. I want to execute code from SDRAM. Code is loaded from internal flash to SDRAM at address 0xc0000000.

But running the code from this region causes hard-fault. Is there a restriction with stm32469 to run code from SDRAM?

Thanks,

Ari

1 ACCEPTED SOLUTION

Accepted Solutions
ari_v2
Senior

Thanks for replies. Inspection of SCB shows that cause for hardfault is memory access.

Although I didn't enable the MPU, cortex M4 protects some regions from executing code. So, even though I could read and write to external memory, execution is forbidden unless configuring the MPU to support this.

Once added to MPU this region with execution enabled, problem solved.

Thanks.

View solution in original post

6 REPLIES 6

> running the code from this region causes hard-fault

Debug the hardfault as usually - there are numerous posts on this forum how to do that, mainly by Clive (Tesla DeLorean).

This is not too dissimilar from compiling and running code shifted in FLASH usually due to bootloaders, so steps related to that apply:

Is the code compiled/linked with the intention to be run from given specific address? Do you also move the vector table and adjust VTOR accordingly?

JW

Imen.D
ST Employee

Hello @ari_v2​ ,

Did you tried to run the example available with STM32CubeF4 MCU package:

Repository\STM32Cube_FW_F4_V1.26.1\Projects\STM32469I_EVAL\Examples\FMC\FMC_SDRAM

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
ari_v2
Senior

Hi Imen,

This example shows how to read\write data from SDRAM. This works fine on my board.

The problem starts when I try to execute code from SDRAM. Is there an example for executing code from external SDRAM?

Hi,

Thanks for response.

Here is the small piece of code that is put SDRAM.

#pragma arm section code = ".XRAM_CODE"

#pragma no_inline

void LedsMaintain(void)

 {

HAL_GPIO_WritePin(GPIOJ, LED_RED_Pin,GPIO_PIN_SET);

for (int i =0; i < 1000000; i++);

HAL_GPIO_WritePin(GPIOJ, LED_RED_Pin,GPIO_PIN_RESET);

for (int i =0; i < 1000000; i++);

}

In scatter file:

...

 RW_XRAM2 0xc0000000 0x00010000 {

  .ANY (.XRAM_CODE)

 }

From main() I load code to SDRAM as follows:

void Load_Xram_Code(void)

{

Xram_Code_Addr = (uint8_t *)(&Image$$RW_XRAM2$$Base);

Xram_Code_Length = (uint32_t)&Image$$RW_XRAM2$$RO$$Length;

Xram_Code_Addr_Load = (uint8_t *)(&Load$$RW_XRAM2$$Base);

Xram_Region_size = Image$$RW_XRAM2$$RO$$Limit - Image$$RW_XRAM2$$Base;

memset(Xram_Code_Addr, 0, 0x1000);

memcpy(Xram_Code_Addr, Xram_Code_Addr_Load, Xram_Code_Length);

}

Stepping with debugger I see that calling LedsMaintain() jump to the assembly code below. But executing first line, address 0xC0000000 causes hardfault:

0xC0000000 B510   PUSH     {r4,lr} ---> hardfault

  34:     HAL_GPIO_WritePin(GPIOJ, LED_RED_Pin,GPIO_PIN_SET); 

0xC0000002 2201   MOVS     r2,#0x01

0xC0000004 4611   MOV      r1,r2

0xC0000006 480A   LDR      r0,[pc,#40] ; @0xC0000030

0xC0000008 F000F816 BL.W     0xC0000038 $Ven$TT$L$$HAL_GPIO_WritePin

....

What could be the cause for hardfault?

Thanks

TDK
Guru

> causes hardfault

Look at the SCB registers to determine the reason for the hardfault and proceed accordingly.

If you feel a post has answered your question, please click "Accept as Solution".
ari_v2
Senior

Thanks for replies. Inspection of SCB shows that cause for hardfault is memory access.

Although I didn't enable the MPU, cortex M4 protects some regions from executing code. So, even though I could read and write to external memory, execution is forbidden unless configuring the MPU to support this.

Once added to MPU this region with execution enabled, problem solved.

Thanks.