Apollo3 MCU Programming: How to use GPIO Output

In this article we will learn how to use GPIO as output on Apollo3 MCU. Before you start developing the code, please go through the Pin mapping. You can download theย Apollo3 MCU Blue Pinoutย details.

If you need help on setting up development environment, please read this article.

How to use GPIO as Output

Whenever we start learning a new MCU, first thing we all want to do quickly is to understand how to blink an LED.

Apollo3 MCU Programming-how to use gpio output

So, let us see how to configure a GPIO as output and control its state to high and low.

First, define a macro for Pin number you want to use. Check evaluation board schematic for LEDs connection with MCU and it’s pin numbers.

#define LED1_PIN		17  // LED on Evaluation Board

declare structure variable for GPIO configuration, always initialize the variable:

am_hal_gpio_pincfg_t led1 = {0}; // Structure variable for GPIO configuration

and then inside main(), before super-loop while(1), you can configure the GPIO:

For details about all the parameters, please check the document \AmbiqSuite-R2.5.1\docs\apollo3_gpio\Apollo3-GPIO.pdf

// Configure LED1 as Output
led1.uFuncSel = 3; // Set GPIO function, see Pinout details
led1.ePullup = AM_HAL_GPIO_PIN_PULLUP_NONE;
led1.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_PUSHPULL;
led1.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_2MA;
am_hal_gpio_pinconfig(LED1_PIN, led1);

You can also create a function where you can declare the variable for GPIO configuration and set all the parameters and call this function before while(1):

void ConfigureGPIOs(void)
{
	am_hal_gpio_pincfg_t led1 ={0}; // Structure variable for GPIO configuration
	
	// Configure LED1 as Output
	led1.uFuncSel = 3; //
	led1.ePullup = AM_HAL_GPIO_PIN_PULLUP_NONE;
	led1.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_PUSHPULL;
	led1.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_2MA;
	am_hal_gpio_pinconfig(LED1_PIN, led1);
	
}

Once the GPIO configuration is done. You can use Write function to make Pin high or low as given below(500 is used for 500mSecond delay):

am_hal_gpio_state_write(LED1_PIN, AM_HAL_GPIO_OUTPUT_SET);
am_util_delay_ms(500);
am_hal_gpio_state_write(LED1_PIN, AM_HAL_GPIO_OUTPUT_CLEAR);
am_util_delay_ms(500);

It’s very simple. I hope this was easy to follow.


I work as an embedded systems design consultant, helping companies build custom embedded products and develop test automation solutions for their PCBs.

If you have any feedback about the blog, you can share it in the comments below or contact me directly.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.