-
Notifications
You must be signed in to change notification settings - Fork 0
/
tTimer.c
167 lines (151 loc) · 5.09 KB
/
tTimer.c
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
158
159
160
161
162
163
164
165
166
167
#include "tinyOS.h"
#if TINYOS_ENABLE_TIMER == 1
static tList tTimerHardList; //"Ӳ"��ʱ���б�
static tList tTimerSoftList; //"��"��ʱ���б�
static tSem tTimerProtectSem; //���ڷ�����ʱ���б���ź���
static tSem tTimerTickSem; //������ʱ���������ж�ͬ���ļ����ź���
void tTimerInit(tTimer * timer, uint32_t delayTicks, uint32_t durationTicks,
void (*timerFunc) (void * arg), void * arg, uint32_t config)
{
tNodeInit(&timer->linkNode);
timer->startDelayTicks = delayTicks;
timer->durationTicks = durationTicks;
timer->timerFunc = timerFunc;
timer->arg = arg;
timer->config = config;
if(delayTicks == 0)
{
timer->delayTicks = durationTicks;
}
else
{
timer->delayTicks = timer->startDelayTicks;
}
timer->state = tTimerCreated;
}
void tTimerStart(tTimer * timer)
{
switch(timer->state)
{
case tTimerCreated:
case tTimerStopped:
timer->delayTicks = timer->startDelayTicks ? timer->startDelayTicks : timer->durationTicks;
timer->state = tTimerStarted;
if(timer->config & TIMER_CONFIG_TYPE_HARD)
{
uint32_t status = tTaskEnterCritical(); //Ӳ��ʱ������ʱ�ӽ����ж��д�������ʹ��critical������
tListAddLast(&tTimerHardList, &timer->linkNode);
tTaskExitCritical(status);
}
else
{
tSemWait(&tTimerProtectSem, 0); //��ʱ�����Ȼ�ȡ�ź������Դ����ʱ��ʱ�������ʱ�ڷ�����ʱ���б��µij�ͻ����
tListAddLast(&tTimerSoftList, &timer->linkNode);
tSemNotify(&tTimerProtectSem);
}
break;
default:
break;
}
}
void tTimerStop(tTimer * timer)
{
switch(timer->state)
{
case tTimerStarted:
case tTimerRunning:
if(timer->config & TIMER_CONFIG_TYPE_HARD)
{
uint32_t status = tTaskEnterCritical();
tListRemove(&tTimerHardList, &timer->linkNode);
tTaskExitCritical(status);
}
else
{
tSemWait(&tTimerProtectSem, 0);
tListRemove(&tTimerSoftList, &timer->linkNode);
tSemNotify(&tTimerProtectSem);
}
timer->state = tTimerStopped;
break;
default:
break;
}
}
void tTimerDestroy(tTimer * timer)
{
tTimerStop(timer);
timer->state = tTimerStopped;
}
void tTimerGetInfo(tTimer * timer, tTimerInfo * info)
{
uint32_t status = tTaskEnterCritical();
info->startDelayTicks = timer->startDelayTicks;
info->durationTicks = timer->durationTicks;
info->timerFunc = timer->timerFunc;
info->arg = timer->arg;
info->config = timer->config;
info->state = timer->state;
tTaskExitCritical(status);
}
//����ָ���Ķ�ʱ���б����ø�����ʱ��������
static void tTimerCallFuncList(tList * timerList)
{
tNode * node;
for(node = timerList->headNode.nextNode; node != &(timerList->headNode); node = node->nextNode)
{
tTimer * timer = tNodeParent(node, tTimer, linkNode);
if((timer->delayTicks == 0) || (--timer->delayTicks == 0))
{
timer->state = tTimerRunning;
timer->timerFunc(timer->arg);
timer->state = tTimerStarted;
if(timer->durationTicks > 0)
{
timer->delayTicks = timer->durationTicks;
}
else
{
tListRemove(timerList, &timer->linkNode);
timer->state = tTimerStopped;
}
}
}
}
//������ʱ���б������
static tTask tTimerTask;
static tTaskStack tTimerTaskStack[TINYOS_TIMERTASK_STACK_SIZE];
static void tTimerSoftTask(void * param)
{
for(; ;)
{
tSemWait(&tTimerTickSem, 0);//�ȴ�ϵͳ���ķ��͵��ж��¼��ź�
tSemWait(&tTimerProtectSem, 0);//��ȡ��ʱ���б�ķ���Ȩ��
tTimerCallFuncList(&tTimerSoftList);//������ʱ���б�
tSemNotify(&tTimerProtectSem);//�ͷŶ�ʱ���б����Ȩ��
}
}
//֪ͨ��ʱģ�飬ϵͳ����tick����
void tTimerModuleTickNotify(void)
{
uint32_t status = tTaskEnterCritical();
tTimerCallFuncList(&tTimerHardList);//����Ӳ��ʱģ��
tTaskExitCritical(status);
tSemNotify(&tTimerTickSem);//֪ͨ��ʱ�����ı仯
}
//��ʱ��ģ���ʼ��
void tTimerModuleInit(void)
{
tListInit(&tTimerHardList);
tListInit(&tTimerSoftList);
tSemInit(&tTimerProtectSem, 1, 1);
tSemInit(&tTimerTickSem, 0, 0);
}
void tTimerInitTask(void)
{
#if TINYOS_TIMERTASK_PRIO >= (TINYOS_PRO_COUNT - 1)
#error "The proprity of timer task must be greater then (TINYOS_PRO_COUNT - 1)"
#endif
tTaskInit(&tTimerTask, tTimerSoftTask, (void *)0,TINYOS_TIMERTASK_PRIO, tTimerTaskStack, TINYOS_TIMERTASK_STACK_SIZE);
}
#endif