-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpunctual.c
195 lines (161 loc) · 4.71 KB
/
punctual.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
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
/**
* @file punctual.c
* @author Atakan S.
* @date 01/01/2018
* @version 1.1
* @brief Scheduled task and timeout framework.
*
* @copyright Copyright (c) 2018 Atakan SARIOGLU ~ www.atakansarioglu.com
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "porty.h"
#ifdef _H_PUNCTUAL // Exclude from compile when not included in projech.h
#include "punctual.h"
#include <string.h>
// Variables.
tPunctualItem puncList[PUNCTUAL_MAX_TASKS]; ///< Array of punctual task items.
uint32_t puncPostscaler = 0; ///< Postscaler to slow down the ISR.
uint8_t puncCnt = 0; ///< Dummy counter for thread safe read write operations.
// Init.
void PunctualInit(void) {
// Clear the divider.
puncPostscaler = 0;
// Clear the list.
memset((uint8_t *) puncList, 0, sizeof(puncList));
}
// Punctual task scheduler.
tPunctualItem *PunctualCreate(uint32_t (*handle)(void *)) {
uint8_t i;
uint8_t intEn;
tPunctualItem *ptr = NULL;
// Save state and disable the interrupts.
intEn = mIsIntEnabled();
mIntDisable();
// Find a slot.
for (i = 0; i < PUNCTUAL_MAX_TASKS; i++) {
if (puncList[i].handle == NULL) {
// Place the handle.
puncList[i].handle = handle;
ptr = &(puncList[i]);
break;
}
}
// Re-enable interrupts.
if (intEn) {
mIntEnable();
}
return ptr;
}
// Destroys a task.
void PunctualDestroy(tPunctualItem *ptr) {
uint8_t intEn;
// Consistency check.
if ((ptr >= puncList) && (ptr <= &puncList[PUNCTUAL_MAX_TASKS])) {
// Save state and disable the interrupts.
intEn = mIsIntEnabled();
mIntDisable();
// Disable ISR operation.
ptr->handle = NULL;
// Re-enable interrupts.
if (intEn) {
mIntEnable();
}
}
}
// The interrupt routine of the scheduler.
void PunctualISR(void) {
tPunctualItem *ptr;
// Increase the divider.
if ((++puncPostscaler) >= PUNCTUAL_ISR_POSTSCALER) {
puncPostscaler = 0;
// Increase the counter.
puncCnt++;
// Scan all slots.
for (ptr = puncList; ptr < &puncList[PUNCTUAL_MAX_TASKS]; ptr++) {
if (ptr->handle != NULL) {
// Call it.
ptr->result = (ptr->handle)(ptr->param);
// Clear the param.
ptr->param = NULL;
}
}
}
}
// Send data to the task.
void PunctualSend(tPunctualItem *ptr, void *data) {
uint8_t intEn;
// Consistency check.
if ((ptr >= puncList) && (ptr <= &puncList[PUNCTUAL_MAX_TASKS])) {
// Save state and disable the interrupts.
intEn = mIsIntEnabled();
mIntDisable();
// Assign the data.
ptr->param = data;
// Re-enable interrupts.
if (intEn) {
mIntEnable();
}
}
}
// Thread-safe read the last result.
uint32_t PunctualReceive(tPunctualItem *ptr) {
uint8_t cnt;
uint32_t result;
// Thread-safe operation.
do{
cnt = puncCnt;
result = ptr->result;
}while(cnt != puncCnt);
return result;
}
// Creates a timeout handle.
void PunctualTimeoutSet(tTimeout *to, tTime ms){
to->period = ms;
to->due = PunctualGetTime() + ms;
}
// Edits a timeout handle.
void PunctualTimeoutEdit(tTimeout *to, tTime ms){
tTime timeNew = to->due - to->period;
to->period = ms;
to->due = timeNew + ms;
}
// Check timeout.
uint8_t PunctualTimeoutCheck(tTimeout *to){
int32_t timeDiff;
// Get signed time difference.
timeDiff = to->due - PunctualGetTime();
// Expiration check.
if(timeDiff <= 0){
// Increase due time.
to->due = to->due + to->period;
// Check the new due.
timeDiff = to->due - PunctualGetTime();
if(timeDiff <= 0){
// If already expired, reset the timeout.
to->due = PunctualGetTime() + to->period;
}
// Expired.
return true;
}else{
// Not expired.
return false;
}
}
#endif // Exclude from compile when not included in projech.h