-
Notifications
You must be signed in to change notification settings - Fork 10
/
tn.c
329 lines (233 loc) · 10.4 KB
/
tn.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
/*
TNKernel real-time kernel
Copyright © 2004, 2013 Yuri Tiomkin
PIC32 version modifications copyright © 2013 Anders Montonen
All rights reserved.
Permission to use, copy, modify, and distribute this software in source
and binary forms and its documentation for any purpose and without fee
is hereby granted, provided that the above copyright notice appear
in all copies and that both that copyright notice and this permission
notice appear in supporting documentation.
THIS SOFTWARE IS PROVIDED BY THE YURI TIOMKIN AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL YURI TIOMKIN OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
/* ver 2.7 */
#include "tn.h"
#include "tn_utils.h"
// The System uses two levels of priorities for the own purpose:
//
// - level 0 (highest) for system timers task
// - level (TN_NUM_PRIORITY-1) (lowest) for system idle task
//---- System's global variables ----
CDLL_QUEUE tn_ready_list[TN_NUM_PRIORITY]; //-- all ready to run(RUNNABLE) tasks
CDLL_QUEUE tn_create_queue; //-- all created tasks
volatile int tn_created_tasks_qty; //-- num of created tasks
CDLL_QUEUE tn_wait_timeout_list; //-- all tasks that wait timeout expiration
unsigned short tn_tslice_ticks[TN_NUM_PRIORITY]; //-- for round-robin only
volatile int tn_system_state; //-- System state -(running/not running/etc.)
TN_TCB * tn_next_task_to_run; //-- Task to be run after switch context
TN_TCB * tn_curr_run_task; //-- Task that is running now
volatile unsigned int tn_ready_to_run_bmp;
volatile unsigned long tn_idle_count;
volatile unsigned long tn_curr_performance;
volatile int tn_int_nest_count;
void * tn_user_sp; //-- Saved task stack pointer
void * tn_int_sp; //-- Saved ISR stack pointer
align_attr_start unsigned int tn_int_stack[TN_INT_STACK_SIZE] align_attr_end;
//-- System tasks
//-- timer task - priority 0 - highest
#if defined (__ICCARM__) // IAR ARM
#pragma data_alignment=8
#endif
align_attr_start unsigned int tn_timer_task_stack[TN_TIMER_STACK_SIZE] align_attr_end;
TN_TCB tn_timer_task;
static void tn_timer_task_func(void * par);
//-- idle task - priority (TN_NUM_PRIORITY-1) - lowest
#if defined (__ICCARM__) // IAR ARM
#pragma data_alignment=8
#endif
align_attr_start unsigned int tn_idle_task_stack[TN_IDLE_STACK_SIZE] align_attr_end;
TN_TCB tn_idle_task;
static void tn_idle_task_func(void * par);
//----------------------------------------------------------------------------
// TN main function (never return)
//----------------------------------------------------------------------------
void tn_start_system(void)
{
int i;
//-- Clear/set all globals (vars, lists, etc)
for(i=0;i < TN_NUM_PRIORITY;i++)
{
queue_reset(&(tn_ready_list[i]));
tn_tslice_ticks[i] = NO_TIME_SLICE;
}
queue_reset(&tn_create_queue);
tn_created_tasks_qty = 0;
tn_system_state = TN_ST_STATE_NOT_RUN;
tn_ready_to_run_bmp = 0;
tn_idle_count = 0;
tn_curr_performance = 0;
tn_next_task_to_run = NULL;
tn_curr_run_task = NULL;
//-- Fill interrupt stack space with TN_FILL_STACK_VAL
for(i=0;i < TN_INT_STACK_SIZE;i++)
tn_int_stack[i] = TN_FILL_STACK_VAL;
//-- Pre-decrement stack
tn_int_sp = &(tn_int_stack[TN_INT_STACK_SIZE]);
//-- System tasks
queue_reset(&tn_wait_timeout_list);
//--- Timer task
tn_task_create((TN_TCB*)&tn_timer_task, //-- task TCB
tn_timer_task_func, //-- task function
0, //-- task priority
&(tn_timer_task_stack //-- task stack first addr in memory
[TN_TIMER_STACK_SIZE-1]),
TN_TIMER_STACK_SIZE, //-- task stack size (in int,not bytes)
NULL, //-- task function parameter
TN_TASK_TIMER); //-- Creation option
//--- Idle task
tn_task_create((TN_TCB*)&tn_idle_task, //-- task TCB
tn_idle_task_func, //-- task function
TN_NUM_PRIORITY-1, //-- task priority
&(tn_idle_task_stack //-- task stack first addr in memory
[TN_IDLE_STACK_SIZE-1]),
TN_IDLE_STACK_SIZE, //-- task stack size (in int,not bytes)
NULL, //-- task function parameter
TN_TASK_IDLE); //-- Creation option
//-- Activate timer & idle tasks
tn_next_task_to_run = &tn_idle_task; //-- Just for the task_to_runnable() proper op
task_to_runnable(&tn_idle_task);
task_to_runnable(&tn_timer_task);
tn_curr_run_task = &tn_idle_task; //-- otherwise it is NULL
//-- Run OS - first context switch
tn_start_exe();
}
//----------------------------------------------------------------------------
static void tn_timer_task_func(void * par)
{
TN_INTSAVE_DATA
volatile TN_TCB * task;
volatile CDLL_QUEUE * curr_que;
//-- User application init - user's objects initial (tasks etc.) creation
tn_app_init();
//-- Enable interrupt here ( include tick int)
tn_cpu_int_enable();
//-------------------------------------------------------------------------
for(;;)
{
//------------ OS timer tick -------------------------------------
tn_disable_interrupt();
curr_que = tn_wait_timeout_list.next;
while(curr_que != &tn_wait_timeout_list)
{
task = get_task_by_timer_queque((CDLL_QUEUE*)curr_que);
if(task->tick_count != TN_WAIT_INFINITE)
{
if(task->tick_count > 0)
{
task->tick_count--;
if(task->tick_count == 0) //-- Time out expiried
{
queue_remove_entry(&(((TN_TCB*)task)->task_queue));
task_wait_complete((TN_TCB*)task);
task->task_wait_rc = TERR_TIMEOUT;
}
}
}
curr_que = curr_que->next;
}
task_curr_to_wait_action(NULL,
TSK_WAIT_REASON_SLEEP,
TN_WAIT_INFINITE);
tn_enable_interrupt();
tn_switch_context();
}
}
//----------------------------------------------------------------------------
// In fact, this task is always in RUNNABLE state
//----------------------------------------------------------------------------
static void tn_idle_task_func(void * par)
{
#ifdef TN_MEAS_PERFORMANCE
TN_INTSAVE_DATA
#endif
for(;;)
{
#ifdef TN_MEAS_PERFORMANCE
tn_disable_interrupt();
#endif
tn_idle_count++;
#ifdef TN_MEAS_PERFORMANCE
tn_enable_interrupt();
#endif
}
}
//--- Set time slice ticks value for priority for round-robin scheduling
//--- If value is NO_TIME_SLICE there are no round-robin scheduling
//--- for tasks with priority. NO_TIME_SLICE is default value.
//----------------------------------------------------------------------------
int tn_sys_tslice_ticks(int priority,int value)
{
TN_INTSAVE_DATA
TN_CHECK_NON_INT_CONTEXT
if(priority <= 0 || priority >= TN_NUM_PRIORITY-1 ||
value < 0 || value > MAX_TIME_SLICE)
return TERR_WRONG_PARAM;
tn_disable_interrupt();
tn_tslice_ticks[priority] = value;
tn_enable_interrupt();
return TERR_NO_ERR;
}
//----------------------------------------------------------------------------
void tn_tick_int_processing()
{
TN_INTSAVE_DATA_INT
volatile CDLL_QUEUE * curr_que; //-- Need volatile here only to solve
volatile CDLL_QUEUE * pri_queue; //-- IAR(c) compiler's high optimization mode problem
volatile int priority;
TN_CHECK_INT_CONTEXT_NORETVAL
tn_idisable_interrupt();
//------- Round -robin (if is used)
priority = tn_curr_run_task->priority;
if(tn_tslice_ticks[priority] != NO_TIME_SLICE)
{
tn_curr_run_task->tslice_count++;
if(tn_curr_run_task->tslice_count > tn_tslice_ticks[priority])
{
tn_curr_run_task->tslice_count = 0;
pri_queue = &(tn_ready_list[priority]);
//-- If ready queue is not empty and qty of queue's tasks > 1
if(!(is_queue_empty((CDLL_QUEUE *)pri_queue)) && pri_queue->next->next != pri_queue)
{
// v.2.7 - Thanks to Vyacheslav Ovsiyenko
//-- Remove task from head and add it to the tail of
//-- ready queue for current priority
curr_que = queue_remove_head(&(tn_ready_list[priority]));
queue_add_tail(&(tn_ready_list[priority]),(CDLL_QUEUE *)curr_que);
}
}
}
//-- Enable a task with priority 0 - tn_timer_task
queue_remove_entry(&(tn_timer_task.task_queue));
tn_timer_task.task_wait_reason = 0;
tn_timer_task.task_state = TSK_STATE_RUNNABLE;
tn_timer_task.pwait_queue = NULL;
tn_timer_task.task_wait_rc = TERR_NO_ERR;
queue_add_tail(&(tn_ready_list[0]), &(tn_timer_task.task_queue));
tn_ready_to_run_bmp |= 1; // priority 0;
tn_next_task_to_run = &tn_timer_task;
tn_ienable_interrupt(); //-- !!! thanks to Audrius Urmanavicius !!!
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------