How do I call a function from a function pointer address in CW5.1(S9S12GA192)?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How do I call a function from a function pointer address in CW5.1(S9S12GA192)?

914 Views
joophliu
Contributor III

Environment:

IDE:Codewarior 5.1,MCU:MC9S12GA192

How do I call a function from a function pointer?

1. If the function name of the called function is directly assigned to the pointer, it is ok to call the function with the pointer, and jump to the function:"HAL_E_IDLE_Task()".

       void (*function_ptr)(void);   

       function_ptr = HAL_E_IDLE_Task;   //or "function_ptr = &HAL_E_IDLE_Task;"   

       (*function_ptr)(); //or "function_ptr();"

This will call the function successfully!

2. But if the function is compiled after the link stored in flash address assigned to the pointer, and then call the function, the program will run away, can not enter the function "HAL_E_IDLE_Task()".

       void (*function_ptr)(void);

       function_ptr = (void (*)())0x0000CF1F; //Function "HAL_E_IDLE_Task()"compiled link addresses:"0x0000CF1F"   

       function_ptr(); //or"(*function_ptr)();"

 This way can not call the function successfully!

 I need to call the function in the second way(Use Function Point address), how can I jump to the function "HAL_E_IDLE_Task()" correctly?

PS:S32DS for SPC5744P(Power Architecture) use second way can  call the function successfully!It is  Complier reason?

Tags (2)
0 Kudos
1 Reply

735 Views
kef2
Senior Contributor IV

Hi,

Many unknowns here. Memory model, etc.. Could you do project demonstrating the issue?

void (*function_ptr)(void) is not universal function pointer on S12. There are functions in nonpaged memory, which return with RST and have to be called using JSR/BSR. There are also functions in paged memory, which return with RTC and have to be called using CALL instruction. It is impossible on S12 to have same function pointer working with both function types. When compiler sees HAL_E_IDLE_Task() declaration with all attributes like near, #pragma CODE_SEG __NEAR_SEG etc, you assing pointer and immediately call it (for testing purposes), compiler outsmarts your bug and uses JSR instruction.

If all you need is to call nonpaged/paged functions via pointer, you need to use near/far keyword in your pointer declaration:

void (* near function_ptr)(void);  

Edward

0 Kudos