Selective compile in C

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

Selective compile in C

Jump to solution
893 Views
bigmac
Specialist III

Hello all,

 

I am using CW6.3 for HCS08 devices.

 

My project uses a generalised array of function pointers -

 

// Table of user function pointers for each timeslot:
static const void (*Usr_func[])(void) = {
   Usr0_tic4,  Usr1_tic4, Usr0_tic8, Usr1_tic8,
   Usr0_tic16, Usr1_tic16, Usr2_tic16, Usr3_tic16 };

 

However, a specific version of the project may not make use of all of the functions. For each unused function, I would like to be able to automatically incorporate an empty function as a default, e.g.

void Usr0_tic4( void) { }

 

However, I have not been able to find a suitable #ifndef that achieves the desired result.  Within the primary project file, I would like to restrict the code to only those functions that are used, if at all possible.  The separate file containing the array would be standardised, and not require to be altered for different projects.

 

This concept works well for assembly code, where selective assembly, based on a non-defined subroutine name, is feasible.  I would like to do as similar thing with the C coded version.  Perhaps someone can suggest a method.  Otherwise, I will need to incorporate all the functions within the primary project file, whether utilised or not (which is probably not the "end of the world").

 

Regards,

Mac

Labels (1)
Tags (1)
0 Kudos
1 Solution
512 Views
sebasira
Senior Contributor I

Hi Mac!

 

If only C support function overriding....

 

I guess maybe the best shot is to use some macros (labels) instead of the function name inside the array; and  also a dummy routine so you will have:

 

Stardard file: (the one with the array):

void dummy (void){}/* So you can handle unused functions these way */#ifndef U0_T4    #define U0_T4      dummy#endif#ifndef U1_T4    #define U1_T4      dummy#endif#ifndef U0_T8    #define U0_T8      dummy#endif
// and so on for every function inside the array// Table of user function pointers for each timeslot:static const void (*Usr_func[])(void) = {   U0_T4,  U1_T4, U0_T8, U1_T8,   U0_T16, U1_T16, U2_T16, U3_T16 };

 

Your project file:

/* Assuming you only use Usr1_tic4 and Usr0_tic8 */#define U1_T4      Usr1_tic4  #define U0_T8      Usr0_tic8

 

Sadly it would mean a major update to your code, but you won't need a dummy function for each not-used routine

 

Hope it helps,

Best Regards!

View solution in original post

0 Kudos
5 Replies
513 Views
sebasira
Senior Contributor I

Hi Mac!

 

If only C support function overriding....

 

I guess maybe the best shot is to use some macros (labels) instead of the function name inside the array; and  also a dummy routine so you will have:

 

Stardard file: (the one with the array):

void dummy (void){}/* So you can handle unused functions these way */#ifndef U0_T4    #define U0_T4      dummy#endif#ifndef U1_T4    #define U1_T4      dummy#endif#ifndef U0_T8    #define U0_T8      dummy#endif
// and so on for every function inside the array// Table of user function pointers for each timeslot:static const void (*Usr_func[])(void) = {   U0_T4,  U1_T4, U0_T8, U1_T8,   U0_T16, U1_T16, U2_T16, U3_T16 };

 

Your project file:

/* Assuming you only use Usr1_tic4 and Usr0_tic8 */#define U1_T4      Usr1_tic4  #define U0_T8      Usr0_tic8

 

Sadly it would mean a major update to your code, but you won't need a dummy function for each not-used routine

 

Hope it helps,

Best Regards!

0 Kudos
512 Views
bigmac
Specialist III

Hello Sebastian,

 

Thankyou for your response.  I was able to implement it without too much change to the existing code.  I created an additional header file (Usr_Project.h) to contain the relevant function macros for the project, and this is #included within the "System.c" file where the #ifndef's and the function pointer array are located.

 

I am kicking myself for not recognizing this limitation of the preprocessor, especially since I recently proposed a similar method for the preprocessor recognition of variable names.

 

Regards,

Mac

 

0 Kudos
512 Views
bigmac
Specialist III

Hello all,

 

On a related issue, I usually experience difficulty with the use of the 'const' keyword, when applied to pointers and functions. 

 

When I previously posted the array of function pointers -

// Table of user function pointers for each timeslot:
const void (*Usr_func[])(void) = { ... };

 

I did not realize at the time, that the array was actually located in RAM, rather than within flash.  With a cross-reference to the format of a vector table, the following incantation placed the array in flash, as required.

void (*const Usr_func[])(void) = { ... };

 

I then successfully tried using the following typdef and array definition, for the array to locate in flash -

typedef void (*funcptr0)(void);

 

const funcptr0 Usr_func[] = { ... };

 

I still have diffulty in understanding how the last two methods become equivalent.  I am obviously missing something.  Perhaps someone would be willing to give me an explanation.

 

Regards,

Mac

 

0 Kudos
512 Views
sebasira
Senior Contributor I

When you define:

const void (*Usr_func[])(void) = { ... };

The pointer is a variable (RAM) and the type returned by the functions are constant

 

When you define: 

void (*const Usr_func[])(void) = { ... };

Now the pointer is constant so it's placed in FLASH

 

And in

const funcptr0 Usr_func[] = { ... };

you're saying that the type is constant.... So if the type is a pointer to function, then is a constant pointer to function

 

0 Kudos
512 Views
sebasira
Senior Contributor I

Your welcome!

 

It's good to know you manage to solve it

0 Kudos