-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathraw_system.c
440 lines (325 loc) · 9.15 KB
/
raw_system.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
/*
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-4 Created by jorya_txj
* xxxxxx please added here
*/
#include <raw_api.h>
/*
************************************************************************************************************************
* Finish interrupt
*
* Description: This function is called to when exit interrupt.
*
* Arguments :NONE
*
*
*
*
*
* Returns
*
* Note(s)
*
************************************************************************************************************************
*/
void raw_finish_int(void)
{
RAW_SR_ALLOC();
#if (CONFIG_RAW_ISR_STACK_CHECK > 0)
/*if you have no idea how to implement this function just write a blank port function*/
port_isr_stack_check();
#endif
/*It should not be zero here*/
RAW_ASSERT(raw_int_nesting != 0);
USER_CPU_INT_DISABLE();
raw_int_nesting--;
/*if still interrupt nested just return */
if (raw_int_nesting) {
USER_CPU_INT_ENABLE();
return;
}
/*if system is locked then just return*/
if (raw_sched_lock) {
USER_CPU_INT_ENABLE();
return;
}
/*get the highest task*/
get_ready_task(&raw_ready_queue);
/*if the current task is still the highest task then we do not need to have interrupt switch*/
if (high_ready_obj == raw_task_active) {
USER_CPU_INT_ENABLE();
return;
}
TRACE_INT_TASK_SWITCH(raw_task_active, high_ready_obj);
/*switch to the highest task, this function is cpu related, thus need to be ported*/
raw_int_switch();
USER_CPU_INT_ENABLE();
}
/*
************************************************************************************************************************
* Timer tick function
*
* Description: This function is called by timer interrupt.
*
* Arguments :None
*
*
*
*
* Returns None
*
* Note(s) Called by your own timer interrupt.
*
*
************************************************************************************************************************
*/
void raw_time_tick(void)
{
#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(raw_task_active->priority);
#endif
/*inform the timer task to update software timer*/
#if (CONFIG_RAW_TIMER > 0)
call_timer_task();
#endif
}
/*
************************************************************************************************************************
* Enter interrupt
*
* Description: This function is called to when enter into interrupt.
*
* Arguments :NONE
*
*
*
*
*
* Returns
*
* Note(s) raw_int_nesting can directly be accessed by interrupt function with cpu interrupt disabled!
*you must invoke raw_enter_interrupt before calling interrupt function.
*you must invoke raw_finish_int after calling interrupt function.
* you must invoke raw_enter_interrupt and raw_finish_int in pair.
*
************************************************************************************************************************
*/
RAW_OS_ERROR raw_enter_interrupt(void)
{
RAW_SR_ALLOC();
RAW_CPU_DISABLE();
if (raw_int_nesting >= INT_NESTED_LEVEL) {
RAW_CPU_ENABLE();
port_system_error_process(RAW_EXCEED_INT_NESTED_LEVEL, 0, 0, 0, 0, 0, 0);
return RAW_EXCEED_INT_NESTED_LEVEL;
}
raw_int_nesting++;
RAW_CPU_ENABLE();
return RAW_SUCCESS;
}
/*
************************************************************************************************************************
* Get system time
*
* Description: This function is called to get system time.
*
* Arguments :NONE
*
*
*
*
*
* Returns
* raw_tick_count: The raw_os time.
* Note(s) Becareful raw_tick_count will reset to 0 when it reach 0xffffffff.
*
************************************************************************************************************************
*/
RAW_TICK_TYPE raw_system_time_get(void)
{
return raw_tick_count;
}
RAW_OS_ERROR block_state_post_process(RAW_TASK_OBJ *task_ptr, void **msg)
{
RAW_U8 state;
RAW_OS_ERROR error_status;
state = task_ptr->block_status;
switch (state) {
case RAW_B_OK: /* We got the message */
/*if deal with msg*/
if (msg) {
*msg = task_ptr->msg;
}
error_status = RAW_SUCCESS;
break;
case RAW_B_ABORT: /* Indicate that we aborted */
error_status = RAW_BLOCK_ABORT;
break;
case RAW_B_TIMEOUT: /* Indicate that it is timeout */
error_status = RAW_BLOCK_TIMEOUT;
break;
case RAW_B_DEL: /* Indicate that object pended on has been deleted */
error_status = RAW_BLOCK_DEL;
break;
default:
RAW_ASSERT(0);
}
return error_status;
}
/*
************************************************************************************************************************
* Set system time
*
* Description: This function is called to set system time.
*
* Arguments :NONE
*
*
*
*
*
* Returns
* raw_tick_count: The raw_os time.
* Note(s)
*
************************************************************************************************************************
*/
RAW_OS_ERROR raw_system_time_set(RAW_TICK_TYPE time)
{
RAW_SR_ALLOC();
if (raw_int_nesting) {
return RAW_NOT_CALLED_BY_ISR;
}
RAW_CRITICAL_ENTER();
raw_tick_count = time;
RAW_CRITICAL_EXIT();
return RAW_SUCCESS;
}
#if (CONFIG_SYSTEM_MEMOPT > 0)
void *raw_memset(void *src, RAW_U8 byte, RAW_U32 count)
{
RAW_U8 *xs = src;
while (count--) {
*xs++ = byte;
}
return src;
}
void *raw_memcpy(void *dest, const void *src, RAW_U32 count)
{
RAW_U8 *tmp = dest;
const RAW_U8 *s = src;
while (count--) {
*tmp++ = *s++;
}
return dest;
}
#endif
RAW_S32 bit_search_first_one(RAW_U32 *base, RAW_U8 offset, RAW_S32 width)
{
register RAW_U32 *cp, v;
register RAW_S32 position;
cp = base;
/*caculate word position to bitmap*/
cp += offset >> 5;
/* clear all bit before offset(not include offset bit), do not need do this if offset is 32, 64, 96, etc......*/
if (offset & 31) {
#if (CONFIG_RAW_LITTLE_ENDIAN > 0)
v = *cp & ~(((RAW_U32)1 << (offset & 31)) - 1);
#else
v = *cp & (((RAW_U32)1 << (32 - (offset & 31))) - 1);
#endif
}
else {
v = *cp;
}
position = 0;
while (position < width) {
if (v){
/*Set right position first time*/
if (!position) {
position -= (offset & 31);
}
#if (CONFIG_RAW_LITTLE_ENDIAN > 0)
if (!(v & 0xffff)) {
v >>= 16;
position += 16;
}
if (!(v & 0xff)) {
v >>= 8;
position += 8;
}
if (!(v & 0xf)) {
v >>= 4;
position += 4;
}
if (!(v & 0x3)) {
v >>= 2;
position += 2;
}
if (!(v & 0x1)) {
++position;
}
#else
if (!(v & 0xffff0000)) {
v <<= 16;
position += 16;
}
if (!(v & 0xff000000)) {
v <<= 8;
position += 8;
}
if (!(v & 0xf0000000)) {
v <<= 4;
position += 4;
}
if (!(v & 0xc0000000)) {
v <<= 2;
position += 2;
}
if (!(v & 0x80000000)) {
++position;
}
#endif
if (position < width) {
return position;
} else {
return -1;
}
}
else {
/*Skip one world*/
if (position) {
position += 32;
} else {
position = 32 - (offset & 31);
}
v = *++cp;
}
}
return -1;
}