OSAL_PwrMgr.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /**************************************************************************************************
  2. Filename: OSAL_pwrmgr.c
  3. Revised: $Date: 2008-10-07 14:47:15 -0700 (Tue, 07 Oct 2008) $
  4. Revision: $Revision: 18212 $
  5. Description: This file contains the OSAL Power Management API.
  6. Copyright 2004-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 "comdef.h"
  37. #include "OnBoard.h"
  38. #include "OSAL.h"
  39. #include "OSAL_Tasks.h"
  40. #include "OSAL_Timers.h"
  41. #include "OSAL_PwrMgr.h"
  42. /*********************************************************************
  43. * MACROS
  44. */
  45. /*********************************************************************
  46. * CONSTANTS
  47. */
  48. /*********************************************************************
  49. * TYPEDEFS
  50. */
  51. /*********************************************************************
  52. * GLOBAL VARIABLES
  53. */
  54. /* This global variable stores the power management attributes.
  55. */
  56. pwrmgr_attribute_t pwrmgr_attribute;
  57. /*********************************************************************
  58. * EXTERNAL VARIABLES
  59. */
  60. /*********************************************************************
  61. * EXTERNAL FUNCTIONS
  62. */
  63. /*********************************************************************
  64. * LOCAL VARIABLES
  65. */
  66. /*********************************************************************
  67. * LOCAL FUNCTION PROTOTYPES
  68. */
  69. /*********************************************************************
  70. * FUNCTIONS
  71. *********************************************************************/
  72. /*********************************************************************
  73. * @fn osal_pwrmgr_init
  74. *
  75. * @brief Initialize the power management system.
  76. *
  77. * @param none.
  78. *
  79. * @return none.
  80. */
  81. void osal_pwrmgr_init( void )
  82. {
  83. pwrmgr_attribute.pwrmgr_device = PWRMGR_ALWAYS_ON; // Default to no power conservation.
  84. pwrmgr_attribute.pwrmgr_task_state = 0; // Cleared. All set to conserve
  85. }
  86. /*********************************************************************
  87. * @fn osal_pwrmgr_device
  88. *
  89. * @brief Sets the device power characteristic.
  90. *
  91. * @param pwrmgr_device - type of power devices. With PWRMGR_ALWAYS_ON
  92. * selection, there is no power savings and the device is most
  93. * likely on mains power. The PWRMGR_BATTERY selection allows the
  94. * HAL sleep manager to enter sleep.
  95. *
  96. * @return none
  97. */
  98. void osal_pwrmgr_device( uint8 pwrmgr_device )
  99. {
  100. pwrmgr_attribute.pwrmgr_device = pwrmgr_device;
  101. }
  102. /*********************************************************************
  103. * @fn osal_pwrmgr_task_state
  104. *
  105. * @brief This function is called by each task to state whether or
  106. * not this task wants to conserve power.
  107. *
  108. * @param task_id - calling task ID.
  109. * state - whether the calling task wants to
  110. * conserve power or not.
  111. *
  112. * @return SUCCESS if task complete
  113. */
  114. uint8 osal_pwrmgr_task_state( uint8 task_id, uint8 state )
  115. {
  116. if ( task_id >= tasksCnt )
  117. return ( INVALID_TASK );
  118. if ( state == PWRMGR_CONSERVE )
  119. {
  120. // Clear the task state flag
  121. pwrmgr_attribute.pwrmgr_task_state &= ~(1 << task_id );
  122. }
  123. else
  124. {
  125. // Set the task state flag
  126. pwrmgr_attribute.pwrmgr_task_state |= (1 << task_id);
  127. }
  128. return ( SUCCESS );
  129. }
  130. #if defined( POWER_SAVING )
  131. /*********************************************************************
  132. * @fn osal_pwrmgr_powerconserve
  133. *
  134. * @brief This function is called from the main OSAL loop when there are
  135. * no events scheduled and shouldn't be called from anywhere else.
  136. *
  137. * @param none.
  138. *
  139. * @return none.
  140. */
  141. void osal_pwrmgr_powerconserve( void )
  142. {
  143. uint16 next;
  144. halIntState_t intState;
  145. // Should we even look into power conservation
  146. if ( pwrmgr_attribute.pwrmgr_device != PWRMGR_ALWAYS_ON )
  147. {
  148. // Are all tasks in agreement to conserve
  149. if ( pwrmgr_attribute.pwrmgr_task_state == 0 )
  150. {
  151. // Hold off interrupts.
  152. HAL_ENTER_CRITICAL_SECTION( intState );
  153. // Get next time-out
  154. next = osal_next_timeout();
  155. // Re-enable interrupts.
  156. HAL_EXIT_CRITICAL_SECTION( intState );
  157. // Put the processor into sleep mode
  158. OSAL_SET_CPU_INTO_SLEEP( next );
  159. }
  160. }
  161. }
  162. #endif /* POWER_SAVING */
  163. /*********************************************************************
  164. *********************************************************************/