Skip to main content
Kkh.1
Associate
July 30, 2020
Solved

running code from internal sram

  • July 30, 2020
  • 12 replies
  • 3268 views

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?

This topic has been closed for replies.
Best answer by Tesla DeLorean

Make sure pointer you're memcpy()ing is EVEN​

12 replies

waclawek.jan
Super User
July 30, 2020

Single-step in disasm. Which instruction and under what context (registers' content) causes the fault?

In 'F4, you can't run code from CCM RAM.

JW

Kkh.1
Kkh.1Author
Associate
July 30, 2020

dear waclaweck

ive check the error occures when i try to run the copied code ( line func(); ).

and im not tryin to run from ccm the address is the sram1 of the micro.

prain
Visitor II
July 30, 2020

I have two questions

1- ​why do you copy your function to address 0x100 from SRAM base. As I know, compiler should manage RAM for you. your function may have conflicts with data that compiler put there. use compiler specific attributes to tell compiler that your function should be placed in RAM.

2- If you want to access memory directly, you should consider alignment.

Kkh.1
Kkh.1Author
Associate
July 30, 2020

should i remove my mian function?

somwhere i read if i put a delay before executing it might work can that have any effect?

Kkh.1
Kkh.1Author
Associate
July 30, 2020

a

Kkh.1
Kkh.1Author
Associate
July 30, 2020

should i remove my main function?

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
July 30, 2020

Make sure pointer you're memcpy()ing is EVEN​

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
TDK
July 30, 2020

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
Kkh.1Author
Associate
July 30, 2020

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
Kkh.1Author
Associate
July 30, 2020

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();