led.c

Go to the documentation of this file.
00001 /* This source file is part of the ATMEL AT32UC3A-SoftwareFramework-1.1.1 Release */
00002 
00003 /*This file is prepared for Doxygen automatic documentation generation.*/
00020 /* Copyright (c) 2007, Atmel Corporation All rights reserved.
00021  *
00022  * Redistribution and use in source and binary forms, with or without
00023  * modification, are permitted provided that the following conditions are met:
00024  *
00025  * 1. Redistributions of source code must retain the above copyright notice,
00026  * this list of conditions and the following disclaimer.
00027  *
00028  * 2. Redistributions in binary form must reproduce the above copyright notice,
00029  * this list of conditions and the following disclaimer in the documentation
00030  * and/or other materials provided with the distribution.
00031  *
00032  * 3. The name of ATMEL may not be used to endorse or promote products derived
00033  * from this software without specific prior written permission.
00034  *
00035  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
00036  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00037  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
00038  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
00039  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00040  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00041  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00042  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00043  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00044  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00045  */
00046 
00047 
00048 #include <avr32/io.h>
00049 #include "preprocessor.h"
00050 #include "compiler.h"
00051 #include "evk1100.h"
00052 #include "led.h"
00053 
00054 
00056 typedef const struct
00057 {
00058   struct
00059   {
00060     U32 PORT;     
00061     U32 PIN_MASK; 
00062   } GPIO; 
00063   struct
00064   {
00065     S32 CHANNEL;  
00066     S32 FUNCTION; 
00067   } PWM;  
00068 } tLED_DESCRIPTOR;
00069 
00070 
00072 static tLED_DESCRIPTOR LED_DESCRIPTOR[LED_COUNT] =
00073 {
00074 #define INSERT_LED_DESCRIPTOR(LED_NO, unused)                 \
00075   {                                                           \
00076     {LED##LED_NO##_GPIO / 32, 1 << (LED##LED_NO##_GPIO % 32)},\
00077     {LED##LED_NO##_PWM,       LED##LED_NO##_PWM_FUNCTION    } \
00078   },
00079   MREPEAT(LED_COUNT, INSERT_LED_DESCRIPTOR, ~)
00080 #undef INSERT_LED_DESCRIPTOR
00081 };
00082 
00083 
00085 static volatile U32 LED_State = (1 << LED_COUNT) - 1;
00086 
00087 
00088 U32 LED_Read_Display(void)
00089 {
00090   return LED_State;
00091 }
00092 
00093 
00094 void LED_Display(U32 leds)
00095 {
00096   tLED_DESCRIPTOR *led_descriptor;
00097   volatile avr32_gpio_port_t *led_gpio_port;
00098 
00099   leds &= (1 << LED_COUNT) - 1;
00100   LED_State = leds;
00101   for (led_descriptor = &LED_DESCRIPTOR[0];
00102        led_descriptor < LED_DESCRIPTOR + LED_COUNT;
00103        led_descriptor++)
00104   {
00105     led_gpio_port = &AVR32_GPIO.port[led_descriptor->GPIO.PORT];
00106     if (leds & 1)
00107     {
00108       led_gpio_port->ovrc  = led_descriptor->GPIO.PIN_MASK;
00109     }
00110     else
00111     {
00112       led_gpio_port->ovrs  = led_descriptor->GPIO.PIN_MASK;
00113     }
00114     led_gpio_port->oders = led_descriptor->GPIO.PIN_MASK;
00115     led_gpio_port->gpers = led_descriptor->GPIO.PIN_MASK;
00116     leds >>= 1;
00117   }
00118 }
00119 
00120 
00121 U32 LED_Read_Display_Mask(U32 mask)
00122 {
00123   return Rd_bits(LED_State, mask);
00124 }
00125 
00126 
00127 void LED_Display_Mask(U32 mask, U32 leds)
00128 {
00129   tLED_DESCRIPTOR *led_descriptor = &LED_DESCRIPTOR[0] - 1;
00130   volatile avr32_gpio_port_t *led_gpio_port;
00131   U8 led_shift;
00132 
00133   mask &= (1 << LED_COUNT) - 1;
00134   Wr_bits(LED_State, mask, leds);
00135   while (mask)
00136   {
00137     led_shift = 1 + ctz(mask);
00138     led_descriptor += led_shift;
00139     led_gpio_port = &AVR32_GPIO.port[led_descriptor->GPIO.PORT];
00140     leds >>= led_shift - 1;
00141     if (leds & 1)
00142     {
00143       led_gpio_port->ovrc  = led_descriptor->GPIO.PIN_MASK;
00144     }
00145     else
00146     {
00147       led_gpio_port->ovrs  = led_descriptor->GPIO.PIN_MASK;
00148     }
00149     led_gpio_port->oders = led_descriptor->GPIO.PIN_MASK;
00150     led_gpio_port->gpers = led_descriptor->GPIO.PIN_MASK;
00151     leds >>= 1;
00152     mask >>= led_shift;
00153   }
00154 }
00155 
00156 
00157 Bool LED_Test(U32 leds)
00158 {
00159   return Tst_bits(LED_State, leds);
00160 }
00161 
00162 
00163 void LED_Off(U32 leds)
00164 {
00165   tLED_DESCRIPTOR *led_descriptor = &LED_DESCRIPTOR[0] - 1;
00166   volatile avr32_gpio_port_t *led_gpio_port;
00167   U8 led_shift;
00168 
00169   leds &= (1 << LED_COUNT) - 1;
00170   Clr_bits(LED_State, leds);
00171   while (leds)
00172   {
00173     led_shift = 1 + ctz(leds);
00174     led_descriptor += led_shift;
00175     led_gpio_port = &AVR32_GPIO.port[led_descriptor->GPIO.PORT];
00176     led_gpio_port->ovrs  = led_descriptor->GPIO.PIN_MASK;
00177     led_gpio_port->oders = led_descriptor->GPIO.PIN_MASK;
00178     led_gpio_port->gpers = led_descriptor->GPIO.PIN_MASK;
00179     leds >>= led_shift;
00180   }
00181 }
00182 
00183 
00184 void LED_On(U32 leds)
00185 {
00186   tLED_DESCRIPTOR *led_descriptor = &LED_DESCRIPTOR[0] - 1;
00187   volatile avr32_gpio_port_t *led_gpio_port;
00188   U8 led_shift;
00189 
00190   leds &= (1 << LED_COUNT) - 1;
00191   Set_bits(LED_State, leds);
00192   while (leds)
00193   {
00194     led_shift = 1 + ctz(leds);
00195     led_descriptor += led_shift;
00196     led_gpio_port = &AVR32_GPIO.port[led_descriptor->GPIO.PORT];
00197     led_gpio_port->ovrc  = led_descriptor->GPIO.PIN_MASK;
00198     led_gpio_port->oders = led_descriptor->GPIO.PIN_MASK;
00199     led_gpio_port->gpers = led_descriptor->GPIO.PIN_MASK;
00200     leds >>= led_shift;
00201   }
00202 }
00203 
00204 
00205 void LED_Toggle(U32 leds)
00206 {
00207   tLED_DESCRIPTOR *led_descriptor = &LED_DESCRIPTOR[0] - 1;
00208   volatile avr32_gpio_port_t *led_gpio_port;
00209   U8 led_shift;
00210 
00211   leds &= (1 << LED_COUNT) - 1;
00212   Tgl_bits(LED_State, leds);
00213   while (leds)
00214   {
00215     led_shift = 1 + ctz(leds);
00216     led_descriptor += led_shift;
00217     led_gpio_port = &AVR32_GPIO.port[led_descriptor->GPIO.PORT];
00218     led_gpio_port->ovrt  = led_descriptor->GPIO.PIN_MASK;
00219     led_gpio_port->oders = led_descriptor->GPIO.PIN_MASK;
00220     led_gpio_port->gpers = led_descriptor->GPIO.PIN_MASK;
00221     leds >>= led_shift;
00222   }
00223 }
00224 
00225 
00226 U32 LED_Read_Display_Field(U32 field)
00227 {
00228   return Rd_bitfield(LED_State, field);
00229 }
00230 
00231 
00232 void LED_Display_Field(U32 field, U32 leds)
00233 {
00234   LED_Display_Mask(field, leds << ctz(field));
00235 }
00236 
00237 
00238 U8 LED_Get_Intensity(U32 led)
00239 {
00240   tLED_DESCRIPTOR *led_descriptor;
00241 
00242   // Check that the argument value is valid.
00243   led = ctz(led);
00244   led_descriptor = &LED_DESCRIPTOR[led];
00245   if (led >= LED_COUNT || led_descriptor->PWM.CHANNEL < 0) return 0;
00246 
00247   // Return the duty cycle value if the LED PWM channel is enabled, else 0.
00248   return (AVR32_PWM.sr & (1 << led_descriptor->PWM.CHANNEL)) ?
00249            AVR32_PWM.channel[led_descriptor->PWM.CHANNEL].cdty : 0;
00250 }
00251 
00252 
00253 void LED_Set_Intensity(U32 leds, U8 intensity)
00254 {
00255   tLED_DESCRIPTOR *led_descriptor = &LED_DESCRIPTOR[0] - 1;
00256   volatile avr32_pwm_channel_t *led_pwm_channel;
00257   volatile avr32_gpio_port_t *led_gpio_port;
00258   U8 led_shift;
00259 
00260   // For each specified LED...
00261   for (leds &= (1 << LED_COUNT) - 1; leds; leds >>= led_shift)
00262   {
00263     // Select the next specified LED and check that it has a PWM channel.
00264     led_shift = 1 + ctz(leds);
00265     led_descriptor += led_shift;
00266     if (led_descriptor->PWM.CHANNEL < 0) continue;
00267 
00268     // Initialize or update the LED PWM channel.
00269     led_pwm_channel = &AVR32_PWM.channel[led_descriptor->PWM.CHANNEL];
00270     if (!(AVR32_PWM.sr & (1 << led_descriptor->PWM.CHANNEL)))
00271     {
00272       led_pwm_channel->cmr = (AVR32_PWM_CPRE_MCK << AVR32_PWM_CPRE_OFFSET) &
00273                              ~(AVR32_PWM_CALG_MASK |
00274                                AVR32_PWM_CPOL_MASK |
00275                                AVR32_PWM_CPD_MASK);
00276       led_pwm_channel->cprd = 0x000000FF;
00277       led_pwm_channel->cdty = intensity;
00278       AVR32_PWM.ena = 1 << led_descriptor->PWM.CHANNEL;
00279     }
00280     else
00281     {
00282       AVR32_PWM.isr;
00283       while (!(AVR32_PWM.isr & (1 << led_descriptor->PWM.CHANNEL)));
00284       led_pwm_channel->cupd = intensity;
00285     }
00286 
00287     // Switch the LED pin to its PWM function.
00288     led_gpio_port = &AVR32_GPIO.port[led_descriptor->GPIO.PORT];
00289     if (led_descriptor->PWM.FUNCTION & 0x1)
00290     {
00291       led_gpio_port->pmr0s = led_descriptor->GPIO.PIN_MASK;
00292     }
00293     else
00294     {
00295       led_gpio_port->pmr0c = led_descriptor->GPIO.PIN_MASK;
00296     }
00297     if (led_descriptor->PWM.FUNCTION & 0x2)
00298     {
00299       led_gpio_port->pmr1s = led_descriptor->GPIO.PIN_MASK;
00300     }
00301     else
00302     {
00303       led_gpio_port->pmr1c = led_descriptor->GPIO.PIN_MASK;
00304     }
00305     led_gpio_port->gperc = led_descriptor->GPIO.PIN_MASK;
00306   }
00307 }

Generated on Wed May 7 14:40:10 2008 for AVR32114 Using the AVR32 LCD Controller by  doxygen 1.5.3-20071008