📄 os_core.lst
字号:
832 1 INT8U i;
833 1 OS_TCB *ptcb1;
834 1 OS_TCB *ptcb2;
835 1
836 1
837 1 OSTCBList = (OS_TCB *)0; /* TCB Initialization
- */
838 1 for (i = 0; i < (OS_LOWEST_PRIO + 1); i++) { /* Clear the priority table
- */
839 2 OSTCBPrioTbl[i] = (OS_TCB *)0;
840 2 }
841 1 ptcb1 = &OSTCBTbl[0];
842 1 ptcb2 = &OSTCBTbl[1];
843 1 for (i = 0; i < (OS_MAX_TASKS + OS_N_SYS_TASKS - 1); i++) { /* Init. list of free TCBs
- */
844 2 ptcb1->OSTCBNext = ptcb2;
845 2 ptcb1++;
846 2 ptcb2++;
847 2 }
848 1 ptcb1->OSTCBNext = (OS_TCB *)0; /* Last OS_TCB
- */
849 1 OSTCBFreeList = &OSTCBTbl[0];
850 1 }
851 /*$PAGE*/
852 /*
853 *********************************************************************************************************
854 * SCHEDULER
855 *
856 * Description: This function is called by other uC/OS-II services to determine whether a new, high
857 * priority task has been made ready to run. This function is invoked by TASK level code
858 * and is not used to reschedule tasks from ISRs (see OSIntExit() for ISR rescheduling).
859 *
860 * Arguments : none
861 *
862 * Returns : none
863 *
864 * Notes : 1) This function is INTERNAL to uC/OS-II and your application should not call it.
865 * 2) Rescheduling is prevented when the scheduler is locked (see OS_SchedLock())
866 *********************************************************************************************************
867 */
C51 COMPILER V6.23a OS_CORE 12/09/2004 16:50:24 PAGE 16
868
869 void OS_Sched (void) KCREENTRANT
870 {
871 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
874 1 INT8U y;
875 1
876 1
877 1 OS_ENTER_CRITICAL();
878 1 if ((OSIntNesting == 0) && (OSLockNesting == 0)) { /* Sched. only if all ISRs done & not locked */
879 2 y = OSUnMapTbl[OSRdyGrp]; /* Get pointer to HPT ready to run */
880 2 OSPrioHighRdy = (INT8U)((y << 3) + OSUnMapTbl[OSRdyTbl[y]]);
881 2 if (OSPrioHighRdy != OSPrioCur) { /* No Ctx Sw if current task is highest rdy */
882 3 OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
883 3 OSCtxSwCtr++; /* Increment context switch counter */
884 3 OS_TASK_SW(); /* Perform a context switch */
885 3 }
886 2 }
887 1 OS_EXIT_CRITICAL();
888 1 }
889 /*$PAGE*/
890 /*
891 *********************************************************************************************************
892 * IDLE TASK
893 *
894 * Description: This task is internal to uC/OS-II and executes whenever no other higher priority tasks
895 * executes because they are ALL waiting for event(s) to occur.
896 *
897 * Arguments : none
898 *
899 * Returns : none
900 *
901 * Note(s) : 1) OSTaskIdleHook() is called after the critical section to ensure that interrupts will be
902 * enabled for at least a few instructions. On some processors (ex. Philips XA), enabling
903 * and then disabling interrupts didn't allow the processor enough time to have interrupts
904 * enabled before they were disabled again. uC/OS-II would thus never recognize
905 * interrupts.
906 * 2) This hook has been added to allow you to do such things as STOP the CPU to conserve
907 * power.
908 *********************************************************************************************************
909 */
910
911 void OS_TaskIdle (void *pdata) KCREENTRANT
912 {
913 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
916 1
917 1
918 1 pdata = pdata; /* Prevent compiler warning for not using 'pdata' */
919 1 for (;;) {
920 2 OS_ENTER_CRITICAL();
921 2 OSIdleCtr++;
922 2 OS_EXIT_CRITICAL();
923 2 OSTaskIdleHook(); /* Call user definable HOOK */
924 2 }
925 1 }
926 /*$PAGE*/
927 /*
928 *********************************************************************************************************
929 * STATISTICS TASK
C51 COMPILER V6.23a OS_CORE 12/09/2004 16:50:24 PAGE 17
930 *
931 * Description: This task is internal to uC/OS-II and is used to compute some statistics about the
932 * multitasking environment. Specifically, OS_TaskStat() computes the CPU usage.
933 * CPU usage is determined by:
934 *
935 * OSIdleCtr
936 * OSCPUUsage = 100 * (1 - ------------) (units are in %)
937 * OSIdleCtrMax
938 *
939 * Arguments : pdata this pointer is not used at this time.
940 *
941 * Returns : none
942 *
943 * Notes : 1) This task runs at a priority level higher than the idle task. In fact, it runs at the
944 * next higher priority, OS_IDLE_PRIO-1.
945 * 2) You can disable this task by setting the configuration #define OS_TASK_STAT_EN to 0.
946 * 3) We delay for 5 seconds in the beginning to allow the system to reach steady state and
947 * have all other tasks created before we do statistics. You MUST have at least a delay
948 * of 2 seconds to allow for the system to establish the maximum value for the idle
949 * counter.
950 *********************************************************************************************************
951 */
952
953 #if OS_TASK_STAT_EN > 0
954 void OS_TaskStat (void *pdata) KCREENTRANT
955 {
956 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
959 1 INT32U run;
960 1 INT32U max;
961 1 INT8S usage;
962 1
963 1
964 1 pdata = pdata; /* Prevent compiler warning for not using 'pdata' */
965 1 while (OSStatRdy == FALSE) {
966 2 OSTimeDly(2 * OS_TICKS_PER_SEC); /* Wait until statistic task is ready */
967 2 }
968 1 max = OSIdleCtrMax / 100L;
969 1 for (;;) {
970 2 OS_ENTER_CRITICAL();
971 2 OSIdleCtrRun = OSIdleCtr; /* Obtain the of the idle counter for the past second */
972 2 run = OSIdleCtr;
973 2 OSIdleCtr = 0L; /* Reset the idle counter for the next second */
974 2 OS_EXIT_CRITICAL();
975 2 if (max > 0L) {
976 3 usage = (INT8S)(100L - run / max);
977 3 if (usage >= 0) { /* Make sure we don't have a negative percentage */
978 4 OSCPUUsage = usage;
979 4 } else {
980 4 OSCPUUsage = 0;
981 4 }
982 3 } else {
983 3 OSCPUUsage = 0;
984 3 max = OSIdleCtrMax / 100L;
985 3 }
986 2 OSTaskStatHook(); /* Invoke user definable hook */
987 2 OSTimeDly(OS_TICKS_PER_SEC); /* Accumulate OSIdleCtr for the next second */
988 2 }
989 1 }
990 #endif
991 /*$PAGE*/
C51 COMPILER V6.23a OS_CORE 12/09/2004 16:50:24 PAGE 18
992 /*
993 *********************************************************************************************************
994 * INITIALIZE TCB
995 *
996 * Description: This function is internal to uC/OS-II and is used to initialize a Task Control Block when
997 * a task is created (see OSTaskCreate() and OSTaskCreateExt()).
998 *
999 * Arguments : prio is the priority of the task being created
1000 *
1001 * ptos is a pointer to the task's top-of-stack assuming that the CPU registers
1002 * have been placed on the stack. Note that the top-of-stack corresponds to a
1003 * 'high' memory location is OS_STK_GROWTH is set to 1 and a 'low' memory
1004 * location if OS_STK_GROWTH is set to 0. Note that stack growth is CPU
1005 * specific.
1006 *
1007 * pbos is a pointer to the bottom of stack. A NULL pointer is passed if called by
1008 * 'OSTaskCreate()'.
1009 *
1010 * id is the task's ID (0..65535)
1011 *
1012 * stk_size is the size of the stack (in 'stack units'). If the stack units are INT8Us
1013 * then, 'stk_size' contains the number of bytes for the stack. If the stack
1014 * units are INT32Us then, the stack contains '4 * stk_size' bytes. The stack
1015 * units are established by the #define constant OS_STK which is CPU
1016 * specific. 'stk_size' is 0 if called by 'OSTaskCreate()'.
1017 *
1018 * pext is a pointer to a user supplied memory area that is used to extend the task
1019 * control block. This allows you to store the contents of floating-point
1020 * registers, MMU registers or anything else you could find useful during a
1021 * context switch. You can even assign a name to each task and store this name
1022 * in this TCB extension. A NULL pointer is passed if called by OSTaskCreate().
1023 *
1024 * opt options as passed to 'OSTaskCreateExt()' or,
1025 * 0 if called from 'OSTaskCreate()'.
1026 *
1027 * Returns : OS_NO_ERR if the call was successful
1028 * OS_NO_MORE_TCB if there are no more free TCBs to be allocated and thus, the task cannot
1029 * be created.
1030 *
1031 * Note : This function is INTERNAL to uC/OS-II and your application should not call it.
1032 *********************************************************************************************************
1033 */
1034
1035 INT8U OS_TCBInit (INT8U prio, OS_STK *ptos, OS_STK *pbos, INT16U id, INT32U stk_size, void *pext, INT16U
-opt) KCREENTRANT
1036 {
1037 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
1040 1 OS_TCB *ptcb;
1041 1
1042 1
1043 1 OS_ENTER_CRITICAL();
1044 1 ptcb = OSTCBFreeList; /* Get a free TCB from the free TCB list */
1045 1 if (ptcb != (OS_TCB *)0) {
1046 2 OSTCBFreeList = ptcb->OSTCBNext; /* Update pointer to free TCB list */
1047 2 OS_EXIT_CRITICAL();
1048 2 ptcb->OSTCBStkPtr = ptos; /* Load Stack pointer in TCB */
1049 2 ptcb->OSTCBPrio = (INT8U)prio; /* Load task priority into TCB */
1050 2 ptcb->OSTCBStat = OS_STAT_RDY; /* Task is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -