I am writing a "gpio" device driver for my AVR ATmega32 microcontroller. The driver has an initialization function that's shown below. I use enums to pass configuration to the function. An example of calling this function is
gpio_init_port(MY_PORT_A, INPUT, HIGH);
This would set PORTA as input with default value HIGH. I use switch statements to check the passed configuration to set the corresponding port as input or output. Now I thought of using #error preprocessor directive in the "default" case of the switch statement in case a user enters a wrong input, for example
gpio_init_port(5, INPUT, HIGH);
so compilation stops and an error message is printed. But I do not know why it's not working as I expect. The part of the code I am referring to is this:
default:
#if (port_number != MY_PORT_A && port_number != MY_PORT_B && port_number != MY_PORT_B && port_number!=MY_PORT_D)
#error "Wrong Input. You have entered invalid port number."
#endif
The expression
(port_number != MY_PORT_A && port_number != MY_PORT_B && port_number != MY_PORT_B && port_number!=MY_PORT_D)
doesn't evaluate and the error message isn't printed. But when I change it to
#if (1)
#error "Wrong Input. You have entered invalid port number."
#endif
It works fine. Another issue that I have is that when I write something like
gpio_init_port(5, ***OUTPUT***, HIGH);
The error message is also printed although I am passing "OUTPUT" this time and this not supposed to happen since the default case is defined only for the "INPUT" case.
gpio.h
______
typedef enum port_number{
MY_PORT_A=0,
MY_PORT_B,
MY_PORT_C,
MY_PORT_D
} port_number_t;
typedef enum port_direction{
INPUT=0,
OUTPUT
} port_direction_t;
typedef enum output_state{
LOW=0,
HIGH
}output_state_t;
void gpio_init_port(port_number_t port_number, port_direction_t port_direction, output_state_t initial_value);
gpio.c
________
#include "gpio.h"
void gpio_init_port(port_number_t port_number, port_direction_t port_direction, output_state_t initial_value)
{
switch (port_direction)
{
case INPUT:
{
switch (port_number)
{
case MY_PORT_A:
DDRA=0x00;
PORTA=initial_value? 0xff:0x00;
break;
case MY_PORT_B:
DDRB=0x00;
PORTB=initial_value? 0xff:0x00;
break;
case MY_PORT_C:
DDRC=0x00;
PORTC=initial_value? 0xff:0x00;
break;
case MY_PORT_D:
DDRD=0x00;
PORTD=initial_value? 0xff:0x00;
break;
default:
#if (port_number != MY_PORT_A && port_number != MY_PORT_B && port_number != MY_PORT_B && port_number!=MY_PORT_D)
#error "Wrong Input. You have entered invalid port number."
#endif
break;
}
}
case OUTPUT:
{
switch (port_number)
{
case MY_PORT_A:
DDRA=0xff;
PORTA=initial_value? 0xff:0x00;
break;
case MY_PORT_B:
DDRB=0xff;
PORTB=initial_value? 0xff:0x00;
break;
case MY_PORT_C:
DDRC=0xff;
PORTC=initial_value? 0xff:0x00;
break;
case MY_PORT_D:
DDRD=0xff;
PORTD=initial_value? 0xff:0x00;
break;
}
}
}
}
So, what is exactly wrong here?
I have read that enums can't be used in conditional compilation, but I don't know if that's truly right and the reason of the problem.
Thanks in advance.