GPIO in DLPS - Automatic Configuration

This sample uses the auto-configuration wake-up feature of the GPIO to detect GPIO input signals in low power mode.

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

Requirements

For hardware requirements, please refer to the Requirements.

Configurations

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

    #define TEST_Pin                      P0_0
    
  2. The entry function is as follows, call this function in main() to run this sample code. For more details, please refer to the Initialization.

    dlps_gpio_wk_auto_config_demo();
    

Building and Downloading

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

Experimental Verification

  1. Press the Reset button on the EVB and the following message is printed in the Debug Analyzer.

    gpio_test: gpio_level 1
    
  2. After initialization is complete, the system is in idle state, it will enter DLPS mode. Observe the entering DLPS message displayed in the Debug Analyzer.

    dlps_store: enter dlps
    
  3. When a low level input is detected on P0_0, the system will be awakened and the interrupt function of the GPIO will be called. The following message is printed in the Debug Analyzer.

    gpio_handler: gpio_level 0
    dlps_restore: exit dlps
    

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\dlps\dlps_gpio_wk_auto_config_demo.c.

GPIO 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.

GPIO Input Initialization Parameters

GPIO Hardware Parameters

GPIO

Pin Number

TEST_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.

DLPS Mode Initialization

  1. Call io_dlps_register() to initialize IO store/restore and do not need to worry about which IO peripheral requires specific handling.

  2. Call io_dlps_register_enter_cb() to register callbacks to DLPS enter stage. Function dlps_store will be executed while entering DLPS.

    1. No action is needed.

  1. Call io_dlps_register_exit_cb() to register callbacks to DLPS exit stage. Function dlps_restore will be executed while exiting from DLPS.

    1. No action is needed.

  2. Call bt_power_mode_set() to set Bluetooth MAC deep sleep mode.

  3. Call power_mode_set() to switch the system to DLPS mode.

Functional Implementation

When connecting P0_0 to GND, detecting a low level of P0_0 triggers a GPIO interrupt. The interrupt handler function gpio_handler prints interrupt information.

static void gpio_handler(uint32_t context)
{
    uint8_t pin_index = (uint32_t)context;
    T_GPIO_LEVEL gpio_level = hal_gpio_get_input_level(pin_index);

    IO_PRINT_INFO1("gpio_handler: gpio_level %d", gpio_level);

    // add user code here
}