hal_led.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /**************************************************************************************************
  2. Filename: hal_led.c
  3. Revised: $Date: 2009-03-13 05:45:44 -0700 (Fri, 13 Mar 2009) $
  4. Revision: $Revision: 19408 $
  5. Description: This file contains the interface to the HAL LED Service.
  6. Copyright 2006-2007 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 */
  198. preBlinkState |= (led & HalLedState);
  199. sts->mode = HAL_LED_MODE_OFF; /* Stop previous blink */
  200. sts->time = period; /* Time for one on/off cycle */
  201. sts->onPct = percent; /* % of cycle LED is on */
  202. sts->todo = numBlinks; /* Number of blink cycles */
  203. if (!numBlinks) sts->mode |= HAL_LED_MODE_FLASH; /* Continuous */
  204. sts->next = osal_GetSystemClock(); /* Start now */
  205. sts->mode |= HAL_LED_MODE_BLINK; /* Enable blinking */
  206. leds ^= led;
  207. }
  208. led <<= 1;
  209. sts++;
  210. }
  211. osal_set_event (Hal_TaskID, HAL_LED_BLINK_EVENT);
  212. }
  213. else
  214. {
  215. HalLedSet (leds, HAL_LED_MODE_ON); /* >= 100%, turn on */
  216. }
  217. }
  218. else
  219. {
  220. HalLedSet (leds, HAL_LED_MODE_OFF); /* No on time, turn off */
  221. }
  222. #elif (HAL_LED == TRUE)
  223. percent = (leds & HalLedState) ? HAL_LED_MODE_OFF : HAL_LED_MODE_ON;
  224. HalLedOnOff (leds, percent); /* Toggle */
  225. #else
  226. // HAL LED is disabled, suppress unused argument warnings
  227. (void) leds;
  228. (void) numBlinks;
  229. (void) percent;
  230. (void) period;
  231. #endif /* BLINK_LEDS && HAL_LED */
  232. }
  233. #if (HAL_LED == TRUE)
  234. /***************************************************************************************************
  235. * @fn HalLedUpdate
  236. *
  237. * @brief Update leds to work with blink
  238. *
  239. * @param none
  240. *
  241. * @return none
  242. ***************************************************************************************************/
  243. void HalLedUpdate (void)
  244. {
  245. uint8 led;
  246. uint8 pct;
  247. uint8 leds;
  248. HalLedControl_t *sts;
  249. uint32 time;
  250. uint16 next;
  251. uint16 wait;
  252. next = 0;
  253. led = HAL_LED_1;
  254. leds = HAL_LED_ALL;
  255. sts = HalLedStatusControl.HalLedControlTable;
  256. /* Check if sleep is active or not */
  257. if (!HalLedStatusControl.sleepActive)
  258. {
  259. while (leds)
  260. {
  261. if (leds & led)
  262. {
  263. if (sts->mode & HAL_LED_MODE_BLINK)
  264. {
  265. time = osal_GetSystemClock();
  266. if (time >= sts->next)
  267. {
  268. if (sts->mode & HAL_LED_MODE_ON)
  269. {
  270. pct = 100 - sts->onPct; /* Percentage of cycle for off */
  271. sts->mode &= ~HAL_LED_MODE_ON; /* Say it's not on */
  272. HalLedOnOff (led, HAL_LED_MODE_OFF); /* Turn it off */
  273. if (!(sts->mode & HAL_LED_MODE_FLASH))
  274. {
  275. sts->todo--; /* Not continuous, reduce count */
  276. if (!sts->todo)
  277. {
  278. sts->mode ^= HAL_LED_MODE_BLINK; /* No more blinks */
  279. }
  280. }
  281. }
  282. else
  283. {
  284. pct = sts->onPct; /* Percentage of cycle for on */
  285. sts->mode |= HAL_LED_MODE_ON; /* Say it's on */
  286. HalLedOnOff (led, HAL_LED_MODE_ON); /* Turn it on */
  287. }
  288. if (sts->mode & HAL_LED_MODE_BLINK)
  289. {
  290. wait = (((uint32)pct * (uint32)sts->time) / 100);
  291. sts->next = time + wait;
  292. }
  293. else
  294. {
  295. /* no more blink, no more wait */
  296. wait = 0;
  297. /* After blinking, set the LED back to the state before it blinks */
  298. HalLedSet (led, ((preBlinkState & led)!=0)?HAL_LED_MODE_ON:HAL_LED_MODE_OFF);
  299. /* Clear the saved bit */
  300. preBlinkState &= (led ^ 0xFF);
  301. }
  302. }
  303. else
  304. {
  305. wait = sts->next - time; /* Time left */
  306. }
  307. if (!next || ( wait && (wait < next) ))
  308. {
  309. next = wait;
  310. }
  311. }
  312. leds ^= led;
  313. }
  314. led <<= 1;
  315. sts++;
  316. }
  317. if (next)
  318. {
  319. osal_start_timerEx(Hal_TaskID, HAL_LED_BLINK_EVENT, next); /* Schedule event */
  320. }
  321. }
  322. }
  323. /***************************************************************************************************
  324. * @fn HalLedOnOff
  325. *
  326. * @brief Turns specified LED ON or OFF
  327. *
  328. * @param leds - LED bit mask
  329. * mode - LED_ON,LED_OFF,
  330. *
  331. * @return none
  332. ***************************************************************************************************/
  333. void HalLedOnOff (uint8 leds, uint8 mode)
  334. {
  335. if (leds & HAL_LED_1)
  336. {
  337. if (mode == HAL_LED_MODE_ON)
  338. {
  339. HAL_TURN_ON_LED1();
  340. }
  341. else
  342. {
  343. HAL_TURN_OFF_LED1();
  344. }
  345. }
  346. if (leds & HAL_LED_2)
  347. {
  348. if (mode == HAL_LED_MODE_ON)
  349. {
  350. HAL_TURN_ON_LED2();
  351. }
  352. else
  353. {
  354. HAL_TURN_OFF_LED2();
  355. }
  356. }
  357. if (leds & HAL_LED_3)
  358. {
  359. if (mode == HAL_LED_MODE_ON)
  360. {
  361. HAL_TURN_ON_LED3();
  362. }
  363. else
  364. {
  365. HAL_TURN_OFF_LED3();
  366. }
  367. }
  368. if (leds & HAL_LED_4)
  369. {
  370. if (mode == HAL_LED_MODE_ON)
  371. {
  372. HAL_TURN_ON_LED4();
  373. }
  374. else
  375. {
  376. HAL_TURN_OFF_LED4();
  377. }
  378. }
  379. /* Remember current state */
  380. if (mode)
  381. {
  382. HalLedState |= leds;
  383. }
  384. else
  385. {
  386. HalLedState &= (leds ^ 0xFF);
  387. }
  388. }
  389. #endif /* HAL_LED */
  390. /***************************************************************************************************
  391. * @fn HalGetLedState
  392. *
  393. * @brief Dim LED2 - Dim (set level) of LED2
  394. *
  395. * @param none
  396. *
  397. * @return led state
  398. ***************************************************************************************************/
  399. uint8 HalLedGetState ()
  400. {
  401. #if (HAL_LED == TRUE)
  402. return HalLedState;
  403. #else
  404. return 0;
  405. #endif
  406. }
  407. /***************************************************************************************************
  408. * @fn HalLedEnterSleep
  409. *
  410. * @brief Store current LEDs state before sleep
  411. *
  412. * @param none
  413. *
  414. * @return none
  415. ***************************************************************************************************/
  416. void HalLedEnterSleep( void )
  417. {
  418. #ifdef BLINK_LEDS
  419. /* Sleep ON */
  420. HalLedStatusControl.sleepActive = TRUE;
  421. #endif /* BLINK_LEDS */
  422. #if (HAL_LED == TRUE)
  423. /* Save the state of each led */
  424. HalSleepLedState = 0;
  425. HalSleepLedState |= HAL_STATE_LED1();
  426. HalSleepLedState |= HAL_STATE_LED2() << 1;
  427. HalSleepLedState |= HAL_STATE_LED3() << 2;
  428. HalSleepLedState |= HAL_STATE_LED4() << 3;
  429. /* TURN OFF all LEDs to save power */
  430. HalLedOnOff (HAL_LED_ALL, HAL_LED_MODE_OFF);
  431. #endif /* HAL_LED */
  432. }
  433. /***************************************************************************************************
  434. * @fn HalLedExitSleep
  435. *
  436. * @brief Restore current LEDs state after sleep
  437. *
  438. * @param none
  439. *
  440. * @return none
  441. ***************************************************************************************************/
  442. void HalLedExitSleep( void )
  443. {
  444. #if (HAL_LED == TRUE)
  445. /* Load back the saved state */
  446. HalLedOnOff(HalSleepLedState, HAL_LED_MODE_ON);
  447. /* Restart - This takes care BLINKING LEDS */
  448. HalLedUpdate();
  449. #endif /* HAL_LED */
  450. #ifdef BLINK_LEDS
  451. /* Sleep OFF */
  452. HalLedStatusControl.sleepActive = FALSE;
  453. #endif /* BLINK_LEDS */
  454. }
  455. /***************************************************************************************************
  456. ***************************************************************************************************/