#include #include #include "timer.h" #include "cfg.h" #if (CHIP_TYPE == CHIP_T113s) #include "asm-generic/siginfo.h" #else #include #endif typedef struct { timer_t tid; int times; int times_max; timer_fn_t fn; void *arg; int inited; }timer_handle_t; static void timer_callback(sigval_t sv) { timer_handle_t *h=(timer_handle_t*)sv.sival_ptr; if(h->fn) { if(h->times_max>0 && h->timestimes_max) { int last=0; h->times++; if(h->times==h->times_max) { last = 1; } h->fn(h->arg, last); } else { h->fn(h->arg, 0); } } } static int set_time(timer_handle_t *h, int t1, int t2) { int r; struct itimerspec it; it.it_value.tv_sec = t1/1000; //��ʱ����ʼ����ʱ�� it.it_value.tv_nsec = (t1%1000)*1000000; it.it_interval.tv_sec = t2/1000; //��ʱ�����Ϊ0�룬��ʾֻ����һ�� it.it_interval.tv_nsec = (t2%1000)*1000000; r = timer_settime(h->tid, 0, &it, NULL); if(r<0) { return -1; } return 0; } int timer_init(void) { int r; struct sigevent sev; timer_handle_t *h=(timer_handle_t*)malloc(sizeof(timer_handle_t)); if(!h) { return -1; } sev.sigev_notify = SIGEV_THREAD; // ֪ͨ����Ϊ�߳� sev.sigev_notify_function = timer_callback; // �ص����� sev.sigev_notify_attributes = NULL; sev.sigev_value.sival_ptr = h; // ���ݸ��ص����������� r = timer_create(CLOCK_REALTIME, &sev, &h->tid); if(r<0) { free(h); return -1; } h->times = 0; h->times_max = 0; h->fn = NULL; h->arg = NULL; h->inited = 1; return (int)h; } int timer_deinit(int h) { int r; timer_handle_t *th=(timer_handle_t*)h; if((h==0)||(h==-1) || !th->inited) { return -1; } r = timer_delete(th->tid); th->inited = 0; free(th); return r; } int timer_start(int h, int t, int times, timer_fn_t fn, void *arg) { int r; timer_handle_t *th=(timer_handle_t*)h; if((h==0)||(h==-1) || !th->inited) { return -1; } set_time(th, 0, 0); th->times = 0; th->times_max = times; th->fn = fn; th->arg = arg; return set_time(th, 1000, t); } int timer_stop(int h) { int r; timer_handle_t *th=(timer_handle_t*)h; if((h==0)||(h==-1) || !th->inited) { return -1; } r = set_time(th, 0, 0); return r; }