Pin Configuration and GPIO Support — Use of pin configuration and GPIO


#include <cyg/hal/hal_io.h>

pin = CYGHWR_HAL_HPS_PINMUX( reg , func );


pin = CYGHWR_HAL_HPS_GPIO( line , mode );


CYGHWR_HAL_HPS_GPIO_OUT ( pin , val );

CYGHWR_HAL_HPS_GPIO_IN ( pin , val );


The HPS HAL provides a number of macros to support the encoding of pin multiplexing information and GPIO pin modes into 32 bit descriptors. This is useful to drivers and other packages that need to configure and use different lines for different devices. Because there is not a simple correspondence between pin multiplexing information and GPIO line numbers, these two things are treated separately.

Pin Multiplexing

A pin multiplexing descriptor is created with CYGHWR_HAL_HPS_PINMUX( reg, func ) which takes the following arguments:

This identifies the PINMUX register which controls this pin. This consists of the name of a pin group parameterized by the pin within that group.
This defines the function code to program into the PINMUX register. There is no systematic consistency between functions and function codes, and the same function for a pin may be represented by different codes in different PINMUX registers. You should refer to the HPS documentation for the correct value to be used here.

The following examples show how this macro may be used:

// UART0 RX line is controlled by register GENERALIO(17), function 2 = UART0.RX

// GPIO 41 is controlled by register FLASHIO(5), function 0 = GPIO 41
#define CYGHWR_HAL_HPS_LED0                     CYGHWR_HAL_HPS_PINMUX(FLASHIO(5),0)

The macro CYGHWR_HAL_HPS_PINMUX_SET( pin ) sets the pin multiplexing setting according to the descriptor passed in.

GPIO Support

A GPIO descriptor is created with CYGHWR_HAL_HPS_GPIO( line, mode) which takes the following arguments:

This gives the GPIO line number, between 0 and 70.
This defines whether this is an input or an output pin, and whether it is de-bounced. It may take the values IN or OUT, or INDB for de-bounced input. GPIO lines 48 to 70 can take their input from one of two pins; modes IN_ALT and INDB_ALT cause the GPIO line to be connected to the alternate input pin.

Additionally, the macro CYGHWR_HAL_HPS_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.

The following examples show how this macro may be used:

// LED 0 is at GPIO41, bank 1 bit 13, output mode
#define CYGHWR_HAL_HPS_LED0_GPIO                CYGHWR_HAL_HPS_GPIO( 41, OUT )

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