Input Interrupt

This sample uses the GPIO input function to detect the GPIO input signal by interrupt.

It is necessary to configure the GPIO to input mode and enable the interrupt function. When the input signal changes, it will trigger the GPIO interrupt, and the input level information will be printed in the interrupt handler.

Users can modify pin information through different macro configurations. For specific macro configurations, refer to Configurations.

Requirements

For requirements, please refer to the Requirements.

Wiring

Connect P4_0 to the external input signal.

Configurations

  1. The following macro can be configured to modify the pin definition.

    #define INPUT_PIN           P4_0
    

Building and Downloading

For building and downloading, please refer to the Building and Downloading.

Experimental Verification

  1. After resetting the EVB, observe the log messages as shown in the Debug Analyzer.

    Start input interrupt test!
    
  2. The external input signal is controlled to change from high to low, and P4_0 detects the falling edge signal to trigger an interrupt. The interrupt handler function GPIO_Input_Handler prints the following information.

    enter GPIO_Pin_Handler success
    

Code Overview

This section mainly introduces the code and process description for initialization and corresponding function implementation in the example.

Source Code Directory

The directory for project file and source code are as follows:

  • Project directory: sdk\samples\peripheral\gpio\input_interrupt\proj

  • Source code directory: sdk\samples\peripheral\gpio\input_interrupt\src

Initialization

The initialization flow for peripherals can refer to Initialization Flow in General Introduction.

  1. Call Pad_Config() and Pinmux_Config() to configure the PAD and PINMUX of the corresponding pins.

    void board_gpio_init(void)
    {
        Pad_Config(INPUT_PIN, PAD_PINMUX_MODE, PAD_IS_PWRON, PAD_PULL_UP, PAD_OUT_ENABLE, PAD_OUT_HIGH);
    
        Pinmux_Config(INPUT_PIN, DWGPIO);
    }
    
  2. Call RCC_PeriphClockCmd() to enable the GPIO clock.

  3. Initialize the GPIO peripheral:

    1. Define the GPIO_InitTypeDef type GPIO_InitStruct, and call GPIO_StructInit() to pre-fill GPIO_InitStruct with default values.

    2. Modify the GPIO_InitStruct parameters as needed. The initialization parameter configurations for GPIO are shown in the table below. Call GPIO_Init() to initialize the GPIO peripheral.

GPIO Initialization Parameters

GPIO Hardware Parameters

Setting in the GPIO_InitStruct

GPIO

GPIO pin

GPIO_InitTypeDef::GPIO_Pin

GPIO_PIN

GPIO direction

GPIO_InitTypeDef::GPIO_Dir

GPIO_DIR_IN

GPIO interrupt

GPIO_InitTypeDef::GPIO_ITCmd

ENABLE

GPIO interrupt trigger

GPIO_InitTypeDef::GPIO_ITTrigger

GPIO_INT_Trigger_EDGE

GPIO interrupt polarity

GPIO_InitTypeDef::GPIO_ITPolarity

GPIO_INT_POLARITY_ACTIVE_LOW

Debounce function

GPIO_InitTypeDef::GPIO_ITDebounce

GPIO_INT_DEBOUNCE_ENABLE

Debounce clock source

GPIO_InitTypeDef::GPIO_DebounceClkSource

GPIO_DEBOUNCE_32K

Debounce clock divide

GPIO_InitTypeDef::GPIO_DebounceClkDiv

GPIO_DEBOUNCE_DIVIDER_1

Debounce Limit

GPIO_InitTypeDef::GPIO_DebounceCntLimit

31

Functional Implementation

When connecting P4_0 to GND (a falling edge on P4_0), detecting a falling edge of P4_0 triggers a GPIO interrupt. The interrupt handler function GPIO_Pin_Handler prints interrupt information.

In the interrupt handler, disabling and masking the corresponding GPIO interrupt should be executed first, and clearing the interrupt flag, unmasking, and enabling the GPIO interrupt should be executed last.

/* Mask and disable interrupt */
GPIO_INTConfig(GPIO_PORT, GPIO_PIN, DISABLE);
GPIO_MaskINTConfig(GPIO_PORT, GPIO_PIN, ENABLE);

DBG_DIRECT("enter GPIO_Pin_Handler success");

/* Clear int flag, unmask and enable interrupt */
GPIO_ClearINTPendingBit(GPIO_PORT, GPIO_PIN);
GPIO_MaskINTConfig(GPIO_PORT, GPIO_PIN, DISABLE);
GPIO_INTConfig(GPIO_PORT, GPIO_PIN, ENABLE);