-
Notifications
You must be signed in to change notification settings - Fork 0
/
WaitTimer.h
233 lines (204 loc) · 6.19 KB
/
WaitTimer.h
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Time.h
*
* Created on: 09.03.2015
* Author: Richard
*
* Changelog:
* 2017 02 05
* added function continueTimer(WaitTimer*), not tested
* 2017 02 06
* refactoring: splitting WaitTimer and Button, changed task reference to number
* 2017 03 05
* inlining functions setTimer(), haltTimer(), continueTimer()
* 2017 03 31
* changed function setTimer(): now checks if active, activates task on start only
* if status is inactive
* changed function stopTimer(): deactivates timer before activating again to comply
* with changes to setTimer()
* 2017 05 04
* added flag WAITTIMER_TASK to identify that the waitScheduler is called from the task
* scheduler. if WAITTIMER_TASK is not defined, the waitScheduler is run directly in
* the timer ISR
*/
#ifndef WAITTIMER_H_
#define WAITTIMER_H_
#include "Task.h"
#include <RSOSDefines.h>
#include <stdint.h>
#include "RSOS_BasicInclude.h"
#ifdef MAXTIMERS
/**
* mask for the actual wait time
*/
#define timer_waitTimeMask 0x0FFF
/**
* mask for the exponent (0, 2, 4)
*/
#define exponentMask 0x3000
/**
* exponent 1
*/
#define WaitTimer_exponent_0 0x0000
/**
* exponent 2
*/
#define WaitTimer_exponent_2 0x1000
/**
* exponent 4
*/
#define WaitTimer_exponent_4 0x2000
/**
* bit identifier: cyclic
*/
#define WaitTimer_isCyclicTimer 0x4000
/**
* bit identifier: active
*/
#define WaitTimer_isActive 0x8000
struct WaitTimer_t;
/**
* WaitTimer structure
* Fields:
* currentWaitTime: the wait time the timer is currently in, decrements per cycle
* status: bit field which holds:
* ACEE WWWW WWWW WWWW
* A: is active
* C: is cyclic
* EE: exponent to wait time (0..4) wait time value is shifted by exponent
* possible combinations:
* 00: shift by 0
* 01: shift by 2
* 10: shift by 4
* 11: reserved
* W...: wait time (0..4095)
* taskOnStart: a task that is scheduled when its WaitTimer is set active
* taskOnStop: a task that is scheduled when its WaitTimer is stopped
* connectedButton: a button connected to this timer, to debounce button
*
* MEMORY:
* this structure takes up 6 Bytes
*
*/
typedef struct WaitTimer_t{
volatile uint16_t status;
volatile uint16_t currentWaitTime;
int8_t taskOnStart;
int8_t taskOnStop;
} WaitTimer;
extern int8_t timers_size;
extern WaitTimer waitTimers_mem[MAXTIMERS];
#ifdef WAITTIMER_TASK
extern Task* task_waitScheduler;
#endif /* WAITTIMER_TASK */
/**
* initialize the wait timer operation.
* Inits a task with priority 0 that schedules the
* waitScheduler. The waitScheduler is responsible for
* counting down the wait time and scheduling the tasks
*
* if WAITTIMER_TASK is defined,
* to operate, a free task structure is needed
* 1x Task
* consider the structures in your RSOSDefines
* the task is scheduled on every timer ISR and run when possible
*
* if WAITTIMER_TASK is not defined,
* the waitScheduler is run directly in the timer ISR.
*/
__EXTERN_C
void Timer_initOperation();
/**
* the waitTimer waitScheduler.
* checks all active WaitTimer, decrements their waitTime, runs the tasks if they stop
*/
__EXTERN_C
void waitScheduler();
/**
* to be called in a timer interrupt routine.
* The task task_waitScheduler is scheduled.
*/
static inline void Timer_ISR() __attribute__((always_inline));
static inline void Timer_ISR()
{
#ifdef WAITTIMER_TASK
scheduleTask(task_waitScheduler);
#else
waitScheduler();
#endif /* WAITTIMER_TASK */
}
/**
* initializes a wait timer. Timer can schedule a Task on starting and on stop
* @param waitTime: the time to wait; 0..250 Ticks (250 Ticks = 5 sec)
* @return a reference to the new WaitTimer
*/
__EXTERN_C
WaitTimer* initWaitTimer(uint16_t waitTime);
/**
* add a task that should be scheduled if the waitTimer is started
* if the timer is cyclic, the task is also scheduled every time the timer reaches zero
* @param waitTimer: the WaitTimer to add the task to
* @param task: the task to add
*/
__EXTERN_C
void setTaskOnStart(WaitTimer* waitTimer, Task* task);
/**
* add a task that should be scheduled if the WaitTimer is stopped
* if the timer is cyclic, the task is scheduled every time the timer reaches zero
* @param waitTimer: the WaitTimer to add the task to
* @param task: the task to add
*/
__EXTERN_C
void setTaskOnStop(WaitTimer* waitTimer, Task* task);
/**
* sets a new wait time to the timer. this only affects the current wait time, after the timer is stopped, the original time is restored
* can be used in combination with haltTimer and continueTimer
* @param waitTimer: the wait timer to set
* @param waitTime: the new wait time
*/
__EXTERN_C
void setNewWaitTime(uint16_t waitTime, WaitTimer* waitTimer);
/**
* sets the specified timer to be started again after it reaches zero
* @param waitTimer: the timer to set cyclic
*/
__EXTERN_C
void setTimerCyclic(WaitTimer* waitTimer);
/**
* Returns the status of the wait timer.
* @param waitTimer: the timer to get the status
* @return true if the timer is currently running, false if it is stopped
*/
static inline RSOS_bool Timer_isActive(WaitTimer* waitTimer) __attribute__((always_inline));
static inline RSOS_bool Timer_isActive(WaitTimer* waitTimer)
{
return waitTimer->status & WaitTimer_isActive ? RSOS_bool_true : RSOS_bool_false;
}
/**
* sets the specified timer active to count
* will activate the task on start if the timer is not running
* @param waitTimer: the timer to set active
*/
__EXTERN_C
void setTimer(WaitTimer* waitTimer);
/**
* stops the specified timer. the end task is not scheduled.
* @param waitTimer: the timer to stop
*/
static inline void haltTimer(WaitTimer* waitTimer) __attribute__((always_inline));
static inline void haltTimer(WaitTimer* waitTimer)
{
waitTimer->status &= ~WaitTimer_isActive;
}
/**
* continues the specified timer. the task on start is not scheduled,
* and the timer is not reset. It continues to run where it was halted.
* @param waitTimer: the timer to continue
*/
static inline void continueTimer(WaitTimer* waitTimer) __attribute__((always_inline));
static inline void continueTimer(WaitTimer* waitTimer)
{
waitTimer->status |= WaitTimer_isActive;
}
#endif /* MAXTIMERS */
#endif /* WAITTIMER_H_ */