mac_sleep.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**************************************************************************************************
  2. Filename: mac_sleep.c
  3. Revised: $Date: 2007-10-29 19:11:44 -0700 (Mon, 29 Oct 2007) $
  4. Revision: $Revision: 15810 $
  5. Description: Describe the purpose and contents of the file.
  6. Copyright 2006-2009 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. /* hal */
  38. #include "hal_types.h"
  39. /* high-level */
  40. #include "mac_pib.h"
  41. /* exported low-level */
  42. #include "mac_low_level.h"
  43. /* low-level specific */
  44. #include "mac_sleep.h"
  45. #include "mac_radio.h"
  46. #include "mac_tx.h"
  47. #include "mac_rx.h"
  48. #include "mac_rx_onoff.h"
  49. /* target specific */
  50. #include "mac_radio_defs.h"
  51. /* debug */
  52. #include "mac_assert.h"
  53. /* ------------------------------------------------------------------------------------------------
  54. * Global Variables
  55. * ------------------------------------------------------------------------------------------------
  56. */
  57. uint8 macSleepState = MAC_SLEEP_STATE_RADIO_OFF;
  58. /**************************************************************************************************
  59. * @fn macSleepWakeUp
  60. *
  61. * @brief Wake up the radio from sleep mode.
  62. *
  63. * @param none
  64. *
  65. * @return none
  66. **************************************************************************************************
  67. */
  68. MAC_INTERNAL_API void macSleepWakeUp(void)
  69. {
  70. /* don't wake up radio if it's already awake */
  71. if (macSleepState == MAC_SLEEP_STATE_AWAKE)
  72. {
  73. return;
  74. }
  75. /* wake up MAC timer */
  76. MAC_RADIO_TIMER_WAKE_UP();
  77. /* if radio was completely off, restore from that state first */
  78. if (macSleepState == MAC_SLEEP_STATE_RADIO_OFF)
  79. {
  80. /* turn on radio power (turns on oscillator too) */
  81. MAC_RADIO_TURN_ON_POWER();
  82. /* power-up initialization of receive logic */
  83. macRxRadioPowerUpInit();
  84. }
  85. else
  86. {
  87. MAC_ASSERT(macSleepState == MAC_SLEEP_STATE_OSC_OFF);
  88. /* turn on the oscillator */
  89. MAC_RADIO_TURN_ON_OSC();
  90. }
  91. /* update sleep state here before requesting to turn on receiver */
  92. macSleepState = MAC_SLEEP_STATE_AWAKE;
  93. /* turn on the receiver if enabled */
  94. macRxOnRequest();
  95. }
  96. /**************************************************************************************************
  97. * @fn macSleep
  98. *
  99. * @brief Puts radio into the selected sleep mode.
  100. *
  101. * @param sleepState - selected sleep level, see #defines in .h file
  102. *
  103. * @return TRUE if radio was successfully put into selected sleep mode.
  104. * FALSE if it was not safe for radio to go to sleep.
  105. **************************************************************************************************
  106. */
  107. MAC_INTERNAL_API uint8 macSleep(uint8 sleepState)
  108. {
  109. halIntState_t s;
  110. /* disable interrupts until macSleepState can be set */
  111. HAL_ENTER_CRITICAL_SECTION(s);
  112. /* assert checks */
  113. MAC_ASSERT(macSleepState == MAC_SLEEP_STATE_AWAKE); /* radio must be awake to put it to sleep */
  114. MAC_ASSERT(macRxFilter == RX_FILTER_OFF); /* do not sleep when scanning or in promiscuous mode */
  115. /* if either RX or TX is active or any RX enable flags are set, it's not OK to sleep */
  116. if (macRxActive || macRxOutgoingAckFlag || macTxActive || macRxEnableFlags)
  117. {
  118. HAL_EXIT_CRITICAL_SECTION(s);
  119. return(FALSE);
  120. }
  121. /* turn off the receiver */
  122. macRxOff();
  123. /* update sleep state variable */
  124. macSleepState = sleepState;
  125. /* macSleepState is now set, re-enable interrupts */
  126. HAL_EXIT_CRITICAL_SECTION(s);
  127. /* put MAC timer to sleep */
  128. MAC_RADIO_TIMER_SLEEP();
  129. /* put radio in selected sleep mode */
  130. if (sleepState == MAC_SLEEP_STATE_OSC_OFF)
  131. {
  132. MAC_RADIO_TURN_OFF_OSC();
  133. }
  134. else
  135. {
  136. MAC_ASSERT(sleepState == MAC_SLEEP_STATE_RADIO_OFF); /* unknown sleep state */
  137. MAC_RADIO_TURN_OFF_POWER();
  138. }
  139. /* radio successfully entered sleep mode */
  140. return(TRUE);
  141. }
  142. /**************************************************************************************************
  143. * Compile Time Integrity Checks
  144. **************************************************************************************************
  145. */
  146. #if ((MAC_SLEEP_STATE_AWAKE == MAC_SLEEP_STATE_OSC_OFF) || \
  147. (MAC_SLEEP_STATE_AWAKE == MAC_SLEEP_STATE_RADIO_OFF))
  148. #error "ERROR! Non-unique state values."
  149. #endif
  150. /**************************************************************************************************
  151. */