OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [AVR32_UC3/] [BOARDS/] [EVK1101/] [led.c] - Blame information for rev 866

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 589 jeremybenn
/*This file is prepared for Doxygen automatic documentation generation.*/
2
/*! \file *********************************************************************
3
 *
4
 * \brief AT32UC3B EVK1101 board LEDs support package.
5
 *
6
 * This file contains definitions and services related to the LED features of
7
 * the EVK1101 board.
8
 *
9
 * - Compiler:           IAR EWAVR32 and GNU GCC for AVR32
10
 * - Supported devices:  All AVR32 AT32UC3B devices can be used.
11
 * - AppNote:
12
 *
13
 * \author               Atmel Corporation: http://www.atmel.com \n
14
 *                       Support and FAQ: http://support.atmel.no/
15
 *
16
 ******************************************************************************/
17
 
18
/* Copyright (c) 2007, Atmel Corporation All rights reserved.
19
 *
20
 * Redistribution and use in source and binary forms, with or without
21
 * modification, are permitted provided that the following conditions are met:
22
 *
23
 * 1. Redistributions of source code must retain the above copyright notice,
24
 * this list of conditions and the following disclaimer.
25
 *
26
 * 2. Redistributions in binary form must reproduce the above copyright notice,
27
 * this list of conditions and the following disclaimer in the documentation
28
 * and/or other materials provided with the distribution.
29
 *
30
 * 3. The name of ATMEL may not be used to endorse or promote products derived
31
 * from this software without specific prior written permission.
32
 *
33
 * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
34
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
35
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
36
 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
37
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
42
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
 */
44
 
45
 
46
#include <avr32/io.h>
47
#include "preprocessor.h"
48
#include "compiler.h"
49
#include "evk1101.h"
50
#include "led.h"
51
 
52
 
53
//! Structure describing LED hardware connections.
54
typedef const struct
55
{
56
  struct
57
  {
58
    U32 PORT;     //!< LED GPIO port.
59
    U32 PIN_MASK; //!< Bit-mask of LED pin in GPIO port.
60
  } GPIO; //!< LED GPIO descriptor.
61
  struct
62
  {
63
    S32 CHANNEL;  //!< LED PWM channel (< 0 if N/A).
64
    S32 FUNCTION; //!< LED pin PWM function (< 0 if N/A).
65
  } PWM;  //!< LED PWM descriptor.
66
} tLED_DESCRIPTOR;
67
 
68
 
69
//! Hardware descriptors of all LEDs.
70
static tLED_DESCRIPTOR LED_DESCRIPTOR[LED_COUNT] =
71
{
72
#define INSERT_LED_DESCRIPTOR(LED_NO, unused)                 \
73
  {                                                           \
74
    {LED##LED_NO##_GPIO / 32, 1 << (LED##LED_NO##_GPIO % 32)},\
75
    {LED##LED_NO##_PWM,       LED##LED_NO##_PWM_FUNCTION    } \
76
  },
77
  MREPEAT(LED_COUNT, INSERT_LED_DESCRIPTOR, ~)
78
#undef INSERT_LED_DESCRIPTOR
79
};
80
 
81
 
82
//! Saved state of all LEDs.
83
static volatile U32 LED_State = (1 << LED_COUNT) - 1;
84
 
85
 
86
U32 LED_Read_Display(void)
87
{
88
  return LED_State;
89
}
90
 
91
 
92
void LED_Display(U32 leds)
93
{
94
  tLED_DESCRIPTOR *led_descriptor;
95
  volatile avr32_gpio_port_t *led_gpio_port;
96
 
97
  leds &= (1 << LED_COUNT) - 1;
98
  LED_State = leds;
99
  for (led_descriptor = &LED_DESCRIPTOR[0];
100
       led_descriptor < LED_DESCRIPTOR + LED_COUNT;
101
       led_descriptor++)
102
  {
103
    led_gpio_port = &AVR32_GPIO.port[led_descriptor->GPIO.PORT];
104
    if (leds & 1)
105
    {
106
      led_gpio_port->ovrc  = led_descriptor->GPIO.PIN_MASK;
107
    }
108
    else
109
    {
110
      led_gpio_port->ovrs  = led_descriptor->GPIO.PIN_MASK;
111
    }
112
    led_gpio_port->oders = led_descriptor->GPIO.PIN_MASK;
113
    led_gpio_port->gpers = led_descriptor->GPIO.PIN_MASK;
114
    leds >>= 1;
115
  }
116
}
117
 
118
 
119
U32 LED_Read_Display_Mask(U32 mask)
120
{
121
  return Rd_bits(LED_State, mask);
122
}
123
 
124
 
125
void LED_Display_Mask(U32 mask, U32 leds)
126
{
127
  tLED_DESCRIPTOR *led_descriptor = &LED_DESCRIPTOR[0] - 1;
128
  volatile avr32_gpio_port_t *led_gpio_port;
129
  U8 led_shift;
130
 
131
  mask &= (1 << LED_COUNT) - 1;
132
  Wr_bits(LED_State, mask, leds);
133
  while (mask)
134
  {
135
    led_shift = 1 + ctz(mask);
136
    led_descriptor += led_shift;
137
    led_gpio_port = &AVR32_GPIO.port[led_descriptor->GPIO.PORT];
138
    leds >>= led_shift - 1;
139
    if (leds & 1)
140
    {
141
      led_gpio_port->ovrc  = led_descriptor->GPIO.PIN_MASK;
142
    }
143
    else
144
    {
145
      led_gpio_port->ovrs  = led_descriptor->GPIO.PIN_MASK;
146
    }
147
    led_gpio_port->oders = led_descriptor->GPIO.PIN_MASK;
148
    led_gpio_port->gpers = led_descriptor->GPIO.PIN_MASK;
149
    leds >>= 1;
150
    mask >>= led_shift;
151
  }
152
}
153
 
154
 
155
Bool LED_Test(U32 leds)
156
{
157
  return Tst_bits(LED_State, leds);
158
}
159
 
160
 
161
void LED_Off(U32 leds)
162
{
163
  tLED_DESCRIPTOR *led_descriptor = &LED_DESCRIPTOR[0] - 1;
164
  volatile avr32_gpio_port_t *led_gpio_port;
165
  U8 led_shift;
166
 
167
  leds &= (1 << LED_COUNT) - 1;
168
  Clr_bits(LED_State, leds);
169
  while (leds)
170
  {
171
    led_shift = 1 + ctz(leds);
172
    led_descriptor += led_shift;
173
    led_gpio_port = &AVR32_GPIO.port[led_descriptor->GPIO.PORT];
174
    led_gpio_port->ovrs  = led_descriptor->GPIO.PIN_MASK;
175
    led_gpio_port->oders = led_descriptor->GPIO.PIN_MASK;
176
    led_gpio_port->gpers = led_descriptor->GPIO.PIN_MASK;
177
    leds >>= led_shift;
178
  }
179
}
180
 
181
 
182
void LED_On(U32 leds)
183
{
184
  tLED_DESCRIPTOR *led_descriptor = &LED_DESCRIPTOR[0] - 1;
185
  volatile avr32_gpio_port_t *led_gpio_port;
186
  U8 led_shift;
187
 
188
  leds &= (1 << LED_COUNT) - 1;
189
  Set_bits(LED_State, leds);
190
  while (leds)
191
  {
192
    led_shift = 1 + ctz(leds);
193
    led_descriptor += led_shift;
194
    led_gpio_port = &AVR32_GPIO.port[led_descriptor->GPIO.PORT];
195
    led_gpio_port->ovrc  = led_descriptor->GPIO.PIN_MASK;
196
    led_gpio_port->oders = led_descriptor->GPIO.PIN_MASK;
197
    led_gpio_port->gpers = led_descriptor->GPIO.PIN_MASK;
198
    leds >>= led_shift;
199
  }
200
}
201
 
202
 
203
void LED_Toggle(U32 leds)
204
{
205
  tLED_DESCRIPTOR *led_descriptor = &LED_DESCRIPTOR[0] - 1;
206
  volatile avr32_gpio_port_t *led_gpio_port;
207
  U8 led_shift;
208
 
209
  leds &= (1 << LED_COUNT) - 1;
210
  Tgl_bits(LED_State, leds);
211
  while (leds)
212
  {
213
    led_shift = 1 + ctz(leds);
214
    led_descriptor += led_shift;
215
    led_gpio_port = &AVR32_GPIO.port[led_descriptor->GPIO.PORT];
216
    led_gpio_port->ovrt  = led_descriptor->GPIO.PIN_MASK;
217
    led_gpio_port->oders = led_descriptor->GPIO.PIN_MASK;
218
    led_gpio_port->gpers = led_descriptor->GPIO.PIN_MASK;
219
    leds >>= led_shift;
220
  }
221
}
222
 
223
 
224
U32 LED_Read_Display_Field(U32 field)
225
{
226
  return Rd_bitfield(LED_State, field);
227
}
228
 
229
 
230
void LED_Display_Field(U32 field, U32 leds)
231
{
232
  LED_Display_Mask(field, leds << ctz(field));
233
}
234
 
235
 
236
U8 LED_Get_Intensity(U32 led)
237
{
238
  tLED_DESCRIPTOR *led_descriptor;
239
 
240
  // Check that the argument value is valid.
241
  led = ctz(led);
242
  led_descriptor = &LED_DESCRIPTOR[led];
243
  if (led >= LED_COUNT || led_descriptor->PWM.CHANNEL < 0) return 0;
244
 
245
  // Return the duty cycle value if the LED PWM channel is enabled, else 0.
246
  return (AVR32_PWM.sr & (1 << led_descriptor->PWM.CHANNEL)) ?
247
           AVR32_PWM.channel[led_descriptor->PWM.CHANNEL].cdty : 0;
248
}
249
 
250
 
251
void LED_Set_Intensity(U32 leds, U8 intensity)
252
{
253
  tLED_DESCRIPTOR *led_descriptor = &LED_DESCRIPTOR[0] - 1;
254
  volatile avr32_pwm_channel_t *led_pwm_channel;
255
  volatile avr32_gpio_port_t *led_gpio_port;
256
  U8 led_shift;
257
 
258
  // For each specified LED...
259
  for (leds &= (1 << LED_COUNT) - 1; leds; leds >>= led_shift)
260
  {
261
    // Select the next specified LED and check that it has a PWM channel.
262
    led_shift = 1 + ctz(leds);
263
    led_descriptor += led_shift;
264
    if (led_descriptor->PWM.CHANNEL < 0) continue;
265
 
266
    // Initialize or update the LED PWM channel.
267
    led_pwm_channel = &AVR32_PWM.channel[led_descriptor->PWM.CHANNEL];
268
    if (!(AVR32_PWM.sr & (1 << led_descriptor->PWM.CHANNEL)))
269
    {
270
      led_pwm_channel->cmr = (AVR32_PWM_CPRE_MCK << AVR32_PWM_CPRE_OFFSET) &
271
                             ~(AVR32_PWM_CALG_MASK |
272
                               AVR32_PWM_CPOL_MASK |
273
                               AVR32_PWM_CPD_MASK);
274
      led_pwm_channel->cprd = 0x000000FF;
275
      led_pwm_channel->cdty = intensity;
276
      AVR32_PWM.ena = 1 << led_descriptor->PWM.CHANNEL;
277
    }
278
    else
279
    {
280
      AVR32_PWM.isr;
281
      while (!(AVR32_PWM.isr & (1 << led_descriptor->PWM.CHANNEL)));
282
      led_pwm_channel->cupd = intensity;
283
    }
284
 
285
    // Switch the LED pin to its PWM function.
286
    led_gpio_port = &AVR32_GPIO.port[led_descriptor->GPIO.PORT];
287
    if (led_descriptor->PWM.FUNCTION & 0x1)
288
    {
289
      led_gpio_port->pmr0s = led_descriptor->GPIO.PIN_MASK;
290
    }
291
    else
292
    {
293
      led_gpio_port->pmr0c = led_descriptor->GPIO.PIN_MASK;
294
    }
295
    if (led_descriptor->PWM.FUNCTION & 0x2)
296
    {
297
      led_gpio_port->pmr1s = led_descriptor->GPIO.PIN_MASK;
298
    }
299
    else
300
    {
301
      led_gpio_port->pmr1c = led_descriptor->GPIO.PIN_MASK;
302
    }
303
    led_gpio_port->gperc = led_descriptor->GPIO.PIN_MASK;
304
  }
305
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.