#4 GPIO package

I. Generic IOs

The GPIO package (CYGPKG_IO_GPIO) provides generic definitions to access and configure the General Purpose IOs of a processor. The package provides a set of functions definition that are required to be implemented by the HAL. HAL supporting the sets of defined functions must implement CYGINT_IO_GPIO_ROUTINE. The set of functions are:

// Change state of a GPIO
inline void cyg_hal_gpio_write( cyg_gpio_t, cyg_gpio_state_t );

// Read state of a GPIO
inline cyg_gpio_state_t cyg_hal_gpio_read( cyg_gpio_t );

// Configure GPIO properties
inline void cyg_hal_gpio_cfg( cyg_gpio_t, cyg_gpio_mode_t );


These functions allow writing, reading and configuring GPIOs of the processor.

II. AT91 GPIO example

In this section is an example how the AT91 processor familly implements the GPIO routine.
Code extracted from /packages/hal/arm/at91/var/current/src/src/hal_misc.c

#if defined(CYGPKG_IO_GPIO)

#include <cyg/io/gpio.h>

inline void
cyg_hal_gpio_write( cyg_gpio_t gpio, cyg_gpio_state_t state )
{
switch( state ) {
case CYG_GPIO_RESET:
HAL_ARM_AT91_GPIO_RESET( gpio );
break;
case CYG_GPIO_SET:
HAL_ARM_AT91_GPIO_SET( gpio );
break;
default:
break;
}
}

inline void
cyg_hal_gpio_cfg( cyg_gpio_t gpio, cyg_gpio_mode_t mode )
{
switch( mode ) {
case CYG_GPIO_IN:
HAL_ARM_AT91_GPIO_CFG_DIRECTION( gpio, AT91_PIN_IN );
break;
case CYG_GPIO_OUT:
HAL_ARM_AT91_GPIO_CFG_DIRECTION( gpio, AT91_PIN_OUT );
break;
default:
break;
}
}

inline cyg_gpio_state_t
cyg_hal_gpio_read( cyg_gpio_t gpio )
{
cyg_bool state;
HAL_ARM_AT91_GPIO_GET( gpio, state );
return ( ( state == 1 ) ? CYG_GPIO_SET : CYG_GPIO_RESET );
}

#endif