www.pudn.com > uCOSV262.rar > os_core.lst
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 1
C51 COMPILER V8.05a, COMPILATION OF MODULE OS_CORE
OBJECT MODULE PLACED IN ..\Output\os_core.obj
COMPILER INVOKED BY: D:\Program Files\keil\C51\BIN\C51.EXE ..\SOURCE\os_core.c LARGE BROWSE INCDIR(..\app;..\Main;..\Por
-t;..\SOURCE) DEBUG OBJECTEXTEND PRINT(..\Output\os_core.lst) OBJECT(..\Output\os_core.obj)
line level source
1 /*
2 *********************************************************************************************************
3 * uC/OS-II
4 * The Real-Time Kernel
5 * CORE FUNCTIONS
6 *
7 * (c) Copyright 1992-2002, Jean J. Labrosse, Weston, FL
8 * All Rights Reserved
9 *
10 * File : OS_CORE.C
11 * By : Jean J. Labrosse
12 *********************************************************************************************************
13 */
14
15 #ifndef OS_MASTER_FILE
16 #define OS_GLOBALS
17 #include "includes.h"
18 #endif
19
20 /*
21 *********************************************************************************************************
22 * MAPPING TABLE TO MAP BIT POSITION TO BIT MASK
23 *
24 * Note: Index into table is desired bit position, 0..7
25 * Indexed value corresponds to bit mask
26 *********************************************************************************************************
27 */
28
29 INT8U const OSMapTbl[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
30
31 /*
32 *********************************************************************************************************
33 * PRIORITY RESOLUTION TABLE
34 *
35 * Note: Index into table is bit pattern to resolve highest priority
36 * Indexed value corresponds to highest priority bit position (i.e. 0..7)
37 *********************************************************************************************************
38 */
39
40 INT8U const OSUnMapTbl[256] = {
41 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x00 to 0x0F */
42 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x10 to 0x1F */
43 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x20 to 0x2F */
44 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x30 to 0x3F */
45 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x40 to 0x4F */
46 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x50 to 0x5F */
47 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x60 to 0x6F */
48 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x70 to 0x7F */
49 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x80 to 0x8F */
50 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0x90 to 0x9F */
51 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xA0 to 0xAF */
52 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xB0 to 0xBF */
53 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xC0 to 0xCF */
54 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xD0 to 0xDF */
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 2
55 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, /* 0xE0 to 0xEF */
56 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 /* 0xF0 to 0xFF */
57 };
58
59 /*$PAGE*/
60 /*
61 *********************************************************************************************************
62 * FUNCTION PROTOTYPES
63 *********************************************************************************************************
64 */
65 static void OS_InitEventList(void);
66 static void OS_InitMisc(void);
67 static void OS_InitRdyList(void);
68 static void OS_InitTaskIdle(void);
69 #if OS_TASK_STAT_EN > 0
static void OS_InitTaskStat(void);
#endif
72 static void OS_InitTCBList(void);
73
74 /*$PAGE*/
75 /*
76 *********************************************************************************************************
77 * GET THE NAME OF A SEMAPHORE, MUTEX, MAILBOX or QUEUE
78 *
79 * Description: This function is used to obtain the name assigned to a semaphore, mutex, mailbox or queue.
80 *
81 * Arguments : pevent is a pointer to the event group. 'pevent' can point either to a semaphore,
82 * a mutex, a mailbox or a queue. Where this function is concerned, the actual
83 * type is irrelevant.
84 *
85 * pname is a pointer to an ASCII string that will receive the name of the semaphore,
86 * mutex, mailbox or queue. The string must be able to hold at least
87 * OS_EVENT_NAME_SIZE characters.
88 *
89 * err is a pointer to an error code that can contain one of the following values:
90 *
91 * OS_NO_ERR if the name was copied to 'pname'
92 * OS_ERR_EVENT_TYPE if 'pevent' is not pointing to the proper event
93 * control block type.
94 * OS_ERR_PNAME_NULL You passed a NULL pointer for 'pname'
95 * OS_ERR_PEVENT_NULL if you passed a NULL pointer for 'pevent'
96 *
97 * Returns : The length of the string or 0 if the 'pevent' is a NULL pointer.
98 *********************************************************************************************************
99 */
100
101 #if OS_EVENT_NAME_SIZE > 0
INT8U OSEventNameGet (OS_EVENT *pevent, char *pname, INT8U *err)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
INT8U len;
OS_ENTER_CRITICAL();
#if OS_ARG_CHK_EN > 0
if (pevent == (OS_EVENT *)0) { /* Is 'pevent' a NULL pointer? */
OS_EXIT_CRITICAL(); /* Yes */
*err = OS_ERR_PEVENT_NULL;
return (0);
}
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 3
if (pname == (char *)0) { /* Is 'pname' a NULL pointer? */
OS_EXIT_CRITICAL(); /* Yes */
*err = OS_ERR_PNAME_NULL;
return (0);
}
#endif
switch (pevent->OSEventType) {
case OS_EVENT_TYPE_SEM:
case OS_EVENT_TYPE_MUTEX:
case OS_EVENT_TYPE_MBOX:
case OS_EVENT_TYPE_Q:
break;
default:
OS_EXIT_CRITICAL();
*err = OS_ERR_EVENT_TYPE;
return (0);
}
(void)strcpy(pname, pevent->OSEventName); /* Yes, copy name from OS_EVENT */
len = strlen(pname);
OS_EXIT_CRITICAL();
*err = OS_NO_ERR;
return (len);
}
#endif
142
143 /*$PAGE*/
144 /*
145 *********************************************************************************************************
146 * ASSIGN A NAME TO A SEMAPHORE, MUTEX, MAILBOX or QUEUE
147 *
148 * Description: This function assigns a name to a semaphore, mutex, mailbox or queue.
149 *
150 * Arguments : pevent is a pointer to the event group. 'pevent' can point either to a semaphore,
151 * a mutex, a mailbox or a queue. Where this function is concerned, it doesn't
152 * matter the actual type.
153 *
154 * pname is a pointer to an ASCII string that will be used as the name of the semaphore,
155 * mutex, mailbox or queue. The string must be able to hold at least
156 * OS_EVENT_NAME_SIZE characters.
157 *
158 * err is a pointer to an error code that can contain one of the following values:
159 *
160 * OS_NO_ERR if the requested task is resumed
161 * OS_ERR_EVENT_TYPE if 'pevent' is not pointing to the proper event
162 * control block type.
163 * OS_ERR_PNAME_NULL You passed a NULL pointer for 'pname'
164 * OS_ERR_PEVENT_NULL if you passed a NULL pointer for 'pevent'
165 *
166 * Returns : None
167 *********************************************************************************************************
168 */
169
170 #if OS_EVENT_NAME_SIZE > 0
void OSEventNameSet (OS_EVENT *pevent, char *pname, INT8U *err)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
INT8U len;
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 4
OS_ENTER_CRITICAL();
#if OS_ARG_CHK_EN > 0
if (pevent == (OS_EVENT *)0) { /* Is 'pevent' a NULL pointer? */
OS_EXIT_CRITICAL(); /* Yes */
*err = OS_ERR_PEVENT_NULL;
return;
}
if (pname == (char *)0) { /* Is 'pname' a NULL pointer? */
OS_EXIT_CRITICAL(); /* Yes */
*err = OS_ERR_PNAME_NULL;
return;
}
#endif
switch (pevent->OSEventType) {
case OS_EVENT_TYPE_SEM:
case OS_EVENT_TYPE_MUTEX:
case OS_EVENT_TYPE_MBOX:
case OS_EVENT_TYPE_Q:
break;
default:
OS_EXIT_CRITICAL();
*err = OS_ERR_EVENT_TYPE;
return;
}
len = strlen(pname); /* Can we fit the string in the storage area? */
if (len > (OS_EVENT_NAME_SIZE - 1)) { /* No */
OS_EXIT_CRITICAL();
*err = OS_ERR_EVENT_NAME_TOO_LONG;
return;
}
(void)strcpy(pevent->OSEventName, pname); /* Yes, copy name to the event control block */
OS_EXIT_CRITICAL();
*err = OS_NO_ERR;
}
#endif
215
216 /*$PAGE*/
217 /*
218 *********************************************************************************************************
219 * INITIALIZATION
220 *
221 * Description: This function is used to initialize the internals of uC/OS-II and MUST be called prior to
222 * creating any uC/OS-II object and, prior to calling OSStart().
223 *
224 * Arguments : none
225 *
226 * Returns : none
227 *********************************************************************************************************
228 */
229
230 void OSInit (void) KCREENTRANT
231 {
232 1 #if OS_VERSION >= 204
233 1 OSInitHookBegin(); /* Call port specific initialization code
- */
234 1 #endif
235 1
236 1 OS_InitMisc(); /* Initialize miscellaneous variables
- */
237 1
238 1 OS_InitRdyList(); /* Initialize the Ready List
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 5
- */
239 1
240 1 OS_InitTCBList(); /* Initialize the free list of OS_TCBs
- */
241 1
242 1 OS_InitEventList(); /* Initialize the free list of OS_EVENTs
- */
243 1
244 1 #if (OS_VERSION >= 251) &amt;&amt; (OS_FLAG_EN > 0) &amt;&amt; (OS_MAX_FLAGS > 0)
OS_FlagInit(); /* Initialize the event flag structures
- */
#endif
247 1
248 1 #if (OS_MEM_EN > 0) &amt;&amt; (OS_MAX_MEM_PART > 0)
OS_MemInit(); /* Initialize the memory manager
- */
#endif
251 1
252 1 #if (OS_Q_EN > 0) &amt;&amt; (OS_MAX_QS > 0)
OS_QInit(); /* Initialize the message queue structure
-s */
#endif
255 1
256 1 OS_InitTaskIdle(); /* Create the Idle Task
- */
257 1 #if OS_TASK_STAT_EN > 0
OS_InitTaskStat(); /* Create the Statistic Task
- */
#endif
260 1
261 1 #if OS_VERSION >= 204
262 1 OSInitHookEnd(); /* Call port specific init. code
- */
263 1 #endif
264 1 }
265 /*$PAGE*/
266 /*
267 *********************************************************************************************************
268 * ENTER ISR
269 *
270 * Description: This function is used to notify uC/OS-II that you are about to service an interrupt
271 * service routine (ISR). This allows uC/OS-II to keep track of interrupt nesting and thus
272 * only perform rescheduling at the last nested ISR.
273 *
274 * Arguments : none
275 *
276 * Returns : none
277 *
278 * Notes : 1) This function should be called ith interrupts already disabled
279 * 2) Your ISR can directly increment OSIntNesting without calling this function because
280 * OSIntNesting has been declared 'global'.
281 * 3) You MUST still call OSIntExit() even though you increment OSIntNesting directly.
282 * 4) You MUST invoke OSIntEnter() and OSIntExit() in pair. In other words, for every call
283 * to OSIntEnter() at the beginning of the ISR you MUST have a call to OSIntExit() at the
284 * end of the ISR.
285 * 5) You are allowed to nest interrupts up to 255 levels deep.
286 * 6) I removed the OS_ENTER_CRITICAL() and OS_EXIT_CRITICAL() around the increment because
287 * OSIntEnter() is always called with interrupts disabled.
288 *********************************************************************************************************
289 */
290
291 void OSIntEnter (void) KCREENTRANT
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 6
292 {
293 1 if (OSRunning == TRUE) {
294 2 if (OSIntNesting < 255u) {
295 3 OSIntNesting++; /* Increment ISR nesting level */
296 3 }
297 2 }
298 1 }
299 /*$PAGE*/
300 /*
301 *********************************************************************************************************
302 * EXIT ISR
303 *
304 * Description: This function is used to notify uC/OS-II that you have completed serviving an ISR. When
305 * the last nested ISR has completed, uC/OS-II will call the scheduler to determine whether
306 * a new, high-priority task, is ready to run.
307 *
308 * Arguments : none
309 *
310 * Returns : none
311 *
312 * Notes : 1) You MUST invoke OSIntEnter() and OSIntExit() in pair. In other words, for every call
313 * to OSIntEnter() at the beginning of the ISR you MUST have a call to OSIntExit() at the
314 * end of the ISR.
315 * 2) Rescheduling is prevented when the scheduler is locked (see OS_SchedLock())
316 *********************************************************************************************************
317 */
318
319 void OSIntExit (void) KCREENTRANT
320 {
321 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
324 1
325 1
326 1 if (OSRunning == TRUE) {
327 2 OS_ENTER_CRITICAL();
328 2 if (OSIntNesting > 0) { /* Prevent OSIntNesting from wrapping */
329 3 OSIntNesting--;
330 3 }
331 2 if (OSIntNesting == 0) { /* Reschedule only if all ISRs complete ... */
332 3 if (OSLockNesting == 0) { /* ... and not locked. */
333 4 OSIntExitY = OSUnMapTbl[OSRdyGrp];
334 4 OSPrioHighRdy = (INT8U)((OSIntExitY << 3) + OSUnMapTbl[OSRdyTbl[OSIntExitY]]);
335 4 if (OSPrioHighRdy != OSPrioCur) { /* No Ctx Sw if current task is highest rdy */
336 5 OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
337 5 #if OS_TASK_PROFILE_EN > 0
338 5 OSTCBHighRdy->OSTCBCtxSwCtr++; /* Inc. # of context switches to this task */
339 5 #endif
340 5 OSCtxSwCtr++; /* Keep track of the number of ctx switches */
341 5 OSIntCtxSw(); /* Perform interrupt level ctx switch */
342 5 }
343 4 }
344 3 }
345 2 OS_EXIT_CRITICAL();
346 2 }
347 1 }
348 /*$PAGE*/
349 /*
350 *********************************************************************************************************
351 * PREVENT SCHEDULING
352 *
353 * Description: This function is used to prevent rescheduling to take place. This allows your application
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 7
354 * to prevent context switches until you are ready to permit context switching.
355 *
356 * Arguments : none
357 *
358 * Returns : none
359 *
360 * Notes : 1) You MUST invoke OSSchedLock() and OSSchedUnlock() in pair. In other words, for every
361 * call to OSSchedLock() you MUST have a call to OSSchedUnlock().
362 *********************************************************************************************************
363 */
364
365 #if OS_SCHED_LOCK_EN > 0
366 void OSSchedLock (void) KCREENTRANT
367 {
368 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
371 1
372 1
373 1 if (OSRunning == TRUE) { /* Make sure multitasking is running */
374 2 OS_ENTER_CRITICAL();
375 2 if (OSLockNesting < 255u) { /* Prevent OSLockNesting from wrapping back to 0 */
376 3 OSLockNesting++; /* Increment lock nesting level */
377 3 }
378 2 OS_EXIT_CRITICAL();
379 2 }
380 1 }
381 #endif
382
383 /*$PAGE*/
384 /*
385 *********************************************************************************************************
386 * ENABLE SCHEDULING
387 *
388 * Description: This function is used to re-allow rescheduling.
389 *
390 * Arguments : none
391 *
392 * Returns : none
393 *
394 * Notes : 1) You MUST invoke OSSchedLock() and OSSchedUnlock() in pair. In other words, for every
395 * call to OSSchedLock() you MUST have a call to OSSchedUnlock().
396 *********************************************************************************************************
397 */
398
399 #if OS_SCHED_LOCK_EN > 0
400 void OSSchedUnlock (void) KCREENTRANT
401 {
402 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
405 1
406 1
407 1 if (OSRunning == TRUE) { /* Make sure multitasking is running */
408 2 OS_ENTER_CRITICAL();
409 2 if (OSLockNesting > 0) { /* Do not decrement if already 0 */
410 3 OSLockNesting--; /* Decrement lock nesting level */
411 3 if (OSLockNesting == 0) { /* See if scheduler is enabled and ... */
412 4 if (OSIntNesting == 0) { /* ... not in an ISR */
413 5 OS_EXIT_CRITICAL();
414 5 OS_Sched(); /* See if a HPT is ready */
415 5 } else {
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 8
416 5 OS_EXIT_CRITICAL();
417 5 }
418 4 } else {
419 4 OS_EXIT_CRITICAL();
420 4 }
421 3 } else {
422 3 OS_EXIT_CRITICAL();
423 3 }
424 2 }
425 1 }
426 #endif
427
428 /*$PAGE*/
429 /*
430 *********************************************************************************************************
431 * START MULTITASKING
432 *
433 * Description: This function is used to start the multitasking process which lets uC/OS-II manages the
434 * task that you have created. Before you can call OSStart(), you MUST have called OSInit()
435 * and you MUST have created at least one task.
436 *
437 * Arguments : none
438 *
439 * Returns : none
440 *
441 * Note : OSStartHighRdy() MUST:
442 * a) Call OSTaskSwHook() then,
443 * b) Set OSRunning to TRUE.
444 * c) Load the context of the task pointed to by OSTCBHighRdy.
445 * d_ Execute the task.
446 *********************************************************************************************************
447 */
448
449 void OSStart (void) KCREENTRANT
450 {
451 1 INT8U y;
452 1 INT8U x;
453 1
454 1
455 1 if (OSRunning == FALSE) {
456 2 y = OSUnMapTbl[OSRdyGrp]; /* Find highest priority's task priority number */
457 2 x = OSUnMapTbl[OSRdyTbl[y]];
458 2 OSPrioHighRdy = (INT8U)((y << 3) + x);
459 2 OSPrioCur = OSPrioHighRdy;
460 2 OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy]; /* Point to highest priority task ready to run */
461 2 OSTCBCur = OSTCBHighRdy;
462 2 OSStartHighRdy(); /* Execute target specific code to start task */
463 2 }
464 1 }
465 /*$PAGE*/
466 /*
467 *********************************************************************************************************
468 * STATISTICS INITIALIZATION
469 *
470 * Description: This function is called by your application to establish CPU usage by first determining
471 * how high a 32-bit counter would count to in 1 second if no other tasks were to execute
472 * during that time. CPU usage is then determined by a low priority task which keeps track
473 * of this 32-bit counter every second but this time, with other tasks running. CPU usage is
474 * determined by:
475 *
476 * OSIdleCtr
477 * CPU Usage (>) = 100 * (1 - ------------)
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 9
478 * OSIdleCtrMax
479 *
480 * Arguments : none
481 *
482 * Returns : none
483 *********************************************************************************************************
484 */
485
486 #if OS_TASK_STAT_EN > 0
void OSStatInit (void)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
OSTimeDly(2); /* Synchronize with clock tick */
OS_ENTER_CRITICAL();
OSIdleCtr = 0L; /* Clear idle counter */
OS_EXIT_CRITICAL();
OSTimeDly(OS_TICKS_PER_SEC); /* Determine MAX. idle counter value for 1 second */
OS_ENTER_CRITICAL();
OSIdleCtrMax = OSIdleCtr; /* Store maximum idle counter count in 1 second */
OSStatRdy = TRUE;
OS_EXIT_CRITICAL();
}
#endif
505 /*$PAGE*/
506 /*
507 *********************************************************************************************************
508 * PROCESS SYSTEM TICK
509 *
510 * Description: This function is used to signal to uC/OS-II the occurrence of a 'system tick' (also known
511 * as a 'clock tick'). This function should be called by the ticker ISR but, can also be
512 * called by a high priority task.
513 *
514 * Arguments : none
515 *
516 * Returns : none
517 *********************************************************************************************************
518 */
519
520 void OSTimeTick (void) KCREENTRANT
521 {
522 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register
- */
OS_CPU_SR cpu_sr;
#endif
525 1 OS_TCB *ptcb;
526 1 BOOLEAN step;
527 1
528 1
529 1 #if OS_TIME_TICK_HOOK_EN > 0
530 1 OSTimeTickHook(); /* Call user definable hook
- */
531 1 #endif
532 1 #if OS_TIME_GET_SET_EN > 0
533 1 OS_ENTER_CRITICAL(); /* Update the 32-bit tick counter
- */
534 1 OSTime++;
535 1 OS_EXIT_CRITICAL();
536 1 #endif
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 10
537 1 if (OSRunning == TRUE) {
538 2 #if OS_TICK_STEP_EN > 0
539 2 switch (OSTickStepState) { /* Determine whether we need to process a tick
- */
540 3 case OS_TICK_STEP_DIS: /* Yes, stepping is disabled
- */
541 3 step = TRUE;
542 3 break;
543 3
544 3 case OS_TICK_STEP_WAIT: /* No, waiting for uC/OS-View to set ...
- */
545 3 step = FALSE; /* .. OSTickStepState to OS_TICK_STEP_ONCE
- */
546 3 break;
547 3
548 3 case OS_TICK_STEP_ONCE: /* Yes, process tick once and wait for next ...
- */
549 3 step = TRUE; /* ... step command from uC/OS-View
- */
550 3 OSTickStepState = OS_TICK_STEP_WAIT;
551 3 break;
552 3
553 3 default: /* Invalid case, correct situation
- */
554 3 step = TRUE;
555 3 OSTickStepState = OS_TICK_STEP_DIS;
556 3 break;
557 3 }
558 2 #else
step = TRUE;
#endif
561 2 if (step == TRUE) {
562 3 ptcb = OSTCBList;
563 3 P3=0xff; /* Point at first TCB in TCB list */
564 3 while (ptcb->OSTCBPrio != OS_IDLE_PRIO) { /* Go through all TCBs in TCB list
- */
565 4 OS_ENTER_CRITICAL();
566 4 if (ptcb->OSTCBDly != 0) { /* Delayed or waiting for event with TO
- */
567 5 if (--ptcb->OSTCBDly == 0) { /* Decrement nbr of ticks to end of delay
- */
568 6 if ((ptcb->OSTCBStat &amt; OS_STAT_SUSPEND) == OS_STAT_RDY) { /* Is task suspended?
- */
569 7 OSRdyGrp |= ptcb->OSTCBBitY; /* No, Make task R-to-R (timed out
-)*/
570 7 OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
571 7 } else { /* Yes, Leave 1 tick to prevent loosing the ...
- */
572 7 ptcb->OSTCBDly = 1; /* ... task when the suspension is removed.
- */
573 7 }
574 6 }
575 5 }
576 4 ptcb = ptcb->OSTCBNext; /* Point at next TCB in TCB list
- */
577 4 OS_EXIT_CRITICAL();
578 4 }
579 3 P3=0x00;
580 3 }
581 2 }
582 1 }
583 /*$PAGE*/
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 11
584 /*
585 *********************************************************************************************************
586 * GET VERSION
587 *
588 * Description: This function is used to return the version number of uC/OS-II. The returned value
589 * corresponds to uC/OS-II's version number multiplied by 100. In other words, version 2.00
590 * would be returned as 200.
591 *
592 * Arguments : none
593 *
594 * Returns : the version number of uC/OS-II multiplied by 100.
595 *********************************************************************************************************
596 */
597
598 INT16U OSVersion (void)KCREENTRANT
599 {
600 1 return (OS_VERSION);
601 1 }
602
603 /*$PAGE*/
604 /*
605 *********************************************************************************************************
606 * DUMMY FUNCTION
607 *
608 * Description: This function doesn't do anything. It is called by OSTaskDel().
609 *
610 * Arguments : none
611 *
612 * Returns : none
613 *********************************************************************************************************
614 */
615
616 #if OS_TASK_DEL_EN > 0
617 void OS_Dummy (void) KCREENTRANT
618 {
619 1 }
620 #endif
621
622 /*$PAGE*/
623 /*
624 *********************************************************************************************************
625 * MAKE TASK READY TO RUN BASED ON EVENT OCCURING
626 *
627 * Description: This function is called by other uC/OS-II services and is used to ready a task that was
628 * waiting for an event to occur.
629 *
630 * Arguments : pevent is a pointer to the event control block corresponding to the event.
631 *
632 * msg is a pointer to a message. This pointer is used by message oriented services
633 * such as MAILBOXEs and QUEUEs. The pointer is not used when called by other
634 * service functions.
635 *
636 * msk is a mask that is used to clear the status byte of the TCB. For example,
637 * OSSemPost() will pass OS_STAT_SEM, OSMboxPost() will pass OS_STAT_MBOX etc.
638 *
639 * Returns : none
640 *
641 * Note : This function is INTERNAL to uC/OS-II and your application should not call it.
642 *********************************************************************************************************
643 */
644 #if OS_EVENT_EN > 0
INT8U OS_EventTaskRdy (OS_EVENT *pevent, void *msg, INT8U msk) KCREENTRANT
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 12
{
OS_TCB *ptcb;
INT8U x;
INT8U y;
INT8U bitx;
INT8U bity;
INT8U prio;
y = OSUnMapTbl[pevent->OSEventGrp]; /* Find HPT waiting for message */
bity = OSMapTbl[y];
x = OSUnMapTbl[pevent->OSEventTbl[y]];
bitx = OSMapTbl[x];
prio = (INT8U)((y << 3) + x); /* Find priority of task getting the msg */
pevent->OSEventTbl[y] &amt;= ~bitx; /* Remove this task from the waiting list */
if (pevent->OSEventTbl[y] == 0x00) {
pevent->OSEventGrp &amt;= ~bity; /* Clr group bit if this was only task pending */
}
ptcb = OSTCBPrioTbl[prio]; /* Point to this task's OS_TCB */
ptcb->OSTCBDly = 0; /* Prevent OSTimeTick() from readying task */
ptcb->OSTCBEventPtr = (OS_EVENT *)0; /* Unlink ECB from this task */
#if ((OS_Q_EN > 0) &amt;&amt; (OS_MAX_QS > 0)) || (OS_MBOX_EN > 0)
ptcb->OSTCBMsg = msg; /* Send message directly to waiting task */
#else
msg = msg; /* Prevent compiler warning if not used */
#endif
ptcb->OSTCBStat &amt;= ~msk; /* Clear bit associated with event type */
if (ptcb->OSTCBStat == OS_STAT_RDY) { /* See if task is ready (could be susp'd) */
OSRdyGrp |= bity; /* Put task in the ready to run list */
OSRdyTbl[y] |= bitx;
}
return (prio);
}
#endif
680 /*$PAGE*/ /*
681 /*
682 *********************************************************************************************************
683 * MAKE TASK WAIT FOR EVENT TO OCCUR
684 *
685 * Description: This function is called by other uC/OS-II services to suspend a task because an event has
686 * not occurred.
687 *
688 * Arguments : pevent is a pointer to the event control block for which the task will be waiting for.
689 *
690 * Returns : none
691 *
692 * Note : This function is INTERNAL to uC/OS-II and your application should not call it.
693 *********************************************************************************************************
694 */
695 #if OS_EVENT_EN > 0
void OS_EventTaskWait (OS_EVENT *pevent)KCREENTRANT
{
INT8U y;
OSTCBCur->OSTCBEventPtr = pevent; /* Store pointer to event control block in TCB */
y = OSTCBCur->OSTCBY; /* Task no longer ready */
OSRdyTbl[y] &amt;= ~OSTCBCur->OSTCBBitX;
if (OSRdyTbl[y] == 0x00) {
OSRdyGrp &amt;= ~OSTCBCur->OSTCBBitY; /* Clear event grp bit if this was only task pending */
}
pevent->OSEventTbl[OSTCBCur->OSTCBY] |= OSTCBCur->OSTCBBitX; /* Put task in waiting list */
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 13
pevent->OSEventGrp |= OSTCBCur->OSTCBBitY;
}
#endif
711 /*$PAGE*/
712 /*
713 *********************************************************************************************************
714 * MAKE TASK READY TO RUN BASED ON EVENT TIMEOUT
715 *
716 * Description: This function is called by other uC/OS-II services to make a task ready to run because a
717 * timeout occurred.
718 *
719 * Arguments : pevent is a pointer to the event control block which is readying a task.
720 *
721 * Returns : none
722 *
723 * Note : This function is INTERNAL to uC/OS-II and your application should not call it.
724 *********************************************************************************************************
725 */
726 #if OS_EVENT_EN > 0
void OS_EventTO (OS_EVENT *pevent) KCREENTRANT
{
INT8U y;
y = OSTCBCur->OSTCBY;
pevent->OSEventTbl[y] &amt;= ~OSTCBCur->OSTCBBitX;
if (pevent->OSEventTbl[y] == 0x00) {
pevent->OSEventGrp &amt;= ~OSTCBCur->OSTCBBitY;
}
OSTCBCur->OSTCBStat = OS_STAT_RDY; /* Set status to ready */
OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0; /* No longer waiting for event */
}
#endif
741 /*$PAGE*/
742 /*
743 *********************************************************************************************************
744 * INITIALIZE EVENT CONTROL BLOCK'S WAIT LIST
745 *
746 * Description: This function is called by other uC/OS-II services to initialize the event wait list.
747 *
748 * Arguments : pevent is a pointer to the event control block allocated to the event.
749 *
750 * Returns : none
751 *
752 * Note : This function is INTERNAL to uC/OS-II and your application should not call it.
753 *********************************************************************************************************
754 */
755 #if OS_EVENT_EN > 0
void OS_EventWaitListInit (OS_EVENT *pevent) KCREENTRANT
{
INT8U *ptbl;
pevent->OSEventGrp = 0x00; /* No task waiting on event */
ptbl = &amt;pevent->OSEventTbl[0];
#if OS_EVENT_TBL_SIZE > 0
*ptbl++ = 0x00;
#endif
#if OS_EVENT_TBL_SIZE > 1
*ptbl++ = 0x00;
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 14
#endif
#if OS_EVENT_TBL_SIZE > 2
*ptbl++ = 0x00;
#endif
#if OS_EVENT_TBL_SIZE > 3
*ptbl++ = 0x00;
#endif
#if OS_EVENT_TBL_SIZE > 4
*ptbl++ = 0x00;
#endif
#if OS_EVENT_TBL_SIZE > 5
*ptbl++ = 0x00;
#endif
#if OS_EVENT_TBL_SIZE > 6
*ptbl++ = 0x00;
#endif
#if OS_EVENT_TBL_SIZE > 7
*ptbl = 0x00;
#endif
}
#endif
797 /*$PAGE*/
798 /*
799 *********************************************************************************************************
800 * INITIALIZATION
801 * INITIALIZE THE FREE LIST OF EVENT CONTROL BLOCKS
802 *
803 * Description: This function is called by OSInit() to initialize the free list of event control blocks.
804 *
805 * Arguments : none
806 *
807 * Returns : none
808 *********************************************************************************************************
809 */
810
811 static void OS_InitEventList (void)
812 {
813 1 #if (OS_EVENT_EN > 0) &amt;&amt; (OS_MAX_EVENTS > 0)
#if (OS_MAX_EVENTS > 1)
INT16U i;
OS_EVENT *pevent1;
OS_EVENT *pevent2;
(void)memset(&amt;OSEventTbl[0], 0, sizeof(OSEventTbl)); /* Clear the event table
- */
pevent1 = &amt;OSEventTbl[0];
pevent2 = &amt;OSEventTbl[1];
for (i = 0; i < (OS_MAX_EVENTS - 1); i++) { /* Init. list of free EVENT control block
-s */
pevent1->OSEventType = OS_EVENT_TYPE_UNUSED;
pevent1->OSEventPtr = pevent2;
#if OS_EVENT_NAME_SIZE > 0
(void)strcpy(pevent1->OSEventName, "?"); /* Unknown name
- */
#endif
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 15
pevent1++;
pevent2++;
}
pevent1->OSEventType = OS_EVENT_TYPE_UNUSED;
pevent1->OSEventPtr = (OS_EVENT *)0;
#if OS_EVENT_NAME_SIZE > 0
(void)strcpy(pevent1->OSEventName, "?");
#endif
OSEventFreeList = &amt;OSEventTbl[0];
#else
OSEventFreeList = &amt;OSEventTbl[0]; /* Only have ONE event control block
- */
OSEventFreeList->OSEventType = OS_EVENT_TYPE_UNUSED;
OSEventFreeList->OSEventPtr = (OS_EVENT *)0;
#if OS_EVENT_NAME_SIZE > 0
(void)strcpy(OSEventFreeList->OSEventName, "?");
#endif
#endif
#endif
847 1 }
848 /*$PAGE*/
849 /*
850 *********************************************************************************************************
851 * INITIALIZATION
852 * INITIALIZE MISCELLANEOUS VARIABLES
853 *
854 * Description: This function is called by OSInit() to initialize miscellaneous variables.
855 *
856 * Arguments : none
857 *
858 * Returns : none
859 *********************************************************************************************************
860 */
861
862 static void OS_InitMisc (void)
863 {
864 1 #if OS_TIME_GET_SET_EN > 0
865 1 OSTime = 0L; /* Clear the 32-bit system clock
- */
866 1 #endif
867 1
868 1 OSIntNesting = 0; /* Clear the interrupt nesting counter
- */
869 1 OSLockNesting = 0; /* Clear the scheduling lock counter
- */
870 1
871 1 OSTaskCtr = 0; /* Clear the number of tasks
- */
872 1
873 1 OSRunning = FALSE; /* Indicate that multitasking not started
- */
874 1
875 1 OSCtxSwCtr = 0; /* Clear the context switch counter
- */
876 1 OSIdleCtr = 0L; /* Clear the 32-bit idle counter
- */
877 1
878 1 #if OS_TASK_STAT_EN > 0
OSIdleCtrRun = 0L;
OSIdleCtrMax = 0L;
OSStatRdy = FALSE; /* Statistic task is not ready
- */
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 16
#endif
883 1 }
884 /*$PAGE*/
885 /*
886 *********************************************************************************************************
887 * INITIALIZATION
888 * INITIALIZE THE READY LIST
889 *
890 * Description: This function is called by OSInit() to initialize the Ready List.
891 *
892 * Arguments : none
893 *
894 * Returns : none
895 *********************************************************************************************************
896 */
897
898 static void OS_InitRdyList (void)
899 {
900 1 INT16U i;
901 1 INT8U *prdytbl;
902 1
903 1
904 1 OSRdyGrp = 0x00; /* Clear the ready list
- */
905 1 prdytbl = &amt;OSRdyTbl[0];
906 1 for (i = 0; i < OS_RDY_TBL_SIZE; i++) {
907 2 *prdytbl++ = 0x00;
908 2 }
909 1
910 1 OSPrioCur = 0;
911 1 OSPrioHighRdy = 0;
912 1
913 1 OSTCBHighRdy = (OS_TCB *)0;
914 1 OSTCBCur = (OS_TCB *)0;
915 1 }
916
917 /*$PAGE*/
918 /*
919 *********************************************************************************************************
920 * INITIALIZATION
921 * CREATING THE IDLE TASK
922 *
923 * Description: This function creates the Idle Task.
924 *
925 * Arguments : none
926 *
927 * Returns : none
928 *********************************************************************************************************
929 */
930
931 static void OS_InitTaskIdle (void)
932 {
933 1 #if OS_TASK_CREATE_EXT_EN > 0
934 1 #if OS_STK_GROWTH == 1
(void)OSTaskCreateExt(OS_TaskIdle,
(void *)0, /* No arguments passed to OS_TaskIdle
-() */
&amt;OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1], /* Set Top-Of-Stack
- */
OS_IDLE_PRIO, /* Lowest priority level
- */
OS_TASK_IDLE_ID,
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 17
&amt;OSTaskIdleStk[0], /* Set Bottom-Of-Stack
- */
OS_TASK_IDLE_STK_SIZE,
(void *)0, /* No TCB extension
- */
OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);/* Enable stack checking + clear stac
-k */
#else
945 1 (void)OSTaskCreateExt(OS_TaskIdle,
946 1 (void *)0, /* No arguments passed to OS_TaskIdle
-() */
947 1 &amt;OSTaskIdleStk[0], /* Set Top-Of-Stack
- */
948 1 OS_IDLE_PRIO, /* Lowest priority level
- */
949 1 OS_TASK_IDLE_ID,
950 1 &amt;OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1], /* Set Bottom-Of-Stack
- */
951 1 OS_TASK_IDLE_STK_SIZE,
952 1 (void *)0, /* No TCB extension
- */
953 1 OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);/* Enable stack checking + clear stac
-k */
954 1 #endif
955 1 #else
#if OS_STK_GROWTH == 1
(void)OSTaskCreate(OS_TaskIdle,
(void *)0,
&amt;OSTaskIdleStk[OS_TASK_IDLE_STK_SIZE - 1],
OS_IDLE_PRIO);
#else
(void)OSTaskCreate(OS_TaskIdle,
(void *)0,
&amt;OSTaskIdleStk[0],
OS_IDLE_PRIO);
#endif
#endif
968 1 }
969 /*$PAGE*/
970 /*
971 *********************************************************************************************************
972 * INITIALIZATION
973 * CREATING THE STATISTIC TASK
974 *
975 * Description: This function creates the Statistic Task.
976 *
977 * Arguments : none
978 *
979 * Returns : none
980 *********************************************************************************************************
981 */
982
983 #if OS_TASK_STAT_EN > 0
static void OS_InitTaskStat (void)
{
#if OS_TASK_CREATE_EXT_EN > 0
#if OS_STK_GROWTH == 1
(void)OSTaskCreateExt(OS_TaskStat,
(void *)0, /* No args passed to OS_TaskStat()*
-/
&amt;OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1], /* Set Top-Of-Stack *
-/
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 18
OS_STAT_PRIO, /* One higher than the idle task *
-/
OS_TASK_STAT_ID,
&amt;OSTaskStatStk[0], /* Set Bottom-Of-Stack *
-/
OS_TASK_STAT_STK_SIZE,
(void *)0, /* No TCB extension *
-/
OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR); /* Enable stack checking + clear *
-/
#else
(void)OSTaskCreateExt(OS_TaskStat,
(void *)0, /* No args passed to OS_TaskStat()*
-/
&amt;OSTaskStatStk[0], /* Set Top-Of-Stack *
-/
OS_STAT_PRIO, /* One higher than the idle task *
-/
OS_TASK_STAT_ID,
&amt;OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1], /* Set Bottom-Of-Stack *
-/
OS_TASK_STAT_STK_SIZE,
(void *)0, /* No TCB extension *
-/
OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR); /* Enable stack checking + clear *
-/
#endif
#else
#if OS_STK_GROWTH == 1
(void)OSTaskCreate(OS_TaskStat,
(void *)0, /* No args passed to OS_TaskStat()*
-/
&amt;OSTaskStatStk[OS_TASK_STAT_STK_SIZE - 1], /* Set Top-Of-Stack *
-/
OS_STAT_PRIO); /* One higher than the idle task *
-/
#else
(void)OSTaskCreate(OS_TaskStat,
(void *)0, /* No args passed to OS_TaskStat()*
-/
&amt;OSTaskStatStk[0], /* Set Top-Of-Stack *
-/
OS_STAT_PRIO); /* One higher than the idle task *
-/
#endif
#endif
}
#endif
1023 /*$PAGE*/
1024 /*
1025 *********************************************************************************************************
1026 * INITIALIZATION
1027 * INITIALIZE THE FREE LIST OF TASK CONTROL BLOCKS
1028 *
1029 * Description: This function is called by OSInit() to initialize the free list of OS_TCBs.
1030 *
1031 * Arguments : none
1032 *
1033 * Returns : none
1034 *********************************************************************************************************
1035 */
1036
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 19
1037 static void OS_InitTCBList (void)
1038 {
1039 1 INT8U i;
1040 1 OS_TCB *ptcb1;
1041 1 OS_TCB *ptcb2;
1042 1
1043 1
1044 1 (void)memset(&amt;OSTCBTbl[0], 0, sizeof(OSTCBTbl)); /* Clear all the TCBs */
1045 1 (void)memset(&amt;OSTCBPrioTbl[0], 0, sizeof(OSTCBPrioTbl)); /* Clear the priority table */
1046 1 ptcb1 = &amt;OSTCBTbl[0];
1047 1 ptcb2 = &amt;OSTCBTbl[1];
1048 1 for (i = 0; i < (OS_MAX_TASKS + OS_N_SYS_TASKS - 1); i++) { /* Init. list of free TCBs */
1049 2 ptcb1->OSTCBNext = ptcb2;
1050 2 #if OS_TASK_NAME_SIZE > 0
(void)strcpy(ptcb1->OSTCBTaskName, "?"); /* Unknown name */
#endif
1053 2 ptcb1++;
1054 2 ptcb2++;
1055 2 }
1056 1 ptcb1->OSTCBNext = (OS_TCB *)0; /* Last OS_TCB */
1057 1 #if OS_TASK_NAME_SIZE > 0
(void)strcpy(ptcb1->OSTCBTaskName, "?");
#endif
1060 1 OSTCBList = (OS_TCB *)0; /* TCB lists initializations */
1061 1 OSTCBFreeList = &amt;OSTCBTbl[0];
1062 1 }
1063 /*$PAGE*/
1064 /*
1065 *********************************************************************************************************
1066 * SCHEDULER
1067 *
1068 * Description: This function is called by other uC/OS-II services to determine whether a new, high
1069 * priority task has been made ready to run. This function is invoked by TASK level code
1070 * and is not used to reschedule tasks from ISRs (see OSIntExit() for ISR rescheduling).
1071 *
1072 * Arguments : none
1073 *
1074 * Returns : none
1075 *
1076 * Notes : 1) This function is INTERNAL to uC/OS-II and your application should not call it.
1077 * 2) Rescheduling is prevented when the scheduler is locked (see OS_SchedLock())
1078 *********************************************************************************************************
1079 */
1080
1081 void OS_Sched (void) KCREENTRANT
1082 {
1083 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
1086 1 INT8U y;
1087 1
1088 1
1089 1 OS_ENTER_CRITICAL();
1090 1 if (OSIntNesting == 0) { /* Schedule only if all ISRs done and ... */
1091 2 if (OSLockNesting == 0) { /* ... scheduler is not locked */
1092 3 y = OSUnMapTbl[OSRdyGrp]; /* Get pointer to HPT ready to run */
1093 3 OSPrioHighRdy = (INT8U)((y << 3) + OSUnMapTbl[OSRdyTbl[y]]);
1094 3 if (OSPrioHighRdy != OSPrioCur) { /* No Ctx Sw if current task is highest rdy */
1095 4 OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
1096 4 #if OS_TASK_PROFILE_EN > 0
1097 4 OSTCBHighRdy->OSTCBCtxSwCtr++; /* Inc. # of context switches to this task */
1098 4 #endif
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 20
1099 4 OSCtxSwCtr++; /* Increment context switch counter */
1100 4 OS_TASK_SW(); /* Perform a context switch */
1101 4 }
1102 3 }
1103 2 }
1104 1 OS_EXIT_CRITICAL();
1105 1 }
1106 /*$PAGE*/
1107 /*
1108 *********************************************************************************************************
1109 * IDLE TASK
1110 *
1111 * Description: This task is internal to uC/OS-II and executes whenever no other higher priority tasks
1112 * executes because they are ALL waiting for event(s) to occur.
1113 *
1114 * Arguments : none
1115 *
1116 * Returns : none
1117 *
1118 * Note(s) : 1) OSTaskIdleHook() is called after the critical section to ensure that interrupts will be
1119 * enabled for at least a few instructions. On some processors (ex. Philips XA), enabling
1120 * and then disabling interrupts didn't allow the processor enough time to have interrupts
1121 * enabled before they were disabled again. uC/OS-II would thus never recognize
1122 * interrupts.
1123 * 2) This hook has been added to allow you to do such things as STOP the CPU to conserve
1124 * power.
1125 *********************************************************************************************************
1126 */
1127
1128 void OS_TaskIdle (void *pdata)KCREENTRANT
1129 {
1130 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
1133 1
1134 1
1135 1 pdata = pdata; /* Prevent compiler warning for not using 'pdata' */
1136 1 for (;;) {
1137 2 OS_ENTER_CRITICAL();
1138 2 OSIdleCtr++;
1139 2 OS_EXIT_CRITICAL();
1140 2 OSTaskIdleHook(); /* Call user definable HOOK */
1141 2 }
1142 1 }
1143 /*$PAGE*/
1144 /*
1145 *********************************************************************************************************
1146 * STATISTICS TASK
1147 *
1148 * Description: This task is internal to uC/OS-II and is used to compute some statistics about the
1149 * multitasking environment. Specifically, OS_TaskStat() computes the CPU usage.
1150 * CPU usage is determined by:
1151 *
1152 * OSIdleCtr
1153 * OSCPUUsage = 100 * (1 - ------------) (units are in >)
1154 * OSIdleCtrMax
1155 *
1156 * Arguments : pdata this pointer is not used at this time.
1157 *
1158 * Returns : none
1159 *
1160 * Notes : 1) This task runs at a priority level higher than the idle task. In fact, it runs at the
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 21
1161 * next higher priority, OS_IDLE_PRIO-1.
1162 * 2) You can disable this task by setting the configuration #define OS_TASK_STAT_EN to 0.
1163 * 3) We delay for 5 seconds in the beginning to allow the system to reach steady state and
1164 * have all other tasks created before we do statistics. You MUST have at least a delay
1165 * of 2 seconds to allow for the system to establish the maximum value for the idle
1166 * counter.
1167 *********************************************************************************************************
1168 */
1169
1170 #if OS_TASK_STAT_EN > 0
void OS_TaskStat (void *pdata)KCREENTRANT
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
INT32U run;
INT32U max;
INT8S usage;
pdata = pdata; /* Prevent compiler warning for not using 'pdata' */
while (OSStatRdy == FALSE) {
OSTimeDly(2 * OS_TICKS_PER_SEC); /* Wait until statistic task is ready */
}
max = OSIdleCtrMax / 100L;
for (;;) {
OS_ENTER_CRITICAL();
OSIdleCtrRun = OSIdleCtr; /* Obtain the of the idle counter for the past second */
run = OSIdleCtr;
OSIdleCtr = 0L; /* Reset the idle counter for the next second */
OS_EXIT_CRITICAL();
if (max > 0L) {
usage = (INT8S)(100L - run / max);
if (usage >= 0) { /* Make sure we don't have a negative percentage */
OSCPUUsage = usage;
} else {
OSCPUUsage = 0;
}
} else {
OSCPUUsage = 0;
max = OSIdleCtrMax / 100L;
}
OSTaskStatHook(); /* Invoke user definable hook */
#if (OS_TASK_STAT_STK_CHK_EN > 0) &amt;&amt; (OS_TASK_CREATE_EXT_EN > 0)
OS_TaskStatStkChk(); /* Check the stacks for each task */
#endif
OSTimeDly(OS_TICKS_PER_SEC); /* Accumulate OSIdleCtr for the next second */
}
}
#endif
1211 /*$PAGE*/
1212 /*
1213 *********************************************************************************************************
1214 * CHECK ALL TASK STACKS
1215 *
1216 * Description: This function is called by OS_TaskStat() to check the stacks of each active task.
1217 *
1218 * Arguments : none
1219 *
1220 * Returns : none
1221 *********************************************************************************************************
1222 */
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 22
1223
1224 #if (OS_TASK_STAT_STK_CHK_EN > 0) &amt;&amt; (OS_TASK_CREATE_EXT_EN > 0)
1225 void OS_TaskStatStkChk (void) KCREENTRANT
1226 {
1227 1 OS_TCB *ptcb;
1228 1 OS_STK_DATA stk_data;
1229 1 INT8U err;
1230 1 INT8U prio;
1231 1
1232 1
1233 1 for (prio = 0; prio <= OS_IDLE_PRIO; prio++) {
1234 2 err = OSTaskStkChk(prio, &amt;stk_data);
1235 2 if (err == OS_NO_ERR) {
1236 3 ptcb = OSTCBPrioTbl[prio];
1237 3 if (ptcb != (OS_TCB *)0) { /* Make sure task 'ptcb' is ... */
1238 4 if (ptcb != (OS_TCB *)1) { /* ... still valid. */
1239 5 #if OS_TASK_PROFILE_EN > 0
1240 5 #if OS_STK_GROWTH == 1
ptcb->OSTCBStkBase = ptcb->OSTCBStkBottom + ptcb->OSTCBStkSize;
#else
1243 5 ptcb->OSTCBStkBase = ptcb->OSTCBStkBottom - ptcb->OSTCBStkSize;
1244 5 #endif
1245 5 ptcb->OSTCBStkUsed = (INT32U)stk_data.OSUsed; /* Store the number of bytes used */
1246 5 #endif
1247 5 }
1248 4 }
1249 3 }
1250 2 }
1251 1 }
1252 #endif
1253 /*$PAGE*/
1254 /*
1255 *********************************************************************************************************
1256 * INITIALIZE TCB
1257 *
1258 * Description: This function is internal to uC/OS-II and is used to initialize a Task Control Block when
1259 * a task is created (see OSTaskCreate() and OSTaskCreateExt()).
1260 *
1261 * Arguments : prio is the priority of the task being created
1262 *
1263 * ptos is a pointer to the task's top-of-stack assuming that the CPU registers
1264 * have been placed on the stack. Note that the top-of-stack corresponds to a
1265 * 'high' memory location is OS_STK_GROWTH is set to 1 and a 'low' memory
1266 * location if OS_STK_GROWTH is set to 0. Note that stack growth is CPU
1267 * specific.
1268 *
1269 * pbos is a pointer to the bottom of stack. A NULL pointer is passed if called by
1270 * 'OSTaskCreate()'.
1271 *
1272 * id is the task's ID (0..65535)
1273 *
1274 * stk_size is the size of the stack (in 'stack units'). If the stack units are INT8Us
1275 * then, 'stk_size' contains the number of bytes for the stack. If the stack
1276 * units are INT32Us then, the stack contains '4 * stk_size' bytes. The stack
1277 * units are established by the #define constant OS_STK which is CPU
1278 * specific. 'stk_size' is 0 if called by 'OSTaskCreate()'.
1279 *
1280 * pext is a pointer to a user supplied memory area that is used to extend the task
1281 * control block. This allows you to store the contents of floating-point
1282 * registers, MMU registers or anything else you could find useful during a
1283 * context switch. You can even assign a name to each task and store this name
1284 * in this TCB extension. A NULL pointer is passed if called by OSTaskCreate().
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 23
1285 *
1286 * opt options as passed to 'OSTaskCreateExt()' or,
1287 * 0 if called from 'OSTaskCreate()'.
1288 *
1289 * Returns : OS_NO_ERR if the call was successful
1290 * OS_NO_MORE_TCB if there are no more free TCBs to be allocated and thus, the task cannot
1291 * be created.
1292 *
1293 * Note : This function is INTERNAL to uC/OS-II and your application should not call it.
1294 *********************************************************************************************************
1295 */
1296
1297 INT8U OS_TCBInit (INT8U prio, OS_STK *ptos, OS_STK *pbos, INT16U id, INT32U stk_size, void *pext, INT16U
-opt)
1298 {
1299 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
1302 1 OS_TCB *ptcb;
1303 1
1304 1
1305 1 OS_ENTER_CRITICAL();
1306 1 ptcb = OSTCBFreeList; /* Get a free TCB from the free TCB list */
1307 1 if (ptcb != (OS_TCB *)0) {
1308 2 OSTCBFreeList = ptcb->OSTCBNext; /* Update pointer to free TCB list */
1309 2 OS_EXIT_CRITICAL();
1310 2 ptcb->OSTCBStkPtr = ptos; /* Load Stack pointer in TCB */
1311 2 ptcb->OSTCBPrio = prio; /* Load task priority into TCB */
1312 2 ptcb->OSTCBStat = OS_STAT_RDY; /* Task is ready to run */
1313 2 ptcb->OSTCBDly = 0; /* Task is not delayed */
1314 2
1315 2 #if OS_TASK_CREATE_EXT_EN > 0
1316 2 ptcb->OSTCBExtPtr = pext; /* Store pointer to TCB extension */
1317 2 ptcb->OSTCBStkSize = stk_size; /* Store stack size */
1318 2 ptcb->OSTCBStkBottom = pbos; /* Store pointer to bottom of stack */
1319 2 ptcb->OSTCBOpt = opt; /* Store task options */
1320 2 ptcb->OSTCBId = id; /* Store task ID */
1321 2 #else
pext = pext; /* Prevent compiler warning if not used */
stk_size = stk_size;
pbos = pbos;
opt = opt;
id = id;
#endif
1328 2
1329 2 #if OS_TASK_DEL_EN > 0
1330 2 ptcb->OSTCBDelReq = OS_NO_ERR;
1331 2 #endif
1332 2
1333 2 ptcb->OSTCBY = (INT8U)(prio >> 3); /* Pre-compute X, Y, BitX and BitY */
1334 2 ptcb->OSTCBBitY = OSMapTbl[ptcb->OSTCBY];
1335 2 ptcb->OSTCBX = (INT8U)(prio &amt; 0x07);
1336 2 ptcb->OSTCBBitX = OSMapTbl[ptcb->OSTCBX];
1337 2
1338 2 #if OS_EVENT_EN > 0
ptcb->OSTCBEventPtr = (OS_EVENT *)0; /* Task is not pending on an event */
#endif
1341 2
1342 2 #if (OS_VERSION >= 251) &amt;&amt; (OS_FLAG_EN > 0) &amt;&amt; (OS_MAX_FLAGS > 0) &amt;&amt; (OS_TASK_DEL_EN > 0)
ptcb->OSTCBFlagNode = (OS_FLAG_NODE *)0; /* Task is not pending on an event flag */
#endif
1345 2
C51 COMPILER V8.05a OS_CORE 04/11/2007 16:19:49 PAGE 24
1346 2 #if (OS_MBOX_EN > 0) || ((OS_Q_EN > 0) &amt;&amt; (OS_MAX_QS > 0))
ptcb->OSTCBMsg = (void *)0; /* No message received */
#endif
1349 2
1350 2 #if OS_TASK_PROFILE_EN > 0
1351 2 ptcb->OSTCBCtxSwCtr = 0L; /* Initialize profiling variables */
1352 2 ptcb->OSTCBCyclesStart = 0L;
1353 2 ptcb->OSTCBCyclesTot = 0L;
1354 2 ptcb->OSTCBStkBase = (OS_STK *)0;
1355 2 ptcb->OSTCBStkUsed = 0L;
1356 2 #endif
1357 2
1358 2 #if OS_TASK_NAME_SIZE > 0
(void)strcpy(ptcb->OSTCBTaskName, "?"); /* Unknown name at task creation */
#endif
1361 2
1362 2 #if OS_VERSION >= 204
1363 2 OSTCBInitHook(ptcb);
1364 2 #endif
1365 2
1366 2 OSTaskCreateHook(ptcb); /* Call user defined hook */
1367 2
1368 2 OS_ENTER_CRITICAL();
1369 2 OSTCBPrioTbl[prio] = ptcb;
1370 2 ptcb->OSTCBNext = OSTCBList; /* Link into TCB chain */
1371 2 ptcb->OSTCBPrev = (OS_TCB *)0;
1372 2 if (OSTCBList != (OS_TCB *)0) {
1373 3 OSTCBList->OSTCBPrev = ptcb;
1374 3 }
1375 2 OSTCBList = ptcb;
1376 2 OSRdyGrp |= ptcb->OSTCBBitY; /* Make task ready to run */
1377 2 OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
1378 2 OS_EXIT_CRITICAL();
1379 2 return (OS_NO_ERR);
1380 2 }
1381 1 OS_EXIT_CRITICAL();
1382 1 return (OS_NO_MORE_TCB);
1383 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 2364 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = 1080 28
PDATA SIZE = ---- ----
DATA SIZE = ---- ----
IDATA SIZE = 9 ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)