forked from jorya/raw-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw_task_0.c
579 lines (390 loc) · 12.9 KB
/
raw_task_0.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
raw os - Copyright (C) Lingjun Chen(jorya_txj).
This file is part of raw os.
raw os is free software; you can redistribute it it under the terms of the
GNU General Public License as published by the Free Software Foundation;
either version 3 of the License, or (at your option) any later version.
raw os is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. if not, write email to jorya.txj@gmail.com
---
A special exception to the LGPL can be applied should you wish to distribute
a combined work that includes raw os, without being obliged to provide
the source code for any proprietary components. See the file exception.txt
for full details of how and when the exception can be applied.
*/
/* 2012-10 Created by jorya_txj
* xxxxxx please added here
*/
#include <raw_api.h>
#if (CONFIG_RAW_TASK_0 > 0)
static void task_0_tick_handler(TASK_0_EVENT_TYPE ev, void *event_data)
{
event_data = event_data;
#if (CONFIG_RAW_USER_HOOK > 0)
raw_tick_hook();
#endif
/*update system time to calculate whether task timeout happens*/
#if (CONFIG_RAW_TICK_TASK > 0)
raw_task_semaphore_put(&tick_task_obj);
#else
tick_list_update();
#endif
/*update task time slice if possible*/
#if (CONFIG_SCHED_FIFO_RR > 0)
calculate_time_slice(ev);
#endif
/*inform the timer task to update software timer*/
#if (CONFIG_RAW_TIMER > 0)
call_timer_task();
#endif
}
/*
************************************************************************************************************************
* Timer tick function
*
* Description: This function is called by timer interrupt if CONFIG_RAW_TASK_0 is enabled.
*
* Arguments :None
*
*
*
*
* Returns None
*
* Note(s) Called by your own system timer interrupt.
*
*
************************************************************************************************************************
*/
RAW_U16 task_0_tick_post(void)
{
RAW_U16 ret;
ret = raw_task_0_post(&task_0_event_handler, raw_task_active->priority, 0);
return ret;
}
static RAW_U16 task_0_post(EVENT_HANLDER *p, TASK_0_EVENT_TYPE ev, void *event_data, RAW_U8 opt_send_method)
{
RAW_U16 task_0_event_position;
RAW_SR_ALLOC();
/*this function should not be called in task*/
if (raw_int_nesting == 0) {
return RAW_NOT_CALLED_BY_TASK;
}
/*fastest way to make task 0 ready*/
RAW_CPU_DISABLE();
/*if message is max, probally interrupt is too fast, please check your interrupt*/
if(task_0_events == MAX_TASK_EVENT) {
RAW_CPU_ENABLE();
TRACE_TASK_0_OVERFLOW(p, ev, event_data);
return RAW_TASK_0_EVENT_EXHAUSTED;
}
++task_0_events;
/*Update the debug information*/
if (task_0_events > peak_events) {
peak_events = task_0_events;
}
if (opt_send_method == SEND_TO_END) {
task_0_event_position = task_0_event_head;
task_0_event_head++;
if (task_0_event_head == MAX_TASK_EVENT) {
task_0_event_head = 0;
}
}
else {
if (task_0_event_end == 0) {
task_0_event_end = MAX_TASK_EVENT;
}
task_0_event_end--;
task_0_event_position = task_0_event_end;
}
/*Store the message*/
task_0_events_queue[task_0_event_position].ev = ev;
task_0_events_queue[task_0_event_position].event_data = event_data;
task_0_events_queue[task_0_event_position].p = p;
RAW_CPU_ENABLE();
return RAW_SUCCESS;
}
/*
************************************************************************************************************************
* Post an event to end of task 0 queue
*
* Description: This function is called to post an event to end of task 0 queue and implement FIFO.
*
* Arguments : p is which event handler
* ---------------------
* ev is the event signal
* ---------------------
* data is the event data
* Returns
*
* Note(s) this function should be called in interrupt to trigger task 0.
*
*
************************************************************************************************************************
*/
RAW_U16 raw_task_0_post(EVENT_HANLDER *p, TASK_0_EVENT_TYPE ev, void *event_data)
{
return task_0_post(p, ev, event_data, SEND_TO_END);
}
/*
************************************************************************************************************************
* Post an event to front of task 0 queue
*
* Description: This function is called to post an event to front of task 0 queue and implement LIFO.
*
* Arguments : p is which event handler
* ---------------------
* ev is the event signal
* ---------------------
* data is the event data
* Returns
*
* Note(s) this function should be called in interrupt to trigger task 0.
*
*
************************************************************************************************************************
*/
RAW_U16 raw_task_0_front_post(EVENT_HANLDER *p, TASK_0_EVENT_TYPE ev, void *event_data)
{
return task_0_post(p, ev, event_data, SEND_TO_FRONT);
}
static void task_0_process(void *pa)
{
TASK_0_EVENT_TYPE ev;
RAW_U8 *data_temp;
EVENT_HANLDER *receiver;
RAW_SR_ALLOC();
pa = pa;
/*to prevent interrupt happen here to cause system crash at task 0 start*/
USER_CPU_INT_DISABLE();
remove_ready_list(&raw_ready_queue, &raw_task_0_obj);
USER_CPU_INT_ENABLE();
while (1) {
/*Get the message info and update it*/
USER_CPU_INT_DISABLE();
if (task_0_events) {
--task_0_events;
/* There are events that we should deliver. */
ev = task_0_events_queue[task_0_event_end].ev;
data_temp = task_0_events_queue[task_0_event_end].event_data;
receiver = task_0_events_queue[task_0_event_end].p;
task_0_event_end++;
if (task_0_event_end == MAX_TASK_EVENT) {
task_0_event_end = 0u;
}
/*lock the scheduler, so event handler can not be switched out*/
raw_sched_lock = 1u;
USER_CPU_INT_ENABLE();
/*exceute the event handler*/
receiver->handle_event(ev, data_temp);
}
else {
/*unlock the scheduler, so scheduler can work*/
raw_sched_lock = 0u;
get_ready_task(&raw_ready_queue);
CONTEXT_SWITCH();
USER_CPU_INT_ENABLE();
}
}
}
void hybrid_int_process(void)
{
TASK_0_EVENT_TYPE hybrid_ev;
RAW_U8 *hybrid_data_temp;
EVENT_HANLDER *hybrid_receiver;
LIST *hybrid_node;
RAW_U8 hybrid_highest_pri;
RAW_SR_ALLOC();
register RAW_U8 hybrid_task_may_switch = 0u;
while (1) {
USER_CPU_INT_DISABLE();
if (task_0_events) {
/*current running task can never be task 0*/
if (raw_int_nesting) {
raw_sched_lock = 0;
USER_CPU_INT_ENABLE();
return;
}
else {
--task_0_events;
/* There are events that we should deliver. */
hybrid_ev = task_0_events_queue[task_0_event_end].ev;
hybrid_data_temp = task_0_events_queue[task_0_event_end].event_data;
hybrid_receiver = task_0_events_queue[task_0_event_end].p;
task_0_event_end++;
if (task_0_event_end == MAX_TASK_EVENT) {
task_0_event_end = 0;
}
USER_CPU_INT_ENABLE();
/*exceute the event handler*/
hybrid_receiver->handle_event(hybrid_ev, hybrid_data_temp);
hybrid_task_may_switch = 1;
}
}
else {
raw_sched_lock = 0;
if (hybrid_task_may_switch) {
hybrid_highest_pri = raw_ready_queue.highest_priority;
/*Highest priority task must be the first element on the list*/
hybrid_node = raw_ready_queue.task_ready_list[hybrid_highest_pri].next;
/*Get the highest priority task object*/
high_ready_obj = list_entry(hybrid_node, RAW_TASK_OBJ, task_list);
/*if highest task is currently task, then no need to do switch and just return*/
if (high_ready_obj == raw_task_active) {
USER_CPU_INT_ENABLE();
return;
}
CONTEXT_SWITCH();
}
USER_CPU_INT_ENABLE();
return;
}
}
}
#if (CONFIG_RAW_ZERO_INTERRUPT > 0)
static void int_msg_handler(TASK_0_EVENT_TYPE ev, void *msg_data)
{
OBJECT_INT_MSG *int_msg;
RAW_U16 int_msg_ret;
RAW_SR_ALLOC();
int_msg = msg_data;
int_msg_ret = RAW_SYSTEM_ERROR;
switch (ev) {
#if (CONFIG_RAW_TASK_SUSPEND > 0)
case RAW_TYPE_SUSPEND:
int_msg_ret = task_suspend((RAW_TASK_OBJ *)(int_msg->object));
break;
#endif
#if (CONFIG_RAW_TASK_RESUME > 0)
case RAW_TYPE_RESUME:
int_msg_ret = task_resume((RAW_TASK_OBJ *)(int_msg->object));
break;
#endif
#if (CONFIG_RAW_SEMAPHORE > 0)
case RAW_TYPE_SEM:
int_msg_ret = semaphore_put((RAW_SEMAPHORE *)(int_msg->object), WAKE_ONE_SEM);
break;
case RAW_TYPE_SEM_ALL:
int_msg_ret = semaphore_put((RAW_SEMAPHORE *)(int_msg->object), WAKE_ALL_SEM);
break;
#endif
#if (CONFIG_RAW_QUEUE > 0)
case RAW_TYPE_Q_FRONT:
int_msg_ret = msg_post((RAW_QUEUE *)(int_msg->object), int_msg->msg, SEND_TO_FRONT, WAKE_ONE_QUEUE);
break;
case RAW_TYPE_Q_END:
int_msg_ret = msg_post((RAW_QUEUE *)(int_msg->object), int_msg->msg, SEND_TO_END, WAKE_ONE_QUEUE);
break;
case RAW_TYPE_Q_ALL:
int_msg_ret = msg_post((RAW_QUEUE *)(int_msg->object), int_msg->msg, int_msg->opt, WAKE_ALL_QUEUE);
break;
#endif
#if (CONFIG_RAW_QUEUE_SIZE > 0)
case RAW_TYPE_Q_SIZE_FRONT:
int_msg_ret = msg_size_post((RAW_QUEUE_SIZE *)(int_msg->object), int_msg->msg, int_msg->msg_size, SEND_TO_FRONT, WAKE_ONE_QUEUE);
break;
case RAW_TYPE_Q_SIZE_END:
int_msg_ret = msg_size_post((RAW_QUEUE_SIZE *)(int_msg->object), int_msg->msg, int_msg->msg_size, SEND_TO_END, WAKE_ONE_QUEUE);
break;
case RAW_TYPE_Q_SIZE_ALL:
int_msg_ret = msg_size_post((RAW_QUEUE_SIZE *)(int_msg->object), int_msg->msg, int_msg->msg_size, int_msg->opt, WAKE_ALL_QUEUE);
break;
#endif
#if (CONFIG_RAW_EVENT > 0)
case RAW_TYPE_EVENT:
int_msg_ret = event_set((RAW_EVENT *)(int_msg->object), int_msg->event_flags, int_msg->opt);
break;
#endif
#if (CONFIG_RAW_IDLE_EVENT > 0)
case RAW_TYPE_IDLE_END_EVENT_POST:
int_msg_ret = event_post((ACTIVE_EVENT_STRUCT *)(int_msg->object), int_msg->msg_size, int_msg->msg, SEND_TO_END);
break;
case RAW_TYPE_IDLE_FRONT_EVENT_POST:
int_msg_ret = event_post((ACTIVE_EVENT_STRUCT *)(int_msg->object), int_msg->msg_size, int_msg->msg, SEND_TO_FRONT);
break;
#endif
default:
RAW_ASSERT(0);
}
if (int_msg_ret != RAW_SUCCESS) {
RAW_ASSERT(0);
}
RAW_CPU_DISABLE();
int_msg->next = free_object_int_msg;
free_object_int_msg = int_msg;
RAW_CPU_ENABLE();
}
static void int_msg_init(void)
{
OBJECT_INT_MSG *p_msg1;
OBJECT_INT_MSG *p_msg2;
RAW_U32 number;
msg_event_handler.handle_event = int_msg_handler;
number = OBJECT_INT_MSG_SIZE;
raw_memset(object_int_msg, 0, sizeof(object_int_msg));
free_object_int_msg = object_int_msg;
/*init the free msg list*/
p_msg1 = object_int_msg;
p_msg2 = object_int_msg;
p_msg2++;
while (--number) {
p_msg1->next = p_msg2;
p_msg1++;
p_msg2++;
}
/*init the last free msg*/
p_msg1->next = 0;
}
RAW_U16 int_msg_post(RAW_U8 type, void *p_obj, void *p_void, MSG_SIZE_TYPE msg_size, RAW_U32 flags, RAW_U8 opt)
{
void *msg_data;
RAW_SR_ALLOC();
RAW_CPU_DISABLE();
if (free_object_int_msg == 0) {
int_msg_full++;
RAW_CPU_ENABLE();
TRACE_INT_MSG_EXHAUSTED();
return RAW_INT_MSG_EXHAUSTED;
}
msg_data = free_object_int_msg;
free_object_int_msg->type = type;
free_object_int_msg->object = p_obj;
free_object_int_msg->msg = p_void;
free_object_int_msg->msg_size = msg_size;
free_object_int_msg->event_flags = flags;
free_object_int_msg->opt = opt;
free_object_int_msg = free_object_int_msg->next;
RAW_CPU_ENABLE();
TRACE_INT_MSG_POST(type, p_obj, p_void, msg_size, flags, opt);
return raw_task_0_post(&msg_event_handler, type, msg_data);
}
#endif
/*
************************************************************************************************************************
* Init task 0
*
* Description: This function is called to init task 0
*
* Arguments :NONE
*
* Returns
*
* Note(s)
*
*
************************************************************************************************************************
*/
void raw_task_0_init(void)
{
/*Create task 0 to handle fast interrupt event*/
raw_task_create(&raw_task_0_obj, (RAW_U8 *)"task_0_object", 0,
0, 0, task_0_stack, TASK_0_STACK_SIZE, task_0_process, 1);
task_0_event_handler.handle_event = task_0_tick_handler;
#if (CONFIG_RAW_ZERO_INTERRUPT > 0)
int_msg_init();
#endif
}
#endif