Sorry, syntax highlight here is awful...
/* GPIO & SIUL macros */
#define PortOffset(pn) (16 * pn)
#define Port(name,pin) ((Port##name) + pin)
#define SIUL_Out(pin,sss) { \
SIUL2.MSCR[Port pin].B.SSS = sss; /* Pin functionality */ \
SIUL2.MSCR[Port pin].B.src=3; /* Maximum slew rate */ \
SIUL2.MSCR[Port pin].B.OBE = 1; /* Output Buffer Enable on */ \
SIUL2.MSCR[Port pin].B.IBE = 0; /* Input Buffer Enable off */ \
}
#define SIUL_In(pin,imcr,sss) { \
SIUL2.IMCR[imcr - 512].B.SSS = sss; /* Pin functionality */ \
SIUL2.MSCR[Port pin].B.OBE = 0; /* Output Buffer Enable off */ \
SIUL2.MSCR[Port pin].B.IBE = 1; /* Input Buffer Enable on */ \
}
/* Set value of GPIO output */
#define GPIO_VO(pin) SIUL2.GPDO[Port pin].B.PDO_4n
/* Get value of GPIO input */
#define GPIO_VI(pin) SIUL2.GPDI[Port pin].B.PDI_4n
/* Setup GPIO as output */
#define GPIO_Out(pin,v) { \
SIUL2.GPDO[Port pin].B.PDO_4n = v; /* Set initial pin value */ \
SIUL_Out(pin,0); /* Pin functionality as GPIO*/ \
}
/* Setup GPIO as input */
#define GPIO_In(pin) { \
SIUL2.MSCR[Port pin].B.SSS = 0; /* Pin functionality as GPIO*/ \
SIUL2.MSCR[Port pin].B.OBE = 0; /* Output Buffer Enable off */ \
SIUL2.MSCR[Port pin].B.IBE = 1; /* Input Buffer Enable on */ \
}
/* Usage example */
#define LED1GIO (A,0)
#define LED1 GPIO_VO(LED1GIO)
void func(void){
/* GPIO as output, initialized as 0 */
GPIO_Out(LED1GIO, 0);
/* Set value of GPIO to 1 */
LED1 = 1;
}