In this article, we will learn how to use GPIO as Input on Apollo3 MCU, both in polling mode and in interrupt mode as both are important and used in different use cases.
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 setting up the development environment, please read this article.

How to use GPIO as Input (Polling)
Configuring the GPIO as input is quite similar to how we configured the GPIO as output.
Define a macro for the Pin you want to use as input. See the evaluation board schematic for pin numbers used for Keys/buttons
#define KEY1_PIN 16
to define the GPIO input configuration structure variable, always initialize the variable:
am_hal_gpio_pincfg_t key1= {0}; // Structure variable for GPIO Inputs configuration
Go through details about all parameters in the document: \AmbiqSuite-R2.5.1\docs\apollo3_gpio\Apollo3-GPIO.pdf
Now, set various parameters for GPIO Input Pin.
// Configure KEY1 as Input
key1.uFuncSel = 3;
key1.ePullup = AM_HAL_GPIO_PIN_PULLUP_NONE;
key1.eGPInput = AM_HAL_GPIO_PIN_INPUT_ENABLE;
key1.eGPRdZero = AM_HAL_GPIO_PIN_RDZERO_READPIN;
am_hal_gpio_pinconfig(KEY1_PIN, key1);
Once the GPIO input is configured, you can read the pin status as given below:
You can capture pin status in a variable, for example: uint32_t buttonStatus, variable needs to be passed to the GPIO Input Read API as shown below:
am_hal_gpio_state_read(KEY1_PIN, AM_HAL_GPIO_INPUT_READ, &buttonStatus);
How to use GPIO as input (Interrupt)
Reading GPIO in pooling mode is useful but many times you need to capture digital input at high speed and need to use interrupt.
Let us see how to set up interrupt on a GPIO input Pin.
To enable interrupt on any GPIO, you need to do a few extra things:
- Configure the interrupt type
- Clear Interrupt on GPIO Input Pin
- enable Interrupt on GPIO Input Pin
- Enable Global Interrupts
- Use ISR for GPIO Input interrupt
Configuring interrupt
// Configure KEY2 as Input (Interrupt)
key1.uFuncSel = 3;
key1.eIntDir = AM_HAL_GPIO_PIN_INTDIR_HI2LO;
key1.eGPInput = AM_HAL_GPIO_PIN_INPUT_ENABLE;
am_hal_gpio_pinconfig(KEY1_PIN, key1);
create mask & enable interrupt
// Clear the GPIO Interrupt (write to clear).
AM_HAL_GPIO_MASKCREATE(GpioIntMask); // this function create a variable, pGpioIntMask, for pin masking
am_hal_gpio_interrupt_clear(AM_HAL_GPIO_MASKBIT(pGpioIntMask, KEY1_PIN));
// Enable the GPIO/button interrupt.
am_hal_gpio_interrupt_enable(AM_HAL_GPIO_MASKBIT(pGpioIntMask, KEY1_PIN));
//Enable GPIO interrupts
NVIC_EnableIRQ(GPIO_IRQn);
//Enable Master Interrupt
am_hal_interrupt_master_enable();
GPIO Input interrupt service routine (ISR), is one method where a common ISR is used and one more method is there where you can register unique ISR per GPIO input. I will explain that later.
Method 1: Common ISR for all GPIO Inputs
void am_gpio_isr(void)
{
uint64_t GpioIntStatusMask = 0;
am_hal_gpio_interrupt_status_get(false, &GpioIntStatusMask); // read interrupt status register to identify which GPIO interrupt came
am_hal_gpio_interrupt_clear(GpioIntStatusMask); // Clear the interrupt
// detect which GPIO interrupt came
if(GpioIntStatusMask & 0x40000LL) // ((0x01 << (Pin Number)) - example for Pin 18
{
// do something;
}
else if(GpioIntStatusMask & 0x80000LL) // ((0x01 << (Pin Number)) - example for Pin 19
{
// do something;
}
}
I hope it was clear and you were able to follow how to use Apollo3 MCU GPIO as input both in polling and in interrupt mode.
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.