This file contains basic GPIO driver functions.
Definition in file gpio.h.
#include <avr32/io.h>
#include "compiler.h"
Go to the source code of this file.
Data Structures | |
struct | gpio_map_t |
A type definition of pins and modules connectivity. More... | |
Defines | |
Interrupt Trigger Modes | |
#define | GPIO_FALLING_EDGE 2 |
Interrupt triggered upon falling edge. | |
#define | GPIO_PIN_CHANGE 0 |
Interrupt triggered upon pin change. | |
#define | GPIO_RISING_EDGE 1 |
Interrupt triggered upon rising edge. | |
Return Values of the GPIO API | |
#define | GPIO_INVALID_ARGUMENT 1 |
Input parameters are out of range. | |
#define | GPIO_SUCCESS 0 |
Function successfully completed. | |
Functions | |
Peripheral Bus Interface | |
Low-speed interface with a non-deterministic number of clock cycles per access. This interface operates with lower clock frequencies (fPB <= fCPU), and its timing is not deterministic since it needs to access a shared bus which may be heavily loaded.
| |
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. | |
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 GPIO_FALLING_EDGE 2 |
Interrupt triggered upon falling edge.
Definition at line 67 of file gpio.h.
Referenced by gpio_configure_edge_detector().
#define GPIO_INVALID_ARGUMENT 1 |
Input parameters are out of range.
Definition at line 58 of file gpio.h.
Referenced by gpio_configure_edge_detector(), gpio_enable_module_pin(), and gpio_enable_pin_interrupt().
#define GPIO_PIN_CHANGE 0 |
Interrupt triggered upon pin change.
Definition at line 65 of file gpio.h.
Referenced by gpio_configure_edge_detector().
#define GPIO_RISING_EDGE 1 |
Interrupt triggered upon rising edge.
Definition at line 66 of file gpio.h.
Referenced by gpio_configure_edge_detector().
#define GPIO_SUCCESS 0 |
Function successfully completed.
Definition at line 57 of file gpio.h.
Referenced by gpio_configure_edge_detector(), gpio_enable_module(), gpio_enable_module_pin(), and gpio_enable_pin_interrupt().
void gpio_clear_pin_interrupt_flag | ( | unsigned int | pin | ) |
void gpio_clr_gpio_open_drain_pin | ( | unsigned int | pin | ) |
Drives a GPIO pin to 0 using open drain.
pin | The pin number. |
Definition at line 321 of file gpio.c.
References GPIO.
00322 { 00323 volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5]; 00324 00325 gpio_port->ovrc = 1 << (pin & 0x1F); // Value to be driven on the I/O line: 0. 00326 gpio_port->oders = 1 << (pin & 0x1F); // The GPIO output driver is enabled for that pin. 00327 gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin. 00328 }
void gpio_clr_gpio_pin | ( | unsigned int | pin | ) |
Drives a GPIO pin to 0.
pin | The pin number. |
Definition at line 292 of file gpio.c.
References GPIO.
00293 { 00294 volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5]; 00295 00296 gpio_port->ovrc = 1 << (pin & 0x1F); // Value to be driven on the I/O line: 0. 00297 gpio_port->oders = 1 << (pin & 0x1F); // The GPIO output driver is enabled for that pin. 00298 gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin. 00299 }
void gpio_disable_pin_glitch_filter | ( | unsigned int | pin | ) |
void gpio_disable_pin_interrupt | ( | unsigned int | pin | ) |
void gpio_disable_pin_pull_up | ( | unsigned int | pin | ) |
void gpio_enable_gpio | ( | const gpio_map_t | gpiomap, | |
unsigned int | size | |||
) |
Enables the GPIO mode of a set of pins.
gpiomap | The pin map. | |
size | The number of pins in gpiomap. |
Definition at line 150 of file gpio.c.
References gpio_enable_gpio_pin(), and gpio_map_t::pin.
00151 { 00152 unsigned int i; 00153 00154 for (i = 0; i < size; i++) 00155 { 00156 gpio_enable_gpio_pin(gpiomap->pin); 00157 gpiomap++; 00158 } 00159 }
void gpio_enable_gpio_pin | ( | unsigned int | pin | ) |
Enables the GPIO mode of a pin.
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 162 of file gpio.c.
References GPIO.
Referenced by gpio_enable_gpio().
00163 { 00164 volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5]; 00165 gpio_port->oderc = 1 << (pin & 0x1F); 00166 gpio_port->gpers = 1 << (pin & 0x1F); 00167 }
int gpio_enable_module | ( | const gpio_map_t | gpiomap, | |
unsigned int | size | |||
) |
Enables specific module modes for a set of pins.
gpiomap | The pin map. | |
size | The number of pins in gpiomap. |
Definition at line 59 of file gpio.c.
References gpio_map_t::function, gpio_enable_module_pin(), GPIO_SUCCESS, and gpio_map_t::pin.
00060 { 00061 int status = GPIO_SUCCESS; 00062 unsigned int i; 00063 00064 for (i = 0; i < size; i++) 00065 { 00066 status |= gpio_enable_module_pin(gpiomap->pin, gpiomap->function); 00067 gpiomap++; 00068 } 00069 00070 return status; 00071 }
int gpio_enable_module_pin | ( | unsigned int | pin, | |
unsigned int | function | |||
) |
Enables a specific module mode for a pin.
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. |
Definition at line 74 of file gpio.c.
References GPIO, GPIO_INVALID_ARGUMENT, and GPIO_SUCCESS.
Referenced by gpio_enable_module().
00075 { 00076 volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5]; 00077 00078 // Enable the correct function. 00079 switch (function) 00080 { 00081 case 0: // A function. 00082 gpio_port->pmr0c = 1 << (pin & 0x1F); 00083 gpio_port->pmr1c = 1 << (pin & 0x1F); 00084 #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED) 00085 gpio_port->pmr2c = 1 << (pin & 0x1F); 00086 #endif 00087 break; 00088 00089 case 1: // B function. 00090 gpio_port->pmr0s = 1 << (pin & 0x1F); 00091 gpio_port->pmr1c = 1 << (pin & 0x1F); 00092 #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED) 00093 gpio_port->pmr2c = 1 << (pin & 0x1F); 00094 #endif 00095 break; 00096 00097 case 2: // C function. 00098 gpio_port->pmr0c = 1 << (pin & 0x1F); 00099 gpio_port->pmr1s = 1 << (pin & 0x1F); 00100 #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED) 00101 gpio_port->pmr2c = 1 << (pin & 0x1F); 00102 #endif 00103 break; 00104 00105 case 3: // D function. 00106 gpio_port->pmr0s = 1 << (pin & 0x1F); 00107 gpio_port->pmr1s = 1 << (pin & 0x1F); 00108 #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED) 00109 gpio_port->pmr2c = 1 << (pin & 0x1F); 00110 #endif 00111 break; 00112 00113 #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED) 00114 case 4: // E function. 00115 gpio_port->pmr0c = 1 << (pin & 0x1F); 00116 gpio_port->pmr1c = 1 << (pin & 0x1F); 00117 gpio_port->pmr2s = 1 << (pin & 0x1F); 00118 break; 00119 00120 case 5: // F function. 00121 gpio_port->pmr0s = 1 << (pin & 0x1F); 00122 gpio_port->pmr1c = 1 << (pin & 0x1F); 00123 gpio_port->pmr2s = 1 << (pin & 0x1F); 00124 break; 00125 00126 case 6: // G function. 00127 gpio_port->pmr0c = 1 << (pin & 0x1F); 00128 gpio_port->pmr1s = 1 << (pin & 0x1F); 00129 gpio_port->pmr2s = 1 << (pin & 0x1F); 00130 break; 00131 00132 case 7: // H function. 00133 gpio_port->pmr0s = 1 << (pin & 0x1F); 00134 gpio_port->pmr1s = 1 << (pin & 0x1F); 00135 gpio_port->pmr2s = 1 << (pin & 0x1F); 00136 break; 00137 #endif 00138 00139 default: 00140 return GPIO_INVALID_ARGUMENT; 00141 } 00142 00143 // Disable GPIO control. 00144 gpio_port->gperc = 1 << (pin & 0x1F); 00145 00146 return GPIO_SUCCESS; 00147 }
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.
pin | The pin number. |
Definition at line 341 of file gpio.c.
References GPIO.
00342 { 00343 volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5]; 00344 gpio_port->gfers = 1 << (pin & 0x1F); 00345 }
int gpio_enable_pin_interrupt | ( | unsigned int | pin, | |
unsigned int | mode | |||
) |
Enables the interrupt of a pin with the specified settings.
pin | The pin number. | |
mode | The trigger mode (GPIO_PIN_CHANGE, GPIO_RISING_EDGE or GPIO_FALLING_EDGE). |
Definition at line 392 of file gpio.c.
References GPIO, gpio_configure_edge_detector(), GPIO_INVALID_ARGUMENT, and GPIO_SUCCESS.
00393 { 00394 volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5]; 00395 00396 // Enable the glitch filter. 00397 gpio_port->gfers = 1 << (pin & 0x1F); 00398 00399 // Configure the edge detector. 00400 if(GPIO_INVALID_ARGUMENT == gpio_configure_edge_detector(pin, mode)) 00401 return(GPIO_INVALID_ARGUMENT); 00402 00403 // Enable interrupt. 00404 gpio_port->iers = 1 << (pin & 0x1F); 00405 00406 return GPIO_SUCCESS; 00407 }
void gpio_enable_pin_pull_up | ( | unsigned int | pin | ) |
Enables the pull-up resistor of a pin.
pin | The pin number. |
Definition at line 197 of file gpio.c.
References GPIO.
00198 { 00199 volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5]; 00200 gpio_port->puers = 1 << (pin & 0x1F); 00201 #if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED) 00202 gpio_port->pderc = 1 << (pin & 0x1F); 00203 #endif 00204 }
int gpio_get_gpio_open_drain_pin_output_value | ( | unsigned int | pin | ) |
Returns the output value set for a GPIO pin using open drain.
pin | The pin number. |
Definition at line 275 of file gpio.c.
References GPIO.
00276 { 00277 volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5]; 00278 return ((gpio_port->oder >> (pin & 0x1F)) & 1) ^ 1; 00279 }
int gpio_get_gpio_pin_output_value | ( | unsigned int | pin | ) |
Returns the output value set for a GPIO pin.
pin | The pin number. |
Definition at line 268 of file gpio.c.
References GPIO.
00269 { 00270 volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5]; 00271 return (gpio_port->ovr >> (pin & 0x1F)) & 1; 00272 }
int gpio_get_pin_interrupt_flag | ( | unsigned int | pin | ) |
int gpio_get_pin_value | ( | unsigned int | pin | ) |
void gpio_set_gpio_open_drain_pin | ( | unsigned int | pin | ) |
Drives a GPIO pin to 1 using open drain.
pin | The pin number. |
Definition at line 312 of file gpio.c.
References GPIO.
00313 { 00314 volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5]; 00315 00316 gpio_port->oderc = 1 << (pin & 0x1F); // The GPIO output driver is disabled for that pin. 00317 gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin. 00318 }
void gpio_set_gpio_pin | ( | unsigned int | pin | ) |
Drives a GPIO pin to 1.
pin | The pin number. |
Definition at line 282 of file gpio.c.
References GPIO.
00283 { 00284 volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5]; 00285 00286 gpio_port->ovrs = 1 << (pin & 0x1F); // Value to be driven on the I/O line: 1. 00287 gpio_port->oders = 1 << (pin & 0x1F); // The GPIO output driver is enabled for that pin. 00288 gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin. 00289 }
void gpio_tgl_gpio_open_drain_pin | ( | unsigned int | pin | ) |
Toggles a GPIO pin using open drain.
pin | The pin number. |
Definition at line 331 of file gpio.c.
References GPIO.
00332 { 00333 volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5]; 00334 00335 gpio_port->ovrc = 1 << (pin & 0x1F); // Value to be driven on the I/O line if the GPIO output driver is enabled: 0. 00336 gpio_port->odert = 1 << (pin & 0x1F); // The GPIO output driver is toggled for that pin. 00337 gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin. 00338 }
void gpio_tgl_gpio_pin | ( | unsigned int | pin | ) |
Toggles a GPIO pin.
pin | The pin number. |
Definition at line 302 of file gpio.c.
References GPIO.
00303 { 00304 volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5]; 00305 00306 gpio_port->ovrt = 1 << (pin & 0x1F); // Toggle the I/O line. 00307 gpio_port->oders = 1 << (pin & 0x1F); // The GPIO output driver is enabled for that pin. 00308 gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin. 00309 }