Contiki-NG
Files | Data Structures | Macros | Typedefs
GPIO Hardware Abstraction Layer

The GPIO HAL provides a set of common functions that can be used in a platform-independent fashion. More...

Files

file  gpio-hal.c
 Implementation of the platform-independent aspects of the GPIO HAL.
 
file  gpio-hal.h
 Header file for the GPIO HAL.
 

Data Structures

struct  gpio_hal_event_handler_s
 Datatype for GPIO event handlers. More...
 

Macros

#define GPIO_HAL_ARCH_SW_TOGGLE   1
 Specifies whether software-based pin toggle is required. More...
 
#define GPIO_HAL_PIN_UNKNOWN   0xFF
 Unknown GPIO. More...
 

Typedefs

typedef uint8_t gpio_hal_pin_t
 GPIO pin number representation.
 
typedef uint32_t gpio_hal_pin_cfg_t
 GPIO pin configuration. More...
 
typedef uint32_t gpio_hal_pin_mask_t
 GPIO pin mask representation.
 
typedef struct gpio_hal_event_handler_s gpio_hal_event_handler_t
 Datatype for GPIO event handlers. More...
 

Core GPIO functions

Functions implemented by the HAL itself

void gpio_hal_register_handler (gpio_hal_event_handler_t *handler)
 Register a function to be called whenever a pin triggers an event. More...
 
void gpio_hal_event_handler (gpio_hal_pin_mask_t pins)
 The platform-independent GPIO event handler. More...
 
void gpio_hal_init (void)
 Initialise the GPIO HAL.
 
#define gpio_hal_pin_to_mask(pin)   ((gpio_hal_pin_mask_t)1 << (pin))
 Convert a pin to a pin mask. More...
 

Functions to be provided by the platform

All the functions below must be provided by the platform's developer.

The HAL offers the developer a number of options of how to provide the required functionality.

  • The developer can provide a symbol. For example, the developer can create a .c file and implement a function called gpio_hal_arch_set_pin()
  • The developer can provide a function-like macro that has the same name as the function declared here. In this scenario, the declaration here will be removed by the pre-processor. For example, the developer can do something like:
#define gpio_hal_arch_write_pin(p, v) platform_sdk_function(p, v)
  • The developer can provide a static inline implementation. For this to work, the developer can do something like:
#define gpio_hal_arch_set_pin(p) set_pin(p)
static inline void set_pin(gpio_hal_pin_t pin) { ... }

In the latter two cases, the developer will likely provide implementations in a header file. In this scenario, one of the platform's configuration files must define GPIO_HAL_CONF_ARCH_HDR_PATH to the name of this header file. For example:

#define GPIO_HAL_CONF_ARCH_HDR_PATH "dev/gpio-hal-arch.h"
void gpio_hal_arch_init (void)
 Perform architecture specific gpio initaliaztion. More...
 
void gpio_hal_arch_interrupt_enable (gpio_hal_pin_t pin)
 Enable interrupts for a gpio pin. More...
 
void gpio_hal_arch_interrupt_disable (gpio_hal_pin_t pin)
 Disable interrupts for a gpio pin. More...
 
void gpio_hal_arch_pin_cfg_set (gpio_hal_pin_t pin, gpio_hal_pin_cfg_t cfg)
 Configure a gpio pin. More...
 
gpio_hal_pin_cfg_t gpio_hal_arch_pin_cfg_get (gpio_hal_pin_t pin)
 Read the configuration of a GPIO pin. More...
 
void gpio_hal_arch_pin_set_input (gpio_hal_pin_t pin)
 Configure a pin as GPIO input. More...
 
void gpio_hal_arch_pin_set_output (gpio_hal_pin_t pin)
 Configure a pin as GPIO output. More...
 
void gpio_hal_arch_set_pin (gpio_hal_pin_t pin)
 Set a GPIO pin to logical high. More...
 
void gpio_hal_arch_clear_pin (gpio_hal_pin_t pin)
 Clear a GPIO pin (logical low) More...
 
void gpio_hal_arch_toggle_pin (gpio_hal_pin_t pin)
 Toggle a GPIO pin. More...
 
uint8_t gpio_hal_arch_read_pin (gpio_hal_pin_t pin)
 Read a GPIO pin. More...
 
void gpio_hal_arch_write_pin (gpio_hal_pin_t pin, uint8_t value)
 Write a GPIO pin. More...
 
void gpio_hal_arch_set_pins (gpio_hal_pin_mask_t pins)
 Set multiple pins to logical high. More...
 
void gpio_hal_arch_clear_pins (gpio_hal_pin_mask_t pins)
 Clear multiple pins to logical low. More...
 
void gpio_hal_arch_toggle_pins (gpio_hal_pin_mask_t pins)
 Toggle multiple pins. More...
 
gpio_hal_pin_mask_t gpio_hal_arch_read_pins (gpio_hal_pin_mask_t pins)
 Read multiple pins. More...
 
void gpio_hal_arch_write_pins (gpio_hal_pin_mask_t pins, gpio_hal_pin_mask_t value)
 Write multiple pins. More...
 

Detailed Description

The GPIO HAL provides a set of common functions that can be used in a platform-independent fashion.

Internally, the GPIO HAL handles edge detection handling and also provides fallback functions for GPIO pin toggling if the hardware does not have a direct method of toggling pins through direct register access.

Macro Definition Documentation

◆ GPIO_HAL_ARCH_SW_TOGGLE

#define GPIO_HAL_ARCH_SW_TOGGLE   1

Specifies whether software-based pin toggle is required.

Some MCUs allow GPIO pin toggling via direct register access. For these MCUs, define GPIO_HAL_CONF_ARCH_SW_TOGGLE to 0 and then implement gpio_hal_arch_toggle_pin() and gpio_hal_arch_toggle_pins()

See also
gpio_hal_arch_toggle_pin()
gpio_hal_arch_toggle_pins()

Definition at line 71 of file gpio-hal.h.

◆ gpio_hal_pin_to_mask

#define gpio_hal_pin_to_mask (   pin)    ((gpio_hal_pin_mask_t)1 << (pin))

Convert a pin to a pin mask.

Parameters
pinThe pin
Returns
The corresponding mask

Definition at line 192 of file gpio-hal.h.

◆ GPIO_HAL_PIN_UNKNOWN

#define GPIO_HAL_PIN_UNKNOWN   0xFF

Unknown GPIO.

A default GPIO value for unknown GPIO

Definition at line 140 of file gpio-hal.h.

Referenced by ext_flash_open().

Typedef Documentation

◆ gpio_hal_event_handler_t

Datatype for GPIO event handlers.

A GPIO event handler is a function that gets called whenever a pin triggers an event. The same handler can be registered to handle events for more than one pin by setting the respective pin's position but in pin_mask.

◆ gpio_hal_pin_cfg_t

typedef uint32_t gpio_hal_pin_cfg_t

GPIO pin configuration.

A logical representation of a pin's configuration. It is an OR combination of GPIO_HAL_PIN_CFG_xyz macros.

Definition at line 85 of file gpio-hal.h.

Function Documentation

◆ gpio_hal_arch_clear_pin()

void gpio_hal_arch_clear_pin ( gpio_hal_pin_t  pin)

Clear a GPIO pin (logical low)

Parameters
pinThe GPIO pin number (0...GPIO_HAL_PIN_COUNT - 1)

It is the platform developer's responsibility to provide an implementation.

The implementation can be provided as a global symbol, an inline function or a function-like macro, as described above.

Definition at line 132 of file gpio-hal-arch.c.

◆ gpio_hal_arch_clear_pins()

void gpio_hal_arch_clear_pins ( gpio_hal_pin_mask_t  pins)

Clear multiple pins to logical low.

Parameters
pinsAn ORd pin mask of the pins to clear

A pin will be set to logical low if its position in pins is set. For example you can clear pins 0 and 3 by passing 0x09.

It is the platform developer's responsibility to provide an implementation.

The implementation can be provided as a global symbol, an inline function or a function-like macro, as described above.

Definition at line 167 of file gpio-hal-arch.c.

References GPIO_A_BASE, GPIO_B_BASE, GPIO_C_BASE, GPIO_CLR_PIN, and GPIO_D_BASE.

◆ gpio_hal_arch_init()

void gpio_hal_arch_init ( void  )

Perform architecture specific gpio initaliaztion.

It is the platform developer's responsibility to provide an implementation.

The implementation can be provided as a global symbol, an inline function or a function-like macro, as described above.

Definition at line 46 of file gpio-hal-arch.c.

◆ gpio_hal_arch_interrupt_disable()

void gpio_hal_arch_interrupt_disable ( gpio_hal_pin_t  pin)

Disable interrupts for a gpio pin.

Parameters
pinThe GPIO pin number (0...GPIO_HAL_PIN_COUNT - 1)

It is the platform developer's responsibility to provide an implementation.

The implementation can be provided as a global symbol, an inline function or a function-like macro, as described above.

Definition at line 63 of file gpio-hal-arch.c.

◆ gpio_hal_arch_interrupt_enable()

void gpio_hal_arch_interrupt_enable ( gpio_hal_pin_t  pin)

Enable interrupts for a gpio pin.

Parameters
pinThe GPIO pin number (0...GPIO_HAL_PIN_COUNT - 1)

It is the platform developer's responsibility to provide an implementation.

The implementation can be provided as a global symbol, an inline function or a function-like macro, as described above.

Definition at line 52 of file gpio-hal-arch.c.

◆ gpio_hal_arch_pin_cfg_get()

gpio_hal_pin_cfg_t gpio_hal_arch_pin_cfg_get ( gpio_hal_pin_t  pin)

Read the configuration of a GPIO pin.

Parameters
pinThe GPIO pin number (0...GPIO_HAL_PIN_COUNT - 1)
Returns
An OR mask of GPIO_HAL_PIN_CFG_xyz

It is the platform developer's responsibility to provide an implementation.

The implementation can be provided as a global symbol, an inline function or a function-like macro, as described above.

Definition at line 97 of file gpio-hal-arch.c.

References config().

◆ gpio_hal_arch_pin_cfg_set()

void gpio_hal_arch_pin_cfg_set ( gpio_hal_pin_t  pin,
gpio_hal_pin_cfg_t  cfg 
)

Configure a gpio pin.

Parameters
pinThe GPIO pin number (0...GPIO_HAL_PIN_COUNT - 1)
cfgThe configuration

cfg is an OR mask of GPIO_HAL_PIN_CFG_xyz

The implementation of this function also has to make sure that pin is configured as software-controlled GPIO.

It is the platform developer's responsibility to provide an implementation.

The implementation can be provided as a global symbol, an inline function or a function-like macro, as described above.

Definition at line 48 of file gpio-hal-arch.c.

References config().

◆ gpio_hal_arch_pin_set_input()

void gpio_hal_arch_pin_set_input ( gpio_hal_pin_t  pin)

Configure a pin as GPIO input.

Parameters
pinThe GPIO pin number (0...GPIO_HAL_PIN_COUNT - 1)

The implementation of this function also has to make sure that pin is configured as software-controlled GPIO.

It is the platform developer's responsibility to provide an implementation.

The implementation can be provided as a global symbol, an inline function or a function-like macro, as described above.

Definition at line 98 of file gpio-hal-arch.c.

◆ gpio_hal_arch_pin_set_output()

void gpio_hal_arch_pin_set_output ( gpio_hal_pin_t  pin)

Configure a pin as GPIO output.

Parameters
pinThe GPIO pin number (0...GPIO_HAL_PIN_COUNT - 1)

The implementation of this function also has to make sure that pin is configured as software-controlled GPIO.

It is the platform developer's responsibility to provide an implementation.

The implementation can be provided as a global symbol, an inline function or a function-like macro, as described above.

Definition at line 109 of file gpio-hal-arch.c.

◆ gpio_hal_arch_read_pin()

uint8_t gpio_hal_arch_read_pin ( gpio_hal_pin_t  pin)

Read a GPIO pin.

Parameters
pinThe GPIO pin number (0...GPIO_HAL_PIN_COUNT - 1)
Return values
0The pin is logical low
1The pin is logical high

It is the platform developer's responsibility to provide an implementation.

The implementation can be provided as a global symbol, an inline function or a function-like macro, as described above.

Definition at line 146 of file gpio-hal-arch.c.

◆ gpio_hal_arch_read_pins()

gpio_hal_pin_mask_t gpio_hal_arch_read_pins ( gpio_hal_pin_mask_t  pins)

Read multiple pins.

Parameters
pinsAn ORd pin mask of the pins to read
Return values
AnORd mask of the pins that are high

If the position of the pin in pins is set and the pin is logical high then the position of the pin in the return value will be set. For example, if you pass 0x09 as the value of pins and the return value is 0x08 then pin 3 is logical high and pin 0 is logical low.

It is the platform developer's responsibility to provide an implementation.

The implementation can be provided as a global symbol, an inline function or a function-like macro, as described above.

Definition at line 176 of file gpio-hal-arch.c.

References GPIO_A_BASE, GPIO_B_BASE, GPIO_C_BASE, GPIO_D_BASE, and GPIO_READ_PIN.

◆ gpio_hal_arch_set_pin()

void gpio_hal_arch_set_pin ( gpio_hal_pin_t  pin)

Set a GPIO pin to logical high.

Parameters
pinThe GPIO pin number (0...GPIO_HAL_PIN_COUNT - 1)

It is the platform developer's responsibility to provide an implementation.

The implementation can be provided as a global symbol, an inline function or a function-like macro, as described above.

Definition at line 120 of file gpio-hal-arch.c.

◆ gpio_hal_arch_set_pins()

void gpio_hal_arch_set_pins ( gpio_hal_pin_mask_t  pins)

Set multiple pins to logical high.

Parameters
pinsAn ORd pin mask of the pins to set

A pin will be set to logical high if its position in pins is set. For example you can set pins 0 and 3 by passing 0x09.

It is the platform developer's responsibility to provide an implementation.

The implementation can be provided as a global symbol, an inline function or a function-like macro, as described above.

Definition at line 158 of file gpio-hal-arch.c.

References GPIO_A_BASE, GPIO_B_BASE, GPIO_C_BASE, GPIO_D_BASE, and GPIO_SET_PIN.

◆ gpio_hal_arch_toggle_pin()

void gpio_hal_arch_toggle_pin ( gpio_hal_pin_t  pin)

Toggle a GPIO pin.

Parameters
pinThe GPIO pin number (0...GPIO_HAL_PIN_COUNT - 1)

Some MCUs allow GPIO pin toggling directly via register access. In this case, it is a good idea to provide an implementation of this function. However, a default, software-based implementation is also provided by the HAL and can be used if the MCU does not have a pin toggle register. To use the HAL function, define GPIO_HAL_ARCH_SW_TOGGLE as 1. To provide your own implementation, define GPIO_HAL_ARCH_SW_TOGGLE as 0.

The implementation can be provided as a global symbol, an inline function or a function-like macro, as described above.

Referenced by leds_single_toggle(), and leds_toggle().

◆ gpio_hal_arch_toggle_pins()

void gpio_hal_arch_toggle_pins ( gpio_hal_pin_mask_t  pins)

Toggle multiple pins.

Parameters
pinsAn ORd pin mask of the pins to toggle

A pin will be toggled if its position in pins is set. For example you can toggle pins 0 and 3 by passing 0x09.

Some MCUs allow GPIO pin toggling directly via register access. In this case, it is a good idea to provide an implementation of this function. However, a default, software-based implementation is also provided by the HAL and can be used if the MCU does not have a pin toggle register. To use the HAL function, define GPIO_HAL_ARCH_SW_TOGGLE as 1. To provide your own implementation, define GPIO_HAL_ARCH_SW_TOGGLE as 0.

The implementation can be provided as a global symbol, an inline function or a function-like macro, as described above.

◆ gpio_hal_arch_write_pin()

void gpio_hal_arch_write_pin ( gpio_hal_pin_t  pin,
uint8_t  value 
)

Write a GPIO pin.

Parameters
pinThe GPIO pin number (0...GPIO_HAL_PIN_COUNT - 1)
value0: Logical low; 1: Logical high

It is the platform developer's responsibility to provide an implementation.

The implementation can be provided as a global symbol, an inline function or a function-like macro, as described above.

Definition at line 148 of file gpio-hal-arch.c.

◆ gpio_hal_arch_write_pins()

void gpio_hal_arch_write_pins ( gpio_hal_pin_mask_t  pins,
gpio_hal_pin_mask_t  value 
)

Write multiple pins.

Parameters
pinsAn ORd pin mask of the pins to write
valueAn ORd mask of the value to write

The function will modify GPIO pins that have their position in the mask set. pins, the function will write the value specified in the corresponding position in value.

For example, you can set pin 3 and clear pin 0 by a single call to this function. To achieve this, pins must be 0x09 and value 0x08.

It is the platform developer's responsibility to provide an implementation.

There is no guarantee that this function will result in an atomic operation.

The implementation can be provided as a global symbol, an inline function or a function-like macro, as described above.

Definition at line 189 of file gpio-hal-arch.c.

◆ gpio_hal_event_handler()

void gpio_hal_event_handler ( gpio_hal_pin_mask_t  pins)

The platform-independent GPIO event handler.

Parameters
pinsOR mask of pins that generated an event

Whenever a GPIO input interrupt occurs (edge or level detection) and an ISR is triggered, the ISR must call this function, passing as argument an ORd mask of the pins that triggered the interrupt. This function will then call the registered event handlers (if any) for the pins that triggered the event. The platform code should make no assumptions as to the order that the handlers will be called.

If a pin set in the mask has an event handler registered, this function will call the registered handler.

This function will not clear any CPU interrupt flags, this should be done by the calling ISR.

See also
gpio_hal_register_handler

Definition at line 61 of file gpio-hal.c.

References list_head().

◆ gpio_hal_register_handler()

void gpio_hal_register_handler ( gpio_hal_event_handler_t handler)

Register a function to be called whenever a pin triggers an event.

Parameters
handlerThe handler representation

The handler must be pre-allocated statically by the caller.

This function can be used to register a function to be called by the HAL whenever a GPIO interrupt occurs.

See also
gpio_hal_event_handler

Definition at line 55 of file gpio-hal.c.

References list_add().