hal_sleep.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /**************************************************************************************************
  2. Filename: hal_sleep.c
  3. Revised: $Date: 2009-01-29 15:49:56 -0800 (Thu, 29 Jan 2009) $
  4. Revision: $Revision: 18906 $
  5. Description: This module contains the HAL power management procedures for the CC2530.
  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. */
  37. #include "hal_types.h"
  38. #include "hal_mcu.h"
  39. #include "hal_board.h"
  40. #include "hal_sleep.h"
  41. #include "hal_led.h"
  42. #include "hal_key.h"
  43. #include "mac_api.h"
  44. #include "OSAL.h"
  45. #include "OSAL_Timers.h"
  46. #include "OSAL_Tasks.h"
  47. #include "OSAL_PwrMgr.h"
  48. #include "OnBoard.h"
  49. #include "hal_drivers.h"
  50. #include "hal_assert.h"
  51. #include "mac_mcu.h"
  52. #if !defined (RTR_NWK) && defined (NWK_AUTO_POLL)
  53. #include "nwk_globals.h"
  54. #include "ZGlobals.h"
  55. #endif
  56. /* ------------------------------------------------------------------------------------------------
  57. * Macros
  58. * ------------------------------------------------------------------------------------------------
  59. */
  60. /* POWER CONSERVATION DEFINITIONS
  61. * Sleep mode H/W definitions (enabled with POWER_SAVING compile option)
  62. */
  63. #define CC2530_PM0 0 /* PM0, Clock oscillators on, voltage regulator on */
  64. #define CC2530_PM1 1 /* PM1, 32.768 kHz oscillators on, voltage regulator on */
  65. #define CC2530_PM2 2 /* PM2, 32.768 kHz oscillators on, voltage regulator off */
  66. #define CC2530_PM3 3 /* PM3, All clock oscillators off, voltage regulator off */
  67. /* HAL power management mode is set according to the power management state. The default
  68. * setting is HAL_SLEEP_OFF. The actual value is tailored to different HW platform. Both
  69. * HAL_SLEEP_TIMER and HAL_SLEEP_DEEP selections will:
  70. * 1. turn off the system clock, and
  71. * 2. halt the MCU.
  72. * HAL_SLEEP_TIMER can be woken up by sleep timer interrupt, I/O interrupt and reset.
  73. * HAL_SLEEP_DEEP can be woken up by I/O interrupt and reset.
  74. */
  75. #define HAL_SLEEP_OFF CC2530_PM0
  76. #define HAL_SLEEP_TIMER CC2530_PM2
  77. #define HAL_SLEEP_DEEP CC2530_PM3
  78. /* MAX_SLEEP_TIME calculation:
  79. * Sleep timer maximum duration = 0xFFFF7F / 32768 Hz = 511.996 seconds
  80. * Round it to 510 seconds or 510000 ms
  81. */
  82. #define MAX_SLEEP_TIME 510000 /* maximum time to sleep allowed by ST */
  83. /* minimum time to sleep, this macro is to:
  84. * 1. avoid thrashing in-and-out of sleep with short OSAL timer (~2ms)
  85. * 2. define minimum safe sleep period
  86. */
  87. #if !defined (PM_MIN_SLEEP_TIME)
  88. #define PM_MIN_SLEEP_TIME 14 /* default to minimum safe sleep time minimum CAP */
  89. #endif
  90. /* The PCON instruction must be 4-byte aligned. The following code may cause excessive power
  91. * consumption if not aligned. See linker file ".xcl" for actual placement.
  92. */
  93. #pragma location = "SLEEP_CODE"
  94. static void halSetSleepMode(void)
  95. {
  96. PCON |= PCON_IDLE;
  97. asm("NOP");
  98. }
  99. /* This value is used to adjust the sleep timer compare value such that the sleep timer
  100. * compare takes into account the amount of processing time spent in function halSleep().
  101. * The first value is determined by measuring the number of sleep timer ticks it from
  102. * the beginning of the function to entering sleep mode. The second value is determined
  103. * by measuring the number of sleep timer ticks from exit of sleep mode to the call to
  104. * osal_adjust_timers().
  105. */
  106. #define HAL_SLEEP_ADJ_TICKS (7 + 10)
  107. #ifndef HAL_SLEEP_DEBUG_POWER_MODE
  108. /* set CC2530 power mode; always use PM2 */
  109. #define HAL_SLEEP_SET_POWER_MODE(mode) st( SLEEPCMD &= ~PMODE; /* clear mode bits */ \
  110. SLEEPCMD |= mode; /* set mode bits */ \
  111. while (!(STLOAD & LDRDY)); \
  112. { \
  113. halSetSleepMode(); \
  114. } \
  115. )
  116. #else
  117. /* Debug: don't set power mode, just block until sleep timer interrupt */
  118. #define HAL_SLEEP_SET_POWER_MODE(mode) st( while(halSleepInt == FALSE); \
  119. halSleepInt = FALSE; )
  120. #endif
  121. /* sleep and external interrupt port masks */
  122. #define STIE_BV BV(5)
  123. #define P0IE_BV BV(5)
  124. #define P1IE_BV BV(4)
  125. #define P2IE_BV BV(1)
  126. /* sleep timer interrupt control */
  127. #define HAL_SLEEP_TIMER_ENABLE_INT() st(IEN0 |= STIE_BV;) /* enable sleep timer interrupt */
  128. #define HAL_SLEEP_TIMER_DISABLE_INT() st(IEN0 &= ~STIE_BV;) /* disable sleep timer interrupt */
  129. #define HAL_SLEEP_TIMER_CLEAR_INT() st(IRCON &= ~0x80;) /* clear sleep interrupt flag */
  130. /* backup interrupt enable registers before sleep */
  131. #define HAL_SLEEP_IE_BACKUP_AND_DISABLE(ien0, ien1, ien2) st(ien0 = IEN0; /* backup IEN0 register */ \
  132. ien1 = IEN1; /* backup IEN1 register */ \
  133. ien2 = IEN2; /* backup IEN2 register */ \
  134. IEN0 &= STIE_BV; /* disable IEN0 except STIE */ \
  135. IEN1 &= P0IE_BV; /* disable IEN1 except P0IE */ \
  136. IEN2 &= (P1IE_BV|P2IE_BV);) /* disable IEN2 except P1IE, P2IE */
  137. /* restore interrupt enable registers before sleep */
  138. #define HAL_SLEEP_IE_RESTORE(ien0, ien1, ien2) st(IEN0 = ien0; /* restore IEN0 register */ \
  139. IEN1 = ien1; /* restore IEN1 register */ \
  140. IEN2 = ien2;) /* restore IEN2 register */
  141. /* convert msec to 320 usec units with round */
  142. #define HAL_SLEEP_MS_TO_320US(ms) (((((uint32) (ms)) * 100) + 31) / 32)
  143. /* for optimized indexing of uint32's */
  144. #if HAL_MCU_LITTLE_ENDIAN()
  145. #define UINT32_NDX0 0
  146. #define UINT32_NDX1 1
  147. #define UINT32_NDX2 2
  148. #define UINT32_NDX3 3
  149. #else
  150. #define UINT32_NDX0 3
  151. #define UINT32_NDX1 2
  152. #define UINT32_NDX2 1
  153. #define UINT32_NDX3 0
  154. #endif
  155. /* ------------------------------------------------------------------------------------------------
  156. * Local Variables
  157. * ------------------------------------------------------------------------------------------------
  158. */
  159. /* HAL power management mode is set according to the power management state.
  160. */
  161. static uint8 halPwrMgtMode = HAL_SLEEP_OFF;
  162. /* stores the sleep timer count upon entering sleep */
  163. static uint32 halSleepTimerStart;
  164. /* stores the accumulated sleep time */
  165. static uint32 halAccumulatedSleepTime;
  166. #ifdef HAL_SLEEP_DEBUG_POWER_MODE
  167. static bool halSleepInt = FALSE;
  168. #endif
  169. /* ------------------------------------------------------------------------------------------------
  170. * Function Prototypes
  171. * ------------------------------------------------------------------------------------------------
  172. */
  173. void halSleepSetTimer(uint32 timeout);
  174. uint32 HalTimerElapsed( void );
  175. /**************************************************************************************************
  176. * @fn halSleep
  177. *
  178. * @brief This function is called from the OSAL task loop using and existing OSAL
  179. * interface. It sets the low power mode of the MAC and the CC2530.
  180. *
  181. * input parameters
  182. *
  183. * @param osal_timeout - Next OSAL timer timeout.
  184. *
  185. * output parameters
  186. *
  187. * None.
  188. *
  189. * @return None.
  190. **************************************************************************************************
  191. */
  192. void halSleep( uint16 osal_timeout )
  193. {
  194. uint32 timeout;
  195. uint32 macTimeout = 0;
  196. halAccumulatedSleepTime = 0;
  197. /* get next OSAL timer expiration converted to 320 usec units */
  198. timeout = HAL_SLEEP_MS_TO_320US(osal_timeout);
  199. if (timeout == 0)
  200. {
  201. timeout = MAC_PwrNextTimeout();
  202. }
  203. else
  204. {
  205. /* get next MAC timer expiration */
  206. macTimeout = MAC_PwrNextTimeout();
  207. /* get lesser of two timeouts */
  208. if ((macTimeout != 0) && (macTimeout < timeout))
  209. {
  210. timeout = macTimeout;
  211. }
  212. }
  213. /* HAL_SLEEP_PM2 is entered only if the timeout is zero and
  214. * the device is a stimulated device.
  215. */
  216. halPwrMgtMode = (timeout == 0) ? HAL_SLEEP_DEEP : HAL_SLEEP_TIMER;
  217. /* DEEP sleep can only be entered when zgPollRate == 0.
  218. * This is to eliminate any possibility of entering PM3 between
  219. * two network timers.
  220. */
  221. #if !defined (RTR_NWK) && defined (NWK_AUTO_POLL)
  222. if ((timeout > HAL_SLEEP_MS_TO_320US(PM_MIN_SLEEP_TIME)) ||
  223. (timeout == 0 && zgPollRate == 0))
  224. #else
  225. if ((timeout > HAL_SLEEP_MS_TO_320US(PM_MIN_SLEEP_TIME)) ||
  226. (timeout == 0))
  227. #endif
  228. {
  229. halIntState_t ien0, ien1, ien2;
  230. HAL_ASSERT(HAL_INTERRUPTS_ARE_ENABLED());
  231. HAL_DISABLE_INTERRUPTS();
  232. /* always use "deep sleep" to turn off radio VREG on CC2530 */
  233. if (MAC_PwrOffReq(MAC_PWR_SLEEP_DEEP) == MAC_SUCCESS)
  234. {
  235. while ( (HAL_SLEEP_MS_TO_320US(halAccumulatedSleepTime) < timeout) || (timeout == 0) )
  236. {
  237. /* get peripherals ready for sleep */
  238. HalKeyEnterSleep();
  239. #ifdef HAL_SLEEP_DEBUG_LED
  240. HAL_TURN_OFF_LED3();
  241. #else
  242. /* use this to turn LEDs off during sleep */
  243. HalLedEnterSleep();
  244. #endif
  245. /* enable sleep timer interrupt */
  246. if (timeout != 0)
  247. {
  248. if (timeout > HAL_SLEEP_MS_TO_320US( MAX_SLEEP_TIME ))
  249. {
  250. timeout -= HAL_SLEEP_MS_TO_320US( MAX_SLEEP_TIME );
  251. halSleepSetTimer(HAL_SLEEP_MS_TO_320US( MAX_SLEEP_TIME ));
  252. }
  253. else
  254. {
  255. /* set sleep timer */
  256. halSleepSetTimer(timeout);
  257. }
  258. /* set up sleep timer interrupt */
  259. HAL_SLEEP_TIMER_CLEAR_INT();
  260. HAL_SLEEP_TIMER_ENABLE_INT();
  261. }
  262. #ifdef HAL_SLEEP_DEBUG_LED
  263. if (halPwrMgtMode == CC2530_PM1)
  264. {
  265. HAL_TURN_ON_LED1();
  266. }
  267. else
  268. {
  269. HAL_TURN_OFF_LED1();
  270. }
  271. #endif
  272. /* save interrupt enable registers and disable all interrupts */
  273. HAL_SLEEP_IE_BACKUP_AND_DISABLE(ien0, ien1, ien2);
  274. HAL_ENABLE_INTERRUPTS();
  275. /* set CC2530 power mode, interrupt is disabled after this function */
  276. HAL_SLEEP_SET_POWER_MODE(halPwrMgtMode);
  277. HAL_DISABLE_INTERRUPTS();
  278. /* restore interrupt enable registers */
  279. HAL_SLEEP_IE_RESTORE(ien0, ien1, ien2);
  280. /* disable sleep timer interrupt */
  281. HAL_SLEEP_TIMER_DISABLE_INT();
  282. /* Calculate timer elasped */
  283. halAccumulatedSleepTime += (HalTimerElapsed() / TICK_COUNT);
  284. /* deduct the sleep time for the next iteration */
  285. if ( osal_timeout > halAccumulatedSleepTime)
  286. {
  287. osal_timeout -= halAccumulatedSleepTime;
  288. }
  289. #ifdef HAL_SLEEP_DEBUG_LED
  290. HAL_TURN_ON_LED3();
  291. #else
  292. /* use this to turn LEDs back on after sleep */
  293. HalLedExitSleep();
  294. #endif
  295. /* handle peripherals; exit loop if key presses */
  296. if ( HalKeyExitSleep() )
  297. {
  298. break;
  299. }
  300. /* exit loop if no timer active */
  301. if ( timeout == 0 ) break;
  302. }
  303. /* power on the MAC; blocks until completion */
  304. MAC_PwrOnReq();
  305. }
  306. HAL_ENABLE_INTERRUPTS();
  307. }
  308. }
  309. /**************************************************************************************************
  310. * @fn halSleepSetTimer
  311. *
  312. * @brief This function sets the CC2530 sleep timer compare value. First it reads and
  313. * stores the value of the sleep timer; this value is used later to update OSAL
  314. * timers. Then the timeout value is converted from 320 usec units to 32 kHz
  315. * period units and the compare value is set to the timeout.
  316. *
  317. * input parameters
  318. *
  319. * @param timeout - Timeout value in 320 usec units. The sleep timer compare is set to
  320. * this value.
  321. *
  322. * output parameters
  323. *
  324. * None.
  325. *
  326. * @return None.
  327. **************************************************************************************************
  328. */
  329. void halSleepSetTimer(uint32 timeout)
  330. {
  331. uint32 ticks;
  332. /* read the sleep timer; ST0 must be read first */
  333. ((uint8 *) &ticks)[UINT32_NDX0] = ST0;
  334. ((uint8 *) &ticks)[UINT32_NDX1] = ST1;
  335. ((uint8 *) &ticks)[UINT32_NDX2] = ST2;
  336. ((uint8 *) &ticks)[UINT32_NDX3] = 0;
  337. /* store value for later */
  338. halSleepTimerStart = ticks;
  339. /* Compute sleep timer compare value. The ratio of 32 kHz ticks to 320 usec ticks
  340. * is 32768/3125 = 10.48576. This is nearly 671/64 = 10.484375.
  341. */
  342. ticks += (timeout * 671) / 64;
  343. /* subtract the processing time spent in function halSleep() */
  344. ticks -= HAL_SLEEP_ADJ_TICKS;
  345. /* set sleep timer compare; ST0 must be written last */
  346. ST2 = ((uint8 *) &ticks)[UINT32_NDX2];
  347. ST1 = ((uint8 *) &ticks)[UINT32_NDX1];
  348. ST0 = ((uint8 *) &ticks)[UINT32_NDX0];
  349. }
  350. /**************************************************************************************************
  351. * @fn TimerElapsed
  352. *
  353. * @brief Determine the number of OSAL timer ticks elapsed during sleep.
  354. *
  355. * input parameters
  356. *
  357. * @param None.
  358. *
  359. * output parameters
  360. *
  361. * None.
  362. *
  363. * @return Number of timer ticks elapsed during sleep.
  364. **************************************************************************************************
  365. */
  366. uint32 TimerElapsed( void )
  367. {
  368. return ( halAccumulatedSleepTime );
  369. }
  370. /**************************************************************************************************
  371. * @fn HalTimerElapsed
  372. *
  373. * @brief Determine the number of OSAL timer ticks elapsed during sleep. This function
  374. * relies on OSAL macro TICK_COUNT to be set to 1; then ticks are calculated in
  375. * units of msec.
  376. *
  377. * input parameters
  378. *
  379. * @param None.
  380. *
  381. * output parameters
  382. *
  383. * None.
  384. *
  385. * @return Number of timer ticks elapsed during sleep.
  386. **************************************************************************************************
  387. */
  388. uint32 HalTimerElapsed( void )
  389. {
  390. uint32 ticks;
  391. /* read the sleep timer; ST0 must be read first */
  392. ((uint8 *) &ticks)[UINT32_NDX0] = ST0;
  393. ((uint8 *) &ticks)[UINT32_NDX1] = ST1;
  394. ((uint8 *) &ticks)[UINT32_NDX2] = ST2;
  395. /* set bit 24 to handle wraparound */
  396. ((uint8 *) &ticks)[UINT32_NDX3] = 0x01;
  397. /* calculate elapsed time */
  398. ticks -= halSleepTimerStart;
  399. /* add back the processing time spent in function halSleep() */
  400. ticks += HAL_SLEEP_ADJ_TICKS;
  401. /* mask off excess if no wraparound */
  402. ticks &= 0x00FFFFFF;
  403. /* Convert elapsed time in milliseconds with round. 1000/32768 = 125/4096 */
  404. return ( ((ticks * 125) + 4095) / 4096 );
  405. }
  406. /**************************************************************************************************
  407. * @fn halSleepWait
  408. *
  409. * @brief Perform a blocking wait.
  410. *
  411. * input parameters
  412. *
  413. * @param duration - Duration of wait in microseconds.
  414. *
  415. * output parameters
  416. *
  417. * None.
  418. *
  419. * @return None.
  420. **************************************************************************************************
  421. */
  422. void halSleepWait(uint16 duration)
  423. {
  424. while (duration--)
  425. {
  426. asm("NOP");
  427. asm("NOP");
  428. asm("NOP");
  429. asm("NOP");
  430. asm("NOP");
  431. asm("NOP");
  432. asm("NOP");
  433. asm("NOP");
  434. }
  435. }
  436. /**************************************************************************************************
  437. * @fn halRestoreSleepLevel
  438. *
  439. * @brief Restore the deepest timer sleep level.
  440. *
  441. * input parameters
  442. *
  443. * @param None
  444. *
  445. * output parameters
  446. *
  447. * None.
  448. *
  449. * @return None.
  450. **************************************************************************************************
  451. */
  452. void halRestoreSleepLevel( void )
  453. {
  454. /* Stubs */
  455. }
  456. /**************************************************************************************************
  457. * @fn halSleepTimerIsr
  458. *
  459. * @brief Sleep timer ISR.
  460. *
  461. * input parameters
  462. *
  463. * None.
  464. *
  465. * output parameters
  466. *
  467. * None.
  468. *
  469. * @return None.
  470. **************************************************************************************************
  471. */
  472. HAL_ISR_FUNCTION(halSleepTimerIsr, ST_VECTOR)
  473. {
  474. HAL_SLEEP_TIMER_CLEAR_INT();
  475. CLEAR_SLEEP_MODE();
  476. /* CC2530 chip bug workaround */
  477. macMcuTimer2OverflowWorkaround();
  478. #ifdef HAL_SLEEP_DEBUG_POWER_MODE
  479. halSleepInt = TRUE;
  480. #endif
  481. }