-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrtos.c
363 lines (290 loc) · 9.85 KB
/
rtos.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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* The MIT License (MIT)
*
* Copyright (c) [2015] [Marco Russi]
*
* 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.
*/
/*
* This file rtos.c represents the source file of the RTOS component.
*
* Author : Marco Russi
*
* Evolution of the file:
* 06/08/2015 - File created - Marco Russi
*
*/
/*
TODO: modify callbacks implementation in the same way of tasks (use flag end manage the counter in the tick int only)
TODO: state switch function shall return a valid value. Do not check it in the execution task function
*/
/* ------------ Inclusions -------------- */
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
#include "tmr.h" /* component timer header file */
#include "rtos_cfg.h" /* component config header file */
#include "rtos.h" /* component header file */
/* ----------- Local constants definitions -------------- */
/* Tasks call counter timeout value */
#define UL_TASK_COUNTER_TIMEOUT ((uint32_t)((RTOS_UL_TASKS_PERIOD_MS * 1000) / RTOS_UL_TICK_PERIOD_US))
/* Max callback time value in ms */
#define U32_CALLBACK_MAX_VALUE_MS ((uint32_t)10000) /* 10 s */
/* First task index */
#define U8_FIRST_TASK_INDEX_VALUE 0
/* ------------- Local typedef definitions ------------- */
/* tick timer enum definition */
enum {
KE_TICK_TIMER_NOT_ELAPSED,
KE_TICK_TIMER_ELAPSED
};
/* ------------- Local variables declaration --------------- */
/* store actual RTOS state (from switch_state) */
static uint8_t rtos_actual_state;
/* store actual tick timer status */
static uint8_t tick_timer_status;
/* store counters value of tasks call */
static uint32_t task_counters;
/* store expired flag of all callbacks */
static bool callback_expired_array[RTOS_CB_ID_MAX_NUM] = {
false,
false,
false,
false,
false
};
/* store enable flag of all callbacks */
static bool callback_enabled_array[RTOS_CB_ID_MAX_NUM] = {
false,
false,
false,
false,
false
};
/* store counters value of all callbacks */
static uint32_t callback_counters_array[RTOS_CB_ID_MAX_NUM];
/* store timeout value of all callbacks */
static uint32_t callback_timeout_array[RTOS_CB_ID_MAX_NUM];
/* store callback function pointers */
static callback_ptr_t callback_functions_ptr_array[RTOS_CB_ID_MAX_NUM] = {
NULL,
NULL,
NULL,
NULL,
NULL
};
/* ------------- Local functions prototypes ------------- */
static uint8_t get_new_state_to_switch(uint8_t);
/* --------------- Exported functions ---------------- */
/* set and start callback */
void rtos_set_callback(uint8_t callback_id,
uint8_t callback_type,
uint32_t timer_period_ms,
void *callback_function_ptr)
{
/* It is important to update timeout variable first and then counter
* variable because an eventual tick interrupt can decrement counter value
* during this operation. Because the timeout value is updated only
* in periodic mode, this temp variable is necessary */
uint32_t temp_counter;
if ((callback_id < RTOS_CB_ID_CHECK)
&& (callback_type < RTOS_CB_TYPE_CHECK)
&& (timer_period_ms <= U32_CALLBACK_MAX_VALUE_MS)
&& (callback_function_ptr != NULL)) {
/* calculate counter value */
temp_counter = (uint32_t)((timer_period_ms * 1000) / RTOS_UL_TICK_PERIOD_US);
/* if periodic callback request */
if (RTOS_CB_TYPE_PERIODIC == callback_type) {
/* store timeout counter value */
callback_timeout_array[callback_id] = temp_counter;
}
/* store counter value */
callback_counters_array[callback_id] = temp_counter;
/* store callback function pointer */
callback_functions_ptr_array[callback_id] = callback_function_ptr;
/* callback enabled. do it as last operation */
callback_enabled_array[callback_id] = true;
} else {
/* invalid parameters */
}
}
/* stop callback */
void rtos_stop_callback(uint8_t callback_id)
{
if (callback_id < RTOS_CB_ID_CHECK) {
/* callback disabled. do it as first operation */
callback_enabled_array[callback_id] = false;
/* clear timeout counter value */
callback_timeout_array[callback_id] = 0;
/* clear callback counter value */
callback_counters_array[callback_id] = 0;
/* clear callback function pointer */
callback_functions_ptr_array[callback_id] = NULL;
} else {
/* invalid parameters */
}
}
/* Manage RTOS tick timer */
void rtos_tick_timer_callback(void)
{
uint8_t callback_index;
/* decrement callback counter for each enabled callback */
for (callback_index = 0; callback_index < RTOS_CB_ID_CHECK; callback_index++) {
/* manage enabled callbacks only */
if (callback_enabled_array[callback_index] == true) {
/* if counter is not yet expired */
if (callback_counters_array[callback_index] > 0) {
/* decrement counter */
callback_counters_array[callback_index]--;
} else {
/* set the flag to call the related callback function */
callback_expired_array[callback_index] = true;
/* if timeout value is valid than re-arm the counter */
if (callback_timeout_array[callback_index] > 0) {
/* callback is still enabled: re-arm counter */
callback_counters_array[callback_index] = callback_timeout_array[callback_index];
} else {
/* it was a single callback: the callback is disabled now */
callback_enabled_array[callback_index] = false;
}
}
} else {
/* else do nothing */
}
}
/* if tasks counter is elapsed set timeout flag */
if (task_counters > 0) {
/* decrement tasks call counter */
task_counters--;
} else {
/* re-arm tasks call counter value */
task_counters = UL_TASK_COUNTER_TIMEOUT;
/* indicate time base over */
tick_timer_status = KE_TICK_TIMER_ELAPSED;
}
}
/* Stop RTOS operation */
void rtos_stop_operation(void)
{
/* stop tick timer */
timer_stop();
}
/* Start RTOS operation: select required state if valid and start RTOS timer */
void rtos_start_operation(uint8_t required_state)
{
/* arm tasks call counter value */
task_counters = UL_TASK_COUNTER_TIMEOUT;
/* check required state validity */
if ((uint8_t)required_state < RTOS_CFG_KE_STATE_MAX_NUM) {
/* select the requested RTOS state */
rtos_actual_state = (uint8_t)required_state;
/* Start tick timer */
timer_setup();
} else {
/* invalid required state: do nothing */
}
}
/* If tick is elapsed then execute all scheduled tasks and select new required state */
void rtos_execute_task(void)
{
uint8_t task_index;
uint8_t rtos_required_state;
uint8_t callback_index;
/* manage callback functions */
for (callback_index = 0; callback_index < RTOS_CB_ID_CHECK; callback_index++) {
/* if callback counter is expired */
if (callback_expired_array[callback_index] == true) {
/* call callback function if pointer is valid (so, single callbacks are called once) */
if (callback_functions_ptr_array[callback_index] != NULL) {
/* call callback function */
(*callback_functions_ptr_array[callback_index])();
/* after function call clear the function pointer if the callback is now disabled */
if (callback_enabled_array[callback_index] == false) {
/* clear function pointer: stop callback */
callback_functions_ptr_array[callback_index] = NULL;
}
} else {
/* else callback is disabled, do nothing */
}
/* clear expired flag */
callback_expired_array[callback_index] = false;
} else {
/* else leave it to expire */
}
}
/* check if RTOS time base is elapsed */
if (KE_TICK_TIMER_ELAPSED == tick_timer_status) {
/* set tick timer not elapsed */
tick_timer_status = KE_TICK_TIMER_NOT_ELAPSED;
/* execute all tasks in actual selected RTOS state */
for (task_index = U8_FIRST_TASK_INDEX_VALUE;
rtos_cfg_states_array[rtos_actual_state][task_index] != NULL;
task_index++) {
/* call actual selected task of actual RTOS state */
(*rtos_cfg_states_array[rtos_actual_state][task_index])();
}
/* load new system state */
rtos_required_state =
get_new_state_to_switch(rtos_actual_state);
/* new system state supported? */
if (rtos_required_state < RTOS_CFG_KE_STATE_MAX_NUM) {
/* enter new system state */
rtos_actual_state = rtos_required_state;
} else {
/* remain in actual system state */
}
} else {
/* do nothing */
}
}
/* -------------- Local functions implementation ----------------- */
/* This function determines the next RTOS mode */
static uint8_t get_new_state_to_switch(uint8_t actual_state)
{
uint8_t required_state_return;
/* switch to system state */
switch (actual_state) {
/* RTOS_CFG_KE_INIT_STATE state? */
case RTOS_CFG_KE_INIT_STATE:
{
/* enter RTOS_CFG_KE_NORMAL_STATE state */
required_state_return = RTOS_CFG_KE_NORMAL_STATE;
break;
}
/* RTOS_CFG_KE_NORMAL_STATE state? */
case RTOS_CFG_KE_NORMAL_STATE:
{
/* remain in RTOS_CFG_KE_NORMAL_STATE state */
required_state_return = RTOS_CFG_KE_NORMAL_STATE;
break;
}
/* RTOS_CFG_KE_SLEEP_STATE state? */
case RTOS_CFG_KE_SLEEP_STATE:
{
break;
}
/* default */
default:
{
break;
}
}
/* return new determined system state */
return required_state_return;
}
/* End of file */