LED.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2025, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*!*****************************************************************************
34  * @file LED.h
35  *
36  * @brief LED driver
37  *
38  * The LED driver is provided for easy access to common LED functionality.
39  * All functionality can be replicated using the GPIO.h and PWM.h APIs.
40  *
41  * @anchor ti_drivers_LED_Synopsis
42  * # Synopsis #
43  *
44  * @code
45  * #include <ti/drivers/apps/LED.h>
46  *
47  * LED_Handle handle;
48  * LED_Params ledParams;
49  *
50  * // Initialize the driver
51  * LED_init();
52  *
53  * // Open LED instance with default parameters
54  * LED_Params_init(&ledParams);
55  * handle = LED_open(CONFIG_LED_0, &ledParams);
56  *
57  * // Turn on, set brightness, and blink
58  * // If LED is configured as GPIO controlled, brightness is ignored
59  * LED_setOn(handle, 80);
60  * LED_startBlinking(handle, 500, LED_BLINK_FOREVER);
61  *
62  * LED_setOff(handle);
63  * LED_close(handle);
64  *
65  * @endcode
66  *
67  * @anchor ti_drivers_LED_Examples
68  * ## Examples #
69  *
70  * * @ref ti_drivers_LED_Examples_config_array "Generic Configuration"
71  * * @ref ti_drivers_LED_Examples_gpio_config "GPIO Configuration"
72  * * @ref ti_drivers_LED_Examples_pwm_led "PWM Mode"
73  *
74  * # Operation #
75  * LED driver simplifies using an LED (may be GPIO or PWM controlled)
76  * available on board and supports following operations -
77  *
78  * 1. To Turn ON/OFF
79  * 2. Blink with requested delay, stop when requested
80  * 3. Vary brightness (can only be done to a PWM controlled LED)
81  * 4. Toggle
82  *
83  * There are also APIs to open and close an LED handle and also one to get
84  * current state of a LED. User can request to set a LED into particular state
85  * while opening itself i.e. to start blink as part of LED_open() call.
86  *
87  * LED_init() must be called before using LED_open().
88  *
89  * ## Defining #LED_Config, #LED_Object and #LED_HWAttrs #
90  * To use the LED driver, an application has to indicate how many LEDs it
91  * wants to operate, of what type (PWM or GPIO controlled), and which GPIO or
92  * PWM to index for each LED.
93  *
94  * Each structure must be defined by the application. The following
95  * example is for an MSP432P401R platform in which four LEDs are available
96  * on board.
97  * The following declarations are placed in "ti_drivers_config.c".
98  * How the gpio indices are defined is detailed in the next section.
99  *
100  * ### LED_config structures #
101  * @anchor ti_drivers_LED_Examples_config_array
102  * "ti_drivers_config.c"
103  * @code
104  * #include <ti/drivers/apps/LED.h>
105  *
106  * LED_Object LED_object[4];
107  *
108  * const LED_HWAttrs LED_hwAttrs[4] = {
109  * {
110  * .index = CONFIG_LED1,
111  * .type = LED_GPIO_CONTROLLED
112  * },
113  * {
114  * .index = CONFIG_LED_RED,
115  * .type = LED_GPIO_CONTROLLED
116  * },
117  * {
118  * .index = CONFIG_NA_GPIO_PWMLED,
119  * .type = LED_PWM_CONTROLLED
120  * },
121  * {
122  * .index = CONFIG_NA_GPIO_PWMLED,
123  * .type = LED_PWM_CONTROLLED
124  * }
125  * };
126  *
127  * const LED_Config LED_config[] = {
128  * {
129  * .object = &LED_object[0],
130  * .hwAttrs = &LED_hwAttrs[0],
131  * },
132  * {
133  * .object = &LED_object[1],
134  * .hwAttrs = &LED_hwAttrs[1],
135  * },
136  * {
137  * .object = &LED_object[2],
138  * .hwAttrs = &LED_hwAttrs[2],
139  * },
140  * {
141  * .object = &LED_object[3],
142  * .hwAttrs = &LED_hwAttrs[3],
143  * }
144  * };
145  *
146  * uint32_t LED_count = 4;
147  *
148  * @endcode
149  *
150  * ##Setting up a GPIO controlled LED #
151  * The following code snippet shows how a GPIO pin controlling an LED is
152  * configured. The index the user provides to LED_open() corresponds to an
153  * entry in the #GPIO_PinConfig array which will source the LED. It is the
154  * user's responsibility to ensure that the pin is configured properly in the
155  * pin array. Typically this means configuring the pin as an output.
156  *
157  * ### GPIO controlled LED #
158  * @anchor ti_drivers_LED_Examples_gpio_config
159  *
160  * The following definitions are in
161  * "ti_drivers_config.h" and "ti_drivers_config.c" respectively. This
162  * example uses GPIO pins 1.0 and 2.0 which control LED1 and RED LED on
163  * LaunchPad respectively. In addition to the structures shown below, the
164  * other GPIO configuration data must exist. See @ref GPIO.h.
165  *
166  * "ti_drivers_config.h"
167  * @code
168  * #define CONFIG_LED1 0
169  * #define CONFIG_LED_RED 1
170  * @endcode
171  *
172  * "ti_drivers_config.c"
173  * @code
174  * #include <ti/drivers/GPIO.h>
175  * GPIO_PinConfig gpioPinConfigs[] = {
176  * GPIOMSP432_P1_0 | GPIO_CFG_OUT_STD | GPIO_CFG_OUT_STR_HIGH | GPIO_CFG_OUT_LOW,
177  * GPIOMSP432_P2_0 | GPIO_CFG_OUT_STD | GPIO_CFG_OUT_STR_HIGH | GPIO_CFG_OUT_LOW,
178  * }
179  *
180  * @endcode
181  *
182  * ## Configuring a PWM controlled LED #
183  * The LED driver allows for an LED to be driven by the PWM driver. This allows
184  * the user to set a brightness level in addition to the other LED features.
185  * The user must specify in the #LED_HWAttrs of each #LED_Config entry which
186  * #PWM_Config the LED instance is allowed to use. LED instances cannot share
187  * a PWM instance.
188  *
189  * The user may specify the period of the PWM signal in the #LED_Params passed
190  * to LED_open(). This is not to be confused with #LED_Params.blinkPeriod
191  * which specifies the default blink period.
192  *
193  * ### Opening a PWM LED #
194  * @anchor ti_drivers_LED_Examples_pwm_led
195  *
196  * We will borrow the 3rd LED_config entry from the
197  * @ref ti_drivers_LED_Examples_config_array
198  *
199  * In "ti_drivers_config.h"
200  * @code
201  * #define CONFIG_LED_0 0
202  * @endcode
203  *
204  * In application code:
205  * @code
206  * #include <ti/drivers/apps/LED.h>
207  *
208  * LED_Handle ledHandle;
209  * LED_Params ledParams;
210  *
211  * LED_init();
212  *
213  * LED_Params_init(&ledParams);
214  * ledParams.pwmPeriod = 100; // 0.1 ms period
215  * ledParams.blinkPeriod = 500; // LED will toggle twice a second
216  * ledParams.setState = LED_STATE_BLINKING; // Start LED blink on open
217  * ledHandle = LED_open(CONFIG_LED_0, &ledParams); // Open the first LED_Config
218  *
219  * // Turn on at half brightness level
220  * LED_setOn(ledHandle, 50);
221  * @endcode
222  *
223  *******************************************************************************
224  */
225 
226 #ifndef ti_drivers_LED__include
227 #define ti_drivers_LED__include
228 
229 #include <stdint.h>
230 #include <stdbool.h>
231 
232 /* Driver Header files */
233 #include <ti/drivers/GPIO.h>
234 #include <ti/drivers/PWM.h>
235 #include <ti/drivers/dpl/ClockP.h>
236 
237 #ifdef __cplusplus
238 extern "C" {
239 #endif
240 
241 #define LED_BRIGHTNESS_MAX 100U /* Max brightness in % is 100%*/
242 #define LED_BRIGHTNESS_MIN 0U /* Max brightness in % is 0%*/
243 
244 #define LED_ON 1U
245 #define LED_OFF 0U
246 
247 #define LED_BLINK_FOREVER 0xFFFF
248 
249 /* Number of user defined LED configurations */
250 extern const uint_least8_t LED_count;
251 
259 typedef enum
260 {
261  LED_NONE = 0,
264 } LED_Type;
265 
274 typedef enum
275 {
279 } LED_State;
280 
290 typedef struct
291 {
293  void *object;
295  void const *hwAttrs;
296 } LED_Config;
297 
302 
310 typedef struct
311 {
312  uint_least8_t index;
313  LED_Type type; /*<! GPIO (binary) or PWM (dimmable) control */
314 } LED_HWAttrs;
315 
321 typedef struct
322 {
323  uint32_t pwmPeriod;
327  LED_State state;
328  LED_State rawState;
330  LED_Type ledType;
331  uint8_t brightness;
332  uint_least8_t gpioIndex;
333  uint16_t togglePeriod;
336  uint16_t blinkCount;
337 } LED_Object;
338 
349 typedef struct
350 {
351  uint32_t pwmPeriod;
352  uint16_t blinkPeriod;
353  uint8_t brightness;
354  LED_State setState;
355 } LED_Params;
356 
368 extern void LED_close(LED_Handle ledHandle);
369 
380 extern LED_State LED_getState(LED_Handle ledHandle);
381 
387 extern void LED_init(void);
388 
411 LED_Handle LED_open(uint_least8_t index, LED_Params *params);
412 
425 extern void LED_Params_init(LED_Params *params);
426 
439 extern bool LED_setBrightnessLevel(LED_Handle ledHandle, uint8_t level);
440 
448 extern bool LED_setOff(LED_Handle ledHandle);
449 
460 extern bool LED_setOn(LED_Handle ledHandle, uint8_t brightness);
461 
478 extern void LED_startBlinking(LED_Handle ledHandle, uint16_t blinkPeriod, uint16_t blinkCount);
479 
486 extern void LED_stopBlinking(LED_Handle ledHandle);
487 
494 extern void LED_toggle(LED_Handle ledHandle);
495 
504 extern void LED_write(LED_Handle ledHandle, bool value);
505 
506 #ifdef __cplusplus
507 }
508 #endif
509 
510 #endif /* ti_drivers_LED__include */
uint16_t togglePeriod
Definition: LED.h:333
LED Parameters.
Definition: LED.h:349
bool LED_setBrightnessLevel(LED_Handle ledHandle, uint8_t level)
Function to set brightness level of a LED.
ADC_Params params
Definition: Driver_Init.h:11
Definition: LED.h:263
LED Object structure.
Definition: LED.h:321
PWM_Handle pwmHandle
Definition: LED.h:324
void LED_close(LED_Handle ledHandle)
Function to close a LED specified by the LED handle.
Definition: LED.h:276
LED_Type type
Definition: LED.h:313
ClockP structure.
Definition: ClockP.h:81
void LED_Params_init(LED_Params *params)
Function to initialize a LED_Params struct to its defaults.
Clock interface for the RTOS Porting Interface.
Definition: LED.h:261
PWM Global configuration.
Definition: PWM.h:438
LED configuration.
Definition: LED.h:290
ClockP_Handle clockHandle
Definition: LED.h:325
uint16_t blinkCount
Definition: LED.h:336
uint8_t brightness
Definition: LED.h:331
LED_State setState
Definition: LED.h:354
Pulse Width Modulation (PWM) driver.
LED_State state
Definition: LED.h:327
void LED_toggle(LED_Handle ledHandle)
Function to toggle an LED.
uint32_t pwmPeriod
Definition: LED.h:323
bool LED_setOff(LED_Handle ledHandle)
Function to turn off an LED.
LED_Handle LED_open(uint_least8_t index, LED_Params *params)
Function to open an instance of LED.
uint32_t pwmPeriod
Definition: LED.h:351
Definition: LED.h:277
void LED_stopBlinking(LED_Handle ledHandle)
Function to stop an LED blinking.
Hardware specific settings for a LED module.
Definition: LED.h:310
uint8_t brightness
Definition: LED.h:353
uint16_t blinkPeriod
Definition: LED.h:352
LED_State rawState
Definition: LED.h:328
bool LED_setOn(LED_Handle ledHandle, uint8_t brightness)
Function to turn on an LED.
LED_Config * LED_Handle
A handle that is returned from a LED_open() call.
Definition: LED.h:301
uint_least8_t gpioIndex
Definition: LED.h:332
LED_State
LED State.
Definition: LED.h:274
LED_Type ledType
Definition: LED.h:330
void LED_init(void)
Function to initialize LED driver.
ClockP_Struct clock
Definition: LED.h:326
General Purpose I/O driver interface.
LED_State LED_getState(LED_Handle ledHandle)
Function to get LED state.
const uint_least8_t LED_count
void const * hwAttrs
Definition: LED.h:295
void LED_write(LED_Handle ledHandle, bool value)
Specify binary state of an LED.
void LED_startBlinking(LED_Handle ledHandle, uint16_t blinkPeriod, uint16_t blinkCount)
Function to start an LED blinking.
Definition: LED.h:278
uint_least8_t index
Definition: LED.h:312
LED_Type
LED types based on control source.
Definition: LED.h:259
void * object
Definition: LED.h:293
Definition: LED.h:262
void * ClockP_Handle
Opaque client reference to an instance of a ClockP.
Definition: ClockP.h:112
© Copyright 1995-2025, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale