We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Bahri Okuroglu Turning off overlaying to use function pointers has a heavy ram size price. Since function pointers are tricky to use, I suggest the following technique. This technique has 2 advantages. The call tree is built automatically by the linker. Also you need only save and pass a char enum instead of a larger function pointer. This usually offsets the cost of the extra array evaluation.
extern char Fn1( char, char ); extern char Fn2( char, char ); extern char Fn3( char, char ); typedef char FnT( char, char ); enum {eFn1, eFn2, eFn3 }; FnT* code pFns[] = { &Fn1, &Fn2, &Fn3 }; void main( void ) { char a = (*pFns[ eFn2 ])( 1, 100 ); }
Jon, After Andrew's and your messages, I've decided to turn the overlaying on and making proper adjustments. First option is to adjust the call tree manually using linker options as in the application note Andrew provided. But if you use this option, you need to make sure that you had changed the linker options once you add a new fptr. Your approach is also nice, but it cannot be used with my design. In fact this is my first embedded project and I developed a simple mechanisms to send events from interrupt routines to main(). With this approach event handler for an event can be changed anytime. For example for the KEYBOARD_EVENT, I have multiple handlers such as main_console_keyboard_handler(), main_menu_keyboard_handler(), etc. main_console_keyboard_handler() is registered by default, but once the ENTER key is pressed main_menu_keyboard_handler() is registered instead. A solution may be to put all handlers in the array and delete all of them from the array on the initialization function of the event module. This way, linker will again generate the call tree properly, but I need to change the event module. Thanks for your kind comments.
Your case is the poster boy for my approach.
typedef void KeyHdlrT( void ); enum {khConsole, khMenu, khOther }; KeyHdlrT* code pKeyHdlrs[] = { &main_console_keyboard_handler, &main_menu_keyboard_handler, &main_other_keyboard_handler }; BYTE KeyHdlr; void OnKey( void ) { (*pKeyHdlrs[ KeyHdlr ])(); } void OnEnter( void ) { KeyHdlr = khMenu; } void main( void ) { KeyHdlr = khConsole; //more code } <\pre>