-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.c
More file actions
158 lines (135 loc) · 3.83 KB
/
timer.c
File metadata and controls
158 lines (135 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include "timer.h"
typedef struct tiemr_
{
sl_uint8_t is_active; // 有效
sl_uint8_t run;
sl_uint32_t period;
sl_uint8_t timer_mode;
sl_uint8_t urgeflag;
sl_uint32_t tick;
sl_uint8_t timeoutflag;
callback cb;
}sl_tiemr;
sl_tiemr timer_pool[TIMER_MAX_NUM];
/**
* @brief 从定时器对象池中返回一个空闲的定时器对象,并设置定时器相关属性
*
* @param timer_id 用于接收生成的定时器对象id
* @param timer_mode 定时器模式 0: 周期循环 1:单次
* @param cb 定时时间到触发的回调函数
* @return sl_err_t
*/
sl_err_t timer_create(sl_uint8_t *timer_id, sl_uint8_t timer_mode, callback cb)
{
int index = 0;
while(timer_pool[index++].is_active && index <= TIMER_MAX_NUM);
if(index > TIMER_MAX_NUM)
return ERR_FULL;
timer_pool[index-1].is_active = TRUE;
timer_pool[index-1].run = FALSE;
timer_pool[index-1].timer_mode = timer_mode;
timer_pool[index-1].cb = cb;
timer_pool[index-1].timeoutflag = 0;
timer_pool[index-1].urgeflag = 0;
*timer_id = index-1;
return ERR_OK;
}
sl_err_t timer_create_urge(sl_uint8_t *timer_id, sl_uint8_t timer_mode, callback cb)
{
int index = 0;
while(timer_pool[index++].is_active && index <= TIMER_MAX_NUM);
if(index > TIMER_MAX_NUM)
return ERR_FULL;
timer_pool[index-1].is_active = TRUE;
timer_pool[index-1].run = FALSE;
timer_pool[index-1].timer_mode = timer_mode;
timer_pool[index-1].cb = cb;
timer_pool[index-1].timeoutflag = 0;
timer_pool[index-1].urgeflag = TRUE;
*timer_id = index-1;
return ERR_OK;
}
/**
* @brief 删除指定id的定时器对象
*
* @param timer_id 待删除的定时器对象id
* @return sl_err_t 成功: ERR_OK 失败:ERR_NOT_EXIST --id 不存在
*/
sl_err_t timer_delete(sl_uint8_t timer_id)
{
if (timer_id >= TIMER_MAX_NUM )
return ERR_NOT_EXIST;
timer_pool[timer_id].is_active = FALSE;
return ERR_OK;
}
sl_err_t timer_start(sl_uint8_t timer_id, sl_uint32_t period)
{
if (timer_id >= TIMER_MAX_NUM )
return ERR_NOT_EXIST;
timer_pool[timer_id].period = period/SL_SYS_TICK;
timer_pool[timer_id].tick = period/SL_SYS_TICK;
timer_pool[timer_id].run = TRUE;
return ERR_OK;
}
sl_err_t timer_reset(sl_uint8_t timer_id)
{
if (timer_id >= TIMER_MAX_NUM )
return ERR_NOT_EXIST;
timer_pool[timer_id].tick = timer_pool[timer_id].period;
return ERR_OK;
}
sl_err_t timer_stop(sl_uint8_t timer_id)
{
if (timer_id >= TIMER_MAX_NUM )
return ERR_NOT_EXIST;
timer_pool[timer_id].run = FALSE;
return ERR_OK;
}
/**
* @brief 定时器对象当时刷新任务,需要放在系统定时中断中执行
*
*/
void timer_tick()
{
for(int i=0; i<TIMER_MAX_NUM; i++)
{
if(timer_pool[i].run)
{
timer_pool[i].tick--;
if (timer_pool[i].tick == 0)
{
timer_pool[i].tick = timer_pool[i].period;
if (timer_pool[i].urgeflag)
{
timer_pool[i].cb();
}
else
{
timer_pool[i].timeoutflag = TRUE;
}
if (timer_pool[i].timer_mode == TIMER_MODE_ONE_SHOT)
{
timer_pool[i].run = 0;
}
}
}
}
}
/**
* @brief 定时器任务循环,放在主循环中调用,当定时器定时时间到,会自动触发对应的定时器回调函数
*
*/
void timer_task_loop()
{
int i;
for (i=0; i<TIMER_MAX_NUM; i++ )
{
if(timer_pool[i].is_active == FALSE)
continue;
if(timer_pool[i].timeoutflag == TRUE)
{
timer_pool[i].cb();
timer_pool[i].timeoutflag = 0;
}
}
}