j嵌入式f_os之定时管理

本文介绍了jf_timer定时器模块的实现原理与使用方法。详细解释了定时器的数据结构、添加定时器的功能以及定时器轮询的过程。适用于需要了解或使用该定时器模块的开发者。
/*
 * jf_timer.h
 *
 *  Created on: Aug 20, 2016
 *      Author: lort
 */

#ifndef JF_OS_JF_TIMER_H_
#define JF_OS_JF_TIMER_H_

#include "jf_types.h"

typedef void (*time_fuc_t) (void *param);

typedef struct
{
    void     *next;
    uint32        decounter;
    uint32        reroad;
    time_fuc_t    fuc;
    void        *param;
}jf_timer_t;


#endif /* JF_OS_JF_TIMER_H_ */
#include "jf_types.h"
#include "jf_timer.h"
#include "jf_mem.h"

jf_timer_t *pjf_time = NULL;
void jf_timer_poll(uint32 ticks)
{
    jf_timer_t *pcur;
    if(pjf_time!=NULL)
    {
        pcur = pjf_time;
        while(pcur!=NULL)
        {
            if(pcur->param!=NULL)
            {
                if(pcur->decounter>=ticks)
                    pcur->decounter-=ticks;
                else
                {
                    pcur->decounter = 0;
                    pcur->fuc(pcur->param);
                    if(pcur->reroad)
                    {
                        pcur->decounter = pcur->reroad;
                    }
                    else
                    {
                        jf_free(pcur->param);
                        pcur->param = NULL;
                    }
                }
            }
            pcur = pcur->next;
        }
    }
}
int jf_timer_add(uint32 tim,uint32 reload, void *fuc,void *param)
{
    jf_timer_t *pcur;
    jf_timer_t *pnew;
    pnew = jf_malloc(sizeof(jf_timer_t));
    if(pnew!=NULL)
    {
        pnew->decounter = tim;
        pnew->reroad = reload;
        pnew->fuc = fuc;
        pnew->param = param;
        pnew->next = NULL;
    }
    else
        return -1;
    if(pjf_time==NULL)
    {
        pjf_time = pnew;
        return 0;
    }
    else
    {
        pcur = pjf_time;
        while(pcur->next!=NULL)
        {
            pcur = pcur->next;
        }
        pcur->next = pnew;
        return 0;
    }
}

 

转载于:https://www.cnblogs.com/lort/p/5790385.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值