Convert absolute address to function pointer in microcontroller

1. Related concepts

 Function pointers and pointer functions are two different concepts.

  A function pointer refers to a pointer that can point to a function , and its type corresponds to the return value type and parameter type of the function. Through function pointers, functions such as callbacks and dynamic calls can be realized.

  A pointer function refers to a function that returns a pointer type. Its return value is a pointer that can be used to point to an address or data . Pointer functions are often used in scenarios such as dynamically allocating memory and returning dynamically generated data structures.

  It should be noted that function pointers and pointer functions are related to pointers, but they are essentially different concepts.

Summarize:

Function pointer: essentially a pointer, pointing to a function. The form is as follows:

void (*pFun)(void);
int (*pFun)(int i,int k);

Pointer function: It is essentially a function and the return value is a pointer. The form is as follows:

int* pFun(int i,int k);

2. How to convert absolute address into function pointer

The previous article " How to Scatterly Load Files on a Microcontroller " is an example. The method is as follows:

int (*pFun)(int,int);//定义指针函数
pFun = (int(*)(int,int))(0x08080001);//my_add_fun 的地址
pFun(num1,num2);

The test code is as follows:

int my_add_test(int num1,int num2)
{
    int sum;
    my_add_init();
    rt_kprintf("my_add_init=0x%08x\r\n",my_add_init);
    void (*pFun1)(void);
    pFun1 = (void(*)(void))(0x0808001d);//my_add_init 的地址
    pFun1();
    sum=my_add_fun(num1,num2);
    rt_kprintf("my_add_fun=0x%08x\r\n",my_add_fun);
    int (*pFun)(int,int);
    pFun = (int(*)(int,int))(0x08080001);//my_add_fun 的地址
    pFun(num1,num2);
    return sum;
}

or

typedef void (*pVoidFun)(void);
typedef int (*pIntFun)(int num1,int num2);


int my_add_test(int num1,int num2)
{
    int sum;
    my_add_init();
    rt_kprintf("my_add_init=0x%08x\r\n",my_add_init);
    void (*pFun1)(void);
    //pFun1 = (void(*)(void))(0x0808001d);//my_add_init de di zhi
    pFun1=(pVoidFun)(0x0808001d);
    pFun1();
    
    sum=my_add_fun(num1,num2);
    rt_kprintf("my_add_fun=0x%08x\r\n",my_add_fun);
    int (*pFun)(int,int);
    //pFun = (int(*)(int,int))(0x08080001);//my_add_fun de di zhi
    pFun=(pIntFun)(0x08080001);
    pFun(num1,num2);
    return sum;
}

practical testing

5766318016f1f0d0a577d5edc18648b1.png

How to know the address of the function?

① Get the absolute address of the function by printing;

② Obtain function address through map file

2f03b61a15ec749bd49760673a2bf042.png

1edbfadc12be606bd588904017c57bbc.png

6582dd7df9262827f2fa06e3348789fd.png

I wonder if everyone has noticed it? The addresses should be 0x08080000 and 0x0808001c, but why did they become 0x08080001 and 0x0808001d.

For specific information, please refer to:

https://bbs.21ic.com/icview-134389-999-1.html
读PC指针时,返回LSB总是为0;写PC指针时,一定要保证LSB为奇数。

Welcome to follow the official account: Embedded Learning and Practice

Guess you like

Origin blog.csdn.net/weixin_46158019/article/details/132353447