summaryrefslogtreecommitdiff
path: root/boards/ek-lm4f120xl/drivers/rgb.c
blob: f3107f2bb338f92def0ade3bdc4f199e680975e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//*****************************************************************************
//
// rgb.c - Evaluation board driver for RGB LED.
//
// Copyright (c) 2012 Texas Instruments Incorporated.  All rights reserved.
// Software License Agreement
// 
// Texas Instruments (TI) is supplying this software for use solely and
// exclusively on TI's microcontroller products. The software is owned by
// TI and/or its suppliers, and is protected under applicable copyright
// laws. You may not combine this software with "viral" open-source
// software in order to form a larger program.
// 
// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
// DAMAGES, FOR ANY REASON WHATSOEVER.
// 
// This is part of revision 9453 of the EK-LM4F120XL Firmware Package.
//
//*****************************************************************************

#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_timer.h"
#include "driverlib/sysctl.h"
#include "driverlib/rom.h"
#include "driverlib/pin_map.h"
#include "driverlib/timer.h"
#include "driverlib/gpio.h"
#include "rgb.h"

//*****************************************************************************
//
//! \addtogroup rgb_api
//! @{
//
//*****************************************************************************

//*****************************************************************************
//
// This is a custom driver that allows the easy manipulation of the RGB LED.
//
// The driver uses the general purpose timers to govern the brightness of the 
// LED through simple PWM output mode of the GP Timers.
//
// A global array contains the current relative color of each of the three
// LEDs. A global float variable controls intensity of the overall mixed color.
//
//*****************************************************************************
static unsigned long  g_ulColors[3];
static float g_fIntensity = 0.3f;

//*****************************************************************************
//
//! Initializes the Timer and GPIO functionality associated with the RGB LED
//!
//! \param ulEnable enables RGB immediately if set.
//!
//! This function must be called during application initialization to
//! configure the GPIO pins to which the LEDs are attached.  It enables
//! the port used by the LEDs and configures each color's Timer. It optionally
//! enables the RGB LED by configuring the GPIO pins and starting the timers.
//!
//! \return None.
//
//*****************************************************************************
void 
RGBInit(unsigned long ulEnable)
{
    //
    // Enable the GPIO Port and Timer for each LED
    //
    SysCtlPeripheralEnable(RED_GPIO_PERIPH);
    SysCtlPeripheralEnable(RED_TIMER_PERIPH);

    SysCtlPeripheralEnable(GREEN_GPIO_PERIPH);
    SysCtlPeripheralEnable(GREEN_TIMER_PERIPH);

    SysCtlPeripheralEnable(BLUE_GPIO_PERIPH);
    SysCtlPeripheralEnable(BLUE_TIMER_PERIPH);

    //
    // Configure each timer for output mode
    //
    HWREG(GREEN_TIMER_BASE + TIMER_O_CFG)   = 0x04;
    HWREG(GREEN_TIMER_BASE + TIMER_O_TAMR)  = 0x0A;
    HWREG(GREEN_TIMER_BASE + TIMER_O_TAILR) = 0xFFFF;

    HWREG(BLUE_TIMER_BASE + TIMER_O_CFG)   = 0x04;
    HWREG(BLUE_TIMER_BASE + TIMER_O_TBMR)  = 0x0A;
    HWREG(BLUE_TIMER_BASE + TIMER_O_TBILR) = 0xFFFF;

    HWREG(RED_TIMER_BASE + TIMER_O_CFG)   = 0x04;
    HWREG(RED_TIMER_BASE + TIMER_O_TBMR)  = 0x0A;
    HWREG(RED_TIMER_BASE + TIMER_O_TBILR) = 0xFFFF;
    
    //
    // Invert the output signals.
    //
    HWREG(RED_TIMER_BASE + TIMER_O_CTL)   |= 0x4000;
    HWREG(GREEN_TIMER_BASE + TIMER_O_CTL)   |= 0x40;
    HWREG(BLUE_TIMER_BASE + TIMER_O_CTL)   |= 0x4000;
    
    if(ulEnable)
    {
        RGBEnable();
    }
}

//*****************************************************************************
//
//! Enable the RGB LED with already configured timer settings
//!
//! This function or RGBDisable should be called during application 
//! initialization to configure the GPIO pins to which the LEDs are attached.  
//! This function enables the timers and configures the GPIO pins as timer
//! outputs.
//!
//! \return None.
//
//*****************************************************************************
void 
RGBEnable(void)
{
    //
    // Enable timers to begin counting
    //
    TimerEnable(RED_TIMER_BASE, TIMER_BOTH);
    TimerEnable(GREEN_TIMER_BASE, TIMER_BOTH);
    TimerEnable(BLUE_TIMER_BASE, TIMER_BOTH);
    
    //
    // Reconfigure each LED's GPIO pad for timer control
    //
    GPIOPinConfigure(GREEN_GPIO_PIN_CFG);
    GPIOPinTypeTimer(GREEN_GPIO_BASE, GREEN_GPIO_PIN);
    GPIOPadConfigSet(GREEN_GPIO_BASE, GREEN_GPIO_PIN, GPIO_STRENGTH_8MA_SC,
                     GPIO_PIN_TYPE_STD);

    GPIOPinConfigure(BLUE_GPIO_PIN_CFG);
    GPIOPinTypeTimer(BLUE_GPIO_BASE, BLUE_GPIO_PIN);
    GPIOPadConfigSet(BLUE_GPIO_BASE, BLUE_GPIO_PIN, GPIO_STRENGTH_8MA_SC,
                     GPIO_PIN_TYPE_STD);

    GPIOPinConfigure(RED_GPIO_PIN_CFG);
    GPIOPinTypeTimer(RED_GPIO_BASE, RED_GPIO_PIN);
    GPIOPadConfigSet(RED_GPIO_BASE, RED_GPIO_PIN, GPIO_STRENGTH_8MA_SC,
                     GPIO_PIN_TYPE_STD);
}

//*****************************************************************************
//
//! Disable the RGB LED by configuring the GPIO's as inputs.
//!
//! This function or RGBEnable should be called during application 
//! initialization to configure the GPIO pins to which the LEDs are attached.  
//! This function disables the timers and configures the GPIO pins as inputs 
//! for minimum current draw.
//!
//! \return None.
//
//*****************************************************************************
void
RGBDisable(void)
{
    //
    // Configure the GPIO pads as general purpose inputs.
    //
    GPIOPinTypeGPIOInput(RED_GPIO_BASE, RED_GPIO_PIN);
    GPIOPinTypeGPIOInput(GREEN_GPIO_BASE, GREEN_GPIO_PIN);
    GPIOPinTypeGPIOInput(BLUE_GPIO_BASE, BLUE_GPIO_PIN);

    //
    // Stop the timer counting.
    //
    TimerDisable(RED_TIMER_BASE, TIMER_BOTH);
    TimerDisable(GREEN_TIMER_BASE, TIMER_BOTH);
    TimerDisable(BLUE_TIMER_BASE, TIMER_BOTH);
}

//*****************************************************************************
//
//! Set the output color and intensity.
//!
//! \param pulRGBColor points to a three element array representing the
//! relative intensity of each color.  Red is element 0, Green is element 1,
//! Blue is element 2. 0x0000 is off.  0xFFFF is fully on.
//!
//! \param fIntensity is used to scale the intensity of all three colors by
//! the same amount.  fIntensity should be between 0.0 and 1.0.  This scale
//! factor is applied to all three colors.
//!
//! This function should be called by the application to set the color and
//! intensity of the RGB LED.
//!
//! \return None.
//
//*****************************************************************************
void
RGBSet(volatile unsigned long * pulRGBColor,  float fIntensity)
{
    RGBColorSet(pulRGBColor);
    RGBIntensitySet(fIntensity);
}

//*****************************************************************************
//
//! Set the output color.
//!
//! \param pulRGBColor points to a three element array representing the
//! relative intensity of each color.  Red is element 0, Green is element 1,
//! Blue is element 2. 0x0000 is off.  0xFFFF is fully on.
//!
//! This function should be called by the application to set the color
//! of the RGB LED.
//!
//! \return None.
//
//*****************************************************************************
void 
RGBColorSet(volatile unsigned long * pulRGBColor)
{
    unsigned long ulColor[3];
    unsigned long ulIndex;

    for(ulIndex=0; ulIndex < 3; ulIndex++)
    {
        g_ulColors[ulIndex] = pulRGBColor[ulIndex];
        ulColor[ulIndex] = (unsigned long) (((float) pulRGBColor[ulIndex]) *
                            g_fIntensity + 0.5f);

        if(ulColor[ulIndex] > 0xFFFF)
        {
            ulColor[ulIndex] = 0xFFFF;
        }
    }

    TimerMatchSet(RED_TIMER_BASE, RED_TIMER, ulColor[RED]);
    TimerMatchSet(GREEN_TIMER_BASE, GREEN_TIMER, ulColor[GREEN]);
    TimerMatchSet(BLUE_TIMER_BASE, BLUE_TIMER, ulColor[BLUE]);
}

//*****************************************************************************
//
//! Set the current output intensity.
//!
//! \param fIntensity is used to scale the intensity of all three colors by
//! the same amount.  fIntensity should be between 0.0 and 1.0.  This scale
//! factor is applied individually to all three colors.
//!
//! This function should be called by the application to set the intensity
//! of the RGB LED.
//!
//! \return None.
//
//*****************************************************************************
void 
RGBIntensitySet(float fIntensity)
{
    g_fIntensity = fIntensity;
    RGBColorSet(g_ulColors);
}

//*****************************************************************************
//
//! Get the output color.
//!
//! \param pulRGBColor points to a three element array representing the
//! relative intensity of each color.  Red is element 0, Green is element 1,
//! Blue is element 2. 0x0000 is off.  0xFFFF is fully on. Caller must allocate
//! and pass a pointer to a three element array of unsigned longs.
//!
//! This function should be called by the application to get the current color
//! of the RGB LED.
//!
//! \return None.
//
//*****************************************************************************
void 
RGBColorGet(unsigned long * pulRGBColor)
{
    unsigned long ulIndex;

    for(ulIndex=0; ulIndex < 3; ulIndex++)
    {
        pulRGBColor[ulIndex] = g_ulColors[ulIndex];
    }
}

//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************