Is there a way to write a generic GPIO code for MSP430?
This can be done with a set of macros:


#define GPIO_MODE_GPIO(...)  GPIO_MODE_GPIO_SUB(__VA_ARGS__)
#define GPIO_MODE_GPIO_SUB(port, pin) (P##port##SEL &amp;= ~(1&lt;&lt;pin)) // making an assumption about register layout here

#define GPIO_OUT_SET(...)  GPIO_OUT_SET_SUB(__VA_ARGS__)
#define GPIO_OUT_SET_SUB(port, pin) (P##port##OUT &amp;= ~(1&lt;&lt;pin))

//etc...


The ## operator does string concatenation in the preprocessor. The two-level macros are used to allow a level of macro expansion to happen between the first and second macros, which allows you to do things like this:

#define LED_IO  1,5
GIPO_OUT_SET(LED_IO);


Without that extra indirection, the preprocessor would complain about not having enough arguments to the GPIO_OUT_SET macro.

This style of system has adapted quite well to every MCU I've used so far, from AVRs to ARMs, and since it's a preprocessor macro it compiles down into the smallest possible set of instructions.