2020-07-30 12:47 AM
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?
Solved! Go to Solution.
2020-07-30 04:35 AM
Make sure pointer you're memcpy()ing is EVEN
2020-07-30 12:58 AM
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
2020-07-30 01:07 AM
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.
2020-07-30 01:26 AM
a
2020-07-30 01:29 AM
should i remove my main function?
2020-07-30 01:30 AM
should i remove my mian function?
somwhere i read if i put a delay before executing it might work can that have any effect?
2020-07-30 01:30 AM
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. =)
2020-07-30 01:30 AM
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.
2020-07-30 02:49 AM
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
2020-07-30 04:35 AM
Make sure pointer you're memcpy()ing is EVEN