I used the following code to specify the address of variable bar at 0x4000 and the address of function fun at 0x8002000.
int bar __attribute__((section(".ARM.__at_0x4000"))); void fun(void) __attribute__((section(".ARM.__at_0x8002000"))); void fun(void) { // some code here }
However, if I print out the address of the above two:
printf("0x%08X 0x%08X\r\n", (uint32_t)&bar, (uint32_t)&fun);
It outputs: 0x4000 0x8002001
If function pointer is used to jump to 0x8002000 it results in HardFault. If jump to 0x8002001 it will execute the function fun correctly.
Could anyone explain why the function not located at 0x8002000?
(But if I look at the output .hex file, there is some data in the address 0x8002000!)
Thanks in advance!
Processor will hard fault when you do something that is illegal. Remember ARM instructions have EVEN addresses, and ODD address implies THUMB instructions. The CM3 can't execute ARM instructions.
The low order bit determines the code type, the processor will load the 16-bit word at 0x8002000 when given 0x8002001.
Best you read and understand the TRM and other core documentation.
Many thanks for your information. I will study more about the thumb instructions.