2024-02-24 05:19 PM
I have both STM32F1038T6 and STM32F401CC. I have been trying to get a code to run from ram for days now.
Im using arduino ide with stm32duino.
i first create a buffer in ram with malloc then copy the code to the buffer using memcpy. The code is in byte array inside a include file.
Like for example
RamAddr is a byte pointer. Also the above code is in a method function that is called in Arduinos' loop()
The code is copied properly. When functionPtr() is called the Program counter loads the Address of the function (Which is the start of the code) and I can see it when debugging it. However the pc increments normally but nothing happens and if left to run it ends up in WWDG_IRQHandler as pointer by Program counter. The mCode code just blinks PC13 on and off and was built from Stm32CubeIde. I tried the blink program by flashing it directly to mcu and it works.
So am i doing something wrong or missing something? If so how can it be done with examples. Because i want to be able to load code from spi flash or memory card
Solved! Go to Solution.
2024-02-25 02:43 PM
https://community.st.com/t5/stm32-mcus-products/bootloader-and-jumping-to-flash-location/td-p/462009
typedef int (*pFunction)(void); void RamCode(void) { static const uint16_t Code[] = { 0xF04F, 0x007B, 0x4770 }; // payload uint8_t Buffer[16]; // as big as required pFunction RunCode; memcpy(Buffer, Code, sizeof(Code)); // Copy code RunCode = (pFunction)&Buffer[1]; // +1 for Thumb code printf("%d\n",RunCode()); }
MOV R0, #123 BX LR
2024-02-24 05:51 PM
Have a Hard Fault Handler to catch failures.
The function pointer needs to be an ODD address. Suggest you print if out to confirm.
ie 0x20002001 for code you copy to 0x20002000
2024-02-24 06:03 PM
In the data sheet it says bit 0 is not set in PC and it must be half word aligned. And i tried odd address before it does the same thing. so im using Even addresses.
2024-02-24 06:51 PM
Try something simple than loads R0 with a value and returns.
You'd need to show the code, and a disassembly to be able to trouble shoot from here.
It needs to be THUMB code, you need to copy on an even address, and call at the odd address. That's how the Cortex-M3 expects things.
Create a pointer for a function you can uses successfully, and print out the value. Say for a qsort() implementation, which takes a pointer for a compare function.
2024-02-25 01:59 PM - edited 2024-02-25 02:03 PM
So if i manual set PC to address of code in ram the code runs but if not it just jumps to HardFault handler. I tried both odd and even address. Worst is the St-link step doesnt let me step through it maybe because its not part of the function but external code. Only lets me step through it if i manually enter the address into the Program counter.
this is the code i use to execute in ram. Also i removed the Push,pop and bx instructions and it does the same thing. However it worked once when push and pop werent included but bx lr was but it immediately caused a fault when it got to digitalwrite after functionptr(). Then it never worked again lol.
2024-02-25 02:43 PM
https://community.st.com/t5/stm32-mcus-products/bootloader-and-jumping-to-flash-location/td-p/462009
typedef int (*pFunction)(void); void RamCode(void) { static const uint16_t Code[] = { 0xF04F, 0x007B, 0x4770 }; // payload uint8_t Buffer[16]; // as big as required pFunction RunCode; memcpy(Buffer, Code, sizeof(Code)); // Copy code RunCode = (pFunction)&Buffer[1]; // +1 for Thumb code printf("%d\n",RunCode()); }
MOV R0, #123 BX LR
2024-02-25 05:37 PM
I got it to work. I was getting the error because i was modifying 4 registers, r1,r2,r3,r4 which were used by rtos so what i did was push these registers then run my code. After code runs, i pop the registers then use BX LR.
also another error i was getting was because of using wrong push and pop instructions. There is two versions of both push and pop. For push there is 0xB4 and 0xB5 and for pop there is 0xBC and 0xBD. I had to use 0xB4(push) with 0xBC(pop) and if i used 0xB5(push) then use 0xBD for pop. Any other combination causes a fault.
for the new code it looks like this
Thanks for the help. Really appreciate it.