Software KeyScan
This sample code guide is designed to help users easily and comprehensively understand KeyScan sample. This sample demonstrates how software KeyScan works in DLPS mode.
Requirements
For hardware requirements, please refer to the Requirements.
Wiring
The EVB is connected to a matrix keyboard module, connect P2_2 to ROW0, P2_3 to ROW1, P2_4 to ROW2, P2_5 to ROW3, P0_0 to COLUMN0, and P0_1 to COLUMN1, P0_2 to COLUMN2, and P0_3 to COLUMN3. The hardware connection of KeyScan sample code is shown in the figure below.

KeyScan Sample Code Hardware Connection Diagram
Configurations
The following macros can be configured to modify the row size and column size definitions.
#define KEYSCAN_ROW_SIZE 4
#define KEYSCAN_ROW_SIZE 4
The following variables can be configured to modify pin definitions.
static uint8_t keyscan_row_pin[KEYSCAN_ROW_SIZE] = {P2_2, P2_3, P2_4, P2_5};
static uint8_t keyscan_column_pin[KEYSCAN_COLUMN_SIZE] = {P0_0, P0_1, P0_2, P0_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.sw_keyscan_demo();
Building and Downloading
For building and downloading, please refer to the Building and Downloading.
Experimental Verification
Press the Reset button on the EVB, when keys are pressed, KeyScan starts scan. After the scan ends, it executes the KeyScan scan end callback function and prints the key information. If keys remain pressed continuously, it will keep printing the key information.
key_data[xx] xx, row at xx, column at xx
After all the keys are released, print the following log.
sw_keyscan_release_cb: All key release
Code Overview
This section introduces the code and process description for initialization and corresponding function implementation in the sample.
Source Code Directory
For project directory, please refer to Source Code Directory.
Source code directory:
sdk\src\sample\io_demo\keyscan\sw_keyscan\sw_keyscan_demo.c
.
KeyScan Initialization
Software KeyScan initialization flow is shown in the following figure.

Software KeyScan Initialization Flow Chart
Define the
T_KEYSCAN_INIT_PARA
typekeyscan_init_para
, and modify thekeyscan_init_para
parameters.Modify the
row_size
parameter to configure row size.Modify the
column_size
parameter to configure column size.Modify the
keyscan_row_pins
parameter to configure row pins.Modify the
keyscan_column_pins
parameter to configure column pins.Modify the
keyscan_scan_end_cb
parameter to register KeyScan scan end callback function to software KeyScan module.Modify the
keyscan_release_cb
parameter to register KeyScan release callback function to software KeyScan module.
Call
sw_keyscan_init
to initialize the software KeyScan.Call
sw_keyscan_filter_data_config
to configure software KeyScan filter data.
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
bt_power_mode_set()
to set Bluetooth MAC deep sleep mode.Call
power_mode_set()
to switch the system to DLPS mode.
Functional Implementation
Callback Handle
After the scan key matrix is completed, the KeyScan scan end callback is executed.
Get the number of pressed keys and key data.
When the release time reaches the set value, if no key is pressed, the KeyScan all release callback is executed.
Print log.
sw_keyscan_release_cb: All key release