Search code examples
cloopsgpiolpc

C GPIO for loop


I have an LPC board and I need to iterate through the pins and check if they are high or low when I connect them with some LEDs/Wire. Problem I find is that the pins are not in a specific order. I have 20 Input GPIOs and 20 Outputs.

I have defined the pins as macro expressions Eg. #define GPIO1 0,1 #define GPIO2 1,19 and then I stored them in an array Eg. gpio_outputs[] = {GPIO1, etc}; but if I try to do a for loop of this array it tells me "too few arguments"

for (i=0;i<sizeof(gpio_outputs);i++){
//  Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, gpio_outputs[i]);
//}

The prototype for Chip_GPIO_SetPinDIROutput is:

Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, PIN_INDEX, PIN_NUMBER);

Now, from what I've been reading, macro expression don't work with this. So my question is how can I iterate through each GPIO without having to do it manually for all 40?


Solution

  • struct GPIO_Attr { uint8_t pinIndex, pinNumber; };
    struct GPIO_Attr gpio_outputs[] = { {GPIO1}, {GPIO1} }; // Extra '{}' is necessary to initialize GPIO_Attr fields
    for (i=0;i<sizeof(gpio_outputs)/sizeof(gpio_outputs[0]);i++){
        Chip_GPIO_SetPinDIROutput(LPC_GPIO_PORT, gpio_outputs[i].pinIndex, gpio_outputs[i].pinNumber);
    }