Introduction
As a keypad scan device, Key-Scan can be applied to simple key, remote control or even game pad. It needs to scan the operations of key press and release accurately and timely.
The major benefit of this device is to free up the CPU from scanning the keypad all the time. It triggers the corresponding interrupts to inform CPU in time. In addition, chip can enter low-power state in most of time, and take little time to wake up and handle the key events.
Features
Supports keypad arrays up to 8 x 8 (64 keys) using 16 GPIOs.
Configurable number of rows and columns to accommodate various keypad sizes.
Hardware debounce with configurable timing for each scan cycle.
Configurable scan clock frequency, scan interval, and key release time.
Comprehensive interrupt management: interrupt generation, masking, clearing, and status reporting.
Simultaneous multiple-key press detection.
Built-in FIFO buffer (12-bit width, 16-entry depth) to store key press and release events.
Two operating modes:
Event Trigger Mode: Responds to press and release events.
Regular Scan Mode: Continuously scans at a high priority.
Low power support: Key press events can wake up the CPU from sleep mode.
Stuck key detection and handling.
Function Description
Work principle
The block diagram of Key-Scan is illustrated in Key-Scan Block Diagram. The Key-Scan contains six main parts:
Interrupt Control: control and manage interrupts.
Control Register and FIFO: configure Key-Scan parameters and FIFO.
Clock: Key-Scan clock, 131K clock is used for stuck row detection.
Keypad Control: contains Key-Scan control, wake up control and key input/output control.
Keypad: up to 8*8 keypad array with use of 16 GPIOs.
Key-Scan Block Diagram
Taking 8*8 keypad array for example, the typical application setup is depicted in 8 x 8 keypad, and external keypad is needed.
8 x 8 keypad
At the beginning, all columns output low, and all rows are set as input with internal pull up.
When there is a key or multiple keys pressed (short between the Column and the Row), the input level of the corresponding row changes from high to low. It triggers an internal state machine to start a Key-Scan cycle to determine the column and row of the key that is pressed. The state machine sets first column as an output low and all other columns are in Hi-Z state. The state machine then scans all rows to determine which keys are being pressed, and then second column is output low until the last column. Firstly, scan circuit waits for a first debounce time and does first scan. Then, the circuit waits for a debounce time and scans again. After the two scan cycles, the circuit decides the current key status, and compares with internal key status table to find which key is pressed or released. After a full scan, the key press or release events are stored in the key event FIFO. Debounce time for the keypad is for each full scan and is configurable, as shown in Key-Scan flow.
Key-Scan flow
Interval time between each full scan is configurable. Once all keys are detected release, there is a release timer to confirm that, then an all release interrupt is triggered and state machine enters idle state. The Key-Scan timing is shown in Key-Scan timing.
Key-Scan timing
Work Mode
Event Trigger Mode
In Event Trigger Mode, key press or release event is stored in the key event FIFO only once in each key press and release operation.
Regular Scan Mode
In Regular Scan Mode, at each full scan, any key press event is stored in the key event FIFO until it is released (in this condition, only key press event is stored in the key event FIFO).
Figure Key-Scan Work Mode shows the difference of two work mode.
Key-Scan Work Mode
FIFO
Key-Scan FIFO structure shows the difference of FIFO items between Event Trigger Mode and Regular Scan Mode during a key press and release.
Key-Scan FIFO structure
Bit |
Indication |
Description |
---|---|---|
[8] |
Event |
|
[7:4] |
Row index |
|
[3:0] |
Column index |
|
When the FIFO is full and another item is pushed into the FIFO, an overflow interrupt is triggered. When the FIFO is empty and is read, an overread interrupt is triggered.
The key value assignment is listed in following Table.
Row0 |
Row1 |
Row2 |
Row3 |
Row4 |
Row5 |
Row6 |
Row7 |
|
---|---|---|---|---|---|---|---|---|
Col0 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
Col1 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
Col2 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
Col3 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
Col4 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
Col5 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
Col6 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
Col7 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
Clock Configuration
There are two clock domains in the Key-Scan design, as shown in Key-Scan Clock.
The green part works in low-power clock domain, whose clock will not be gated in low power modes.
The blue part works under bus clock domain (40MHz), whose clock will be gated in low power modes. In bus clock domain, some of the logic works in scan_clk frequency that is divided from bus clock.
Key-Scan Clock
The Key-Scan trigger signal is generated by lower frequency (131K). When the low power clock detects a key press, hardware starts scan by scan clock to confirm the key values.
Shadow Key
Problems with shadow keys may occur during multi-key detection. The applications requiring three-key combinations (such as Ctrl + Alt + Del ), or any other combinations) must ensure that the three keys are wired in appropriate key positions to avoid shadow key (or appearing like that a 4th key has been pressed).
To avoid shadow key, it is best to keep 3-button combinations that are pressed on separate rows and columns. Considering the situation with the keypad is described in KeyPad Example.
In KeyPad Example, there is a 4*3 keypad matrix, connected to Row0 ~ Row3, and Column0 ~ Column2. All of the rows are configured as inputs with pull-up resistors. The columns are configured as outputs, driving low. When a key press is made, one of the row inputs is pulled low, letting the scan circuit know that a key has been pressed, then the KeyScan starts the key scanning algorithm. This algorithm sweeps the output low across the columns, such that only 1 column is driven low at a time. While this is done to each column, the KeyScan reads the row inputs, to determine which keys on a column are being pressed.
KeyPad Example
The shadow key can occur when multiple keys are pressed, which makes it appear that additional keys (actually not being pressed) are being pressed. In shadow key, key 11, key 12, and key 21 are pressed, which causes a shadow key issue. Since Row1 becomes pulled to ground through key 11 (which is pulled through key 12 when Column1 is transmitting a low), when Column1 is driving low, the scan circuit sees a low signal at both Row0 and Row1. This falsely triggers key 22 as being pressed (the key highlighted as yellow).
The reason is that keypad matrix connects the columns and the rows together. When Column1 is driving low, the low gets transmitted onto Row0 via key 12. When key 11 is being pressed, this shorts Column0 to ground, and then shorts Row1 to Column0. In this process, Row1 is shorted to Column1, which is the reason shadow Key occurs.
shadow key
Keypad matrix can support multiple key presses properly when choosing the layout carefully. Figure correct key combination shows a 3-button combination which works as expected.
correct key combination
Stuck Key
In actual application scenarios, the situation of key stuck (not released after being pressed) may occur. In the case of key stuck, to make sure that the system can still enter low power mode and can be woken up through Key-Scan, users can enable auto_check_key_stuck
function.
When the auto_check_key_stuck
function is disabled, the state machine of Key-Scan will keep scanning as long as the key stuck, and cannot enter IDLE state unless the keys are released. Thus, the system cannot enter low power mode.
When the auto_check_key_stuck
function is enabled:
When the key stuck time exceeds the configured stuck_time_threshold, the stuck_event_raw_int_status bit in the KS_ISR_RAW register will be set. If the stuck event interrupt is enabled at the same time, the stuck_event_int_status bit in the KS_ISR register will be set, and the software will jump to the interrupt handling function of Key-Scan.
In the interrupt handling function, the software can locate the stuck key or the row of stuck key by reading the KS_STUCK_KEY_STATUS and the KS_STUCK_KEY_ROW_STATUS1 registers, and sets the default state of the stuck row through the KS_STUCK_CFG1 register. At the same time, the software clears the current stuck interrupt by writing the KS_ICR register.
When all default interrupt is enabled, the software will jump to the all default interrupt handling function.
The next time when the state machine of Key-Scan scans, it will detect that the current state of the stuck row is the same as the configured default state, and consider the state is normal, then enter IDLE state. Thus, the system can smoothly enter the low power mode.
In the IDLE state, if the stuck key is released, the Key-Scan will wake up the system and trigger scanning since it detects that the current state of the stuck row is different from the configured default state, and report a stuck interrupt at the same time. If all keys are released and all release interrupt is enabled, then an all release interrupt will be generated.
The software gets the state of the stuck row by reading the KS_STUCK_KEY_ROW_STATUS1 register in stuck interrupt handling function., and sets the default state of the stuck row again.
If all default interrupt is enabled, there will be an all default interrupt.
The figure Stuck Key detect describes the detection process of stuck button pressing and releasing:
Stuck Key detect
Caution
Since the auto_check_key_stuck
function configures the key stuck status on a per-row basis, the following corner cases need to be noted:
When other keys in the same row are pressed or released after a key stuck occurs:
In low power state, pressing or releasing other keys in the same row cannot wake up the system. Since the stuck key connects its row and column, the row is pulled low. When other keys in this row are pressed or released, it does not affect the row’s level, so these actions cannot be detected.
After the keyscan wakes up, if other keys in the same row as the stuck key are pressed or released during scanning, they can be detected normally. However, if the release timer has already expired after wakeup and the state machine has entered the idle state, these key presses or releases cannot be detected.
When keys in rows other than the stuck row are pressed or released after a key stuck occurs:
Pressing or releasing keys not in the same row will pull the level of their respective rows low or high accordingly, so these keys can be detected.
If the system is in low power state, such actions can also wake up the circuit as expected.
Low Power Management
In practical applications, keeping Key-Scan in Active mode can result in high power consumption. To save power and optimize performance, Key-Scan can operate in Sleep mode, during which only partial wakeup circuitry remains active and the clock source is 131KHz. In Sleep mode, the CPU enters a low power state to reduce energy consumption. When a key is pressed, the CPU is awakened and Key-Scan begins scanning to determine which keys are pressed. Key-Scan will continue scanning as long as the key is held down. When all keys are released and no other events occur, the CPU returns to sleep mode.
When a key is stuck and the Key-Scan enters the idle state, the stuck row is directly connected to the corresponding column, resulting in the output level of the row being low. As a consequence, the pull-up resistor causes leakage current at the I/O port. At this time, the current generated by the pull-up resistor on the row flows along two paths: one part flows through the stuck key to the corresponding column, and the other part flows directly to the row input pin. This increases power consumption, as shown in Schematic diagram of leakage current caused by a stuck key.
If the periodic detection function for the stuck row is not enabled, Key-Scan will continuously scan each row in turn, including the stuck row. In this situation, the continuous leakage current keeps the power consumption elevated.
To reduce leakage duration and power consumption, the periodic detection function for the stuck row must be enabled. The software can configure two timing parameters:
stuckrow_detect_time
andstuckrow_detect_time_interval
. After enabling periodic detection, Key-Scan will check the status of the stuck row at intervals specified bystuckrow_detect_time_interval
, and each detection lasts for the period set bystuckrow_detect_time
. During the detection interval, the stuck row’s I/O pin is set to no pull-up (high impedance); during the detection period, it is set to input with pull-up enabled. If the system is in low power mode at this time, the stuck row detection operation will not wake up the system. In addition, for other rows that are not stuck, the I/O port status is continuously monitored to ensure response speed.
Note
This periodic detection function of the stuck row is only valid when auto_check_key_stuck
function is enabled, otherwise it will not take effect.
Schematic diagram of leakage current caused by a stuck key