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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
//*****************************************************************************
//
// rgb.c - Evaluation board driver for RGB LED.
//
// Copyright (c) 2012-2014 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 2.1.0.12573 of the EK-TM4C123GXL Firmware Package.
//
//*****************************************************************************
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_timer.h"
#include "inc/hw_ints.h"
#include "driverlib/sysctl.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/pin_map.h"
#include "driverlib/timer.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.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.
//
// This implementation consumes the following hardware resources
// - Wide Timer 5B for blinking the entire RGB unit.
// - Timer 0B intensity of an RGB element
// - Timer 1A intensity of an RGB element
// - Timer 1B intensity of an RGB element
//
//*****************************************************************************
static uint32_t g_ui32Colors[3];
static float g_fIntensity = 0.3f;
//*****************************************************************************
//
//! Wide Timer interrupt to handle blinking effect of the RGB
//!
//! This function is called by the hardware interrupt controller on a timeout
//! of the wide timer. This function must be in the NVIC table in the startup
//! file. When called will toggle the enable flag to turn on or off the entire
//! RGB unit. This creates a blinking effect. A wide timer is used since the
//! blink is intended to be visible to the human eye and thus is expected to
//! have a frequency between 15 and 0.1 hz. Currently blink duty is fixed at
//! 50%.
//!
//! \return None.
//
//*****************************************************************************
void
RGBBlinkIntHandler(void)
{
static unsigned long ulFlags;
//
// Clear the timer interrupt.
//
ROM_TimerIntClear(WTIMER5_BASE, TIMER_TIMB_TIMEOUT);
//
// Toggle the flag for the blink timer.
//
ulFlags ^= 1;
if(ulFlags)
{
RGBEnable();
}
else
{
RGBDisable();
}
}
//*****************************************************************************
//
//! Initializes the Timer and GPIO functionality associated with the RGB LED
//!
//! \param ui32Enable 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(uint32_t ui32Enable)
{
//
// Enable the GPIO Port and Timer for each LED
//
ROM_SysCtlPeripheralEnable(RED_GPIO_PERIPH);
ROM_SysCtlPeripheralEnable(RED_TIMER_PERIPH);
ROM_SysCtlPeripheralEnable(GREEN_GPIO_PERIPH);
ROM_SysCtlPeripheralEnable(GREEN_TIMER_PERIPH);
ROM_SysCtlPeripheralEnable(BLUE_GPIO_PERIPH);
ROM_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(ui32Enable)
{
RGBEnable();
}
//
// Setup the blink functionality
//
ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_WTIMER5);
ROM_TimerConfigure(WTIMER5_BASE, TIMER_CFG_B_PERIODIC | TIMER_CFG_SPLIT_PAIR);
ROM_TimerLoadSet64(WTIMER5_BASE, 0xFFFFFFFFFFFFFFFF);
ROM_IntEnable(INT_WTIMER5B);
ROM_TimerIntEnable(WTIMER5_BASE, TIMER_TIMB_TIMEOUT);
}
//*****************************************************************************
//
//! 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
//
ROM_TimerEnable(RED_TIMER_BASE, TIMER_BOTH);
ROM_TimerEnable(GREEN_TIMER_BASE, TIMER_BOTH);
ROM_TimerEnable(BLUE_TIMER_BASE, TIMER_BOTH);
//
// Reconfigure each LED's GPIO pad for timer control
//
ROM_GPIOPinConfigure(GREEN_GPIO_PIN_CFG);
ROM_GPIOPinTypeTimer(GREEN_GPIO_BASE, GREEN_GPIO_PIN);
MAP_GPIOPadConfigSet(GREEN_GPIO_BASE, GREEN_GPIO_PIN, GPIO_STRENGTH_8MA_SC,
GPIO_PIN_TYPE_STD);
ROM_GPIOPinConfigure(BLUE_GPIO_PIN_CFG);
ROM_GPIOPinTypeTimer(BLUE_GPIO_BASE, BLUE_GPIO_PIN);
MAP_GPIOPadConfigSet(BLUE_GPIO_BASE, BLUE_GPIO_PIN, GPIO_STRENGTH_8MA_SC,
GPIO_PIN_TYPE_STD);
ROM_GPIOPinConfigure(RED_GPIO_PIN_CFG);
ROM_GPIOPinTypeTimer(RED_GPIO_BASE, RED_GPIO_PIN);
MAP_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.
//
ROM_GPIOPinTypeGPIOInput(RED_GPIO_BASE, RED_GPIO_PIN);
ROM_GPIOPinTypeGPIOInput(GREEN_GPIO_BASE, GREEN_GPIO_PIN);
ROM_GPIOPinTypeGPIOInput(BLUE_GPIO_BASE, BLUE_GPIO_PIN);
//
// Stop the timer counting.
//
ROM_TimerDisable(RED_TIMER_BASE, TIMER_BOTH);
ROM_TimerDisable(GREEN_TIMER_BASE, TIMER_BOTH);
ROM_TimerDisable(BLUE_TIMER_BASE, TIMER_BOTH);
}
//*****************************************************************************
//
//! Set the output color and intensity.
//!
//! \param pui32RGBColor 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 uint32_t * pui32RGBColor, float fIntensity)
{
RGBColorSet(pui32RGBColor);
RGBIntensitySet(fIntensity);
}
//*****************************************************************************
//
//! Set the output color.
//!
//! \param pui32RGBColor 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 uint32_t * pui32RGBColor)
{
uint32_t ui32Color[3];
uint32_t ui32Index;
for(ui32Index=0; ui32Index < 3; ui32Index++)
{
g_ui32Colors[ui32Index] = pui32RGBColor[ui32Index];
ui32Color[ui32Index] = (uint32_t) (((float) pui32RGBColor[ui32Index]) *
g_fIntensity + 0.5f);
if(ui32Color[ui32Index] > 0xFFFF)
{
ui32Color[ui32Index] = 0xFFFF;
}
}
ROM_TimerMatchSet(RED_TIMER_BASE, RED_TIMER, ui32Color[RED]);
ROM_TimerMatchSet(GREEN_TIMER_BASE, GREEN_TIMER, ui32Color[GREEN]);
ROM_TimerMatchSet(BLUE_TIMER_BASE, BLUE_TIMER, ui32Color[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_ui32Colors);
}
//*****************************************************************************
//
//! Sets the blink rate of the RGB Led
//!
//! \param fRate is the blink rate in hertz.
//!
//! This function controls the blink rate of the RGB LED in auto blink mode.
//! to enable blinking pass a non-zero floating pointer number. To disable
//! pass 0.0f as the argument. Calling this function will override the current
//! RGBDisable or RGBEnable status.
//!
//! \return None.
//
//*****************************************************************************
void
RGBBlinkRateSet(float fRate)
{
uint64_t ui64Load;
if(fRate == 0.0f)
{
//
// Disable the timer and enable the RGB. If blink rate is zero we
// assume we want the RGB to be enabled. To disable call RGBDisable
//
ROM_TimerDisable(WTIMER5_BASE, TIMER_B);
RGBEnable();
}
else
{
//
// Keep the math in floating pointing until the end so that we keep as
// much precision as we can.
//
ui64Load = (uint64_t) (((float)SysCtlClockGet()) / (fRate * 2.0f));
ROM_TimerLoadSet(WTIMER5_BASE, TIMER_B, ui64Load);
ROM_TimerEnable(WTIMER5_BASE, TIMER_B);
}
}
//*****************************************************************************
//
//! Get the output color.
//!
//! \param pui32RGBColor 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 uint32_ts.
//!
//! This function should be called by the application to get the current color
//! of the RGB LED.
//!
//! \return None.
//
//*****************************************************************************
void
RGBColorGet(uint32_t * pui32RGBColor)
{
uint32_t ui32Index;
for(ui32Index=0; ui32Index < 3; ui32Index++)
{
pui32RGBColor[ui32Index] = g_ui32Colors[ui32Index];
}
}
//*****************************************************************************
//
// Close the Doxygen group.
//! @}
//
//*****************************************************************************
|