Key - Edge Trigger

This sample uses the GPIO input function to implement key functionality. The key is edge-triggered.

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

Requirements

For hardware requirements, please refer to the Requirements.

Wiring

Connect P0_0 and KEY1 on the EVB.

The hardware connection of GPIO key edge sample code is shown in the figure below.

../../../_images/gpio_key.png

GPIO Key Sample Code Hardware Connection Diagram

Configurations

  1. The following macros can be configured to modify key pin definitions.

    #define KEY_PIN                       ADC_0
    
  2. The following macros can be configured to modify the debounce time of the key.

    #define KEY_DEBOUNCE_TIME             (10)            //10ms
    
  3. The entry function is as follows, call this function in main() to run this sample code. For more details, please refer to the Initialization.

    gpio_edge_key();
    

Building and Downloading

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

Experimental Verification

  1. Press the Reset button on the EVB.

  2. Press KEY1, the following message is printed in the Debug Analyzer.

    key_handler: Key press
    
  3. Release KEY1, the following message is printed in the Debug Analyzer.

    key_handler: Key release
    

Code Overview

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

Source Code Directory

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

  • For project directory, please refer to Source Code Directory.

  • Source code directory: sdk\sample\io_demo\gpio\key\edge\gpio_edge_key.c.

Initialization

The initialization flow for peripherals can refer to Initialization Flow.

  1. Call hal_gpio_init() to enable GPIO clock.

    hal_gpio_init();
    
  2. Call hal_gpio_int_init() to initialize GPIO interrupt.

    hal_gpio_int_init();
    
  3. Call hal_gpio_set_debounce_time() to set GPIO debounce time.

    hal_gpio_set_debounce_time(30);
    
  4. Call hal_gpio_init_pin() to initialize the GPIO peripheral and call hal_gpio_set_up_irq() to initialize interrupt-related parameters. The GPIO initialization parameters are configured as shown in the table below.

Key Initialization Parameters

Key Hardware Parameters

Key

Pin Number

KEY_PIN

GPIO Type

GPIO_TYPE_AUTO

GPIO Mode

GPIO_DIR_INPUT

GPIO Pull Value

GPIO_PULL_UP

Interrupt Type

GPIO_IRQ_EDGE

Interrupt Polarity

GPIO_IRQ_ACTIVE_LOW

Debounce Enable

true

  1. Call hal_gpio_register_isr_callback() to register gpio interrupt callback.

  2. Call hal_gpio_irq_enable() to enable gpio interrupt.

Functional Implementation

When KEY1 is pressed, an interrupt is triggered once, and when KEY1 is released, another interrupt is triggered. The interrupt handler function key_handler will perform the following actions.

  1. Call hal_gpio_irq_disable() to disable GPIO interrupt.

  2. Call hal_gpio_get_input_level() to get the input level of key.

  3. Based on the key’s level, change the interrupt polarity by calling hal_gpio_irq_change_polarity().

  4. Call hal_gpio_irq_enable() to enable GPIO interrupt.

static void key_handler(uint32_t key_index)
{
    /*  Disable GPIO interrupt */
    hal_gpio_irq_disable(key_index);

    key_status = hal_gpio_get_input_level(key_index);

    if (key_status)
    {
        IO_PRINT_INFO0("key_handler: Key release");
        hal_gpio_irq_change_polarity(key_index, GPIO_IRQ_ACTIVE_LOW);
    }
    else
    {
        IO_PRINT_INFO0("key_handler: Key press");
        hal_gpio_irq_change_polarity(key_index, GPIO_IRQ_ACTIVE_HIGH);
    }

    /*  Enable GPIO interrupt */
    hal_gpio_irq_enable(key_index);
}