hal_sleep.c 20 KB

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