Nut/OS  4.10.3
API Reference
gpio.c
Go to the documentation of this file.
00001 
00037 /* This source file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */
00038 
00039 /*This file has been prepared for Doxygen automatic documentation generation.*/
00055 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
00056  *
00057  * Redistribution and use in source and binary forms, with or without
00058  * modification, are permitted provided that the following conditions are met:
00059  *
00060  * 1. Redistributions of source code must retain the above copyright notice, this
00061  * list of conditions and the following disclaimer.
00062  *
00063  * 2. Redistributions in binary form must reproduce the above copyright notice,
00064  * this list of conditions and the following disclaimer in the documentation
00065  * and/or other materials provided with the distribution.
00066  *
00067  * 3. The name of Atmel may not be used to endorse or promote products derived
00068  * from this software without specific prior written permission.
00069  *
00070  * 4. This software may only be redistributed and used in connection with an Atmel
00071  * AVR product.
00072  *
00073  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
00074  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00075  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00076  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
00077  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00078  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00079  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00080  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00081  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00082  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00083  *
00084  */
00085 
00086 #include <arch/avr32/gpio.h>
00087 
00089 #define GPIO  AVR32_GPIO
00090 
00091 
00094 
00095 
00096 
00097 int gpio_enable_module(const gpio_map_t gpiomap, unsigned int size)
00098 {
00099   int status = GPIO_SUCCESS;
00100   unsigned int i;
00101 
00102   for (i = 0; i < size; i++)
00103   {
00104     status |= gpio_enable_module_pin(gpiomap->pin, gpiomap->function);
00105     gpiomap++;
00106   }
00107 
00108   return status;
00109 }
00110 
00111 
00112 int gpio_enable_module_pin(unsigned int pin, unsigned int function)
00113 {
00114   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00115 
00116   // Enable the correct function.
00117   switch (function)
00118   {
00119   case 0: // A function.
00120     gpio_port->pmr0c = 1 << (pin & 0x1F);
00121     gpio_port->pmr1c = 1 << (pin & 0x1F);
00122 #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
00123     gpio_port->pmr2c = 1 << (pin & 0x1F);
00124 #endif
00125     break;
00126 
00127   case 1: // B function.
00128     gpio_port->pmr0s = 1 << (pin & 0x1F);
00129     gpio_port->pmr1c = 1 << (pin & 0x1F);
00130 #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
00131     gpio_port->pmr2c = 1 << (pin & 0x1F);
00132 #endif
00133     break;
00134 
00135   case 2: // C function.
00136     gpio_port->pmr0c = 1 << (pin & 0x1F);
00137     gpio_port->pmr1s = 1 << (pin & 0x1F);
00138 #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
00139     gpio_port->pmr2c = 1 << (pin & 0x1F);
00140 #endif
00141     break;
00142 
00143   case 3: // D function.
00144     gpio_port->pmr0s = 1 << (pin & 0x1F);
00145     gpio_port->pmr1s = 1 << (pin & 0x1F);
00146 #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
00147     gpio_port->pmr2c = 1 << (pin & 0x1F);
00148 #endif
00149     break;
00150 
00151 #if defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
00152   case 4: // E function.
00153     gpio_port->pmr0c = 1 << (pin & 0x1F);
00154     gpio_port->pmr1c = 1 << (pin & 0x1F);
00155     gpio_port->pmr2s = 1 << (pin & 0x1F);
00156     break;
00157     
00158   case 5: // F function.
00159     gpio_port->pmr0s = 1 << (pin & 0x1F);
00160     gpio_port->pmr1c = 1 << (pin & 0x1F);
00161     gpio_port->pmr2s = 1 << (pin & 0x1F);
00162     break;
00163     
00164   case 6: // G function.
00165     gpio_port->pmr0c = 1 << (pin & 0x1F);
00166     gpio_port->pmr1s = 1 << (pin & 0x1F);
00167     gpio_port->pmr2s = 1 << (pin & 0x1F);
00168     break;
00169     
00170   case 7: // H function.
00171     gpio_port->pmr0s = 1 << (pin & 0x1F);
00172     gpio_port->pmr1s = 1 << (pin & 0x1F);
00173     gpio_port->pmr2s = 1 << (pin & 0x1F);
00174     break;
00175 #endif
00176 
00177   default:
00178     return GPIO_INVALID_ARGUMENT;
00179   }
00180 
00181   // Disable GPIO control.
00182   gpio_port->gperc = 1 << (pin & 0x1F);
00183 
00184   return GPIO_SUCCESS;
00185 }
00186 
00187 
00188 void gpio_enable_gpio(const gpio_map_t gpiomap, unsigned int size)
00189 {
00190   unsigned int i;
00191 
00192   for (i = 0; i < size; i++)
00193   {
00194     gpio_enable_gpio_pin(gpiomap->pin);
00195     gpiomap++;
00196   }
00197 }
00198 
00199 
00200 void gpio_enable_gpio_pin(unsigned int pin)
00201 {
00202   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00203   gpio_port->oderc = 1 << (pin & 0x1F);
00204   gpio_port->gpers = 1 << (pin & 0x1F);
00205 }
00206 
00207 
00208 // The open-drain mode is not synthesized on the current AVR32 products.
00209 // If one day some AVR32 products have this feature, the corresponding part
00210 // numbers should be listed in the #if below.
00211 // Note that other functions are available in this driver to use pins with open
00212 // drain in GPIO mode. The advantage of the open-drain mode functions over these
00213 // other functions is that they can be used not only in GPIO mode but also in
00214 // module mode.
00215 #if 0
00216 
00217 
00218 void gpio_enable_pin_open_drain(unsigned int pin)
00219 {
00220   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00221   gpio_port->odmers = 1 << (pin & 0x1F);
00222 }
00223 
00224 
00225 void gpio_disable_pin_open_drain(unsigned int pin)
00226 {
00227   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00228   gpio_port->odmerc = 1 << (pin & 0x1F);
00229 }
00230 
00231 
00232 #endif
00233 
00234 
00235 void gpio_enable_pin_pull_up(unsigned int pin)
00236 {
00237   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00238   gpio_port->puers = 1 << (pin & 0x1F);
00239 #if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
00240   gpio_port->pderc = 1 << (pin & 0x1F);
00241 #endif
00242 }
00243 
00244 
00245 void gpio_disable_pin_pull_up(unsigned int pin)
00246 {
00247   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00248   gpio_port->puerc = 1 << (pin & 0x1F);
00249 }
00250 
00251 #if defined(AVR32_GPIO_200_H_INCLUDED) || defined(AVR32_GPIO_210_H_INCLUDED) || defined(AVR32_GPIO_211_H_INCLUDED)
00252 // Added support of Pull-up Resistor, Pull-down Resistor and Buskeeper Control.
00253 
00258 void gpio_enable_pin_pull_down(unsigned int pin)
00259 {
00260   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00261   gpio_port->puerc = 1 << (pin & 0x1F);
00262   gpio_port->pders = 1 << (pin & 0x1F);
00263 }
00264 
00269 void gpio_disable_pin_pull_down(unsigned int pin)
00270 {
00271   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00272   gpio_port->pderc = 1 << (pin & 0x1F);
00273 }
00274 
00279 void gpio_enable_pin_buskeeper(unsigned int pin)
00280 {
00281   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00282   gpio_port->puers = 1 << (pin & 0x1F);
00283   gpio_port->pders = 1 << (pin & 0x1F);
00284 }
00285 
00290 void gpio_disable_pin_buskeeper(unsigned int pin)
00291 {
00292   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00293   gpio_port->puerc = 1 << (pin & 0x1F);
00294   gpio_port->pderc = 1 << (pin & 0x1F);
00295 }
00296 
00297 #endif
00298 
00299 int gpio_get_pin_value(unsigned int pin)
00300 {
00301   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00302   return (gpio_port->pvr >> (pin & 0x1F)) & 1;
00303 }
00304 
00305 
00306 int gpio_get_gpio_pin_output_value(unsigned int pin)
00307 {
00308   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00309   return (gpio_port->ovr >> (pin & 0x1F)) & 1;
00310 }
00311 
00312 
00313 int gpio_get_gpio_open_drain_pin_output_value(unsigned int pin)
00314 {
00315   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00316   return ((gpio_port->oder >> (pin & 0x1F)) & 1) ^ 1;
00317 }
00318 
00319 
00320 void gpio_set_gpio_pin(unsigned int pin)
00321 {
00322   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00323 
00324   gpio_port->ovrs  = 1 << (pin & 0x1F); // Value to be driven on the I/O line: 1.
00325   gpio_port->oders = 1 << (pin & 0x1F); // The GPIO output driver is enabled for that pin.
00326   gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
00327 }
00328 
00329 
00330 void gpio_clr_gpio_pin(unsigned int pin)
00331 {
00332   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00333 
00334   gpio_port->ovrc  = 1 << (pin & 0x1F); // Value to be driven on the I/O line: 0.
00335   gpio_port->oders = 1 << (pin & 0x1F); // The GPIO output driver is enabled for that pin.
00336   gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
00337 }
00338 
00339 
00340 void gpio_tgl_gpio_pin(unsigned int pin)
00341 {
00342   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00343 
00344   gpio_port->ovrt  = 1 << (pin & 0x1F); // Toggle the I/O line.
00345   gpio_port->oders = 1 << (pin & 0x1F); // The GPIO output driver is enabled for that pin.
00346   gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
00347 }
00348 
00349 
00350 void gpio_set_gpio_open_drain_pin(unsigned int pin)
00351 {
00352   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00353 
00354   gpio_port->oderc = 1 << (pin & 0x1F); // The GPIO output driver is disabled for that pin.
00355   gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
00356 }
00357 
00358 
00359 void gpio_clr_gpio_open_drain_pin(unsigned int pin)
00360 {
00361   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00362 
00363   gpio_port->ovrc  = 1 << (pin & 0x1F); // Value to be driven on the I/O line: 0.
00364   gpio_port->oders = 1 << (pin & 0x1F); // The GPIO output driver is enabled for that pin.
00365   gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
00366 }
00367 
00368 
00369 void gpio_tgl_gpio_open_drain_pin(unsigned int pin)
00370 {
00371   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00372 
00373   gpio_port->ovrc  = 1 << (pin & 0x1F); // Value to be driven on the I/O line if the GPIO output driver is enabled: 0.
00374   gpio_port->odert = 1 << (pin & 0x1F); // The GPIO output driver is toggled for that pin.
00375   gpio_port->gpers = 1 << (pin & 0x1F); // The GPIO module controls that pin.
00376 }
00377 
00378 
00379 void gpio_enable_pin_glitch_filter(unsigned int pin)
00380 {
00381   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00382   gpio_port->gfers = 1 << (pin & 0x1F);
00383 }
00384 
00385 
00386 void gpio_disable_pin_glitch_filter(unsigned int pin)
00387 {
00388   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00389   gpio_port->gferc = 1 << (pin & 0x1F);
00390 }
00391 
00400 static int gpio_configure_edge_detector(unsigned int pin, unsigned int mode)
00401 {
00402   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00403   
00404   // Configure the edge detector.
00405   switch (mode)
00406   {
00407   case GPIO_PIN_CHANGE:
00408     gpio_port->imr0c = 1 << (pin & 0x1F);
00409     gpio_port->imr1c = 1 << (pin & 0x1F);
00410     break;
00411 
00412   case GPIO_RISING_EDGE:
00413     gpio_port->imr0s = 1 << (pin & 0x1F);
00414     gpio_port->imr1c = 1 << (pin & 0x1F);
00415     break;
00416 
00417   case GPIO_FALLING_EDGE:
00418     gpio_port->imr0c = 1 << (pin & 0x1F);
00419     gpio_port->imr1s = 1 << (pin & 0x1F);
00420     break;
00421 
00422   default:
00423     return GPIO_INVALID_ARGUMENT;
00424   }
00425 
00426   return GPIO_SUCCESS;
00427 }
00428 
00429 
00430 int gpio_enable_pin_interrupt(unsigned int pin, unsigned int mode)
00431 {
00432   volatile avr32_gpio_port_t  *gpio_port = &GPIO.port[pin >> 5];
00433 
00434   // Enable the glitch filter.
00435   gpio_port->gfers = 1 << (pin & 0x1F);
00436 
00437   // Configure the edge detector.
00438   if(GPIO_INVALID_ARGUMENT == gpio_configure_edge_detector(pin, mode))
00439     return(GPIO_INVALID_ARGUMENT);
00440 
00441   // Enable interrupt.
00442   gpio_port->iers = 1 << (pin & 0x1F);
00443 
00444   return GPIO_SUCCESS;
00445 }
00446 
00447 
00448 void gpio_disable_pin_interrupt(unsigned int pin)
00449 {
00450   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00451   gpio_port->ierc = 1 << (pin & 0x1F);
00452 }
00453 
00454 
00455 int gpio_get_pin_interrupt_flag(unsigned int pin)
00456 {
00457   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00458   return (gpio_port->ifr >> (pin & 0x1F)) & 1;
00459 }
00460 
00461 
00462 void gpio_clear_pin_interrupt_flag(unsigned int pin)
00463 {
00464   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00465   gpio_port->ifrc = 1 << (pin & 0x1F);
00466 }
00467 
00468 
00469 //#
00470 //# Peripheral Event System Support.
00471 //#
00472 #if UC3L
00473 int gpio_configure_pin_periph_event_mode(unsigned int pin, unsigned int mode, unsigned int use_igf)
00474 {
00475   volatile avr32_gpio_port_t *gpio_port = &GPIO.port[pin >> 5];
00476 
00477   if(TRUE == use_igf)
00478   {
00479     // Enable the glitch filter.
00480     gpio_port->gfers = 1 << (pin & 0x1F);
00481   }
00482   else
00483   {
00484     // Disable the glitch filter.
00485     gpio_port->gferc = 1 << (pin & 0x1F);
00486   }
00487 
00488   // Configure the edge detector.
00489   return(gpio_configure_edge_detector(pin, mode));
00490 }
00491 
00492 #endif
00493