GPIO Support — Use of GPIO


#include <cyg/hal/hal_io.h>

desc = CYGHWR_HAL_BCM283X_GPIO( pin , alt , mode );

desc = CYGHWR_HAL_BCM283X_GPIO_VAR( pin , alt , mode );


desc = CYGHWR_HAL_BCM283X_GPIO_GET ( pin );

CYGHWR_HAL_BCM283X_GPIO_OUT ( desc , val );

CYGHWR_HAL_BCM283X_GPIO_IN ( desc , val );


The BCM283X HAL provides a number of macros to support the encoding of pin multiplexing and GPIO pin modes into a 32 bit descriptor. This is useful to drivers and other packages that need to configure and use different lines for different devices. Pin multiplexing is handled in the GPIO controller, so it is handled by the same interface.

A GPIO descriptor is created with CYGHWR_HAL_BCM283X_GPIO( pin, alt, mode) which takes the following arguments:

This gives the GPIO pin number, between 0 and 53.
This gives the pin's function. It may be one of GPIO_IN, GPIO_OUT, ALT0, ALT1, ALT2, ALT3, ALT4 or ALT5. The first two set the pin to be a GPIO input or output respectively, the remainder select one of six alternate functions for the pin, usually assigning it to a particular device signal. The alternate function mappings can be found in the BCM2835 documentation, and may also be seen using the RedBoot gpio table command.

This defines any additional properties that this pin should have. These either define the signal input condition on which an interrupt is raised, or the nature of any pull-up or -down to be applied to the pin. The values RISING and FALLING program the line to interrupt on a rising or falling edge; similarly HIGH and LOW interrupt on high or low levels. The values PULL_DOWN and PULL_UP apply a pull resistor in the given direction.

The value in this argument is the last element of a macro of the form, CYGHWR_HAL_BCM283X_GPIO_MODE_XXXXX. It is possible to define additional macros than enable combinations of modes. The HAL defines some such macros: EDGE causes and interrupt on any signal edge; PULL_UP_FALLING applies a pull up on the line and interrupts on a falling edge, and PULL_DOWN_RISING does the inverse.

Additionally, the macro CYGHWR_HAL_BCM283X_GPIO_NONE may be used in place of a pin descriptor and has a value that no valid descriptor can take. It may therefore be used as a placeholder where no GPIO pin is present or to be used. It may be passed to any GPIO macro which takes a descriptor and will be treated as a no-op.

The following examples show how this macro may be used:

// ACT LED is on GPIO16 on some boards
#define CYGHWR_HAL_PI_LED                       CYGHWR_HAL_BCM283X_GPIO( 16, GPIO_OUT, NONE )

// UART TX and TX pins are on GPIO14 and GPIO15
#define CYGHWR_HAL_BCM283X_UART0_TX             CYGHWR_HAL_BCM283X_GPIO( 14, ALT0, NONE )
#define CYGHWR_HAL_BCM283X_UART0_RX             CYGHWR_HAL_BCM283X_GPIO( 15, ALT0, NONE )

Most of the remaining macros all take a GPIO descriptor as an argument. CYGHWR_HAL_BCM283X_GPIO_SET configures the pin according to the descriptor and must be called before any the following macros. CYGHWR_HAL_BCM283X_GPIO_OUT sets the pin output to the value of the least significant bit of the val argument. The val argument of CYGHWR_HAL_BCM283X_GPIO_IN should be a pointer to an int, which will be set to 0 if the pin input is zero, and 1 otherwise.

The macro CYGHWR_HAL_BCM283X_GPIO_GET takes a pin number as an argument and returns a descriptor that encodes the current state of that pin. The CYGHWR_HAL_BCM283X_GPIO_VAR performs the same descriptor encoding as CYGHWR_HAL_BCM283X_GPIO except that the arguments are not interpreted as macro name fragments. This is useful for generating a descriptor at runtime from variable field values.