CodeBrick笔记
低功耗
1. 时序系统,时间间隔到了会执行
void module_task_process(void)
{
    const task_item_t *t;
    for (t = &task_tbl_start + 1; t < &task_tbl_end; t++) {
        if  ((get_tick() - *t->timer) >= t->interval) {
            *t->timer = get_tick();
            t->handle();
        }
    }
}
2. 低功耗任务是正常任务的一种
static void pm_task(void)
{
    pm_process();
}task_register("pm", pm_task, 0); 
3. 低功耗使能+系统空闲 => 进入低功耗
void pm_process(void)
{
    if (!pm_watch.enable || !system_is_idle())
        return;
    system_goto_sleep();
}
3.1 空闲判断:轮询所有任务的空闲标志位
static bool system_is_idle(void)
{
    const pm_item_t *it;
    for (it = &pm_tbl_start + 1; it < &pm_tbl_end; it++) {
        if (it->idle != NULL && !it->idle())
            return false;
    }
    return true;
}
3.2 轮询所有低功耗任务的sleep_notify,获得最近下次唤醒时间,
static void system_goto_sleep(void)
{
    const pm_item_t   *it;
    const pm_adapter_t *adt;
    unsigned int sleep_time;
    unsigned int tmp;
    
    adt = pm_watch.adt;
    
    sleep_time = adt->max_sleep_time;
    
    //休眠处理
    for (it = &pm_tbl_start + 1; it < &pm_tbl_end; it++) {
        if (it->sleep_notify == NULL)
            continue;
        tmp = it->sleep_notify();          //休眠请求,并得到设备期待下次唤醒时间
        if (tmp && tmp < sleep_time)       //计算出所有设备中的最小允许休眠时间
            sleep_time = tmp;
    }
    
    adt->goto_sleep(sleep_time);
    
    //唤醒处理
    for (it = &pm_tbl_start + 1; it < &pm_tbl_end; it++) {
        if (it->wakeup_notify == NULL)
            continue;
        it->wakeup_notify();
    }
}
 
进入低功耗逻辑
低功耗任务示例
#define pm_dev_register(name, idle, sleep_notify, wakeup_notify)\
__pm_item_register(name, idle, sleep_notify, wakeup_notify)
static unsigned int  key_sleep_notify(void)
{
    return (key_busy(&key) || readkey()) ? 20 : 0;    /* 非空闲时20ms要唤醒1次*/
} pm_dev_register("key", NULL, key_sleep_notify, NULL);
static bool system_is_idle(void)
{
    return is_timeout(system_idle_time, 3000) && tty.rx_isempty() && tty.tx_isempty();
}pm_dev_register("sys", system_is_idle, NULL, NULL);
static unsigned int  led_sleep_notify(void)
{
    int i;
    for (i = 0; i < LED_TYPE_MAX; i++)
        if (blink_dev_busy(&led[i]))
            return 100;                               //如果有设备活动,最低100ms唤醒1次           
            
    return 0;
} pm_dev_register("led", NULL, led_sleep_notify, NULL);
 
