OSAL_PWRMGR.C osal Power Management API C file
HAL_SLEEP.C Power Management files at the bottom
Power Management Structural Body
typedef struct
{
UInt16 pwrmgr_task_state; //Task status
UInt16 pwrmgr_next_timeout; //Next time Out
UInt16 Accumulated_sleep_time; //Sleep time
Uint8 Pwrmgr_device; //Power Management device properties, with pwrmgr_always_on and
//pwrmgr_battery Two kinds
} pwrmgr_attribute_t;
#define PWRMGR_ALWAYS_ON 0
#define Pwrmgr_battery 1
choosing pwrmgr_always_on will not go into sleep mode, and choosing Pwrmgr_battery will allow HAL to manage the CPU into sleep lite or sleep deep state.
#define PWRMGR_CONSERVE 0
#define Pwrmgr_hold 1
low-power flag, mainly used in the osal_pwrmgr_task_state () function, to flag whether each task requires low power consumption.
extern pwrmgr_attribute_t Pwrmgr_attribute;
defines a global variable for power management.
Function
/*********************************************************************
* @fn Osal_pwrmgr_init
*
* @brief Initialize the power management function, this function in the osal.c inside the Osal_init_system () call, also * is in the osal system initialization when the power management mode to not go to sleep The state of the mode.
*
* @param none.
*
* @return None.
*/
void Osal_pwrmgr_init (void)
{
Pwrmgr_attribute.pwrmgr_device = pwrmgr_always_on; //No sleep mode by default
pwrmgr_attribute.pwrmgr_task_state = 0; //Clear 0
}
/*********************************************************************
* @fn Osal_pwrmgr_device
*
* @brief Set the Power management device properties.
*
* @param Pwrmgr_device-Choose pwrmgr_always_on words will not go into sleep mode, select
* Pwrmgr_battery will allow HAL to manage the CPU into sleep lite or sleep deep state.
*
* @return None
void Osal_pwrmgr_device (Uint8 pwrmgr_device)
{
Pwrmgr_attribute.pwrmgr_device = Pwrmgr_device;
}
/*********************************************************************
* @fn Osal_pwrmgr_task_state
*
* @brief This function can be called by each task to set whether the task supports low-power operation, such as
* Each task does not support low power consumption and will not operate in low power mode.
*
* @param task_id– task ID
* state– task requires low power consumption
*
* @return SUCCESS If task complete
*/
Uint8 osal_pwrmgr_task_state (uint8 task_id, uint8 State)
{
if (task_id >= taskscnt)
return (Invalid_task);
if (state = = Pwrmgr_conserve)
{
//Clear 0
Pwrmgr_attribute.pwrmgr_task_state &= ~ (1 << task_id);
}
Else
{
// position
Pwrmgr_attribute.pwrmgr_task_state |= (1 << task_id);
}
return (SUCCESS);
}
#if defined (power_saving)
/*********************************************************************
* @fn Osal_pwrmgr_powerconserve
*
* @brief This function is called in the Osal loop if no events need to be executed, the device enters
* Sleep mode, this function can not be called elsewhere.
* You need to open the Power_saving macro definition.
*
* @param none.
*
* @return None.
*/
void Osal_pwrmgr_powerconserve (void)
{
UInt16 Next;
halintstate_t intstate;
//First check if low power is supported
if (pwrmgr_attribute.pwrmgr_device! = pwrmgr_always_on)
{
//Whether all tasks support low power
if (pwrmgr_attribute.pwrmgr_task_state = = 0)
{
//Off interrupt
Hal_enter_critical_section (intstate);
//Query the software timer list to get the most recent overflow time
Next = Osal_next_timeout ();
//Open Interrupt
Hal_exit_critical_section (intstate);
//Put the system into sleep mode
Osal_set_cpu_into_sleep (next);
}
}
}
#endif/* power_saving */
The add red part is a macro definition, defined in OnBoard.h.
#define OSAL_SET_CPU_INTO_SLEEP (timeout) halsleep (timeout);
Halsleep (timeout) is defined in the HAL_SLEEP.C.
This involves some of the operation of the CC2530 Power Management register. You can see the code specifically.
/ * HAL Power management mode is set to power management state, the default state is Hal_sleep_off. The rest of the settings turn off the * closed system clock to stop the CPU.
* Hal_sleep_timer mode can be awakened by Sleep Timer interrupt and IO interrupt as well as reset.
* Hal_sleep_deep mode can be recalled by IO interrupt and reset.
*/
#define Hal_sleep_off CC2530_PM0
#define Hal_sleep_timer CC2530_PM2
#define HAL_SLEEP_DEEP CC2530_PM3
#define CC2530_PM0 0
#define CC2530_PM1 1
#define CC2530_PM2 2
#define CC2530_PM3 3
#define Max_sleep_time 510000
The maximum sleep time is 510000ms.
Summary
In fact, in this function can be seen in the osal is used in the sleep timer to control the sleep time, the system is initialized in the power control structure of the Pwrmgr_device device property is set to Pwrmgr_always_on, so the default does not enter the hibernation state. The Osal api of void Osal_pwrmgr_device (Uint8 pwrmgr_device) must be called in the application layer to set the osal into hibernation.
Such a api--uint8 osal_pwrmgr_task_state (uint8 task_id, uint8 State) is required in a user task to set whether this task supports hibernation, and if a task does not support hibernation, The entire system will not enter sleep mode. This has a related query in void Osal_pwrmgr_powerconserve (void).
In the main loop of osal, void Osal_start_system (void) calls the Osal_pwrmgr_powerconserve function.
void Osal_start_system (void)
{
#if!defined (zbit) &&!defined (ubit)
for (;;) Forever Loop
#endif
{
Uint8 idx = 0;
Osaltimeupdate ();
Hal_processpoll (); This replaces Mt_serialpoll () and Osal_check_timer ().
do {
if (Tasksevents[idx])//Task is highest.
{
Break
}
} while (++idx < taskscnt);
if (IDX < taskscnt)
{
UInt16 events;
halintstate_t intstate;
Hal_enter_critical_section (intstate);
events = Tasksevents[idx];
TASKSEVENTS[IDX] = 0; Clear the Events for this task.
Hal_exit_critical_section (intstate);
Events = (Tasksarr[idx]) (IDX, events); The most critical sentence, as shown in Figure one, runs the corresponding task
Hal_enter_critical_section (intstate);
TASKSEVENTS[IDX] |= events; Add back unprocessed events to the current task.
Hal_exit_critical_section (intstate);
}
#if defined (power_saving)
else/complete pass through all tasks events with no activity?
{
Osal_pwrmgr_powerconserve (); Put the Processor/system into sleep
}
#endif
}
}
Indicates that the Osal system discovers that no events need to be processed after checking all the task events so that the Osal_pwrmgr_powerconserve () function will be called when the Power_saving macro definition is open, in which case the function will enter hibernation according to the selection system.
Exit hibernation
Hibernation is exited when an IO interrupt or reset occurs, or when the sleep timer is interrupted. If it is an IO interrupt or a sleep timer interrupt exits, it will return to the place where it went into hibernation and proceed downward, and the reset exits to the initial part of the program.