gpio.c File Reference


Detailed Description

GPIO driver for AVR32 UC3.

This file defines a useful set of functions for the GPIO.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file gpio.c.

#include "gpio.h"

Go to the source code of this file.

Defines

#define GPIO   AVR32_GPIO
 GPIO module instance.

Functions

Peripheral Bus Interface
void gpio_clear_pin_interrupt_flag (unsigned int pin)
 Clears the interrupt flag of a pin.
void gpio_clr_gpio_open_drain_pin (unsigned int pin)
 Drives a GPIO pin to 0 using open drain.
void gpio_clr_gpio_pin (unsigned int pin)
 Drives a GPIO pin to 0.
static int gpio_configure_edge_detector (unsigned int pin, unsigned int mode)
 Configure the edge detector of an input pin.
int gpio_configure_pin_periph_event_mode (unsigned int pin, unsigned int mode, unsigned int use_igf)
 Configure the peripheral event trigger mode of a pin.
void gpio_disable_pin_glitch_filter (unsigned int pin)
 Disables the glitch filter of a pin.
void gpio_disable_pin_interrupt (unsigned int pin)
 Disables the interrupt of a pin.
void gpio_disable_pin_pull_up (unsigned int pin)
 Disables the pull-up resistor of a pin.
void gpio_enable_gpio (const gpio_map_t gpiomap, unsigned int size)
 Enables the GPIO mode of a set of pins.
void gpio_enable_gpio_pin (unsigned int pin)
 Enables the GPIO mode of a pin.
int gpio_enable_module (const gpio_map_t gpiomap, unsigned int size)
 Enables specific module modes for a set of pins.
int gpio_enable_module_pin (unsigned int pin, unsigned int function)
 Enables a specific module mode for a pin.
void gpio_enable_pin_glitch_filter (unsigned int pin)
 Enables the glitch filter of a pin.
int gpio_enable_pin_interrupt (unsigned int pin, unsigned int mode)
 Enables the interrupt of a pin with the specified settings.
void gpio_enable_pin_pull_up (unsigned int pin)
 Enables the pull-up resistor of a pin.
int gpio_get_gpio_open_drain_pin_output_value (unsigned int pin)
 Returns the output value set for a GPIO pin using open drain.
int gpio_get_gpio_pin_output_value (unsigned int pin)
 Returns the output value set for a GPIO pin.
int gpio_get_pin_interrupt_flag (unsigned int pin)
 Gets the interrupt flag of a pin.
int gpio_get_pin_value (unsigned int pin)
 Returns the value of a pin.
void gpio_set_gpio_open_drain_pin (unsigned int pin)
 Drives a GPIO pin to 1 using open drain.
void gpio_set_gpio_pin (unsigned int pin)
 Drives a GPIO pin to 1.
void gpio_tgl_gpio_open_drain_pin (unsigned int pin)
 Toggles a GPIO pin using open drain.
void gpio_tgl_gpio_pin (unsigned int pin)
 Toggles a GPIO pin.


Define Documentation

#define GPIO   AVR32_GPIO


Function Documentation

void gpio_clear_pin_interrupt_flag ( unsigned int  pin  ) 

Clears the interrupt flag of a pin.

Parameters:
pin The pin number.

Definition at line 425 of file gpio.c.

References GPIO.

00426 {
00427   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00428   gpio_port->ifrc = 1 << (pin & 0x1F);
00429 }

void gpio_clr_gpio_open_drain_pin ( unsigned int  pin  ) 

Drives a GPIO pin to 0 using open drain.

Parameters:
pin The pin number.

Definition at line 322 of file gpio.c.

References GPIO.

00323 {
00324   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00325 
00326   gpio_port->ovrc  = 1 << (pin & 0x1F); // Value to be driven on the I/O line: 0.
00327   gpio_port->oders = 1 << (pin & 0x1F); // The GPIO output driver is enabled for that pin.
00328   gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
00329 }

void gpio_clr_gpio_pin ( unsigned int  pin  ) 

Drives a GPIO pin to 0.

Parameters:
pin The pin number.

Definition at line 293 of file gpio.c.

References GPIO.

00294 {
00295   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00296 
00297   gpio_port->ovrc  = 1 << (pin & 0x1F); // Value to be driven on the I/O line: 0.
00298   gpio_port->oders = 1 << (pin & 0x1F); // The GPIO output driver is enabled for that pin.
00299   gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
00300 }

static int gpio_configure_edge_detector ( unsigned int  pin,
unsigned int  mode 
) [static]

Configure the edge detector of an input pin.

Parameters:
pin The pin number.
mode The edge detection mode (GPIO_PIN_CHANGE, GPIO_RISING_EDGE or GPIO_FALLING_EDGE).
Returns:
GPIO_SUCCESS or GPIO_INVALID_ARGUMENT.

Definition at line 363 of file gpio.c.

References GPIO, GPIO_FALLING_EDGE, GPIO_INVALID_ARGUMENT, GPIO_PIN_CHANGE, GPIO_RISING_EDGE, and GPIO_SUCCESS.

Referenced by gpio_configure_pin_periph_event_mode(), and gpio_enable_pin_interrupt().

00364 {
00365   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00366   
00367   // Configure the edge detector.
00368   switch (mode)
00369   {
00370   case GPIO_PIN_CHANGE:
00371     gpio_port->imr0c = 1 << (pin & 0x1F);
00372     gpio_port->imr1c = 1 << (pin & 0x1F);
00373     break;
00374 
00375   case GPIO_RISING_EDGE:
00376     gpio_port->imr0s = 1 << (pin & 0x1F);
00377     gpio_port->imr1c = 1 << (pin & 0x1F);
00378     break;
00379 
00380   case GPIO_FALLING_EDGE:
00381     gpio_port->imr0c = 1 << (pin & 0x1F);
00382     gpio_port->imr1s = 1 << (pin & 0x1F);
00383     break;
00384 
00385   default:
00386     return GPIO_INVALID_ARGUMENT;
00387   }
00388 
00389   return GPIO_SUCCESS;
00390 }

int gpio_configure_pin_periph_event_mode ( unsigned int  pin,
unsigned int  mode,
unsigned int  use_igf 
)

Configure the peripheral event trigger mode of a pin.

Parameters:
pin The pin number.
mode The trigger mode (GPIO_PIN_CHANGE, GPIO_RISING_EDGE or GPIO_FALLING_EDGE).
use_igf use the Input Glitch Filter (TRUE) or not (FALSE).
Returns:
GPIO_SUCCESS or GPIO_INVALID_ARGUMENT.

Definition at line 438 of file gpio.c.

References GPIO, and gpio_configure_edge_detector().

00439 {
00440   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00441 
00442   if(TRUE == use_igf)
00443   {
00444     // Enable the glitch filter.
00445     gpio_port->gfers = 1 << (pin & 0x1F);
00446   }
00447   else
00448   {
00449     // Disable the glitch filter.
00450     gpio_port->gferc = 1 << (pin & 0x1F);
00451   }
00452 
00453   // Configure the edge detector.
00454   return(gpio_configure_edge_detector(pin, mode));
00455 }

void gpio_disable_pin_glitch_filter ( unsigned int  pin  ) 

Disables the glitch filter of a pin.

Parameters:
pin The pin number.

Definition at line 349 of file gpio.c.

References GPIO.

00350 {
00351   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00352   gpio_port->gferc = 1 << (pin & 0x1F);
00353 }

void gpio_disable_pin_interrupt ( unsigned int  pin  ) 

Disables the interrupt of a pin.

Parameters:
pin The pin number.

Definition at line 411 of file gpio.c.

References GPIO.

00412 {
00413   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00414   gpio_port->ierc = 1 << (pin & 0x1F);
00415 }

void gpio_disable_pin_pull_up ( unsigned int  pin  ) 

Disables the pull-up resistor of a pin.

Parameters:
pin The pin number.

Definition at line 208 of file gpio.c.

References GPIO.

00209 {
00210   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00211   gpio_port->puerc = 1 << (pin & 0x1F);
00212 }

void gpio_enable_gpio ( const gpio_map_t  gpiomap,
unsigned int  size 
)

Enables the GPIO mode of a set of pins.

Parameters:
gpiomap The pin map.
size The number of pins in gpiomap.

Definition at line 151 of file gpio.c.

References gpio_enable_gpio_pin(), and gpio_map_t::pin.

00152 {
00153   unsigned int i;
00154 
00155   for (i = 0; i < size; i++)
00156   {
00157     gpio_enable_gpio_pin(gpiomap->pin);
00158     gpiomap++;
00159   }
00160 }

void gpio_enable_gpio_pin ( unsigned int  pin  ) 

Enables the GPIO mode of a pin.

Parameters:
pin The pin number.
Refer to the product header file `uc3x.h' (where x is the part number; e.g. x = a0512) for pin definitions. E.g., to enable the GPIO mode of PX21, AVR32_PIN_PX21 can be used. Module pins such as AVR32_PWM_3_PIN for PWM channel 3 can also be used to release module pins for GPIO.

Definition at line 163 of file gpio.c.

References GPIO.

Referenced by gpio_enable_gpio().

00164 {
00165   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00166   gpio_port->oderc = 1 << (pin & 0x1F);
00167   gpio_port->gpers = 1 << (pin & 0x1F);
00168 }

int gpio_enable_module ( const gpio_map_t  gpiomap,
unsigned int  size 
)

Enables specific module modes for a set of pins.

Parameters:
gpiomap The pin map.
size The number of pins in gpiomap.
Returns:
GPIO_SUCCESS or GPIO_INVALID_ARGUMENT.

Definition at line 60 of file gpio.c.

References gpio_map_t::function, gpio_enable_module_pin(), GPIO_SUCCESS, and gpio_map_t::pin.

00061 {
00062   int status = GPIO_SUCCESS;
00063   unsigned int i;
00064 
00065   for (i = 0; i < size; i++)
00066   {
00067     status |= gpio_enable_module_pin(gpiomap->pin, gpiomap->function);
00068     gpiomap++;
00069   }
00070 
00071   return status;
00072 }

int gpio_enable_module_pin ( unsigned int  pin,
unsigned int  function 
)

Enables a specific module mode for a pin.

Parameters:
pin The pin number.
Refer to the product header file `uc3x.h' (where x is the part number; e.g. x = a0512) for module pins. E.g., to enable a PWM channel output, the pin number can be AVR32_PWM_3_PIN for PWM channel 3.
function The pin function.
Refer to the product header file `uc3x.h' (where x is the part number; e.g. x = a0512) for module pin functions. E.g., to enable a PWM channel output, the pin function can be AVR32_PWM_3_FUNCTION for PWM channel 3.
Returns:
GPIO_SUCCESS or GPIO_INVALID_ARGUMENT.

Definition at line 75 of file gpio.c.

References GPIO, GPIO_INVALID_ARGUMENT, and GPIO_SUCCESS.

Referenced by gpio_enable_module().

00076 {
00077   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00078 
00079   // Enable the correct function.
00080   switch (function)
00081   {
00082   case 0: // A function.
00083     gpio_port->pmr0c = 1 << (pin & 0x1F);
00084     gpio_port->pmr1c = 1 << (pin & 0x1F);
00085 #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
00086     gpio_port->pmr2c = 1 << (pin & 0x1F);
00087 #endif
00088     break;
00089 
00090   case 1: // B function.
00091     gpio_port->pmr0s = 1 << (pin & 0x1F);
00092     gpio_port->pmr1c = 1 << (pin & 0x1F);
00093 #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
00094     gpio_port->pmr2c = 1 << (pin & 0x1F);
00095 #endif
00096     break;
00097 
00098   case 2: // C function.
00099     gpio_port->pmr0c = 1 << (pin & 0x1F);
00100     gpio_port->pmr1s = 1 << (pin & 0x1F);
00101 #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
00102     gpio_port->pmr2c = 1 << (pin & 0x1F);
00103 #endif
00104     break;
00105 
00106   case 3: // D function.
00107     gpio_port->pmr0s = 1 << (pin & 0x1F);
00108     gpio_port->pmr1s = 1 << (pin & 0x1F);
00109 #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
00110     gpio_port->pmr2c = 1 << (pin & 0x1F);
00111 #endif
00112     break;
00113 
00114 #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
00115   case 4: // E function.
00116     gpio_port->pmr0c = 1 << (pin & 0x1F);
00117     gpio_port->pmr1c = 1 << (pin & 0x1F);
00118     gpio_port->pmr2s = 1 << (pin & 0x1F);
00119     break;
00120     
00121   case 5: // F function.
00122     gpio_port->pmr0s = 1 << (pin & 0x1F);
00123     gpio_port->pmr1c = 1 << (pin & 0x1F);
00124     gpio_port->pmr2s = 1 << (pin & 0x1F);
00125     break;
00126     
00127   case 6: // G function.
00128     gpio_port->pmr0c = 1 << (pin & 0x1F);
00129     gpio_port->pmr1s = 1 << (pin & 0x1F);
00130     gpio_port->pmr2s = 1 << (pin & 0x1F);
00131     break;
00132     
00133   case 7: // H function.
00134     gpio_port->pmr0s = 1 << (pin & 0x1F);
00135     gpio_port->pmr1s = 1 << (pin & 0x1F);
00136     gpio_port->pmr2s = 1 << (pin & 0x1F);
00137     break;
00138 #endif
00139 
00140   default:
00141     return GPIO_INVALID_ARGUMENT;
00142   }
00143 
00144   // Disable GPIO control.
00145   gpio_port->gperc = 1 << (pin & 0x1F);
00146 
00147   return GPIO_SUCCESS;
00148 }

void gpio_enable_pin_glitch_filter ( unsigned int  pin  ) 

Enables the glitch filter of a pin.

When the glitch filter is enabled, a glitch with duration of less than 1 clock cycle is automatically rejected, while a pulse with duration of 2 clock cycles or more is accepted. For pulse durations between 1 clock cycle and 2 clock cycles, the pulse may or may not be taken into account, depending on the precise timing of its occurrence. Thus for a pulse to be guaranteed visible it must exceed 2 clock cycles, whereas for a glitch to be reliably filtered out, its duration must not exceed 1 clock cycle. The filter introduces 2 clock cycles latency.

Parameters:
pin The pin number.

Definition at line 342 of file gpio.c.

References GPIO.

00343 {
00344   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00345   gpio_port->gfers = 1 << (pin & 0x1F);
00346 }

int gpio_enable_pin_interrupt ( unsigned int  pin,
unsigned int  mode 
)

Enables the interrupt of a pin with the specified settings.

Parameters:
pin The pin number.
mode The trigger mode (GPIO_PIN_CHANGE, GPIO_RISING_EDGE or GPIO_FALLING_EDGE).
Returns:
GPIO_SUCCESS or GPIO_INVALID_ARGUMENT.

Definition at line 393 of file gpio.c.

References GPIO, gpio_configure_edge_detector(), GPIO_INVALID_ARGUMENT, and GPIO_SUCCESS.

00394 {
00395   volatile avr32_gpio_port_t  *gpio_port = &GPIO.port[pin >> 5];
00396 
00397   // Enable the glitch filter.
00398   gpio_port->gfers = 1 << (pin & 0x1F);
00399 
00400   // Configure the edge detector.
00401   if(GPIO_INVALID_ARGUMENT == gpio_configure_edge_detector(pin, mode))
00402     return(GPIO_INVALID_ARGUMENT);
00403 
00404   // Enable interrupt.
00405   gpio_port->iers = 1 << (pin & 0x1F);
00406 
00407   return GPIO_SUCCESS;
00408 }

void gpio_enable_pin_pull_up ( unsigned int  pin  ) 

Enables the pull-up resistor of a pin.

Parameters:
pin The pin number.

Definition at line 198 of file gpio.c.

References GPIO.

00199 {
00200   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00201   gpio_port->puers = 1 << (pin & 0x1F);
00202 #if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
00203   gpio_port->pderc = 1 << (pin & 0x1F);
00204 #endif
00205 }

int gpio_get_gpio_open_drain_pin_output_value ( unsigned int  pin  ) 

Returns the output value set for a GPIO pin using open drain.

Parameters:
pin The pin number.
Returns:
The pin output value.
Note:
This function must be used in conjunction with gpio_set_gpio_open_drain_pin, gpio_clr_gpio_open_drain_pin and gpio_tgl_gpio_open_drain_pin.

Definition at line 276 of file gpio.c.

References GPIO.

00277 {
00278   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00279   return ((gpio_port->oder >> (pin & 0x1F)) & 1) ^ 1;
00280 }

int gpio_get_gpio_pin_output_value ( unsigned int  pin  ) 

Returns the output value set for a GPIO pin.

Parameters:
pin The pin number.
Returns:
The pin output value.
Note:
This function must be used in conjunction with gpio_set_gpio_pin, gpio_clr_gpio_pin and gpio_tgl_gpio_pin.

Definition at line 269 of file gpio.c.

References GPIO.

00270 {
00271   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00272   return (gpio_port->ovr >> (pin & 0x1F)) & 1;
00273 }

int gpio_get_pin_interrupt_flag ( unsigned int  pin  ) 

Gets the interrupt flag of a pin.

Parameters:
pin The pin number.
Returns:
The pin interrupt flag.

Definition at line 418 of file gpio.c.

References GPIO.

00419 {
00420   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00421   return (gpio_port->ifr >> (pin & 0x1F)) & 1;
00422 }

int gpio_get_pin_value ( unsigned int  pin  ) 

Returns the value of a pin.

Parameters:
pin The pin number.
Returns:
The pin value.

Definition at line 262 of file gpio.c.

References GPIO.

00263 {
00264   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00265   return (gpio_port->pvr >> (pin & 0x1F)) & 1;
00266 }

void gpio_set_gpio_open_drain_pin ( unsigned int  pin  ) 

Drives a GPIO pin to 1 using open drain.

Parameters:
pin The pin number.

Definition at line 313 of file gpio.c.

References GPIO.

00314 {
00315   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00316 
00317   gpio_port->oderc = 1 << (pin & 0x1F); // The GPIO output driver is disabled for that pin.
00318   gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
00319 }

void gpio_set_gpio_pin ( unsigned int  pin  ) 

Drives a GPIO pin to 1.

Parameters:
pin The pin number.

Definition at line 283 of file gpio.c.

References GPIO.

00284 {
00285   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00286 
00287   gpio_port->ovrs  = 1 << (pin & 0x1F); // Value to be driven on the I/O line: 1.
00288   gpio_port->oders = 1 << (pin & 0x1F); // The GPIO output driver is enabled for that pin.
00289   gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
00290 }

void gpio_tgl_gpio_open_drain_pin ( unsigned int  pin  ) 

Toggles a GPIO pin using open drain.

Parameters:
pin The pin number.

Definition at line 332 of file gpio.c.

References GPIO.

00333 {
00334   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00335 
00336   gpio_port->ovrc  = 1 << (pin & 0x1F); // Value to be driven on the I/O line if the GPIO output driver is enabled: 0.
00337   gpio_port->odert = 1 << (pin & 0x1F); // The GPIO output driver is toggled for that pin.
00338   gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
00339 }

void gpio_tgl_gpio_pin ( unsigned int  pin  ) 

Toggles a GPIO pin.

Parameters:
pin The pin number.

Definition at line 303 of file gpio.c.

References GPIO.

00304 {
00305   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00306 
00307   gpio_port->ovrt  = 1 << (pin & 0x1F); // Toggle the I/O line.
00308   gpio_port->oders = 1 << (pin & 0x1F); // The GPIO output driver is enabled for that pin.
00309   gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
00310 }


Generated on Thu Dec 17 19:58:35 2009 for AVR32 - GPIO Driver - Local Bus Interface by  doxygen 1.5.5