Port
Port
array of pointers to the GPIO ports and then iterate over both the ports and pins.
Here's how you can do it:
#include <stdint.h>
int main() {
const int num_ports = sizeof(gpio_ports) / sizeof(gpio_ports[0]);
const int num_pins = 16; // Assume 16 pins per port
return 0;
}
Explanation:
1. Array of Ports:
The gpio_ports array stores pointers to the GPIO port structures (e.g., GPIOA,
GPIOB).
2. Pin Iteration:
The outer loop iterates over the ports, and the inner loop iterates over pins (0 to
15).
3. Dynamic Configuration:
All pins in all specified ports are configured as outputs in the setup loop.
4. Customization:
Modify the gpio_ports array or the num_pins variable to match your hardware.
Notes:
Ensure the GPIO peripheral clocks are enabled before accessing GPIO registers
(specific to your microcontroller).
This approach is scalable and works well for controlling multiple pins across
multiple ports.