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?

1 ACCEPTED SOLUTION

Accepted Solutions

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

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

View solution in original post

12 REPLIES 12

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

prain
Senior III

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
Associate II

a

Kkh.1
Associate II

should i remove my main function?

should i remove my mian function?

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

dear prain

first:

i copied it there because im sure at that place the sram is not used cuz my main function is almost empty it just enables a gpio for debug :

int main()

{

//HAL Enable GPIOA_6 as Output

 do { \

                    __IO uint32_t tmpreg = 0x00U; \

                    SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOAEN);\

                    /* Delay after an RCC peripheral clock enabling */ \

                    tmpreg = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOAEN);\

                    UNUSED(tmpreg); \

                     } while(0U);

while(1);

}

second:

i need to put a variable size code in this address and the code may change during operation so i need to handle the copying when its up and running.

btw if u could guide me through how i can put some finctions in ram during copilation i woub be thankful. =)

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.

A complete guide is here:

​http://blog.atollic.com/using-gnu-gcc-on-arm-cortex-devices-placing-code-and-data-on-special-memory-addresses-using-the-gnu-ld-linker

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

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