0

I have a C Project in MPLAB IDE where I want the main.c file to be able to be used for more than one HWs I have. The main difference is that the pins of the PIC are differently connected.

The idea is to create different .h and .c files for each HW my main file supports and only include this one that corresponds to the HW for which I want to build.

Something like this:

#if defined( HW_1 )
  #include "hw1.h"
#elif defined( HW_2 )
  #include "hw2.h"
#endif

This extra files (hw1.c and hw2.c) include mainly definitions of the functions of the pins, such as:

#define GPI_PG_3V3 RD2

and related functions.

But the problem is that different HWs may have same functionalities, but on different pins. That means the same #define variables may exist for different HWs.

Although I include the respective .h file according to my HW, in the structure of the MPLAB I have the same project and this means I have to include all .h files at the same time. But this leads to the problem that the compiler thinks there are various definitions of the same variable, e.g.:

Error   [237] S:\PIC_Code\ACCEED_4420\Source\acceed1480.c; 23. function "_initGpio" redefined

Does anyone have a good idea on how to solve this? One idea that came to me is to have different projects for all the different HWs I have. Is that the only solution? In such a case it would be difficult how to structure the project directories.

nickagian
  • 673
  • 1
  • 5
  • 9
  • Project Configurations is feature that can create project inside project. Is it available in your version ? It is available in MPLAB X – Punit Vara Dec 07 '15 at 16:52
  • @PunitVara Unfortunately I haven't found anything relevant. I only see build configuration, but this is between release and debug. What does this feature in MPLAB X do? – nickagian Jan 05 '16 at 13:52

1 Answers1

1

You might want to take a look at this question. Your problem would be more easily resolved with C++ namespace, but since you use C you will probably have to use some hacks. In the question, the accepted answer probably doesn't fit your case, and the second might add a slight overhead (especially since you seem to use an old compiler). You should try one of the macro hacks in the other answers.

My suggestion, if your compiler doesn't scream, is to use some macro magic to conditionally compile and includes some files. It has the advantage of not compiling everything, which might save some space if the compiler is bad (since it's MPLAB 8, it might be).

In your "my_hw.h" :

// change this line to change the hardware code
#define THE_DRIVER_TO_USE hw1


#define STRINGIFY2(x) #x
#define STRINGIFY(x) STRINGIFY2(x)

#include STRINGIFY(THE_DRIVER_TO_USE.h)

In some "hw_includer.c" file, you put that, and never change it :

// optional macro, see below
#define ALLOWED_TO_COMPILE 1
// yes, include a .c
#include STRINGIFY(THE_DRIVER_TO_USE.c)

Now you just have to change the THE_DRIVER_TO_USE macro to swap drivers, and include my_hw.h instead of hwX.h. It will include the right .h, and compile the right .c. The requirement to this is to NOT compile the hwX.c by themselves. You either exclude them from compilation in the IDE, or put all the code in the .c inside a guard #ifdef ALLOWED_TO_COMPILE #endif if you can't.

Community
  • 1
  • 1
ElderBug
  • 5,926
  • 16
  • 25
  • This is actually interesting what you are suggesting. But I have lost you a bit. You said the requirement is to NOT compile the hwX.c by themselves. But how will it be compiled? Only by #including it? I also didn't know one can #include a .c file. The other option you said with the "ALLOWED_TO_COMPILE" is probably a good idea. I can put all the code in guards and allow -with #defines- only one hwX.c to actually be compiled. – nickagian Jan 05 '16 at 13:59
  • @user1428960 Including doesn't do much more than copy-pasting the included file, so the trick is to only compile "hw_includer.c" which will contain the right 'real' .c. Thus the 'real' .c must not be previously compiled or else they will be compiled multiple times. Usually, there are 2 types of IDE. Some blindly compile all compilable files (.c for C), others only compile files that were added in a category, usually called "sources". Depending on the type of MPLAB 8 (I forgot), it might be easier to just exclude the specific .c, or to use the macro trick. – ElderBug Jan 05 '16 at 14:38