KEYSCAN#
Example#
The following example demonstrates how to configure and use the KEYSCAN module to detect key presses on a keypad matrix.
// Global variables for key press tracking
volatile bool key_pressed = false;
volatile uint8_t detected_key = 0xFF;
// Interrupt handler for key press detection
void KEYSCAN_IRQHandler(void)
{
uint32_t flags = sl_hal_keyscan_get_interrupts();
if (flags & KEYSCAN_IF_KEY) {
// Read the pressed key data
uint32_t key_data = KEYSCAN->KEYDATA;
uint8_t row = (key_data & _KEYSCAN_KEYDATA_ROW_MASK) >> _KEYSCAN_KEYDATA_ROW_SHIFT;
uint8_t col = (key_data & _KEYSCAN_KEYDATA_COL_MASK) >> _KEYSCAN_KEYDATA_COL_SHIFT;
// Calculate key index (assuming 3 columns)
detected_key = row * 3 + col;
key_pressed = true;
// Clear the interrupt flag
sl_hal_keyscan_clear_interrupts(KEYSCAN_IF_KEY);
}
}
// Example of configuring and using KEYSCAN
void keyscan_example(void)
{
// Configure KEYSCAN with custom settings
sl_hal_keyscan_init_t keyscan_init = SL_HAL_KEYSCAN_INIT_DEFAULT;
// Configure for a 3x4 keypad
keyscan_init.column_number = 3;
keyscan_init.row_number = 4;
// Increase debounce delay to 20ms for improved reliability
keyscan_init.debounce_delay = SL_HAL_KEYSCAN_DELAY_20MS;
// Initialize KEYSCAN with our configuration
sl_hal_keyscan_init(&keyscan_init);
// Enable KEY interrupt
sl_hal_keyscan_enable_interrupts(KEYSCAN_IEN_KEY);
// Enable NVIC interrupt for KEYSCAN
sl_interrupt_manager_enable_irq(KEYSCAN_IRQn);
// Enable KEYSCAN peripheral
sl_hal_keyscan_enable();
sl_hal_keyscan_wait_sync();
// Start keypad scanning
sl_hal_keyscan_start_scan();
// At this point, the KEYSCAN peripheral will automatically scan the keypad
// and generate interrupts when keys are pressed. The interrupt handler will
// process these events.
// Main application code would go here
// ...
// When done with scanning, clean up
sl_hal_keyscan_stop_scan();
sl_hal_keyscan_disable_interrupts(KEYSCAN_IEN_KEY);
sl_hal_keyscan_clear_interrupts(KEYSCAN_IF_KEY);
sl_hal_keyscan_disable();
sl_hal_keyscan_wait_ready();
sl_hal_keyscan_reset();
sl_hal_keyscan_wait_ready();
}
Modules#
Enumerations#
KEYSCAN configuration delay values.
Functions#
Initializes KEYSCAN module.
Enables KEYSCAN module.
Disables KEYSCAN module.
Waits for the KEYSCAN to complete resetting or disabling procedure.
Waits for the KEYSCAN to complete all synchronization of register changes and commands.
Starts KEYSCAN scan.
Stops the KEYSCAN scan.
Restores KEYSCAN to its reset state.
Gets KEYSCAN STATUS register value.
Enables one or more KEYSCAN interrupts.
Disables one or more KEYSCAN interrupts.
Clears one or more pending KEYSCAN interrupts.
Gets pending KEYSCAN interrupt flags.
Gets enabled and pending KEYSCAN interrupt flags.
Sets one or more pending KEYSCAN interrupts from Software.
Macros#
Suggested default values for KEYSCAN configuration structure.
Enumeration Documentation#
sl_hal_keyscan_delay_t#
sl_hal_keyscan_delay_t
KEYSCAN configuration delay values.
Enumerator | |
---|---|
SL_HAL_KEYSCAN_DELAY_2MS | 2 ms delay. |
SL_HAL_KEYSCAN_DELAY_4MS | 4 ms delay. |
SL_HAL_KEYSCAN_DELAY_6MS | 6 ms delay. |
SL_HAL_KEYSCAN_DELAY_8MS | 8 ms delay. |
SL_HAL_KEYSCAN_DELAY_10MS | 10 ms delay. |
SL_HAL_KEYSCAN_DELAY_12MS | 12 ms delay. |
SL_HAL_KEYSCAN_DELAY_14MS | 14 ms delay. |
SL_HAL_KEYSCAN_DELAY_16MS | 16 ms delay. |
SL_HAL_KEYSCAN_DELAY_18MS | 18 ms delay. |
SL_HAL_KEYSCAN_DELAY_20MS | 20 ms delay. |
SL_HAL_KEYSCAN_DELAY_22MS | 22 ms delay. |
SL_HAL_KEYSCAN_DELAY_24MS | 24 ms delay. |
SL_HAL_KEYSCAN_DELAY_26MS | 26 ms delay. |
SL_HAL_KEYSCAN_DELAY_28MS | 28 ms delay. |
SL_HAL_KEYSCAN_DELAY_30MS | 30 ms delay. |
SL_HAL_KEYSCAN_DELAY_32MS | 32 ms delay. |
Function Documentation#
sl_hal_keyscan_init#
void sl_hal_keyscan_init (const sl_hal_keyscan_init_t * init)
Initializes KEYSCAN module.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_hal_keyscan_init_t * | [in] | init | A pointer to the KEYSCAN initialization structure variable. |
sl_hal_keyscan_enable#
void sl_hal_keyscan_enable (void )
Enables KEYSCAN module.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
sl_hal_keyscan_disable#
void sl_hal_keyscan_disable (void )
Disables KEYSCAN module.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Note
The disabling of the module could take some time. This function will not wait for the disabling to finish before returning. Use the function sl_hal_keyscan_wait_ready to wait for the module to be fully disable.
sl_hal_keyscan_wait_ready#
void sl_hal_keyscan_wait_ready (void )
Waits for the KEYSCAN to complete resetting or disabling procedure.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
sl_hal_keyscan_wait_sync#
void sl_hal_keyscan_wait_sync (void )
Waits for the KEYSCAN to complete all synchronization of register changes and commands.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
sl_hal_keyscan_start_scan#
void sl_hal_keyscan_start_scan (void )
Starts KEYSCAN scan.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Note
This function will send a start command to the KEYSCAN peripheral. The sl_hal_keyscan_wait_sync function can be used to wait for the start command to be executed.
This function requires the KEYSCAN to be enabled.
sl_hal_keyscan_stop_scan#
void sl_hal_keyscan_stop_scan (void )
Stops the KEYSCAN scan.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Note
This function will send a stop command to the KEYSCAN peripheral. The sl_hal_keyscan_wait_sync function can be used to wait for the stop command to be executed.
This function requires the KEYSCAN to be enabled.
sl_hal_keyscan_reset#
void sl_hal_keyscan_reset (void )
Restores KEYSCAN to its reset state.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Note
The resetting of the module could take some time. This function will not wait for the resetting to finish before returning. Use the function sl_hal_keyscan_wait_ready to wait for the module to be fully reset.
sl_hal_keyscan_get_status#
uint32_t sl_hal_keyscan_get_status (void )
Gets KEYSCAN STATUS register value.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Returns
Current STATUS register value.
sl_hal_keyscan_enable_interrupts#
void sl_hal_keyscan_enable_interrupts (uint32_t flags)
Enables one or more KEYSCAN interrupts.
Type | Direction | Argument Name | Description |
---|---|---|---|
uint32_t | [in] | flags | KEYSCAN interrupt sources to enable. Use a set of interrupt flags OR-ed together to set multiple interrupt sources. |
Note
Depending on the use, a pending interrupt may already be set prior to enabling the interrupt. To ignore a pending interrupt, consider using sl_hal_keyscan_clear_interrupts prior to enabling the interrupt.
sl_hal_keyscan_disable_interrupts#
void sl_hal_keyscan_disable_interrupts (uint32_t flags)
Disables one or more KEYSCAN interrupts.
Type | Direction | Argument Name | Description |
---|---|---|---|
uint32_t | [in] | flags | KEYSCAN interrupt sources to disable. Use a set of interrupt flags OR-ed together to disable multiple interrupt sources. |
sl_hal_keyscan_clear_interrupts#
void sl_hal_keyscan_clear_interrupts (uint32_t flags)
Clears one or more pending KEYSCAN interrupts.
Type | Direction | Argument Name | Description |
---|---|---|---|
uint32_t | [in] | flags | KEYSCAN interrupt sources to clear. Use a set of interrupt flags OR-ed together to clear multiple interrupt sources. |
sl_hal_keyscan_get_interrupts#
uint32_t sl_hal_keyscan_get_interrupts (void )
Gets pending KEYSCAN interrupt flags.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Note
Event bits are not cleared by using this function.
Returns
Pending KEYSCAN interrupt sources. Returns a set of interrupt flags OR-ed together for multiple interrupt sources.
sl_hal_keyscan_get_enabled_interrupts#
uint32_t sl_hal_keyscan_get_enabled_interrupts (void )
Gets enabled and pending KEYSCAN interrupt flags.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Useful for handling more interrupt sources in the same interrupt handler.
Note
Interrupt flags are not cleared by using this function.
Returns
Pending and enabled KEYSCAN interrupt sources. The return value is the bitwise AND of
the enabled interrupt sources in KEYSCAN_IEN and
the pending interrupt flags KEYSCAN_IF.
sl_hal_keyscan_set_interrupts#
void sl_hal_keyscan_set_interrupts (uint32_t flags)
Sets one or more pending KEYSCAN interrupts from Software.
Type | Direction | Argument Name | Description |
---|---|---|---|
uint32_t | [in] | flags | KEYSCAN interrupt sources to set to pending. Use a set of interrupt flags OR-ed together to set multiple interrupt sources. |