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:

  1. To initialize the button: sl_si91x_button_init

  2. To return the current state (pressed or released) of the pin associated with a button: sl_si91x_button_pin_state

  3. To toggle the value of a local variable associated with the state of button: sl_si91x_button_state_toggle

  4. 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#

sl_button_t

Macros#

A set of numerical macros for use with the button APIs.

#define

To configure or to trigger the interrupt based on logic levels (LOW, HIGH, or both) or edge changes (rising, falling, or both).

#define

Interrupt on level low/pressed button state can be configured.

#define

Interrupt on level low/pressed and high/released button state can be configured.

#define

Interrupt on level rising edge of the button press.

#define

Interrupt on level falling edge of the button press.

#define

Interrupt on level rising edge and falling edge of the button press.

#define

Button state is pressed.

#define

Button state is released.

#define

Button state is invalid.

Functions#

void
sl_si91x_button_init(const sl_button_t *handle)

To initialize the specified button with the given configuration.

int8_t

To get the current state (pressed or released) of a button.

int8_t

To return the current state (pressed or released) of the pin associated with a button.

void
sl_si91x_button_pin_isr(uint8_t pin, uint8_t state)

To handle the interrupt when a button changes its state.

void

To toggle the value of a local variable associated with the state of a button.

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.

void
sl_si91x_button_isr(uint8_t pin, int8_t state)

To handle the interrupt when a button changes its state.

Macros Documentation#

HIGH_LEVEL_INTERRUPT#

#define HIGH_LEVEL_INTERRUPT
Value:
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.


Definition at line 52 of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h

LOW_LEVEL_INTERRUPT#

#define LOW_LEVEL_INTERRUPT
Value:
2U

Interrupt on level low/pressed button state can be configured.


Definition at line 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_INTERRUPT
Value:
  3U

Interrupt on level low/pressed and high/released button state can be configured.


Definition at line 54 of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h

RISE_EDGE_INTERRUPT#

#define RISE_EDGE_INTERRUPT
Value:
4U

Interrupt on level rising edge of the button press.


Definition at line 56 of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h

FALL_EDGE_INTERRUPT#

#define FALL_EDGE_INTERRUPT
Value:
8U

Interrupt on level falling edge of the button press.


Definition at line 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_INTERRUPT
Value:
12U

Interrupt on level rising edge and falling edge of the button press.


Definition at line 58 of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h

BUTTON_PRESSED#

#define BUTTON_PRESSED
Value:
1

Button state is pressed.


Definition at line 59 of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h

BUTTON_RELEASED#

#define BUTTON_RELEASED
Value:
0

Button state is released.


Definition at line 60 of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h

BUTTON_STATE_INVALID#

#define BUTTON_STATE_INVALID
Value:
-1

Button state is invalid.


Definition at line 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.

Parameters
[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.


Definition at line 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.

Parameters
[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


Definition at line 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.

Parameters
[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


Definition at line 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.

Parameters
[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.


Definition at line 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.

Parameters
[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.


Definition at line 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.

Parameters
[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.


Definition at line 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.

Parameters
[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.


Definition at line 193 of file components/device/silabs/si91x/mcu/drivers/hardware_drivers/button/inc/sl_si91x_button.h