Supported ICs

IC

RTL8721Dx

RTL8726E

RTL8720E

RTL8730E

Supported

Y

N

N

N

Introduction

As a keypad scanning device, Key-Scan supports up to 8x8 keypad arrays with configurable rows and columns. It features multi-key detection and low-power operation modes.

Block Diagram

The block diagram of Key-Scan is illustrated in the following figure.

../../_images/key_scan_block_diagram.svg

Key-Scan contains 6 key Components:

  • Interrupt Control: control and manage interrupts

  • Registers and FIFO: configure Key-Scan parameters and FIFO

  • Clock: 131KHz clock for stuck row detection

  • Keypad Control: scanning logic, wakeup control, and input/output control

  • Keypad: 16 GPIOs for 8x8 array implementation

Keypad

The columns and rows of Key-Scan are configurable, and the maximum columns or rows can be as 8.

../../_images/keypad_array.svg

Keypad array

  • 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), 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.

Work Mode

Key-Scan contains two work modes: Event Trigger Mode and Regular Scan Mode.

Work mode

Description

Event Trigger

key press/release event is stored in the FIFO only once during each key press/release operation

Regular Scan

At each full scan, any key press event is stored in the FIFO until it is released

The following figure shows the difference of FIFO items between Event Trigger Mode and Regular Scan Mode during a key press and release.

../../_images/difference_FIFO_items_between_two_work_modes.svg

FIFO Mechanism

The FIFO of Key-Scan has a depth of 16 and an entry width of 12 bits. The FIFO structure of Key-Scan is illustrated below.

../../_images/FIFO_structure.svg

Bit

Indication

Description

[8]

Event

  • 1: key press event

  • 0: key release event

[7:4]

Row index

  • 1: row 0

  • 2: row 1

  • 3: row 2

  • 8: row 7

[3:0]

Column index

  • 1: column 0

  • 2: column 1

  • 3: column 2

  • 8: column 7

Low Power

Active Mode

In active mode, the Key-Scan remains powered on continuously.

  • Column Scanning: When the Key-Scan scans column x (where x represents any column), it sets column x to a low output state while placing all other columns in a high-impedance (Hi-Z) state.

  • Row Detection: After setting the column, the Key-Scan scans all rows to detect which keys are being pressed or released, completing a column scan.

  • Sequential Scanning: The Key-Scan scans columns sequentially and performs periodic scans column by column.

Sleep Mode

In practical applications, keeping the Key-Scan in active mode consumes a significant amount of power. To save the power and optimize the performance, the Key-Scan typically operates in sleep mode.

  • Low-Power State: In sleep mode, the CPU enters a low-power state to reduce energy consumption.

  • Wake-Up Mechanism: When a key is pressed, the CPU is awakened, and the Key-Scan begins scanning to determine which keys are pressed. The Key-Scan continues scanning while the keys remain pressed.

  • Return to Sleep: After the keys are released and no other events occur, the CPU re-enters the sleep state.

Stuck Key

Key stuck may occur in some applications. Without a solution for stuck keys, CPU will keep active to perform continuous keypad scans, preventing it from entering the low-power mode and resulting in high power consumption.

To solve this problem, the Key-Scan supports automatic check key stuck function. When enabling this function, if a key stuck time is over than stuck time threshold, this key will be regarded as a stuck key. Read key stuck status registers to get the stuck key, and set the default status of the stuck key row to let CPU enter low power mode.

To reduce the power consumption, stuck row detection function may be enabled to set the detection time interval for row pins with no pull.

Usage

Normal Operation

To use Key-Scan in either Event Trigger Mode or Regular Scan Mode, perform the following steps:

  1. Configure the pin multiplexing for Key-Scan keypads.

    1. Set the column pins to no pull and configure the column pin multiplexing function.

      PAD_PullCtrl(pad_colx, GPIO_PuPd_NOPULL);
      Pinmux_Config(pad_colx, PINMUX_FUNCTION_KEY_COLx);
      
    2. Set the row pins to pull-up and configure the row pin multiplexing function.

      PAD_PullCtrl(pad_rowx, GPIO_PuPd_UP);
      Pinmux_Config(pad_rowx, PINMUX_FUNCTION_KEY_ROWx);
      
  2. Initialize Key-Scan parameters.

    Select the number of key rows and columns (one bit corresponds to one row or column) according to keypads, and set work mode to Event Trigger Mode or Regular Scan Mode, etc.

    KeyScan_StructInit(&KeyScan_InitStruct);
    KeyScan_InitStruct.KS_ColSel = 0xFF;  //8 columns
    KeyScan_InitStruct.KS_RowSel = 0xFF;  //8 rows
    KeyScan_InitStruct.KS_WorkMode = KS_EVENT_TRIGGER_MODE;
    KeyScan_Init(KeyScan, &KeyScan_InitStruct);
    
  3. Enable Key-Scan interrupt and register Key-Scan interrupt handler.

  4. Enable Key-Scan.

    KeyScan_Cmd(KeyScan, ENABLE);
    
  5. Wait for the Key-Scan interrupt and handle items.

Key Stuck Handling

When a stuck key occurs, perform the following steps:

  1. Configure the pin multiplexing for Key-Scan keypads.

    1. Set the column pins to no pull and configure the column pin multiplexing function.

      PAD_PullCtrl(pad_colx, GPIO_PuPd_NOPULL);
      Pinmux_Config(pad_colx, PINMUX_FUNCTION_KEY_COLx);
      
    2. Set the row pins to pull-up and configure the row pin multiplexing function.

      PAD_PullCtrl(pad_rowx, GPIO_PuPd_UP);
      Pinmux_Config(pad_rowx, PINMUX_FUNCTION_KEY_ROWx);
      
  2. Initialize Key-Scan parameters.

    Select the number of key rows and columns (one bit corresponds to one row or column) according to keypads, and set work mode to Regular Scan Mode, etc.

    KeyScan_StructInit(&KeyScan_InitStruct);
    KeyScan_InitStruct.KS_ColSel = 0xFF;  //8 columns
    KeyScan_InitStruct.KS_RowSel = 0xFF;  //8 rows
    KeyScan_Init(KeyScan, &KeyScan_InitStruct);
    
  3. Enable the Key-Scan stuck key auto-detection function.

    KeyScan_StuckAutoCmd(KeyScan, ENABLE);
    
  4. Set the stuck time threshold and configure the stuck row detection time and interval.

    KeyScan_SetStuckThreshold(KeyScan, 10);  //stuck time threshold: 10ms
    KeyScan_StuckPeriodicalPull(KeyScan,2000,4000);//pull time:2000us, no pull time:4000us
    

5. Enable the Key-Scan stuck key and all default interrupts, and register the Key-Scan interrupt handler. 7. Enable Key-Scan.

KeyScan_Cmd(KeyScan, ENABLE);
  1. Wait for the Key-Scan interrupt and handle it.

    • In stuck key event interrupt, get the row status, set the default row status to indicate stuck row, and mask all the keys of the stuck row.

      row_status = KeyScan_GetStuckRow(KeyScan);
      KeyScan_SetStuckRow(KeyScan, row_status);
      
    • In all default interrupts, reset row default status to initial value, disable stuck key event interrupt and all default interrupts, enable scan event interrupt and all release the interrupt, then other keys except the stuck key can work normally.

      KeyScan_INTConfig(KeyScan, KS_BIT_ALL_DEFAULT_INT_MASK | KS_BIT_STUCK_EVENT_INT_MASK, DISABLE);
      KeyScan_SetStuckRow(KeyScan, 0);
      KeyScan_INTConfig(KeyScan, KS_BIT_ALL_RELEASE_INT_MASK | KS_BIT_SCAN_EVENT_INT_MASK, ENABLE);
      
  2. Key or keys except the stuck key press or release will generate the corresponding interrupts normally.