1 /*
2  * Copyright 2018-2020 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef __LED_H__
10 #define __LED_H__
11 
12 #include "fsl_common.h"
13 #include "fsl_adapter_gpio.h"
14 /*!
15  * @addtogroup LED
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 /*! @brief Definition to determine whether enable dimming. */
23 #ifndef LED_DIMMING_ENABLEMENT
24 #define LED_DIMMING_ENABLEMENT (0U) /*!< Enable or disable the dimming feature */
25 #endif
26 
27 /*! @brief Definition to determine whether enable color wheel. */
28 #ifndef LED_COLOR_WHEEL_ENABLEMENT
29 #define LED_COLOR_WHEEL_ENABLEMENT (0U) /*!< Enable or disable the color wheel feature */
30 #endif
31 
32 /*! @brief Definition to determine whether use confgure structure. */
33 #ifndef LED_USE_CONFIGURE_STRUCTURE
34 #define LED_USE_CONFIGURE_STRUCTURE (1U) /*!< Enable or disable the confgure structure pointer */
35 #endif
36 /*! @brief The handle of LED */
37 typedef void *led_handle_t;
38 
39 /*! @brief Definition of LED handle size. */
40 #if (defined(LED_DIMMING_ENABLEMENT) && (LED_DIMMING_ENABLEMENT > 0U))
41 /* HAL_GPIO_HANDLE_SIZE * 3 + HAL_PWM_HANDLE_SIZE *3 + LED dedicated size */
42 #define LED_HANDLE_SIZE ((16U * 3U) + (8U * 3U) + 40U)
43 #else
44 #if (defined(LED_USE_CONFIGURE_STRUCTURE) && (LED_USE_CONFIGURE_STRUCTURE > 0U))
45 /* HAL_GPIO_HANDLE_SIZE * 3 + LED dedicated size */
46 #define LED_HANDLE_SIZE ((16U * 3U) + 32U)
47 #else
48 /* HAL_GPIO_HANDLE_SIZE * 3 + LED dedicated size */
49 #define LED_HANDLE_SIZE ((16U * 3U) + 36U)
50 #endif
51 #endif
52 
53 /*!
54  * @brief Defines the led handle
55  *
56  * This macro is used to define a 4 byte aligned led handle.
57  * Then use "(led_handle_t)name" to get the led handle.
58  *
59  * The macro should be global and could be optional. You could also define led handle by yourself.
60  *
61  * This is an example,
62  * @code
63  * LED_HANDLE_DEFINE(ledHandle);
64  * @endcode
65  *
66  * @param name The name string of the led handle.
67  */
68 #define LED_HANDLE_DEFINE(name) uint32_t name[((LED_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t))]
69 
70 /*!
71  * @brief Defines the led handle array
72  *
73  * This macro is used to define a 4 byte aligned led handle array.
74  * Then use "(led_handle_t)name[0]" to get the first led handle.
75  *
76  * The macro should be global and could be optional. You could also define these led handle by yourself.
77  *
78  * This is an example,
79  * @code
80  * LED_HANDLE_ARRAY_DEFINE(ledHandleArray,1);
81  * @endcode
82  *
83  * @param name The name string of the led handle array.
84  * @param count The amount of led handle.
85  */
86 #define LED_HANDLE_ARRAY_DEFINE(name, count) \
87     uint32_t name[count][((LED_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t))]
88 
89 /*! @brief Definition of LED timer interval,unit is ms. */
90 #define LED_TIMER_INTERVAL (100U)
91 
92 /*! @brief Definition of LED dimming update interval,unit is ms. */
93 #define LED_DIMMING_UPDATE_INTERVAL (100U)
94 
95 /*! @brief Definition of LED flash cycle forever. */
96 #define LED_FLASH_CYCLE_FOREVER (0xFFFFFFFFU)
97 
98 /*! @brief Definition of LED blip interval,unit is ms. */
99 #define LED_BLIP_INTERVAL (250U)
100 
101 /*! @brief The status type of LED */
102 typedef enum _led_status
103 {
104     kStatus_LED_Success          = kStatus_Success,                  /*!< Success */
105     kStatus_LED_Error            = MAKE_STATUS(kStatusGroup_LED, 1), /*!< Failed */
106     kStatus_LED_InvalidParameter = MAKE_STATUS(kStatusGroup_LED, 2), /*!< Invalid parameter*/
107 } led_status_t;
108 
109 /*! @brief The flash type of LED */
110 typedef enum _led_flash_type
111 {
112     kLED_FlashOneColor = 0x00U, /*!< Fast with one color */
113 #if (defined(LED_COLOR_WHEEL_ENABLEMENT) && (LED_COLOR_WHEEL_ENABLEMENT > 0U))
114     kLED_FlashColorWheel, /*!< Fast with color wheel */
115 #endif                    /* (defined(LED_COLOR_WHEEL_ENABLEMENT) && (LED_COLOR_WHEEL_ENABLEMENT > 0U)) */
116 } led_flash_type_t;
117 
118 /*! @brief The color struct of LED */
119 typedef uint32_t led_color_t;
120 
121 /*! @brief Definition to set LED color. */
122 #define LED_MAKE_COLOR(r, g, b) ((led_color_t)((((led_color_t)b) << 16) | (((led_color_t)g) << 8) | ((led_color_t)r)))
123 
124 /*! @brief The color type of LED */
125 enum _led_color
126 {
127     kLED_Black      = LED_MAKE_COLOR(0, 0, 0),       /*!< Black */
128     kLED_Red        = LED_MAKE_COLOR(255, 0, 0),     /*!< Red */
129     kLED_Green      = LED_MAKE_COLOR(0, 255, 0),     /*!< Green */
130     kLED_Yellow     = LED_MAKE_COLOR(255, 255, 0),   /*!< Yellow */
131     kLED_Blue       = LED_MAKE_COLOR(0, 0, 255),     /*!< Blue */
132     kLED_Pink       = LED_MAKE_COLOR(255, 0, 255),   /*!< Pink */
133     kLED_Aquamarine = LED_MAKE_COLOR(0, 255, 255),   /*!< Aquamarine */
134     kLED_White      = LED_MAKE_COLOR(255, 255, 255), /*!< White */
135 };
136 
137 #if defined(__CC_ARM)
138 #pragma anon_unions
139 #endif
140 /*! @brief The pin config struct of LED */
141 typedef struct _led_pin_config
142 {
143     uint8_t dimmingEnable; /*!< dimming enable, 0 - disable, 1 - enable */
144     union
145     {
146 #if (defined(LED_USE_CONFIGURE_STRUCTURE) && (LED_USE_CONFIGURE_STRUCTURE > 0U))
147         hal_gpio_pin_config_t gpio;
148 #else
149         struct
150         {
151             uint8_t port;            /*!< GPIO Port */
152             uint8_t pin;             /*!< GPIO Pin */
153             uint8_t pinStateDefault; /*!< GPIO Pin voltage when LED is off (0 - low level, 1 - high level)*/
154         } gpio;
155 #endif
156         struct
157         {
158             uint32_t sourceClock;    /*!< The clock source of the PWM module */
159             uint8_t instance;        /*!< PWM instance of the pin */
160             uint8_t channel;         /*!< PWM channel of the pin */
161             uint8_t pinStateDefault; /*!< The Pin voltage when LED is off (0 - low level, 1 - high level)*/
162         } dimming;
163     };
164 } led_pin_config_t;
165 
166 /*! @brief The pin config struct of rgb LED */
167 typedef struct _led_rgb_config
168 {
169     led_pin_config_t redPin;   /*!< Red pin setting */
170     led_pin_config_t greenPin; /*!< Green pin setting */
171     led_pin_config_t bluePin;  /*!< Blue pin setting */
172 } led_rgb_config_t;
173 
174 /*! @brief The pin config struct of monochrome LED */
175 typedef struct _led_monochrome_config
176 {
177     led_pin_config_t monochromePin; /*!< Monochrome pin setting */
178 } led_monochrome_config_t;
179 
180 /*! @brief The type of LED */
181 typedef enum _led_type
182 {
183     kLED_TypeRgb        = 0x01U, /*!< RGB LED */
184     kLED_TypeMonochrome = 0x02U, /*!< Monochrome LED */
185 } led_type_t;
186 
187 /*! @brief The config struct of LED */
188 typedef struct _led_config
189 {
190     led_type_t type;
191     union
192     {
193         led_rgb_config_t ledRgb;               /*!< RGB setting */
194         led_monochrome_config_t ledMonochrome; /*!< Monochrome setting */
195     };
196 } led_config_t;
197 
198 /*! @brief The flash config struct of LED */
199 typedef struct _led_flash_config
200 {
201     uint32_t times;             /*!< Flash times, LED_FLASH_CYCLE_FOREVER for forever */
202     uint16_t period;            /*!< Flash period, unit is ms */
203     led_flash_type_t flashType; /*!< Flash type, one color or color wheel. Refer to #led_flash_type_t */
204     uint8_t duty;               /*!< Duty of the LED on for one period (duration = duty * period / 100). */
205 } led_flash_config_t;
206 
207 /*******************************************************************************
208  * API
209  ******************************************************************************/
210 
211 #if defined(__cplusplus)
212 extern "C" {
213 #endif /* _cplusplus */
214 
215 /*!
216  * @brief Initializes a LED with the LED handle and the user configuration structure.
217  *
218  * This function configures the LED with user-defined settings. The user can configure the configuration
219  * structure. The parameter ledHandle is a pointer to point to a memory space of size #LED_HANDLE_SIZE allocated by the
220  * caller. The LED supports two types LED, RGB and monochrome. Please refer to #led_type_t. These two types can be set
221  * by using #led_config_t. The LED also supports LED dimming mode.
222  *
223  * Example below shows how to use this API to configure the LED.
224  * For monochrome LED,
225  *  @code
226  *   static LED_HANDLE_DEFINE(s_ledMonochromeHandle);
227  *   led_config_t ledMonochromeConfig;
228  *   ledMonochromeConfig.type = kLED_TypeMonochrome;
229  *   ledMonochromeConfig.ledMonochrome.monochromePin.dimmingEnable = 0;
230  *   ledMonochromeConfig.ledMonochrome.monochromePin.gpio.port = 0;
231  *   ledMonochromeConfig.ledMonochrome.monochromePin.gpio.pin = 1;
232  *   ledMonochromeConfig.ledMonochrome.monochromePin.gpio.pinStateDefault = 0;
233  *   LED_Init((led_handle_t)s_ledMonochromeHandle, &ledMonochromeConfig);
234  *  @endcode
235  * For rgb LED,
236  *  @code
237  *   static LED_HANDLE_DEFINE(s_ledRgbHandle);
238  *   led_config_t ledRgbConfig;
239  *   ledRgbConfig.type = kLED_TypeRgb;
240  *   ledRgbConfig.ledRgb.redPin.dimmingEnable = 0;
241  *   ledRgbConfig.ledRgb.redPin.gpio.port = 0;
242  *   ledRgbConfig.ledRgb.redPin.gpio.pin = 1;
243  *   ledRgbConfig.ledRgb.redPin.gpio.pinStateDefault = 0;
244  *   ledRgbConfig.ledRgb.greenPin.dimmingEnable = 0;
245  *   ledRgbConfig.ledRgb.greenPin.gpio.port = 0;
246  *   ledRgbConfig.ledRgb.greenPin.gpio.pin = 2;
247  *   ledRgbConfig.ledRgb.greenPin.gpio.pinStateDefault = 0;
248  *   ledRgbConfig.ledRgb.bluePin.dimmingEnable = 0;
249  *   ledRgbConfig.ledRgb.bluePin.gpio.port = 0;
250  *   ledRgbConfig.ledRgb.bluePin.gpio.pin = 3;
251  *   ledRgbConfig.ledRgb.bluePin.gpio.pinStateDefault = 0;
252  *   LED_Init((led_handle_t)s_ledRgbHandle, &ledRgbConfig);
253  *  @endcode
254  * For dimming monochrome LED,
255  *  @code
256  *   static LED_HANDLE_DEFINE(s_ledMonochromeHandle);
257  *   led_config_t ledMonochromeConfig;
258  *   ledMonochromeConfig.type = kLED_TypeMonochrome;
259  *   ledMonochromeConfig.ledMonochrome.monochromePin.dimmingEnable = 1;
260  *   ledMonochromeConfig.ledMonochrome.monochromePin.dimming.sourceClock = 48000000;
261  *   ledMonochromeConfig.ledMonochrome.monochromePin.dimming.instance = 0;
262  *   ledMonochromeConfig.ledMonochrome.monochromePin.dimming.channel = 1;
263  *   ledMonochromeConfig.ledMonochrome.monochromePin.dimming.pinStateDefault = 0;
264  *   LED_Init((led_handle_t)s_ledMonochromeHandle, &ledMonochromeConfig);
265  *  @endcode
266  * For multiple LEDs,
267  *  @code
268  *   static LED_HANDLE_ARRAY_DEFINE(s_ledArrayHandle, count);
269  *   led_config_t ledArrayConfig[count];
270  *   for(uint8_t i = 0; i < count; i++ )
271  *   {
272  *       ledArrayConfig[i].type = kLED_TypeMonochrome;
273  *       ledArrayConfig[i].ledMonochrome.monochromePin.dimmingEnable = 1;
274  *       ledArrayConfig[i].ledMonochrome.monochromePin.dimming.sourceClock = 48000000;
275  *       ledArrayConfig[i].ledMonochrome.monochromePin.dimming.instance = 0;
276  *       ledArrayConfig[i].ledMonochrome.monochromePin.dimming.channel = 1;
277  *       ledArrayConfig[i].ledMonochrome.monochromePin.dimming.pinStateDefault = 0;
278  *       LED_Init((led_handle_t)s_ledArrayHandle[i], &ledArrayConfig[i]);
279  *   }
280  *  @endcode
281  *
282  * @param ledHandle Pointer to point to a memory space of size #LED_HANDLE_SIZE allocated by the caller.
283  * The handle should be 4 byte aligned, because unaligned access doesn't be supported on some devices.
284  * You can define one handle in the following two ways:
285  * #LED_HANDLE_DEFINE(ledHandle);
286  * or
287  * uint32_t ledHandle[((LED_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t))];
288  * You can define multiple handles in the following way:
289  * #LED_HANDLE_ARRAY_DEFINE(ledHandleArray, count);
290  * @param  ledConfig Pointer to user-defined configuration structure.
291  *         please note, if the LED_USE_CONFIGURE_STRUCTURE is set to 1, then user must use const buffer
292  *         forledConfig, LED module will directly use the const buffer.
293  * @retval kStatus_LED_Error An error occurred.
294  * @retval kStatus_LED_Success LED initialization succeed.
295  */
296 led_status_t LED_Init(led_handle_t ledHandle, const led_config_t *ledConfig);
297 
298 /*!
299  * @brief Deinitializes a LED instance.
300  *
301  * This function deinitializes the LED instance.
302  *
303  * @param ledHandle LED handle pointer.
304  * @retval kStatus_LED_Success LED de-initialization succeed.
305  */
306 led_status_t LED_Deinit(led_handle_t ledHandle);
307 
308 /*!
309  * @brief Sets the LED color.
310  *
311  * This function sets the LED color. The function only supports the RGB LED.
312  * The default color is #kLED_White. Please refer to #LED_MAKE_COLOR(r,g,b).
313  *
314  * @param ledHandle LED handle pointer.
315  * @param ledRgbColor LED color.
316  * @retval kStatus_LED_Error An error occurred.
317  * @retval kStatus_LED_Success Color setting succeed.
318  */
319 led_status_t LED_SetColor(led_handle_t ledHandle, led_color_t ledRgbColor);
320 
321 /*!
322  * @brief Turns on or off the LED.
323  *
324  * This function turns on or off the led.
325  *
326  * @param ledHandle LED handle pointer.
327  * @param turnOnOff Setting value, 1 - turns on, 0 - turns off.
328  * @retval kStatus_LED_Error An error occurred.
329  * @retval kStatus_LED_Success Successfully turn on or off the LED.
330  */
331 led_status_t LED_TurnOnOff(led_handle_t ledHandle, uint8_t turnOnOff);
332 
333 /*!
334  * @brief Blips the LED.
335  *
336  * This function blips the led.
337  *
338  * @param ledHandle LED handle pointer.
339  * @retval kStatus_LED_Error An error occurred.
340  * @retval kStatus_LED_Success Successfully blip the LED.
341  */
342 led_status_t LED_Blip(led_handle_t ledHandle);
343 
344 /*!
345  * @brief Flashes the LED.
346  *
347  * This function flashes the led. The flash configuration is passed by using #led_flash_config_t.
348  *
349  * @param ledHandle LED handle pointer.
350  * @param ledFlash LED flash configuration.
351  * @retval kStatus_LED_Error An error occurred.
352  * @retval kStatus_LED_Success Successfully flash the LED.
353  */
354 led_status_t LED_Flash(led_handle_t ledHandle, led_flash_config_t *ledFlash);
355 
356 /*!
357  * @brief Adjusts the brightness of the LED.
358  *
359  * This function adjust the brightness of the LED.
360  *
361  * @param ledHandle LED handle pointer.
362  * @param dimmingPeriod The duration of the dimming (unit is ms).
363  * @param increasement Brighten or dim (1 - brighten, 0 - dim).
364  * @retval kStatus_LED_Error An error occurred.
365  * @retval kStatus_LED_Success Successfully adjust the brightness of the LED.
366  */
367 led_status_t LED_Dimming(led_handle_t ledHandle, uint16_t dimmingPeriod, uint8_t increasement);
368 
369 /*!
370  * @brief Prepares to enter low power consumption.
371  *
372  * This function is used to prepare to enter low power consumption.
373  *
374  * @param ledHandle LED handle pointer.
375  * @retval kStatus_LED_Success Successful operation.
376  */
377 led_status_t LED_EnterLowpower(led_handle_t ledHandle);
378 
379 /*!
380  * @brief Restores from low power consumption.
381  *
382  * This function is used to restore from low power consumption.
383  *
384  * @param ledHandle LED handle pointer.
385  * @retval kStatus_LED_Success Successful operation.
386  */
387 led_status_t LED_ExitLowpower(led_handle_t ledHandle);
388 
389 #if defined(__cplusplus)
390 }
391 #endif
392 /*! @}*/
393 #endif /* __LED_H__ */
394