LED.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2021, 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  * // Assume our LED is configured to be a PWM LED
51  * LED_Params_init(&ledParams);
52  * ledHandle = LED_open(CONFIG_LED0, &ledParams);
53  *
54  * // Turn on, set brightness, and blink
55  * LED_setOn(handle, 80);
56  * LED_startBlinking(handle, 500, LED_BLINK_FOREVER);
57  *
58  * LED_setOff(handle);
59  * LED_close(handle);
60  *
61  * @endcode
62  *
63  * @anchor ti_drivers_LED_Examples
64  * ## Examples #
65  *
66  * * @ref ti_drivers_LED_Examples_config_array "Generic Configuration"
67  * * @ref ti_drivers_LED_Examples_gpio_config "GPIO Configuration"
68  * * @ref ti_drivers_LED_Examples_pwm_led "PWM Mode"
69  *
70  * # Operation #
71  * LED driver simplifies using an LED (may be GPIO or PWM controlled)
72  * available on board and supports following operations -
73  *
74  * 1. To Turn ON/OFF
75  * 2. Blink with requested delay, stop when requested
76  * 3. Vary brightness (can only be done to a PWM controlled LED)
77  * 4. Toggle
78  *
79  * There are also APIs to open and close an LED handle and also one to get
80  * current state of a LED. User can request to set a LED into particular state
81  * while opening itself i.e. to start blink as part of LED_open() call.
82  *
83  * LED_init() must be called before using LED_open().
84  *
85  * ## Defining #LED_Config, #LED_Object and #LED_HWAttrs #
86  * To use the LED driver, an application has to indicate how many LEDs it
87  * wants to operate, of what type (PWM or GPIO controlled), and which GPIO or
88  * PWM to index for each LED.
89  *
90  * Each structure must be defined by the application. The following
91  * example is for an MSP432P401R platform in which four LEDs are available
92  * on board.
93  * The following declarations are placed in "ti_drivers_config.c".
94  * How the gpio indices are defined is detailed in the next section.
95  *
96  * ### LED_config structures #
97  * @anchor ti_drivers_LED_Examples_config_array
98  * "ti_drivers_config.c"
99  * @code
100  * #include <ti/drivers/apps/LED.h>
101  *
102  * LED_Object LED_object[4];
103  *
104  * const LED_HWAttrs LED_hwAttrs[4] = {
105  * {
106  * .index = CONFIG_LED1,
107  * .type = LED_GPIO_CONTROLLED
108  * },
109  * {
110  * .index = CONFIG_LED_RED,
111  * .type = LED_GPIO_CONTROLLED
112  * },
113  * {
114  * .index = CONFIG_NA_GPIO_PWMLED,
115  * .type = LED_PWM_CONTROLLED
116  * },
117  * {
118  * .index = CONFIG_NA_GPIO_PWMLED,
119  * .type = LED_PWM_CONTROLLED
120  * }
121  * };
122  *
123  * const LED_Config LED_config[] = {
124  * {
125  * .object = &LED_object[0],
126  * .hwAttrs = &LED_hwAttrs[0],
127  * },
128  * {
129  * .object = &LED_object[1],
130  * .hwAttrs = &LED_hwAttrs[1],
131  * },
132  * {
133  * .object = &LED_object[2],
134  * .hwAttrs = &LED_hwAttrs[2],
135  * },
136  * {
137  * .object = &LED_object[3],
138  * .hwAttrs = &LED_hwAttrs[3],
139  * }
140  * };
141  *
142  * uint32_t LED_count = 4;
143  *
144  * @endcode
145  *
146  * ##Setting up a GPIO controlled LED #
147  * The following code snippet shows how a GPIO pin controlling an LED is
148  * configured. The index the user provides to LED_open() corresponds to an
149  * entry in the #GPIO_PinConfig array which will source the LED. It is the
150  * user's responsibility to ensure that the pin is configured properly in the
151  * pin array. Typically this means configuring the pin as an output.
152  *
153  * ### GPIO controlled LED #
154  * @anchor ti_drivers_LED_Examples_gpio_config
155  *
156  * The following definitions are in
157  * "ti_drivers_config.h" and "ti_drivers_config.c" respectively. This
158  * example uses GPIO pins 1.0 and 2.0 which control LED1 and RED LED on
159  * LaunchPad respectively. In addition to the structures shown below, the
160  * other GPIO configuration data must exist. See @ref GPIO.h.
161  *
162  * "ti_drivers_config.h"
163  * @code
164  * #define CONFIG_LED1 0
165  * #define CONFIG_LED_RED 1
166  * @endcode
167  *
168  * "ti_drivers_config.c"
169  * @code
170  * #include <ti/drivers/GPIO.h>
171  * GPIO_PinConfig gpioPinConfigs[] = {
172  * GPIOMSP432_P1_0 | GPIO_CFG_OUT_STD | GPIO_CFG_OUT_STR_HIGH | GPIO_CFG_OUT_LOW,
173  * GPIOMSP432_P2_0 | GPIO_CFG_OUT_STD | GPIO_CFG_OUT_STR_HIGH | GPIO_CFG_OUT_LOW,
174  * }
175  *
176  * @endcode
177  *
178  * ## Configuring a PWM controlled LED #
179  * The LED driver allows for an LED to be driven by the PWM driver. This allows
180  * the user to set a brightness level in addition to the other LED features.
181  * The user must specify in the #LED_HWAttrs of each #LED_Config entry which
182  * #PWM_Config the LED instance is allowed to use. LED instances cannot share
183  * a PWM instance.
184  *
185  * The user may specify the period of the PWM signal in the #LED_Params passed
186  * to LED_open(). This is not to be confused with #LED_Params.blinkPeriod
187  * which specifies the default blink period.
188  *
189  * ### Opening a PWM LED #
190  * @anchor ti_drivers_LED_Examples_pwm_led
191  *
192  * We will borrow the 3rd LED_config entry from the
193  * @ref ti_drivers_LED_Examples_config_array
194  *
195  * In "ti_drivers_config.h"
196  * @code
197  * #define CONFIG_LED0 0
198  * @endcode
199  *
200  * In application code:
201  * @code
202  * #include <ti/drivers/apps/LED.h>
203  *
204  * LED_Handle LEDHandle;
205  * LED_Params ledParams;
206  *
207  * LED_Params_init(&ledParams);
208  * ledParams.pwmPeriod = 100; // 0.1 ms period
209  * ledParams.blinkPeriod = 500; // LED will toggle twice a second
210  * ledParams.setState = LED_STATE_BLINKING; // Start LED blink on open
211  * ledHandle = LED_open(CONFIG_LED0, &ledParams); // Open the first LED_Config
212  *
213  * // Turn on at half brightness level
214  * LED_setOn(ledHandle, 50);
215  * @endcode
216  *
217  *******************************************************************************
218  */
219 
220 #ifndef ti_drivers_LED__include
221 #define ti_drivers_LED__include
222 
223 #include <stdint.h>
224 #include <stdbool.h>
225 
226 /* Driver Header files */
227 #include <ti/drivers/GPIO.h>
228 #include <ti/drivers/PWM.h>
229 #include <ti/drivers/dpl/ClockP.h>
230 
231 #ifdef __cplusplus
232 extern "C" {
233 #endif
234 
235 #define LED_BRIGHTNESS_MAX 100U /* Max brightness in % is 100%*/
236 #define LED_BRIGHTNESS_MIN 0U /* Max brightness in % is 0%*/
237 
238 #define LED_ON 1U
239 #define LED_OFF 0U
240 
241 #define LED_BLINK_FOREVER 0xFFFF
242 
243 /* Number of user defined LED configurations */
244 extern const uint_least8_t LED_count;
245 
253 typedef enum
254 {
255  LED_NONE = 0,
258 } LED_Type;
259 
268 typedef enum
269 {
273 } LED_State;
274 
284 typedef struct
285 {
287  void *object;
289  void const *hwAttrs;
290 } LED_Config;
291 
296 
304 typedef struct
305 {
306  uint_least8_t index;
307  LED_Type type; /*<! GPIO (binary) or PWM (dimmable) control */
308 } LED_HWAttrs;
309 
315 typedef struct
316 {
317  uint32_t pwmPeriod;
321  LED_State state;
322  LED_State rawState;
324  LED_Type ledType;
325  uint8_t brightness;
326  uint_least8_t gpioIndex;
327  uint16_t togglePeriod;
330  uint16_t blinkCount;
331 } LED_Object;
332 
343 typedef struct
344 {
345  uint32_t pwmPeriod;
346  uint16_t blinkPeriod;
347  uint8_t brightness;
348  LED_State setState;
349 } LED_Params;
350 
362 extern void LED_close(LED_Handle ledHandle);
363 
374 extern LED_State LED_getState(LED_Handle ledHandle);
375 
381 extern void LED_init(void);
382 
405 LED_Handle LED_open(uint_least8_t index, LED_Params *params);
406 
419 extern void LED_Params_init(LED_Params *params);
420 
433 extern bool LED_setBrightnessLevel(LED_Handle ledHandle, uint8_t level);
434 
442 extern bool LED_setOff(LED_Handle ledHandle);
443 
454 extern bool LED_setOn(LED_Handle ledHandle, uint8_t brightness);
455 
472 extern void LED_startBlinking(LED_Handle ledHandle, uint16_t blinkPeriod, uint16_t blinkCount);
473 
480 extern void LED_stopBlinking(LED_Handle ledHandle);
481 
488 extern void LED_toggle(LED_Handle ledHandle);
489 
498 extern void LED_write(LED_Handle ledHandle, bool value);
499 
500 #ifdef __cplusplus
501 }
502 #endif
503 
504 #endif /* ti_drivers_LED__include */
uint16_t togglePeriod
Definition: LED.h:327
LED Parameters.
Definition: LED.h:343
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:257
LED Object structure.
Definition: LED.h:315
PWM_Handle pwmHandle
Definition: LED.h:318
void LED_close(LED_Handle ledHandle)
Function to close a LED specified by the LED handle.
Definition: LED.h:270
LED_Type type
Definition: LED.h:307
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:255
PWM Global configuration.
Definition: PWM.h:438
LED configuration.
Definition: LED.h:284
ClockP_Handle clockHandle
Definition: LED.h:319
uint16_t blinkCount
Definition: LED.h:330
uint8_t brightness
Definition: LED.h:325
LED_State setState
Definition: LED.h:348
Pulse Width Modulation (PWM) driver.
LED_State state
Definition: LED.h:321
void LED_toggle(LED_Handle ledHandle)
Function to toggle an LED.
uint32_t pwmPeriod
Definition: LED.h:317
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:345
Definition: LED.h:271
void LED_stopBlinking(LED_Handle ledHandle)
Function to stop an LED blinking.
Hardware specific settings for a LED module.
Definition: LED.h:304
uint8_t brightness
Definition: LED.h:347
uint16_t blinkPeriod
Definition: LED.h:346
LED_State rawState
Definition: LED.h:322
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:295
uint_least8_t gpioIndex
Definition: LED.h:326
LED_State
LED State.
Definition: LED.h:268
LED_Type ledType
Definition: LED.h:324
void LED_init(void)
Function to initialize LED driver.
ClockP_Struct clock
Definition: LED.h:320
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:289
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:272
uint_least8_t index
Definition: LED.h:306
LED_Type
LED types based on control source.
Definition: LED.h:253
void * object
Definition: LED.h:287
Definition: LED.h:256
void * ClockP_Handle
Opaque client reference to an instance of a ClockP.
Definition: ClockP.h:112
© Copyright 1995-2024, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale