I'm developing embedded C code on EFM32 Cortex M3 processors, and after a few months the code is beginning to get crazy... By this I mean that we changed the hardware so we get different versions, on which we changed some components, moved some IOs, have different states for theses at start up...
So I'm trying to clean it up a bit:
Where I had some very big files organised like this:
/*============================================================================*/
/* File : BSP_gpio.h */
/* Processor : EFM32GG980F1024 */
/*----------------------------------------------------------------------------*/
/* Description : gpio driver */
/*----------------------------------------------------------------------------*/
#ifndef BSP_GPIO_H
#define BSP_GPIO_H
#ifdef EXTERN
#undef EXTERN
#endif
#ifdef BSP_GPIO_C
#define EXTERN
#else
#define EXTERN extern
#endif
typedef enum
{
GPIO_INIT = 0,
GPIO_WAKEUP = 1,
GPIO_SLEEP = 2,
} GPIO_MODE;
/* Definition / conts */
#define PIO_PIN_HIGH 1
#define PIO_PIN_LOW 0
#ifdef HW_v1
... hundreds of lines...
#elif defined HW_v2
... hundreds of lines...
#endif
#endif
I try to separate the different versions in separated files and try something like this:
#ifdef HW_v1
#include "BSP_gpio_HW1.h"
#elif defined HW_v2
#include "BSP_gpio_HW2.h"
#endif
With the same type of header for each "subfile" (until the enum). The aim is that in every other ".c" file we include the "BSP_gpio.h" and it automatically includes the file corresponding to the hardware used.
The first problem is that the compilation depends on where I include the subfile. For example, I have a function "void BSP_GPIO_set(GPIO_MODE mode)" which uses the enum "GPIO_MODE" and is different in the two hardware versions (because the state of the IOs is not the same on the two hardwares). If I include the subfile before the declaration, it doesn't know the type "GPIO_MODE" and makes a compilation error, even if I #include "BSP_gpio.h" in the subfiles. So I just put this at the end of the file and it works, even if I don't really like it...
The second problem appears when I have a variable declared as extern which I want to use in both subfiles and in others C files. Lets say I put this line just before "#ifdef HW_v1":
EXTERN int numberOfToggles;
The "EXTERN" word is nothing in my "BSP_gpio.c" file as I define BSP_GPIO_C at the beginning of it, and is the keyword extern in every other file where I include "BSP_gpio.h". When I build my project, it compiles but I have a linker error : "Duplicate definitions for numberOfToggles in BSP_gpio.o and BSP_gpio_HW2.o" and I can't find a solution for that.
I'm ready to change the architecture of my project if anyone has a proper solution for that!