cancel
Showing results for 
Search instead for 
Did you mean: 

running code from internal sram

Kkh.1
Associate II

hey there.

for a while ive been tryin to copy some codes from flash to internal sram and run it.

ive read the datasheet, programming manual and cortex manual and they all confirm that it is possible to run code from sram, however when i copy the codes i get aforced hardfault which with durther inspection i realized is a bus fault indeed. i tried it on both stm32f103 and stm32f407 and its not working on either. here is the assembly function im copying into the sram:

myadd PROC

        EXPORT myadd      [WEAK]

        NOP

NOP

BX lr

        ENDP

and here is the function that copies and runs the copied code:

void test()

{

memcpy((void *)(SRAM_BASE + 0x100), &myadd, 200);

Delay(500);

//void (*func)() = (void (*)())((int32_t)(SRAM_BASE + 0x100) | 0x1);

void (*func)() = (void (*)())((int32_t)(SRAM_BASE + 0x100) | 0x1);

func();

}

during the error i only get bus imprecise error and other flags are zero.

anybody can see any problem with my code or its just that stm32 mcus cannot run code from sram or should i configure some cortex peripherals?

12 REPLIES 12
TDK
Guru

I don't think copying things to an arbitrary point at the start of SRAM is going to end particularly well. How do you know that space is free?

Modify your linker script to run that function from RAM. The linker will take care of where to place it and your startup script will load it.

If you feel a post has answered your question, please click "Accept as Solution".
Kkh.1
Associate II

well once and for all i found the answer.

the st does not suitably doucument its products and its documentations are so incomplite that people should just find out their problems through trial and error.

i wish st cared about its customers and put some efforts in fixing its incomplite and useless documentation.

the chip stm32f013 does not include any kind of mpu(memory protection unit) but for some reasone which i could not find in any of the board, mcu, or even cortex duments the code in sram cannot be exectued unless its located somwhere after the address 0x2000b002.

i placed the code there and it worked fine without using linker scripts and by simply copying the code into the sram.

but the problem is that the sram on this chip is actually 20kb which means its end address will no reach the mentioned address and i have no idea how that thing is working and what is that memory in that adress and why the st has never mentioend it in any of its documents.

Kkh.1
Associate II

the problem was the source flash code and the thumb bit (bit 0) which i forgot to and to zero. so i did and its working properly that you all.

the code:

//-------------------------in the startup.s file-----------------------------

ALIGN

myadd PROC

        EXPORT myadd

        NOP

NOP

BX lr

        ENDP

//------------------------------------------------------------------

memcpy((void *)(SRAM_BASE + 0x000), (void*)((int)&myadd & (0xFFFFFFFE)), 20);

void (*func)() = (void (*)())((int32_t)(SRAM_BASE + 0x000) | 0x1);

func();