]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - sub1ghz-sensor-to-cloud/tida01476.git/blob - sensor_cc1310lp/Application/MAC/OSAL/osal_pwrmgr.c
Initial Commit
[sub1ghz-sensor-to-cloud/tida01476.git] / sensor_cc1310lp / Application / MAC / OSAL / osal_pwrmgr.c
1 /******************************************************************************
3  @file  osal_pwrmgr.c
5  @brief This file contains the OSAL Power Management API.
7  Group: WCS, LPC, BTS
8  Target Device: CC13xx
10  ******************************************************************************
11  
12  Copyright (c) 2004-2017, Texas Instruments Incorporated
13  All rights reserved.
15  Redistribution and use in source and binary forms, with or without
16  modification, are permitted provided that the following conditions
17  are met:
19  *  Redistributions of source code must retain the above copyright
20     notice, this list of conditions and the following disclaimer.
22  *  Redistributions in binary form must reproduce the above copyright
23     notice, this list of conditions and the following disclaimer in the
24     documentation and/or other materials provided with the distribution.
26  *  Neither the name of Texas Instruments Incorporated nor the names of
27     its contributors may be used to endorse or promote products derived
28     from this software without specific prior written permission.
30  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
32  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
37  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
38  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
39  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
40  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  ******************************************************************************
43  Release Name: simplelink_cc13x0_sdk_1_30_00_06"
44  Release Date: 2017-03-08 14:43:25
45  *****************************************************************************/
47 /*********************************************************************
48  * INCLUDES
49  */
51 #include "comdef.h"
52 #include "onboard.h"
53 #include "osal.h"
54 #include "osal_tasks.h"
55 #include "osal_timers.h"
56 #include "osal_pwrmgr.h"
58 #ifdef USE_ICALL
59 #ifdef ICALL_JT
60   #include "icall_jt.h"
61 #else
62   #include <icall.h>
63 #endif /* ICALL_JT */   
64 #endif /* USE_ICALL */
66 #ifdef OSAL_PORT2TIRTOS
67 /* Direct port to TI-RTOS API */
68 #if defined CC26XX
69 #include <ti/drivers/Power.h>
70 #include <ti/drivers/power/PowerCC26XX.h>
71 #endif /* CC26XX */
72 #endif /* OSAL_PORT2TIRTOS */
74 /*********************************************************************
75  * MACROS
76  */
78 /*********************************************************************
79  * CONSTANTS
80  */
82 /*********************************************************************
83  * TYPEDEFS
84  */
86 /*********************************************************************
87  * GLOBAL VARIABLES
88  */
90 /* This global variable stores the power management attributes.
91  */
92 pwrmgr_attribute_t pwrmgr_attribute;
93 #if defined USE_ICALL || defined OSAL_PORT2TIRTOS
94 uint8 pwrmgr_initialized = FALSE;
95 #endif /* defined USE_ICALL || defined OSAL_PORT2TIRTOS */
97 /*********************************************************************
98  * EXTERNAL VARIABLES
99  */
101 /*********************************************************************
102  * EXTERNAL FUNCTIONS
103  */
105 /*********************************************************************
106  * LOCAL VARIABLES
107  */
109 /*********************************************************************
110  * LOCAL FUNCTION PROTOTYPES
111  */
113 /*********************************************************************
114  * FUNCTIONS
115  *********************************************************************/
117 /*********************************************************************
118  * @fn      osal_pwrmgr_init
119  *
120  * @brief   Initialize the power management system.
121  *
122  * @param   none.
123  *
124  * @return  none.
125  */
126 void osal_pwrmgr_init( void )
128 #if !defined USE_ICALL && !defined OSAL_PORT2TIRTOS
129   pwrmgr_attribute.pwrmgr_device = PWRMGR_ALWAYS_ON; // Default to no power conservation.
130 #endif /* USE_ICALL */
131   pwrmgr_attribute.pwrmgr_task_state = 0;            // Cleared.  All set to conserve
132 #if defined USE_ICALL || defined OSAL_PORT2TIRTOS
133   pwrmgr_initialized = TRUE;
134 #endif /* defined USE_ICALL || defined OSAL_PORT2TIRTOS */
137 #if !defined USE_ICALL && !defined OSAL_PORT2TIRTOS
138 /*********************************************************************
139  * @fn      osal_pwrmgr_device
140  *
141  * @brief   Sets the device power characteristic.
142  *
143  * @param   pwrmgr_device - type of power devices. With PWRMGR_ALWAYS_ON
144  *          selection, there is no power savings and the device is most
145  *          likely on mains power. The PWRMGR_BATTERY selection allows the
146  *          HAL sleep manager to enter sleep.
147  *
148  * @return  none
149  */
150 void osal_pwrmgr_device( uint8 pwrmgr_device )
152   pwrmgr_attribute.pwrmgr_device = pwrmgr_device;
154 #endif /* !defined USE_ICALL && !defined OSAL_PORT2TIRTOS*/
156 /*********************************************************************
157  * @fn      osal_pwrmgr_task_state
158  *
159  * @brief   This function is called by each task to state whether or
160  *          not this task wants to conserve power.
161  *
162  * @param   task_id - calling task ID.
163  *          state - whether the calling task wants to
164  *          conserve power or not.
165  *
166  * @return  TRUE if power is required; FALSE is power is not required
167  */
168 uint8 osal_pwrmgr_task_state( uint8 task_id, uint8 state )
170   halIntState_t intState;
171   bool pwrRequired = TRUE;
173   if ( task_id >= tasksCnt )
174     return ( pwrRequired );
176 #if defined USE_ICALL || defined OSAL_PORT2TIRTOS
177   if ( !pwrmgr_initialized )
178   {
179     /* If voting is made before this module is initialized,
180      * pwrmgr_task_state will reset later when the module is
181      * initialized, and cause incorrect activity count.
182      */
183     return ( pwrRequired );
184   }
185 #endif /* defined USE_ICALL || defined OSAL_PORT2TIRTOS */
187   HAL_ENTER_CRITICAL_SECTION( intState );
189   if ( state == PWRMGR_CONSERVE )
190   {
191 #if defined USE_ICALL || defined OSAL_PORT2TIRTOS
192     uint16 cache = pwrmgr_attribute.pwrmgr_task_state;
193 #endif /* defined USE_ICALL || defined OSAL_PORT2TIRTOS */
194     // Clear the task state flag
195     pwrmgr_attribute.pwrmgr_task_state &= ~(1 << task_id );
196 #if defined USE_ICALL || defined OSAL_PORT2TIRTOS
197     if (cache != 0 && pwrmgr_attribute.pwrmgr_task_state == 0)
198     {
199 #ifdef USE_ICALL
200       /* Decrement activity counter */
201       pwrRequired = ICall_pwrUpdActivityCounter(FALSE);
202 #else /* USE_ICALL */
203       Power_releaseConstraint(PowerCC26XX_SD_DISALLOW);
204       Power_releaseConstraint(PowerCC26XX_SB_DISALLOW);
205 #endif /* USE_ICALL */
206     }
207 #endif /* defined USE_ICALL || defined OSAL_PORT2TIRTOS */
208   }
209   else
210   {
211 #if defined USE_ICALL || defined OSAL_PORT2TIRTOS
212     if (pwrmgr_attribute.pwrmgr_task_state == 0)
213     {
214 #ifdef USE_ICALL
215       /* Increment activity counter */
216       (void)ICall_pwrUpdActivityCounter(TRUE);
217 #else /* USE_ICALL */
218       Power_setConstraint(PowerCC26XX_SD_DISALLOW);
219       Power_setConstraint(PowerCC26XX_SB_DISALLOW);
220 #endif /* USE_ICALL */
221     }
222 #endif /* defined USE_ICALL || defined OSAL_PORT2TIRTOS */
223     // Set the task state flag
224     pwrmgr_attribute.pwrmgr_task_state |= (1 << task_id);
225   }
227   HAL_EXIT_CRITICAL_SECTION( intState );
229   return ( pwrRequired );
232 #if defined( POWER_SAVING ) && !(defined USE_ICALL || defined OSAL_PORT2TIRTOS)
233 /*********************************************************************
234  * @fn      osal_pwrmgr_powerconserve
235  *
236  * @brief   This function is called from the main OSAL loop when there are
237  *          no events scheduled and shouldn't be called from anywhere else.
238  *
239  * @param   none.
240  *
241  * @return  none.
242  */
243 void osal_pwrmgr_powerconserve( void )
245   uint32        next;
246   halIntState_t intState;
248   // Should we even look into power conservation
249   if ( pwrmgr_attribute.pwrmgr_device != PWRMGR_ALWAYS_ON )
250   {
251     // Are all tasks in agreement to conserve
252     if ( pwrmgr_attribute.pwrmgr_task_state == 0 )
253     {
254       // Hold off interrupts.
255       HAL_ENTER_CRITICAL_SECTION( intState );
257       // Get next time-out
258       next = osal_next_timeout();
260       // Re-enable interrupts.
261       HAL_EXIT_CRITICAL_SECTION( intState );
263       // Put the processor into sleep mode
264       OSAL_SET_CPU_INTO_SLEEP( next );
265     }
266   }
268 #endif /* POWER_SAVING */
270 /*********************************************************************
271 *********************************************************************/