hal_led.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /**************************************************************************************************
  2. Filename: hal_led.c
  3. Revised: $Date: 2012-02-14 12:43:32 -0800 (Tue, 14 Feb 2012) $
  4. Revision: $Revision: 29281 $
  5. Description: This file contains the interface to the HAL LED Service.
  6. Copyright 2006-2012 Texas Instruments Incorporated. All rights reserved.
  7. IMPORTANT: Your use of this Software is limited to those specific rights
  8. granted under the terms of a software license agreement between the user
  9. who downloaded the software, his/her employer (which must be your employer)
  10. and Texas Instruments Incorporated (the "License"). You may not use this
  11. Software unless you agree to abide by the terms of the License. The License
  12. limits your use, and you acknowledge, that the Software may not be modified,
  13. copied or distributed unless embedded on a Texas Instruments microcontroller
  14. or used solely and exclusively in conjunction with a Texas Instruments radio
  15. frequency transceiver, which is integrated into your product. Other than for
  16. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  17. works of, modify, distribute, perform, display or sell this Software and/or
  18. its documentation for any purpose.
  19. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  20. PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  21. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  22. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  23. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  24. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  25. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  26. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  27. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  28. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  29. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  30. Should you have any questions regarding your right to use this Software,
  31. contact Texas Instruments Incorporated at www.TI.com.
  32. **************************************************************************************************/
  33. /***************************************************************************************************
  34. * INCLUDES
  35. ***************************************************************************************************/
  36. #include "hal_mcu.h"
  37. #include "hal_defs.h"
  38. #include "hal_types.h"
  39. #include "hal_drivers.h"
  40. #include "hal_led.h"
  41. #include "osal.h"
  42. #include "hal_board.h"
  43. /***************************************************************************************************
  44. * CONSTANTS
  45. ***************************************************************************************************/
  46. /***************************************************************************************************
  47. * MACROS
  48. ***************************************************************************************************/
  49. /***************************************************************************************************
  50. * TYPEDEFS
  51. ***************************************************************************************************/
  52. /* LED control structure */
  53. typedef struct {
  54. uint8 mode; /* Operation mode */
  55. uint8 todo; /* Blink cycles left */
  56. uint8 onPct; /* On cycle percentage */
  57. uint16 time; /* On/off cycle time (msec) */
  58. uint32 next; /* Time for next change */
  59. } HalLedControl_t;
  60. typedef struct
  61. {
  62. HalLedControl_t HalLedControlTable[HAL_LED_DEFAULT_MAX_LEDS];
  63. uint8 sleepActive;
  64. } HalLedStatus_t;
  65. /***************************************************************************************************
  66. * GLOBAL VARIABLES
  67. ***************************************************************************************************/
  68. static uint8 HalLedState; // LED state at last set/clr/blink update
  69. #if HAL_LED == TRUE
  70. static uint8 HalSleepLedState; // LED state at last set/clr/blink update
  71. static uint8 preBlinkState; // Original State before going to blink mode
  72. // bit 0, 1, 2, 3 represent led 0, 1, 2, 3
  73. #endif
  74. #ifdef BLINK_LEDS
  75. static HalLedStatus_t HalLedStatusControl;
  76. #endif
  77. /***************************************************************************************************
  78. * LOCAL FUNCTION
  79. ***************************************************************************************************/
  80. #if (HAL_LED == TRUE)
  81. void HalLedUpdate (void);
  82. void HalLedOnOff (uint8 leds, uint8 mode);
  83. #endif /* HAL_LED */
  84. /***************************************************************************************************
  85. * FUNCTIONS - API
  86. ***************************************************************************************************/
  87. /***************************************************************************************************
  88. * @fn HalLedInit
  89. *
  90. * @brief Initialize LED Service
  91. *
  92. * @param init - pointer to void that contains the initialized value
  93. *
  94. * @return None
  95. ***************************************************************************************************/
  96. void HalLedInit (void)
  97. {
  98. #if (HAL_LED == TRUE)
  99. /* Initialize all LEDs to OFF */
  100. HalLedSet (HAL_LED_ALL, HAL_LED_MODE_OFF);
  101. #endif /* HAL_LED */
  102. #ifdef BLINK_LEDS
  103. /* Initialize sleepActive to FALSE */
  104. HalLedStatusControl.sleepActive = FALSE;
  105. #endif
  106. }
  107. /***************************************************************************************************
  108. * @fn HalLedSet
  109. *
  110. * @brief Tun ON/OFF/TOGGLE given LEDs
  111. *
  112. * @param led - bit mask value of leds to be turned ON/OFF/TOGGLE
  113. * mode - BLINK, FLASH, TOGGLE, ON, OFF
  114. * @return None
  115. ***************************************************************************************************/
  116. uint8 HalLedSet (uint8 leds, uint8 mode)
  117. {
  118. #if (defined (BLINK_LEDS)) && (HAL_LED == TRUE)
  119. uint8 led;
  120. HalLedControl_t *sts;
  121. switch (mode)
  122. {
  123. case HAL_LED_MODE_BLINK:
  124. /* Default blink, 1 time, D% duty cycle */
  125. HalLedBlink (leds, 1, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME);
  126. break;
  127. case HAL_LED_MODE_FLASH:
  128. /* Default flash, N times, D% duty cycle */
  129. HalLedBlink (leds, HAL_LED_DEFAULT_FLASH_COUNT, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME);
  130. break;
  131. case HAL_LED_MODE_ON:
  132. case HAL_LED_MODE_OFF:
  133. case HAL_LED_MODE_TOGGLE:
  134. led = HAL_LED_1;
  135. leds &= HAL_LED_ALL;
  136. sts = HalLedStatusControl.HalLedControlTable;
  137. while (leds)
  138. {
  139. if (leds & led)
  140. {
  141. if (mode != HAL_LED_MODE_TOGGLE)
  142. {
  143. sts->mode = mode; /* ON or OFF */
  144. }
  145. else
  146. {
  147. sts->mode ^= HAL_LED_MODE_ON; /* Toggle */
  148. }
  149. HalLedOnOff (led, sts->mode);
  150. leds ^= led;
  151. }
  152. led <<= 1;
  153. sts++;
  154. }
  155. break;
  156. default:
  157. break;
  158. }
  159. #elif (HAL_LED == TRUE)
  160. LedOnOff(leds, mode);
  161. #else
  162. // HAL LED is disabled, suppress unused argument warnings
  163. (void) leds;
  164. (void) mode;
  165. #endif /* BLINK_LEDS && HAL_LED */
  166. return ( HalLedState );
  167. }
  168. /***************************************************************************************************
  169. * @fn HalLedBlink
  170. *
  171. * @brief Blink the leds
  172. *
  173. * @param leds - bit mask value of leds to be blinked
  174. * numBlinks - number of blinks
  175. * percent - the percentage in each period where the led
  176. * will be on
  177. * period - length of each cycle in milliseconds
  178. *
  179. * @return None
  180. ***************************************************************************************************/
  181. void HalLedBlink (uint8 leds, uint8 numBlinks, uint8 percent, uint16 period)
  182. {
  183. #if (defined (BLINK_LEDS)) && (HAL_LED == TRUE)
  184. uint8 led;
  185. HalLedControl_t *sts;
  186. if (leds && percent && period)
  187. {
  188. if (percent < 100)
  189. {
  190. led = HAL_LED_1;
  191. leds &= HAL_LED_ALL;
  192. sts = HalLedStatusControl.HalLedControlTable;
  193. while (leds)
  194. {
  195. if (leds & led)
  196. {
  197. /* Store the current state of the led before going to blinking if not already blinking */
  198. if(sts->mode < HAL_LED_MODE_BLINK )
  199. preBlinkState |= (led & HalLedState);
  200. sts->mode = HAL_LED_MODE_OFF; /* Stop previous blink */
  201. sts->time = period; /* Time for one on/off cycle */
  202. sts->onPct = percent; /* % of cycle LED is on */
  203. sts->todo = numBlinks; /* Number of blink cycles */
  204. if (!numBlinks) sts->mode |= HAL_LED_MODE_FLASH; /* Continuous */
  205. sts->next = osal_GetSystemClock(); /* Start now */
  206. sts->mode |= HAL_LED_MODE_BLINK; /* Enable blinking */
  207. leds ^= led;
  208. }
  209. led <<= 1;
  210. sts++;
  211. }
  212. // Cancel any overlapping timer for blink events
  213. osal_stop_timerEx(Hal_TaskID, HAL_LED_BLINK_EVENT);
  214. osal_set_event (Hal_TaskID, HAL_LED_BLINK_EVENT);
  215. }
  216. else
  217. {
  218. HalLedSet (leds, HAL_LED_MODE_ON); /* >= 100%, turn on */
  219. }
  220. }
  221. else
  222. {
  223. HalLedSet (leds, HAL_LED_MODE_OFF); /* No on time, turn off */
  224. }
  225. #elif (HAL_LED == TRUE)
  226. percent = (leds & HalLedState) ? HAL_LED_MODE_OFF : HAL_LED_MODE_ON;
  227. HalLedOnOff (leds, percent); /* Toggle */
  228. #else
  229. // HAL LED is disabled, suppress unused argument warnings
  230. (void) leds;
  231. (void) numBlinks;
  232. (void) percent;
  233. (void) period;
  234. #endif /* BLINK_LEDS && HAL_LED */
  235. }
  236. #if (HAL_LED == TRUE)
  237. /***************************************************************************************************
  238. * @fn HalLedUpdate
  239. *
  240. * @brief Update leds to work with blink
  241. *
  242. * @param none
  243. *
  244. * @return none
  245. ***************************************************************************************************/
  246. void HalLedUpdate (void)
  247. {
  248. uint8 led;
  249. uint8 pct;
  250. uint8 leds;
  251. HalLedControl_t *sts;
  252. uint32 time;
  253. uint16 next;
  254. uint16 wait;
  255. next = 0;
  256. led = HAL_LED_1;
  257. leds = HAL_LED_ALL;
  258. sts = HalLedStatusControl.HalLedControlTable;
  259. /* Check if sleep is active or not */
  260. if (!HalLedStatusControl.sleepActive)
  261. {
  262. while (leds)
  263. {
  264. if (leds & led)
  265. {
  266. if (sts->mode & HAL_LED_MODE_BLINK)
  267. {
  268. time = osal_GetSystemClock();
  269. if (time >= sts->next)
  270. {
  271. if (sts->mode & HAL_LED_MODE_ON)
  272. {
  273. pct = 100 - sts->onPct; /* Percentage of cycle for off */
  274. sts->mode &= ~HAL_LED_MODE_ON; /* Say it's not on */
  275. HalLedOnOff (led, HAL_LED_MODE_OFF); /* Turn it off */
  276. if (!(sts->mode & HAL_LED_MODE_FLASH))
  277. {
  278. sts->todo--; /* Not continuous, reduce count */
  279. }
  280. }
  281. else if ( (!sts->todo) && !(sts->mode & HAL_LED_MODE_FLASH) )
  282. {
  283. sts->mode ^= HAL_LED_MODE_BLINK; /* No more blinks */
  284. }
  285. else
  286. {
  287. pct = sts->onPct; /* Percentage of cycle for on */
  288. sts->mode |= HAL_LED_MODE_ON; /* Say it's on */
  289. HalLedOnOff (led, HAL_LED_MODE_ON); /* Turn it on */
  290. }
  291. if (sts->mode & HAL_LED_MODE_BLINK)
  292. {
  293. wait = (((uint32)pct * (uint32)sts->time) / 100);
  294. sts->next = time + wait;
  295. }
  296. else
  297. {
  298. /* no more blink, no more wait */
  299. wait = 0;
  300. /* After blinking, set the LED back to the state before it blinks */
  301. HalLedSet (led, ((preBlinkState & led)!=0)?HAL_LED_MODE_ON:HAL_LED_MODE_OFF);
  302. /* Clear the saved bit */
  303. preBlinkState &= (led ^ 0xFF);
  304. }
  305. }
  306. else
  307. {
  308. wait = sts->next - time; /* Time left */
  309. }
  310. if (!next || ( wait && (wait < next) ))
  311. {
  312. next = wait;
  313. }
  314. }
  315. leds ^= led;
  316. }
  317. led <<= 1;
  318. sts++;
  319. }
  320. if (next)
  321. {
  322. osal_start_timerEx(Hal_TaskID, HAL_LED_BLINK_EVENT, next); /* Schedule event */
  323. }
  324. }
  325. }
  326. /***************************************************************************************************
  327. * @fn HalLedOnOff
  328. *
  329. * @brief Turns specified LED ON or OFF
  330. *
  331. * @param leds - LED bit mask
  332. * mode - LED_ON,LED_OFF,
  333. *
  334. * @return none
  335. ***************************************************************************************************/
  336. void HalLedOnOff (uint8 leds, uint8 mode)
  337. {
  338. if (leds & HAL_LED_1)
  339. {
  340. if (mode == HAL_LED_MODE_ON)
  341. {
  342. HAL_TURN_ON_LED1();
  343. }
  344. else
  345. {
  346. HAL_TURN_OFF_LED1();
  347. }
  348. }
  349. if (leds & HAL_LED_2)
  350. {
  351. if (mode == HAL_LED_MODE_ON)
  352. {
  353. HAL_TURN_ON_LED2();
  354. }
  355. else
  356. {
  357. HAL_TURN_OFF_LED2();
  358. }
  359. }
  360. if (leds & HAL_LED_3)
  361. {
  362. if (mode == HAL_LED_MODE_ON)
  363. {
  364. HAL_TURN_ON_LED3();
  365. }
  366. else
  367. {
  368. HAL_TURN_OFF_LED3();
  369. }
  370. }
  371. if (leds & HAL_LED_4)
  372. {
  373. if (mode == HAL_LED_MODE_ON)
  374. {
  375. HAL_TURN_ON_LED4();
  376. }
  377. else
  378. {
  379. HAL_TURN_OFF_LED4();
  380. }
  381. }
  382. /* Remember current state */
  383. if (mode)
  384. {
  385. HalLedState |= leds;
  386. }
  387. else
  388. {
  389. HalLedState &= (leds ^ 0xFF);
  390. }
  391. }
  392. #endif /* HAL_LED */
  393. /***************************************************************************************************
  394. * @fn HalGetLedState
  395. *
  396. * @brief Dim LED2 - Dim (set level) of LED2
  397. *
  398. * @param none
  399. *
  400. * @return led state
  401. ***************************************************************************************************/
  402. uint8 HalLedGetState ()
  403. {
  404. #if (HAL_LED == TRUE)
  405. return HalLedState;
  406. #else
  407. return 0;
  408. #endif
  409. }
  410. /***************************************************************************************************
  411. * @fn HalLedEnterSleep
  412. *
  413. * @brief Store current LEDs state before sleep
  414. *
  415. * @param none
  416. *
  417. * @return none
  418. ***************************************************************************************************/
  419. void HalLedEnterSleep( void )
  420. {
  421. #ifdef BLINK_LEDS
  422. /* Sleep ON */
  423. HalLedStatusControl.sleepActive = TRUE;
  424. #endif /* BLINK_LEDS */
  425. #if (HAL_LED == TRUE)
  426. /* Save the state of each led */
  427. HalSleepLedState = 0;
  428. HalSleepLedState |= HAL_STATE_LED1();
  429. HalSleepLedState |= HAL_STATE_LED2() << 1;
  430. HalSleepLedState |= HAL_STATE_LED3() << 2;
  431. HalSleepLedState |= HAL_STATE_LED4() << 3;
  432. /* TURN OFF all LEDs to save power */
  433. HalLedOnOff (HAL_LED_ALL, HAL_LED_MODE_OFF);
  434. #endif /* HAL_LED */
  435. }
  436. /***************************************************************************************************
  437. * @fn HalLedExitSleep
  438. *
  439. * @brief Restore current LEDs state after sleep
  440. *
  441. * @param none
  442. *
  443. * @return none
  444. ***************************************************************************************************/
  445. void HalLedExitSleep( void )
  446. {
  447. #if (HAL_LED == TRUE)
  448. /* Load back the saved state */
  449. HalLedOnOff(HalSleepLedState, HAL_LED_MODE_ON);
  450. /* Restart - This takes care BLINKING LEDS */
  451. HalLedUpdate();
  452. #endif /* HAL_LED */
  453. #ifdef BLINK_LEDS
  454. /* Sleep OFF */
  455. HalLedStatusControl.sleepActive = FALSE;
  456. #endif /* BLINK_LEDS */
  457. }
  458. /***************************************************************************************************
  459. ***************************************************************************************************/