Telink 官网提供的 SDK “telink_hawk_ble_sdk_v1.3.0_20190906”,其中“blt_soft_timer.c”这个文件提供的函数接口只适用于,在其他地方创建和删除定时任务,不适用于在已经创建的定时任务中,创建或者删除其他定时任务。
如果在已经创建的定时任务中,创建或者删除其他定时任务,就会出现一些莫名其妙的问题,主要是因为打乱了原有的时序排列。究其根本原因是没有对定时器列表做原子操作。
我的定时应用只是在mainloop中执行,没放在中断或其他地方,也是为了保护数据不会被打乱。
blt_soft_timer_process(MAINLOOP_ENTRY);
下面我对c文件做了更改,主要思路是在执行回调前,先保存此回调的timer信息,执行完回调后,再从有可能已经被更改过的timer列表中找回现在正在调用的timer信息。
对“blt_soft_timer.c”我做了如下更改
1,添加全局变量 uint8_t m_global_ch_flag = 0;
2,在如下两个接口函数中添加 m_global_ch_flag = 1;
int blt_soft_timer_add(blt_timer_callback_t func, u32 interval_us)
int blt_soft_timer_delete(blt_timer_callback_t func)
3,增加自定义函数 uint8_t get_soft_timer_index(blt_timer_callback_t func)
//找到任务是否还在
//返回值不为0xff表示还存在
uint8_t get_soft_timer_index(blt_timer_callback_t func)
{
for(int i=0; i<blt_timer.currentNum; i++){
if(blt_timer.timer[i].cb == func){
return i;
}
}
return 0xff;
}
4,更改 void blt_soft_timer_process(int type)
void blt_soft_timer_process(int type)
{
if(type == CALLBACK_ENTRY){ //callback trigger
}
u32 now = clock_time();
if(!blt_timer.currentNum){
bls_pm_setAppWakeupLowPower(0, 0); //disable
return;
}
if( !blt_is_timer_expired(blt_timer.timer[0].t, now) ){
return;
}
int change_flg = 0;
int result;
do{//----------------<add>---------------------
for(int i=0; i<blt_timer.currentNum; i++){
m_global_ch_flag = 0;//----------------<add>---------------------
if(blt_is_timer_expired(blt_timer.timer[i].t ,now) ){ //timer trigger
if(blt_timer.timer[i].cb == NULL){
write_reg32(0x8000, 0x11111122); while(1); //debug ERR
}
else{
//----------------<add>---------------------
//首先记录下来
blt_time_event_t pre_excute_event = blt_timer.timer[i];
uint8_t post_excute_event_index = i;
result = blt_timer.timer[i].cb();
//----------------<add>---------------------
//发现有变化
if( m_global_ch_flag == 1){
//找到之前的任务索引
post_excute_event_index =get_soft_timer_index(pre_excute_event.cb);
}
//----------------<add>---------------------
//原先执行的东东还存在
if(post_excute_event_index != 0xff
&& blt_timer.timer[post_excute_event_index].interval == pre_excute_event.interval
&& blt_timer.timer[post_excute_event_index].t == pre_excute_event.t){
if(result < 0){
blt_soft_timer_delete_by_index(post_excute_event_index);
m_global_ch_flag = 1;//----------------<add>---------------------
}
else if(result == 0){
change_flg = 1;
blt_timer.timer[post_excute_event_index].t = now + blt_timer.timer[post_excute_event_index].interval;
}
else{ //set new timer interval
change_flg = 1;
blt_timer.timer[post_excute_event_index].interval = result * CLOCK_16M_SYS_TIMER_CLK_1US;
blt_timer.timer[post_excute_event_index].t = now + blt_timer.timer[post_excute_event_index].interval;
}
}
//----------------<add>---------------------
if(m_global_ch_flag == 1){
break;
}
}
}
}
}while(m_global_ch_flag == 1);//----------------<add>---------------------
if(blt_timer.currentNum ){ //timer table not empty
if(change_flg){
blt_soft_timer_sort();
}
if( (u32)(blt_timer.timer[0].t - now) < 8000 * CLOCK_16M_SYS_TIMER_CLK_1MS){
bls_pm_setAppWakeupLowPower(blt_timer.timer[0].t, 1);
}
else{
bls_pm_setAppWakeupLowPower(0, 0); //disable
}
}
else{
bls_pm_setAppWakeupLowPower(0, 0); //disable
}
}
另外,当BLE处于stable状态时,此 blt_soft_timer不再具有低功耗功能,因此在此文件中调用bls_pm_setAppWakeupLowPower中相应的位置,加上用户自定义的低功耗处理,最后在blt_pm_proc()中进行真实的低功耗设置即可。如下
static uint8_t m_usr_pm_en = 0;
static uint32_t m_usr_wakeup_tick = 0;
static uint8_t m_usr_timer_num = 0;
void usr_pm_setAppWakeupLowPower(u32 wakeup_tick, u8 enable, u8 timer_num)
{
m_usr_pm_en = enable;
m_usr_wakeup_tick = wakeup_tick;
m_usr_timer_num = timer_num;
}
void blt_pm_proc(void)
{
if( get_ble_state() == BLE_IDLE
&& get_key_task_pending_flag() == false
&& get_battery_charge_state() == STATE_NO_CHARGE)
{
if(m_usr_pm_en == 1)
{
if(!blt_is_timer_expired(m_usr_wakeup_tick, clock_time()))
{
cpu_sleep_wakeup(SUSPEND_MODE, PM_WAKEUP_CORE|PM_WAKEUP_TIMER, m_usr_wakeup_tick);
}
}
else
{
if(m_usr_timer_num == 0)
{
cpu_sleep_wakeup(SUSPEND_MODE, PM_WAKEUP_CORE, 0);
}
}
}
}
if(blt_timer.currentNum ){ //timer table not empty
if(change_flg){
blt_soft_timer_sort();
}
if( (u32)(blt_timer.timer[0].t - now) < 8000 * CLOCK_16M_SYS_TIMER_CLK_1MS){
bls_pm_setAppWakeupLowPower(blt_timer.timer[0].t, 1);
usr_pm_setAppWakeupLowPower(blt_timer.timer[0].t, 1 , 1);//----------------<add>---------------------
}
else{
bls_pm_setAppWakeupLowPower(0, 0); //disable
usr_pm_setAppWakeupLowPower(0, 0, 1);//----------------<add>---------------------
}
}
else{
bls_pm_setAppWakeupLowPower(0, 0); //disable
usr_pm_setAppWakeupLowPower(0, 0, 0);//----------------<add>---------------------
}