-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaitTimer.c
131 lines (112 loc) · 3.06 KB
/
WaitTimer.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
/*
* Time.c
*
* Created on: 09.03.2015
* Author: Richard
*/
#include "WaitTimer.h"
#include <HardwareAdaptionLayer.h>
#ifdef MAXTIMERS
#ifdef WAITTIMER_TASK
Task* task_waitScheduler = 0;
#endif /* WAITTIMER_TASK */
void waitScheduler();
void Timer_initOperation()
{
#ifdef WAITTIMER_TASK
task_waitScheduler = addTask(0, waitScheduler);
#endif /* WAITTIMER_TASK */
}
static inline uint16_t Timer_getExponentAndTime(uint16_t time) __attribute__((always_inline));
static inline uint16_t Timer_getExponentAndTime(uint16_t time) {
uint16_t timeCpy = time;
uint16_t exponent = 0;
while (timeCpy & ~timer_waitTimeMask) {
timeCpy >>= 2;
exponent += 2;
}
switch (exponent) {
case 0: break;
case 2: exponent = WaitTimer_exponent_2; break;
case 4: exponent = WaitTimer_exponent_4; break;
default: exponent = 0; break;
}
return (exponent | timeCpy);
}
WaitTimer* initWaitTimer(uint16_t waitTime)
{
waitTimers_mem[timers_size].status = 0;
waitTimers_mem[timers_size].status |= Timer_getExponentAndTime(waitTime);
waitTimers_mem[timers_size].currentWaitTime = 0;
waitTimers_mem[timers_size].taskOnStart = -1;
waitTimers_mem[timers_size].taskOnStop = -1;
timers_size += 1;
return &waitTimers_mem[timers_size-1];
}
static inline void stopTimer(WaitTimer* waitTimer) __attribute__((always_inline));
static inline void stopTimer(WaitTimer* waitTimer) {
if (waitTimer->taskOnStop != -1) {
scheduleTask(&task_mem[waitTimer->taskOnStop]);
}
waitTimer->status &= ~WaitTimer_isActive;
if (waitTimer->status & WaitTimer_isCyclicTimer) {
setTimer(waitTimer);
}
}
void setTaskOnStart(WaitTimer* waitTimer, Task* task)
{
waitTimer->taskOnStart = getTaskNumber(task);
}
void setTaskOnStop(WaitTimer* waitTimer, Task* task)
{
waitTimer->taskOnStop = getTaskNumber(task);
}
void setNewWaitTime(uint16_t waitTime, WaitTimer* waitTimer)
{
waitTimer->currentWaitTime = waitTime;
}
void setTimerCyclic(WaitTimer* waitTimer)
{
waitTimer->status |= WaitTimer_isCyclicTimer;
}
void setTimer(WaitTimer* waitTimer)
{
if (!(waitTimer->status & WaitTimer_isActive))
{
if (waitTimer->taskOnStart != -1)
{
scheduleTask(&task_mem[waitTimer->taskOnStart]);
}
switch (waitTimer->status & exponentMask) {
case WaitTimer_exponent_0: waitTimer->currentWaitTime = waitTimer->status & timer_waitTimeMask; break;
case WaitTimer_exponent_2: waitTimer->currentWaitTime = (waitTimer->status & timer_waitTimeMask) << 2; break;
case WaitTimer_exponent_4: waitTimer->currentWaitTime = (waitTimer->status & timer_waitTimeMask) << 4; break;
}
waitTimer->status |= WaitTimer_isActive;
}
}
void waitScheduler()
{
waitSchedulerEntered();
signed char i;
WaitTimer* wT;
for (i=timers_size; i>0; i--)
{
wT = &waitTimers_mem[i-1];
if (wT->status & WaitTimer_isActive)
{
if (wT->currentWaitTime != 0)
{
{
wT->currentWaitTime -= 1;
}
}
else
{
stopTimer(wT);
}
}
}
waitSchedulerExited();
}
#endif /* MAXTIMERS */