This question is about programming but as its about programming electronics so I thought of posting it here instead of stackoverflow . I have just started my electronics projects. I have designed two applications and have done their hardware and firmware part myself. Here in this question I want to know how to write an efficient C program and how to manage all the files in the project.
Lets say in an application, we have below parts:
- GPIOs
- Serial Communication
- RTC (I2C Communication)
- SD Card (SPI Communication)
The way I work is to create different files for every module. Like for Serial Communication:
//serial.c
//function to initialize serial communication
void serialInit()
{
}
//function to send data
void serialSend()
{
}
//function to receive data
void serialReceive()
{
}
So in serial.c I have made all the required functions for serial communication.
//serial.h
void serialInit();
void serialSend();
void serialReceive();
/*
* All the variable used in serialInit(), serialSend(), serialReceive().
* char rxData = 0;
* int i,j=0; ..etc
*/
and in serial.h I include all the variables used in serial.c. Same goes for every file like rtc.c rtc.h sdcard.c sdcard.h. I main.c I include all the header files:
//main.c
#include "serial.h"
#include "rtc.h"
#include "sdcard.h"
main.c contain all the logic and programming part. All the variable used inside main.c is defined in main.h. Now problem sometime occurs here. As main.h contains all the variables and if I need to include main.h in any other source file, it gives error: multiple definition. I have searched for this error and have found many solutions to it. I want to know the effective of handling this problem. What are some good practice for designing firmwares. Is the way I am writing is correct way or not. What are other methods for handling major embedded firmwares.?