I have a program that accesses a certain I/O controller using memory-mapped I/O. I currently store the base address of this controller in a const
variable.
This leads to a code structure something like this:
gpio.h:
int gpioFunc (unsigned int pin, unsigned int func);
int setGpio (unsigned int pin, unsigned int val);
gpio.c:
#include "gpio.h"
unsigned volatile int* const gpioAddr = (unsigned int*) 0x20200000;
int gpioFunc (unsigned int pin, unsigned int func)
{
//code that uses the variable
}
int setGpio (unsigned int pin, unsigned int val)
{
//code that uses the variable
}
main.c:
#include "gpio.h"
void main ()
{
gpioFunc (16,1);
setGpio(16,0);
hlt();
}
I have always been uncertain of the best practices in C as to where to put constants. Does this way make sense, or would it be better practice to put it somewhere else? If so, where?