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
The following macros can be configured to modify GPIO pin definitions.
#define TEST_Pin P0_0
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
Press the Reset button on the EVB and the following message is printed in the Debug Analyzer.
gpio_test: gpio_level 1
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
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.
Call
hal_gpio_init()
to enable GPIO clock.hal_gpio_init();
Call
hal_gpio_int_init()
to initialize GPIO interrupt.hal_gpio_int_init();
Call
hal_gpio_set_debounce_time()
to set GPIO debounce time.hal_gpio_set_debounce_time(30);
Call
hal_gpio_init_pin()
to initialize the GPIO peripheral and callhal_gpio_set_up_irq()
to initialize interrupt-related parameters. The GPIO initialization parameters are configured as shown in the table below.
GPIO Hardware Parameters |
GPIO |
---|---|
Pin Number |
|
GPIO Type |
|
GPIO Mode |
|
GPIO Pull Value |
|
Interrupt Type |
|
Interrupt Polarity |
|
Debounce Enable |
|
Call
hal_gpio_register_isr_callback()
to register gpio interrupt callback.Call
hal_gpio_irq_enable()
to enable gpio interrupt.
DLPS Mode Initialization
Call
io_dlps_register()
to initialize IO store/restore and do not need to worry about which IO peripheral requires specific handling.Call
io_dlps_register_enter_cb()
to register callbacks to DLPS enter stage. Functiondlps_store
will be executed while entering DLPS.No action is needed.
Call
io_dlps_register_exit_cb()
to register callbacks to DLPS exit stage. Functiondlps_restore
will be executed while exiting from DLPS.No action is needed.
Call
bt_power_mode_set()
to set Bluetooth MAC deep sleep mode.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
}