I know it's not possible to use pointers to functions in XC files but it is in C.
How can I use these functions in xc files? Is there a workaround with a special library?
Thanks
Using pointers to functions
-
- Newbie
- Posts: 1
- Joined: Thu Apr 06, 2017 9:50 am
-
- XCore Addict
- Posts: 169
- Joined: Fri Oct 23, 2015 10:23 am
(from memory)
As function pointers are not valid syntax in XC, all you can do it pass around an opaque handle e.g. an unsafe pointer such as 'void * unsafe'.
The creation and dereferencing will need to happen in a C file, with the C file casting to and from the function pointer and 'void * unsafe'.
I can't think of a reason why you couldn't have a set of shim C functions that do the creation and dereference of 'void * unsafe' functions pointers for you.
Sorry, no special library to do the wrap & casting for you.
The issue with function pointers is that the compiler can no longer calculate stack usage.
This is solved (hidden feature in 14.2) by you giving the compiler a group of possible functions a function pointer could be:
Function declaration:
Function pointer definition:
Thus, the compiler now knows that 'fp' will be any of the functions in group 'G1' and it can work out the worse case resource usage.
The compiler flag 'Wxcore-fptrgroup' will check you haven't missed any.
As function pointers are not valid syntax in XC, all you can do it pass around an opaque handle e.g. an unsafe pointer such as 'void * unsafe'.
The creation and dereferencing will need to happen in a C file, with the C file casting to and from the function pointer and 'void * unsafe'.
I can't think of a reason why you couldn't have a set of shim C functions that do the creation and dereference of 'void * unsafe' functions pointers for you.
Sorry, no special library to do the wrap & casting for you.
The issue with function pointers is that the compiler can no longer calculate stack usage.
This is solved (hidden feature in 14.2) by you giving the compiler a group of possible functions a function pointer could be:
Function declaration:
Code: Select all
__attribute__((fptrgroup("G1")))
void func() {/*implementation*/} // this is NOT a declaration - we need to know the stack/resource usage!
Code: Select all
__attribute__((fptrgroup("G1")))
void (*fp)(); // we create a variable (we could initialise it to any of the functions in G1 e.g. '=func').
The compiler flag 'Wxcore-fptrgroup' will check you haven't missed any.