Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointer to function in ROM

Tags:

c

embedded

I have microcontroler that I am working with. When debugging it is necessary to call a function from that is hard coded in ROM. Technical Reference shows how to do this:

# define Device_cal (void(*)(void))0x3D7C80

and calling procedure looks like this:

(*Device_cal)()

I can't understand what actually happens here, so my question is: How does it work?

like image 967
mhl Avatar asked Jan 19 '12 00:01

mhl


3 Answers

void (*) (void) is a type. It's a pointer to a function that takes no parameter and returns void.

(void(*)(void)) 0x3D7C80 casts the 0x3D7C80 integer to this function pointer.

(*Device_cal)() calls the function.

(Device_cal)() would do the exactly the same.

The parentheses around *Device_cal and Device_cal are required because otherwise the cast to the integer would not have the higher precedence.

like image 134
ouah Avatar answered Oct 24 '22 07:10

ouah


The #define causes (*Device_cal)() to be expanded into this immediately before compiling:

(*(void(*)(void))0x3D7C80)()

The void(*)(void) is a declaration for a function pointer that takes void and returns void types. The (*()) represents a cast for the next token in the expression (0x3D7C80). Thus this asks to treat the data at location 0x3D7C80 as a function. The final () calls the function with no arguments.

like image 29
sarnold Avatar answered Oct 24 '22 08:10

sarnold


well, you "define" a pointer to function, and call it. void(*)(void) mean a pointer to function, that gets no arguments, and return void. If you cast 0x3D7C80 to that type, and call it, you basically call the function that its address is 0x3D7C80.

like image 25
asaelr Avatar answered Oct 24 '22 09:10

asaelr