Button
Introduction#
The SI91x button driver is a platform-level software module designed to manage button initialization and reading. It provides a wide range of functions for handling button operations, such as initialization, state monitoring, and callback management for state changes. The driver supports flexible interrupt configurations, allowing detection based on button state, edge transitions, or a combination of both. All button functions are accessed via the generic driver, which internally calls specific functions for the corresponding button.
Configuration#
All button instances are configured using the sl_button_t struct. This struct along with a function definition for initializing Button is automatically generated when an Button is set up using Simplicity Studio's wizard. This struct sl_button_t struct is automatically generated into the following files sl_si91x_button_instances.h and sl_si91x_button_instances.c. The samples below are for a single instance called "btn0".
// sl_si91x_button_instances.c
#include "sl_si91x_button_pin_config.h"
#include "sl_si91x_button_instances.h"
#include "sl_si91x_button_btn0_config.h"
sl_button_t const button_btn0 = {
.port = SL_BUTTON_BTN0_PORT,
.pin = SL_BUTTON_BTN0_PIN,
.button_number = SL_BUTTON_BTN0_NUMBER,
#ifdef SL_BUTTON_BTN0_PAD
.pad = SL_BUTTON_BTN0_PAD,
#endif
.interrupt_config = SL_BUTTON_CONFIG_BTN0_INTR
};
void button_init_instances(void)
{
sl_si91x_button_init(&button_btn0);
}
Note
The sl_si91x_button_instances.c file is shown with only one instance, but if more were in use they would all appear in this .c file.
// sl_si91x_button_instances.h
#ifndef SL_SI91X_BUTTON_INSTANCES_H
#define SL_SI91X_BUTTON_INSTANCES_H
#include "sl_si91x_button.h"
extern const sl_button_t button_btn0;
void button_init_instances(void);
void sl_simple_button_init_instances(void);
#endif // BUTTON_INSTANCES_H
Note
The sl_si91x_button_instances.h file is shown with only one instance, but if more were in use they would all appear in this .h file.
For more information on configuring available parameters, see the respective peripheral example readme document and Peripheral Configuration.
Usage#
After the button structures are defined, button functions can be called by passing an instance of sl_button_t, which will internally redirect to the type-specific version of the function. The commonly used functions include the following:
To initialize the button: sl_si91x_button_init
To return the current state (pressed or released) of the pin associated with a button: sl_si91x_button_pin_state
To toggle the value of a local variable associated with the state of button: sl_si91x_button_state_toggle
A callback that user can modify for the application: sl_si91x_button_isr
The sl_si91x_button_init function is automatically invoked when the application starts.
The button driver supports interrupt mode, with or without debounce functionality.The application can implement sl_si91x_button_pin_state to determine the current state of the button when needed. Additionally, sl_si91x_button_state_toggle can be implemented by the application to toggle the "soft" state, enabling interrupts and their callbacks. The sl_si91x_button_isr is a callback function triggered by a change in the button's state, and the application can implement this function to define the desired actions upon a state change.
Modules#
Macros#
A set of numerical macros for use with the button APIs.
To configure or to trigger the interrupt based on logic levels (LOW, HIGH, or both) or edge changes (rising, falling, or both).
Interrupt on level low/pressed button state can be configured.
Interrupt on level low/pressed and high/released button state can be configured.
Interrupt on level rising edge of the button press.
Interrupt on level falling edge of the button press.
Interrupt on level rising edge and falling edge of the button press.
Button state is pressed.
Button state is released.
Button state is invalid.
Functions#
To initialize the specified button with the given configuration.
To get the current state (pressed or released) of a button.
To return the current state (pressed or released) of the pin associated with a button.
To handle the interrupt when a button changes its state.
To toggle the value of a local variable associated with the state of a button.
To set the state (pressed or released) of the pin associated with a button.
To handle the interrupt when a button changes its state.
Macros Documentation#
HIGH_LEVEL_INTERRUPT#
#define HIGH_LEVEL_INTERRUPTValue:
1U
To configure or to trigger the interrupt based on logic levels (LOW, HIGH, or both) or edge changes (rising, falling, or both).
Interrupt on level high/released button state.
52
of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h
LOW_LEVEL_INTERRUPT#
#define LOW_LEVEL_INTERRUPTValue:
2U
Interrupt on level low/pressed button state can be configured.
53
of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h
HIGH_LEVEL_AND_LOW_LEVEL_INTERRUPT#
#define HIGH_LEVEL_AND_LOW_LEVEL_INTERRUPTValue:
3U
Interrupt on level low/pressed and high/released button state can be configured.
54
of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h
RISE_EDGE_INTERRUPT#
#define RISE_EDGE_INTERRUPTValue:
4U
Interrupt on level rising edge of the button press.
56
of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h
FALL_EDGE_INTERRUPT#
#define FALL_EDGE_INTERRUPTValue:
8U
Interrupt on level falling edge of the button press.
57
of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h
RISE_EDGE_AND_FALL_EDGE_INTERRUPT#
#define RISE_EDGE_AND_FALL_EDGE_INTERRUPTValue:
12U
Interrupt on level rising edge and falling edge of the button press.
58
of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h
BUTTON_PRESSED
#define BUTTON_PRESSEDValue:
1
Button state is pressed.
59
of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h
BUTTON_RELEASED
#define BUTTON_RELEASEDValue:
0
Button state is released.
60
of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h
BUTTON_STATE_INVALID
#define BUTTON_STATE_INVALIDValue:
-1
Button state is invalid.
61
of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h
Function Documentation#
sl_si91x_button_init
void sl_si91x_button_init (const sl_button_t * handle)
To initialize the specified button with the given configuration.
[in] | handle | Pointer to an sl_button_t button configuration structure containing the specific button details. |
This function configures the GPIO settings and interrupt configurations for the specified button. It handles different configurations based on the board version and button number. For UULP (Ultra-Low Power) and M4 (High-Performance) GPIOs, it sets the pin direction, pin mode, and interrupt settings.
The button configuration structure must be properly initialized before calling this function.
The specified button will be configured and ready for use with the specified settings.
Note
This function handles different configurations based on the board version and button number. For UULP (Ultra-Low Power) and M4 (High-Performance) GPIOs, it sets the pin direction, pin mode, and interrupt settings.
92
of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h
sl_si91x_button_state_get
int8_t sl_si91x_button_state_get (uint8_t pin)
To get the current state (pressed or released) of a button.
[in] | pin | The button pin being queried, either SL_BUTTON_BTN0_PIN or SL_BUTTON_BTN1_PIN as defined. |
This function returns the shadow state of the button rather than reading the actual state of the pin. It is correlated with the interrupts and their callbacks, providing a "soft" state.
Returns
int8_t The state of the button:
BUTTON_PRESSED if the button is pressed.
BUTTON_RELEASED if the button is not pressed.
BUTTON_STATE_INVALID if the pin does not match any known button.
108
of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h
sl_si91x_button_pin_state
int8_t sl_si91x_button_pin_state (uint8_t pin)
To return the current state (pressed or released) of the pin associated with a button.
[in] | pin | The button pin being queried, either BUTTON0 pin or BUTTON1 pin as defined. |
This function reads the actual state of the pin and can be used on startup to determine the initial position of the buttons.
Returns
int8_t The state of the button:
BUTTON_PRESSED if the button is pressed.
BUTTON_RELEASED if the button is not pressed.
123
of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h
sl_si91x_button_pin_isr
void sl_si91x_button_pin_isr (uint8_t pin, uint8_t state)
To handle the interrupt when a button changes its state.
[in] | pin | The button pin which has changed state, either BUTTON0 pin or BUTTON1 pin as defined. |
[out] | state | The new state of the button referenced by the button parameter, either BUTTON_PRESSED if the button has been pressed or BUTTON_RELEASED if the button has been released. |
This callback is called in the interrupt context whenever a button changes its state. It must be implemented by the application to handle the functionality in response to the button state change.
Note
This function should contain the functionality to be executed in response to changes of state in each of the buttons or callbacks to the appropriate functionality.
142
of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h
sl_si91x_button_state_toggle
void sl_si91x_button_state_toggle (uint8_t pin)
To toggle the value of a local variable associated with the state of a button.
[in] | pin | The button pin which has changed state, either BUTTON0 pin or BUTTON1 pin as defined. |
This function toggles the state of a local variable that tracks the state of the specified button. It must be implemented by the application to handle the functionality in response to the button state change.
Note
This function should contain the functionality to be executed in response to changes of state in each of the buttons or callbacks to the appropriate functionality.
156
of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h
sl_si91x_button_state_set
void sl_si91x_button_state_set (uint8_t pin, int8_t state)
To set the state (pressed or released) of the pin associated with a button.
[in] | pin | The button pin which has changed state, either BUTTON0 pin or BUTTON1 pin as defined. |
[in] | state | The state of the button to be set:
|
This function sets the state of the specified button pin. It must be implemented by the application to handle the functionality in response to changes of state in each of the buttons or callbacks to the appropriate functionality.
Note
This function should contain the functionality to be executed in response to changes of state in each of the buttons or callbacks to the appropriate functionality.
174
of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h
sl_si91x_button_isr
void sl_si91x_button_isr (uint8_t pin, int8_t state)
To handle the interrupt when a button changes its state.
[in] | pin | The button pin which has changed state, either BUTTON0 pin or BUTTON1 pin as defined. |
[in] | state | The new state of the button referenced by the button parameter, either BUTTON_PRESSED if the button has been pressed or BUTTON_RELEASED if the button has been released. |
This callback is called in the interrupt context whenever a button changes its state. It must be implemented by the application to handle the functionality in response to the button state change.
Note
This function should contain the functionality to be executed in response to changes of state in each of the buttons or callbacks to the appropriate functionality.
193
of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h