GPIO - General Purpose Input Output#
General Purpose Input Output peripheral.
Introduction#
This module contains functions to control the GPIO peripheral of Silicon Labs 32-bit MCUs and SoCs. The GPIO peripheral is used for interrupt configuration, pin configuration and direct pin manipulation as well as routing for peripheral pin connections.
Example#
The following example demonstrates the basic GPIO operations available in the Silicon Labs GPIO API. This example shows how to:
Define and configure a GPIO pin as an output
Control pin state (high, low, toggle)
Read the pin's input state
Configure and enable external interrupts
This code can be used as a starting point for controlling LEDs, reading button inputs, or configuring interrupt-driven GPIO events in your application.
{
// Define GPIO pin
sl_gpio_t led_pin = {
.port = 0, // Port A
.pin = 0 // Pin 0
};
// Configure pin as push-pull output
sl_hal_gpio_set_pin_mode(&led_pin, SL_GPIO_MODE_PUSH_PULL, false);
// Set pin high
sl_hal_gpio_set_pin(&led_pin);
// Wait a moment
for(volatile int i = 0; i < 100000; i++);
// Clear pin (set low)
sl_hal_gpio_clear_pin(&led_pin);
// Toggle pin
sl_hal_gpio_toggle_pin(&led_pin);
// Read pin input
bool pin_state = sl_hal_gpio_get_pin_input(&led_pin);
// Configure external interrupt
int32_t int_no = sl_hal_gpio_configure_external_interrupt(&led_pin,
SL_HAL_GPIO_INTERRUPT_UNAVAILABLE,
SL_GPIO_INTERRUPT_RISING_EDGE);
// Enable interrupt
sl_hal_gpio_enable_interrupts(1 << int_no);
}
Functions#
Set the mode for a GPIO pin.
Get the mode for a GPIO pin.
Configure the GPIO external pin interrupt by connecting external interrupt id with gpio pin.
Enable GPIO pin wake-up from EM4.
Configure EM4WU pins as external level-sensitive interrupts.
Lock the GPIO configuration.
Unlock the GPIO configuration.
Gets the GPIO configuration state.
Set a single pin in GPIO data out register to 1.
Set bits GPIO data out register to 1.
Set GPIO port data out register.
Set slewrate for pins on a GPIO port which are configured into normal modes.
Get slewrate for pins on a GPIO port.
Set slewrate for pins on a GPIO port which are configured into alternate modes.
Get slewrate for pins on a GPIO port which are configured into alternate modes.
Set a single pin in GPIO data out port register to 0.
Set bits in DOUT register for a port to 0.
Read the pad value for a single pin in a GPIO port.
Get current setting for a pin in a GPIO port data out register.
Read the pad values for GPIO port.
Get current setting for a GPIO port data out register.
Toggle a single pin in GPIO port data out register.
Toggle pins in GPIO port data out register.
Enable one or more GPIO interrupts.
Disable one or more GPIO interrupts.
Clear one or more pending GPIO interrupts.
Set one or more pending GPIO interrupts from SW.
Get pending GPIO interrupts.
Get enabled GPIO interrupts.
Get enabled and pending GPIO interrupt flags.
The available external interrupt number getter.
The available em4 wakeup interrupt number getter.
Disable GPIO pin wake-up from EM4.
Enable GPIO pin retention of output enable, output value, pull enable, and pull direction in EM4.
Check which GPIO pin(s) that caused a wake-up from EM4.
Enable/Disable serial wire output pin.
Enable/disable serial wire clock pin.
Enable/disable serial wire data I/O pin.
Macros#
Define for port specific pin mask.
Define for port specific pin count.
Validation of port.
Validation of port and pin.
Max interrupt lines for external and EM4 interrupts.
Shift value for EM4WUEN.
Masks for even and odd interrupt bits.
Validation of mode.
Validation of interrupt number and pin.
Invalid Slewrate.
Function Documentation#
sl_hal_gpio_set_pin_mode#
void sl_hal_gpio_set_pin_mode (const sl_gpio_t * gpio, sl_gpio_mode_t mode, bool output_value)
Set the mode for a GPIO pin.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_gpio_t * | [in] | gpio | Pointer to GPIO structure with port and pin |
sl_gpio_mode_t | [in] | mode | The desired pin mode. |
bool | [in] | output_value | A value to set for the pin in the DOUT register. The DOUT setting is important for some input mode configurations to determine the pull-up/down direction. |
sl_hal_gpio_get_pin_mode#
sl_gpio_mode_t sl_hal_gpio_get_pin_mode (const sl_gpio_t * gpio)
Get the mode for a GPIO pin.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_gpio_t * | [in] | gpio | Pointer to GPIO structure with port and pin |
Returns
Return the pin mode.
sl_hal_gpio_configure_external_interrupt#
int32_t sl_hal_gpio_configure_external_interrupt (const sl_gpio_t * gpio, int32_t int_no, sl_gpio_interrupt_flag_t flags)
Configure the GPIO external pin interrupt by connecting external interrupt id with gpio pin.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_gpio_t * | [in] | gpio | Pointer to GPIO structure with port and pin |
int32_t | [in] | int_no | The interrupt number to trigger. |
sl_gpio_interrupt_flag_t | [in] | flags | Interrupt configuration flags. sl_hal_gpio_interrupt_flag_t for more information. |
Note
This function configure the pin interrupt with pin ,port and external interrupt id as input. If external interrupt id is provided as input it will be considered as the input or else available interrupt number will be generated by looping through the interrupt group and will be used. User can provide SL_HAL_GPIO_INTERRUPT_UNAVAILABLE if user don't want to provide interrupt id.
the pin number can be selected freely within a group. Interrupt numbers are divided into 4 groups (int_no / 4) and valid pin number within the interrupt groups are: 0: pins 0-3 (interrupt number 0-3) 1: pins 4-7 (interrupt number 4-7) 2: pins 8-11 (interrupt number 8-11) 3: pins 12-15 (interrupt number 12-15)
It is recommended to disable interrupts before configuring the GPIO pin interrupt. See sl_hal_gpio_disable_interrupts() for more information. The GPIO interrupt handler must be in place before enabling the interrupt. Notice that any pending interrupt for the selected interrupt is cleared by this function. Notice that only interrupt will be configured by this function. It is not enabled. It is recommended to enable interrupts after configuring the GPIO pin interrupt if needed. See sl_hal_gpio_enable_interrupts() for more information.
Returns
Return the available interrupt number
sl_hal_gpio_enable_pin_em4_wakeup#
void sl_hal_gpio_enable_pin_em4_wakeup (uint32_t pinmask, uint32_t polaritymask)
Enable GPIO pin wake-up from EM4.
Type | Direction | Argument Name | Description |
---|---|---|---|
uint32_t | [in] | pinmask | A bitmask containing the bitwise logic OR of which GPIO pin(s) to enable. |
uint32_t | [in] | polaritymask | A bitmask containing the bitwise logic OR of GPIO pin(s) wake-up polarity. |
When the function exits, EM4 mode can be safely entered.
Note
It is assumed that the GPIO pin modes are set correctly. Valid modes are SL_GPIO_MODE_INPUT and SL_GPIO_MODE_INPUT_PULL.
sl_hal_gpio_configure_wakeup_em4_external_interrupt#
int32_t sl_hal_gpio_configure_wakeup_em4_external_interrupt (const sl_gpio_t * gpio, int32_t int_no, bool polarity)
Configure EM4WU pins as external level-sensitive interrupts.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_gpio_t * | [in] | gpio | Pointer to GPIO structure with port and pin |
int32_t | [in] | int_no | The EM4WU interrupt number to trigger. |
bool | [in] | polarity | true = Active high level-sensitive interrupt. false = Active low level-sensitive interrupt. |
Note
It is recommended to disable interrupts before configuring the GPIO pin interrupt. See sl_hal_gpio_disable_interrupts() for more information. The provided port, pin and int_no inputs should be valid EM4 related parameters because there are dedicated port, pin and EM4 Wakeup interrupt combination for configuring the port, pin for EM4 functionality. User can provide SL_HAL_GPIO_INTERRUPT_UNAVAILABLE if user don't want to provide interrupt id. The GPIO interrupt handler must be in place before enabling the interrupt. Notice that any pending interrupt for the selected interrupt is cleared by this function. Notice that any only EM4WU interrupt is configured by this function. It is not enabled. It is recommended to enable interrupts after configuring the GPIO pin interrupt if needed. See sl_hal_gpio_enable_interrupts() for more information.
Returns
Return the available EM4WU interrupt number
sl_hal_gpio_lock#
void sl_hal_gpio_lock (void )
Lock the GPIO configuration.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Note
Configuration lock affects the GPIO_Px_MODEL, GPIO_Px_MODEH, GPIO_Px_CTRL, GPIO_Px_PINLOCKN, GPIO_EXTIPSELL, GPIO_EXTIPSELH, GPIO_EXTIPINSELL, GPIO_EXTIPINSELH, GPIO_INSENSE, GPIO_ROUTE, GPIO_ROUTEPEN, and GPIO_ROUTELOC0 registers when they are present on a specific device.
Unwanted or accidental changes to GPIO configuration can be avoided by using the configuration lock register. Any value other than 0xA534 written to GPIO_LOCK enables the configuration lock. Pins are unlocked by a reset or by writing 0xA534 to the GPIO_LOCK register.
sl_hal_gpio_unlock#
void sl_hal_gpio_unlock (void )
Unlock the GPIO configuration.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Note
Configuration lock affects the GPIO_Px_MODEL, GPIO_Px_MODEH, GPIO_Px_CTRL, GPIO_Px_PINLOCKN, GPIO_EXTIPSELL, GPIO_EXTIPSELH, GPIO_EXTIPINSELL, GPIO_EXTIPINSELH, GPIO_INSENSE, GPIO_ROUTE, GPIO_ROUTEPEN, and GPIO_ROUTELOC0 registers when they are present on a specific device.
Unwanted or accidental changes to GPIO configuration can be avoided by using the configuration lock register. Any value other than 0xA534 written to GPIO_LOCK enables the configuration lock. Pins are unlocked by a reset or by writing 0xA534 to the GPIO_LOCK register.
sl_hal_gpio_get_lock_status#
uint32_t sl_hal_gpio_get_lock_status (void )
Gets the GPIO configuration state.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Returns
Return the GPIO lock state.
sl_hal_gpio_set_pin#
void sl_hal_gpio_set_pin (const sl_gpio_t * gpio)
Set a single pin in GPIO data out register to 1.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_gpio_t * | [in] | gpio | Pointer to GPIO structure with port and pin |
sl_hal_gpio_set_port#
void sl_hal_gpio_set_port (sl_gpio_port_t port, uint32_t pins)
Set bits GPIO data out register to 1.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_gpio_port_t | [in] | port | The GPIO port to access. |
uint32_t | [in] | pins | Bit mask for bits to set to 1 in DOUT register. |
sl_hal_gpio_set_port_value#
void sl_hal_gpio_set_port_value (sl_gpio_port_t port, uint32_t val, uint32_t mask)
Set GPIO port data out register.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_gpio_port_t | [in] | port | The GPIO port to access. |
uint32_t | [in] | val | Value to write to port data out register. |
uint32_t | [in] | mask | Mask indicating which bits to modify. |
sl_hal_gpio_set_slew_rate#
sl_status_t sl_hal_gpio_set_slew_rate (const sl_gpio_t * gpio, uint8_t slewrate)
Set slewrate for pins on a GPIO port which are configured into normal modes.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_gpio_t * | [in] | gpio | Pointer to GPIO structure with port and pin |
uint8_t | [in] | slewrate | The slewrate to configure for pins on this GPIO port. |
Returns
SL_STATUS_OK if there's no error. SL_STATUS_INVALID_PARAMETER if port is invalid.
sl_hal_gpio_get_slew_rate#
sl_status_t sl_hal_gpio_get_slew_rate (const sl_gpio_t * gpio, uint8_t * slewrate)
Get slewrate for pins on a GPIO port.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_gpio_t * | [in] | gpio | Pointer to GPIO structure with port and pin |
uint8_t * | [out] | slewrate | Pointer to store the slewrate of selected GPIO port. |
Returns
SL_STATUS_OK if there's no error. SL_STATUS_INVALID_PARAMETER if port is invalid.
sl_hal_gpio_set_slew_rate_alternate#
void sl_hal_gpio_set_slew_rate_alternate (sl_gpio_port_t port, uint8_t slewrate_alt)
Set slewrate for pins on a GPIO port which are configured into alternate modes.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_gpio_port_t | [in] | port | The GPIO port to configure. |
uint8_t | [in] | slewrate_alt | The slewrate to configure for pins using alternate modes on this GPIO port. |
sl_hal_gpio_get_slew_rate_alternate#
uint8_t sl_hal_gpio_get_slew_rate_alternate (sl_gpio_port_t port)
Get slewrate for pins on a GPIO port which are configured into alternate modes.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_gpio_port_t | [in] | port | The GPIO port to access to get slew rate. |
Returns
Return the alternate slewrate setting for selected GPIO port.
sl_hal_gpio_clear_pin#
void sl_hal_gpio_clear_pin (const sl_gpio_t * gpio)
Set a single pin in GPIO data out port register to 0.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_gpio_t * | [in] | gpio | Pointer to GPIO structure with port and pin |
sl_hal_gpio_clear_port#
void sl_hal_gpio_clear_port (sl_gpio_port_t port, uint32_t pins)
Set bits in DOUT register for a port to 0.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_gpio_port_t | [in] | port | The GPIO port to access. |
uint32_t | [in] | pins | Bit mask for bits to clear in DOUT register. |
sl_hal_gpio_get_pin_input#
bool sl_hal_gpio_get_pin_input (const sl_gpio_t * gpio)
Read the pad value for a single pin in a GPIO port.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_gpio_t * | [in] | gpio | Pointer to GPIO structure with port and pin. |
Returns
The pin value, 0 or 1.
sl_hal_gpio_get_pin_output#
bool sl_hal_gpio_get_pin_output (const sl_gpio_t * gpio)
Get current setting for a pin in a GPIO port data out register.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_gpio_t * | [in] | gpio | Pointer to GPIO structure with port and pin. |
Returns
The DOUT setting for the requested pin, 0 or 1.
sl_hal_gpio_get_port_input#
uint32_t sl_hal_gpio_get_port_input (sl_gpio_port_t port)
Read the pad values for GPIO port.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_gpio_port_t | [in] | port | The GPIO port to access. |
Returns
The pad values for the GPIO port.
sl_hal_gpio_get_port_output#
uint32_t sl_hal_gpio_get_port_output (sl_gpio_port_t port)
Get current setting for a GPIO port data out register.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_gpio_port_t | [in] | port | The GPIO port to access. |
Returns
The data out setting for the requested port.
sl_hal_gpio_toggle_pin#
void sl_hal_gpio_toggle_pin (const sl_gpio_t * gpio)
Toggle a single pin in GPIO port data out register.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_gpio_t * | [in] | gpio | Pointer to GPIO structure with port and pin. |
sl_hal_gpio_toggle_port#
void sl_hal_gpio_toggle_port (sl_gpio_port_t port, uint32_t pins)
Toggle pins in GPIO port data out register.
Type | Direction | Argument Name | Description |
---|---|---|---|
sl_gpio_port_t | [in] | port | The GPIO port to access. |
uint32_t | [in] | pins | Bit mask with pins to toggle. |
sl_hal_gpio_enable_interrupts#
void sl_hal_gpio_enable_interrupts (uint32_t flags)
Enable one or more GPIO interrupts.
Type | Direction | Argument Name | Description |
---|---|---|---|
uint32_t | [in] | flags | GPIO interrupt sources to enable. |
sl_hal_gpio_disable_interrupts#
void sl_hal_gpio_disable_interrupts (uint32_t flags)
Disable one or more GPIO interrupts.
Type | Direction | Argument Name | Description |
---|---|---|---|
uint32_t | [in] | flags | GPIO interrupt sources to disable. |
sl_hal_gpio_clear_interrupts#
void sl_hal_gpio_clear_interrupts (uint32_t flags)
Clear one or more pending GPIO interrupts.
Type | Direction | Argument Name | Description |
---|---|---|---|
uint32_t | [in] | flags | Bitwise logic OR of GPIO interrupt sources to clear. |
sl_hal_gpio_set_interrupts#
void sl_hal_gpio_set_interrupts (uint32_t flags)
Set one or more pending GPIO interrupts from SW.
Type | Direction | Argument Name | Description |
---|---|---|---|
uint32_t | [in] | flags | GPIO interrupt sources to set to pending. |
sl_hal_gpio_get_pending_interrupts#
uint32_t sl_hal_gpio_get_pending_interrupts (void )
Get pending GPIO interrupts.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Returns
GPIO interrupt sources pending.
sl_hal_gpio_get_enabled_interrupts#
uint32_t sl_hal_gpio_get_enabled_interrupts (void )
Get enabled GPIO interrupts.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Returns
Enabled GPIO interrupt sources.
sl_hal_gpio_get_enabled_pending_interrupts#
uint32_t sl_hal_gpio_get_enabled_pending_interrupts (void )
Get enabled and pending GPIO interrupt flags.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Returns
Enabled and pending interrupt sources.
Note
Useful for handling more interrupt sources in the same interrupt handler.
sl_hal_gpio_get_external_interrupt_number#
int32_t sl_hal_gpio_get_external_interrupt_number (uint8_t pin, uint32_t enabled_interrupts_mask)
The available external interrupt number getter.
Type | Direction | Argument Name | Description |
---|---|---|---|
uint8_t | [in] | pin | The GPIO pin to access. |
uint32_t | [in] | enabled_interrupts_mask | Contains enabled GPIO interrupts mask. |
Returns
The available interrupt number based on interrupt and pin grouping.
sl_hal_gpio_get_em4_interrupt_number#
int32_t sl_hal_gpio_get_em4_interrupt_number (const sl_gpio_t * gpio)
The available em4 wakeup interrupt number getter.
Type | Direction | Argument Name | Description |
---|---|---|---|
const sl_gpio_t * | [in] | gpio | Pointer to GPIO structure with port and pin. |
Returns
The available em4 wakeup interrupt number based on associated port and pin.
sl_hal_gpio_disable_pin_em4_wakeup#
void sl_hal_gpio_disable_pin_em4_wakeup (uint32_t pinmask)
Disable GPIO pin wake-up from EM4.
Type | Direction | Argument Name | Description |
---|---|---|---|
uint32_t | [in] | pinmask | Bit mask containing the bitwise logic OR of which GPIO pin(s) to disable. |
sl_hal_gpio_set_pin_em4_retention#
void sl_hal_gpio_set_pin_em4_retention (bool enable)
Enable GPIO pin retention of output enable, output value, pull enable, and pull direction in EM4.
Type | Direction | Argument Name | Description |
---|---|---|---|
bool | [in] | enable | true - enable EM4 pin retention. false - disable EM4 pin retention. |
Note
The behavior of this function depends on the configured GPIO retention mode. If the GPIO retention mode is configured to be "SWUNLATCH" then this function will not change anything. If the retention mode is anything else then this function will set the GPIO retention mode to "EM4EXIT" when the enable argument is true, and "Disabled" when false.
sl_hal_gpio_get_pin_em4_wakeup_cause#
uint32_t sl_hal_gpio_get_pin_em4_wakeup_cause (void )
Check which GPIO pin(s) that caused a wake-up from EM4.
Type | Direction | Argument Name | Description |
---|---|---|---|
void | N/A |
Returns
Bit mask containing the bitwise logic OR of which GPIO pin(s) caused the wake-up.
sl_hal_gpio_enable_debug_swo#
void sl_hal_gpio_enable_debug_swo (bool enable)
Enable/Disable serial wire output pin.
Type | Direction | Argument Name | Description |
---|---|---|---|
bool | [in] | enable | false - disable serial wire viewer pin (default after reset). true - enable serial wire viewer pin. |
Note
Enabling this pin is not sufficient to fully enable serial wire output, which is also dependent on issues outside the GPIO module.
If debug port is locked, SWO pin is not disabled automatically. To avoid information leakage through SWO, disable SWO pin after locking debug port.
sl_hal_gpio_enable_debug_swd_clk#
void sl_hal_gpio_enable_debug_swd_clk (bool enable)
Enable/disable serial wire clock pin.
Type | Direction | Argument Name | Description |
---|---|---|---|
bool | [in] | enable | false - disable serial wire clock. true - enable serial wire clock (default after reset). |
Note
Disabling SWDClk will disable the debug interface, which may result in a lockout if done early in startup (before debugger is able to halt core).
sl_hal_gpio_enable_debug_swd_io#
void sl_hal_gpio_enable_debug_swd_io (bool enable)
Enable/disable serial wire data I/O pin.
Type | Direction | Argument Name | Description |
---|---|---|---|
bool | [in] | enable | false - disable serial wire data pin. true - enable serial wire data pin (default after reset). |
Note
Disabling SWDClk will disable the debug interface, which may result in a lockout if done early in startup (before debugger is able to halt core).