Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
LED.h File Reference

Detailed Description

LED driver.

The LED driver is provided for easy access to common LED functionality. All functionality can be replicated using the GPIO.h and PWM.h APIs.


LED_Handle handle;
LED_Params ledParams;
// Assume our LED is configured to be a PWM LED
ledHandle = LED_open(CONFIG_LED0, &ledParams);
// Turn on, set brightness, and blink
LED_setOn(handle, 80);



LED driver simplifies using an LED (may be GPIO or PWM controlled) available on board and supports following operations -

1. To Turn ON/OFF
2. Blink with requested delay, stop when requested
3. Vary brightness (can only be done to a PWM controlled LED)
4. Toggle

There are also APIs to open and close an LED handle and also one to get current state of a LED. User can request to set a LED into particular state while opening itself i.e. to start blink as part of LED_open() call.

LED_init() must be called before using LED_open().

Defining LED_Config, LED_Object and LED_HWAttrs

To use the LED driver, an application has to indicate how many LEDs it wants to operate, of what type (PWM or GPIO controlled), and which GPIO or PWM to index for each LED.

Each structure must be defined by the application. The following example is for an MSP432P401R platform in which four LEDs are available on board. The following declarations are placed in "ti_drivers_config.c". How the gpio indices are defined is detailed in the next section.

LED_config structures


LED_Object LED_object[4];
const LED_HWAttrs LED_hwAttrs[4] = {
.index = CONFIG_LED1,
.index = CONFIG_LED_RED,
const LED_Config LED_config[] = {
.object = &LED_object[0],
.hwAttrs = &LED_hwAttrs[0],
.object = &LED_object[1],
.hwAttrs = &LED_hwAttrs[1],
.object = &LED_object[2],
.hwAttrs = &LED_hwAttrs[2],
.object = &LED_object[3],
.hwAttrs = &LED_hwAttrs[3],
uint32_t LED_count = 4;

Setting up a GPIO controlled LED

The following code snippet shows how a GPIO pin controlling an LED is configured. The index the user provides to LED_open() corresponds to an entry in the GPIO_PinConfig array which will source the LED. It is the user's responsibility to ensure that the pin is configured properly in the pin array. Typically this means configuring the pin as an output.

GPIO controlled LED

The following definitions are in "ti_drivers_config.h" and "ti_drivers_config.c" respectively. This example uses GPIO pins 1.0 and 2.0 which control LED1 and RED LED on LaunchPad respectively. In addition to the structures shown below, the other GPIO configuration data must exist. See GPIO.h.


#define CONFIG_LED1 0
#define CONFIG_LED_RED 1


Configuring a PWM controlled LED

The LED driver allows for an LED to be driven by the PWM driver. This allows the user to set a brightness level in addition to the other LED features. The user must specify in the LED_HWAttrs of each LED_Config entry which PWM_Config the LED instance is allowed to use. LED instances cannot share a PWM instance.

The user may specify the period of the PWM signal in the LED_Params passed to LED_open(). This is not to be confused with LED_Params.blinkPeriod which specifies the default blink period.

Opening a PWM LED

We will borrow the 3rd LED_config entry from the ti_drivers_LED_Examples_config_array

In "ti_drivers_config.h"

#define CONFIG_LED0 0

In application code:

LED_Handle LEDHandle;
LED_Params ledParams;
ledParams.pwmPeriod = 100; // 0.1 ms period
ledParams.blinkPeriod = 500; // LED will toggle twice a second
ledParams.setState = LED_STATE_BLINKING; // Start LED blink on open
ledHandle = LED_open(CONFIG_LED0, &ledParams); // Open the first LED_Config
// Turn on at half brightness level
LED_setOn(ledHandle, 50);
#include <stdint.h>
#include <stdbool.h>
#include <ti/drivers/GPIO.h>
#include <ti/drivers/PWM.h>
#include <ti/drivers/dpl/ClockP.h>
Include dependency graph for LED.h:

Go to the source code of this file.

Data Structures

struct  LED_Config
 LED configuration. More...
struct  LED_HWAttrs
 Hardware specific settings for a LED module. More...
struct  LED_Object
 LED Object structure. More...
struct  LED_Params
 LED Parameters. More...


#define LED_BRIGHTNESS_MAX   100U /* Max brightness in % is 100%*/
#define LED_BRIGHTNESS_MIN   0U /* Max brightness in % is 0%*/
#define LED_ON   1U
#define LED_OFF   0U


typedef LED_ConfigLED_Handle
 A handle that is returned from a LED_open() call. More...


 LED types based on control source. More...
 LED State. More...


void LED_close (LED_Handle ledHandle)
 Function to close a LED specified by the LED handle. More...
LED_State LED_getState (LED_Handle ledHandle)
 Function to get LED state. More...
void LED_init ()
 Function to initialize LED driver. More...
LED_Handle LED_open (uint_least8_t index, LED_Params *params)
 Function to open an instance of LED. More...
void LED_Params_init (LED_Params *params)
 Function to initialize a LED_Params struct to its defaults. More...
bool LED_setBrightnessLevel (LED_Handle ledHandle, uint8_t level)
 Function to set brightness level of a LED. More...
bool LED_setOff (LED_Handle ledHandle)
 Function to turn off an LED. More...
bool LED_setOn (LED_Handle ledHandle, uint8_t brightness)
 Function to turn on an LED. More...
void LED_startBlinking (LED_Handle ledHandle, uint16_t blinkPeriod, uint16_t blinkCount)
 Function to start an LED blinking. More...
void LED_stopBlinking (LED_Handle ledHandle)
 Function to stop an LED blinking. More...
void LED_toggle (LED_Handle ledHandle)
 Function to toggle an LED. More...
void LED_write (LED_Handle ledHandle, bool value)
 Specify binary state of an LED. More...


const uint_least8_t LED_count

Macro Definition Documentation


#define LED_BRIGHTNESS_MAX   100U /* Max brightness in % is 100%*/


#define LED_BRIGHTNESS_MIN   0U /* Max brightness in % is 0%*/


#define LED_ON   1U


#define LED_OFF   0U



Typedef Documentation

§ LED_Handle

A handle that is returned from a LED_open() call.

Enumeration Type Documentation

§ LED_Type

enum LED_Type

LED types based on control source.

A LED can be controlled by GPIO or PWM. Only a PWM controlled LED can be operated to show brightness variation. An unopened instance will be of type LED_NONE.


§ LED_State

enum LED_State

LED State.

A LED can be in OFF, ON or BLINKING state

State of particular LED may be tied with a warning/alert in system which a thread/task may want to know.


Function Documentation

§ LED_close()

void LED_close ( LED_Handle  ledHandle)

Function to close a LED specified by the LED handle.

This call will destruct associated clock, turn off LED, and close the PWM instance if applicable.

LED_open() had to be called first.
ledHandleA LED_Handle returned from LED_open()

§ LED_getState()

LED_State LED_getState ( LED_Handle  ledHandle)

Function to get LED state.

This function may be useful in scenarios if a LED state(ON/OFF/BLINKING) is tied with some system warning/alerts

ledHandleA LED_Handle returned from LED_open()
The LED State

§ LED_init()

void LED_init ( )

Function to initialize LED driver.

This function will initialize the LED driver.

§ LED_open()

LED_Handle LED_open ( uint_least8_t  index,
LED_Params params 

Function to open an instance of LED.

Function to open an LED instance in the LED_config array. The GPIO or PWM configuartions must already exist before this function is called. The LED_Params input can be used to specify the run time options of the LED instance.

LED_init() has to be called first
indexIndex into the LED_config array specifying the LED_Config that is to be used to open the LED.
*paramsA pointer to LED_Params structure. If NULL, it will use default values.
A LED_Handle on success, or a NULL on failure.
See also

§ LED_Params_init()

void LED_Params_init ( LED_Params params)

Function to initialize a LED_Params struct to its defaults.

paramsA pointer to LED_Params structure for initialization.

The default parameters are:

  • LED initially off
  • Blink period of zero
  • Max brightness (for PWM LED only)
  • PWM period of 1 ms (for PWM LED only)

§ LED_setBrightnessLevel()

bool LED_setBrightnessLevel ( LED_Handle  ledHandle,
uint8_t  level 

Function to set brightness level of a LED.

Ignores brightness settings if LED is not PWM controlled. Fails if requested brightness is above 100%.

ledHandleA LED_Handle
levelBrightness level in terms of percentage (0-100)
true on success or false upon failure.

§ LED_setOff()

bool LED_setOff ( LED_Handle  ledHandle)

Function to turn off an LED.

ledHandleAn LED_Handle
true on success or false upon failure.

§ LED_setOn()

bool LED_setOn ( LED_Handle  ledHandle,
uint8_t  brightness 

Function to turn on an LED.

ledHandleAn LED_Handle
brightnessBrightness level in terms of percentage 0-100%. Is ignored for non PWM LEDs.
true on success or false upon failure.

§ LED_startBlinking()

void LED_startBlinking ( LED_Handle  ledHandle,
uint16_t  blinkPeriod,
uint16_t  blinkCount 

Function to start an LED blinking.

ledHandleAn LED_Handle
blinkPeriodValue in ms which determines how often the LED blinks. A value of 1000 will cause the LED to blink once a second. The maximum value is ~65 seconds or 0xFFFF ms.
blinkCountIf not set to LED_BLINK_FOREVER, the LED will blink the specified number of times and then will turn off. A value of zero will stop the LED blinking. Maximum number of blinks is 0x7FFF or 32,767 blinks. An input exceeding this value will be truncated to 0x7FFF.

§ LED_stopBlinking()

void LED_stopBlinking ( LED_Handle  ledHandle)

Function to stop an LED blinking.

ledHandleAn LED_Handle

§ LED_toggle()

void LED_toggle ( LED_Handle  ledHandle)

Function to toggle an LED.

ledHandleAn LED_Handle

§ LED_write()

void LED_write ( LED_Handle  ledHandle,
bool  value 

Specify binary state of an LED.

ledHandleAn LED_Handle
valueTRUE for on, FALSE for off

Variable Documentation

§ LED_count

const uint_least8_t LED_count
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale