📄 os_cpu_c.c
字号:
/*
*********************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
* Linux Port
*
* File: os_cpu_c.c
* By: Philip Mitchell* Email: mitchell_philip@hotmail.com
* Version: v1.00
*
* Description
* ------------
* This linux port for the ucos uses pthreads (NPTL) to produce context switching between tasks. Each
* of the tasks is started as a thread with only one of the threads running at any time. The asynchronous context
* switching is achieved with the use of alarm signals and pthread condition variables. Each task is wrapped in
* the thread wrapper function which intially stops the thread from runnning by waiting for the threads condition
* variable. Only when all the thread wrappers have been started and are waiting for their condition variables is the
* OSStartHighRdy allowed to set the condition variable for the highest priority task. After this occurs the system is running
* and only one thread is active with all others waiting on their condition variables.
* The context switches occur asynchronously through an alarm signal. The signal handler for the alarm calls OSIntExit() which
* in turn calls OSIntCtxSw(). This function first sets the condition variable for the new high priority task and then waits on
* the condition variable for the current task. This setting of the highest priority condition variable and then waiting for
* current threads condition variable is how the context switching is simulated. The condition variables are controlled with
* mutexes to ensure that only a single task is ever running at one time. A context switch that occurs from within a thread
* is invoked by the OS_TASK_SW() call, this function simply calls OSIntCtxSw() described above and results in the same set of
* operations.
*
* One extra capability of this port is to provide a system reset function which calls an externally defined function
* void OSSystemResetHook(void). This user defined function could call execv which would cause the process to restart.
*
* Remember to include link to pthread when linking (-lpthread).
*
* Notes on debugging using gdb
* -----------------------------
* There seems to be a problem with gdb (kernel version 2.6.22, gdb version 6.7, and pthreads version NPTL 2.6.1) when using execv with the system reset
* function described above. When intially debugging the system, the "info threads" command works as intended but cannot show thread information if used after
* the system reset function (described above). It seems that gdb cannot detect new threads after a call to execv from a thread. To get around this, you
* can start the ucos application and gdb separately, then use the gdb 'attach' command to debug the application.
*
*********************************************************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <ucos_ii.h>
/* Check OS_VERSION for compatibility with this port */
#if OS_VERSION < 204
#error OS_VERSION must be >= 204. This port uses OSTCBInitHook.
#endif
/*#if OS_CPU_HOOKS_EN > 0
*********************************************************************************************************
* GLOBAL VARIABLES
*********************************************************************************************************
*/
int nNumThreadsCreated=0;
int nNumThreadsStarted=0;
/* Condition variable to indicate whether all threads are waiting for their condition variables. i.e.
the system is ready to run. */
pthread_cond_t cvThreadWrapper;
/* Condition variables for all threads. Index for thread is stored in its stack. */
pthread_cond_t grcvThread[ OS_LOWEST_PRIO ];
/* Array of threads */
pthread_t threadTask[ OS_LOWEST_PRIO ];
/* Context switching control mutex */
pthread_mutex_t mutThread;
/*
*********************************************************************************************************
* Linux Port Function Prototypes
*********************************************************************************************************
*/
void (*pF)(char*);
typedef struct FuncInfoType
{
void* pFunc;
void* pArgs;
int nThreadIdx;} FuncInfo;int OSMinStkSize(){ return( sizeof( FuncInfo ) ); }
#if OS_SYSTEM_RESET_EN > 0
extern void OSSystemResetHook(void);
#endif
static void InitLinuxPort();static void InitTick();static void AlarmSigHandler( int signum );
/*
*********************************************************************************************************
* UCOS Function Prototypes
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* TASK CREATION HOOK
*
* Description: This function is called when a task is created.
*
* Arguments : ptcb is a pointer to the task control block of the task being created.
*
* Note(s) : 1) Interrupts are disabled during this call.
*********************************************************************************************************
*/
void OSTaskCreateHook(OS_TCB *ptcb)
{#if OS_CPU_HOOKS_EN > 0#endif}
/*
*********************************************************************************************************
* TASK DELETION HOOK
*
* Description: This function is called when a task is deleted.
*
* Arguments : ptcb is a pointer to the task control block of the task being deleted.
*
* Note(s) : 1) Interrupts are disabled during this call.
*********************************************************************************************************
*/
void OSTaskDelHook (OS_TCB *ptcb)
{#if OS_CPU_HOOKS_EN > 0#endif
}
/*
*********************************************************************************************************
* TASK SWITCH HOOK
*
* Description: This function is called when a task switch is performed. This allows you to perform other
* operations during a context switch.
*
* Arguments : none
*
* Note(s) : 1) Interrupts are disabled during this call.
* 2) It is assumed that the global pointer 'OSTCBHighRdy' points to the TCB of the task that
* will be 'switched in' (i.e. the highest priority task) and, 'OSTCBCur' points to the
* task being switched out (i.e. the preempted task).
*********************************************************************************************************
*/
void OSTaskSwHook (void)
{#if (OS_CPU_HOOKS_EN > 0)#endif
}
/*
*********************************************************************************************************
* STATISTIC TASK HOOK
*
* Description: This function is called every second by uC/OS-II's statistics task. This allows your
* application to add functionality to the statistics task.
*
* Arguments : none
*********************************************************************************************************
*/
void OSTaskStatHook (void)
{#if (OS_TASK_STAT_HOOK_EN > 0)#endif
}
/*
*********************************************************************************************************
* TICK HOOK
*
* Description: This function is called every tick.
*
* Arguments : none
*
* Note(s) : 1) Interrupts may or may not be ENABLED during this call.
*********************************************************************************************************
*/
void OSTimeTickHook (void)
{#if (OS_CPU_HOOKS_EN > 0) && (OS_TIME_TICK_HOOK_EN > 0)#endif
}
/*
*********************************************************************************************************
* Init HOOK begin & end
*
* Description: Called in OSInit.
*
* Arguments : pointer to TCB
*********************************************************************************************************
*/
void OSInitHookBegin (void)
{#if OS_CPU_HOOKS_EN > 0 && OS_VERSION >= 204
InitLinuxPort();#endif
}
void OSInitHookEnd (void)
{#if OS_CPU_HOOKS_EN > 0 && OS_VERSION >= 204#endif
}
/*
*********************************************************************************************************
* IDLE HOOK
*
* Description: This function is called by uC/OS-ii IDLE Task.
*
* Arguments : none
*
* Note(s) : 1) Interrupts may or may not be ENABLED during this call.
*********************************************************************************************************
*/
void OSTaskIdleHook (void)
{#if OS_CPU_HOOKS_EN > 0 && OS_VERSION >= 251
select(0, NULL, NULL, NULL, NULL);#endif
}
/*
*********************************************************************************************************
* TASK STACK INIT
*
* Description: This function stores the task information on the task stack for retrieval at a later time.
*
* Arguments : pd pointer to task function.* pdata pointer to task data.* ptos pointer to top of task stack. Pthreads uses its own stack for tasks but this* has to be > sizeof( FuncInfo ).* opt not used
*
*********************************************************************************************************
*/
OS_STK* OSTaskStkInit (void (*task)(void* pd), void* pdata, OS_STK* ptos, INT16U opt)
{
FuncInfo* pFuncInfo = (FuncInfo*)( ((INT8U*)ptos) - sizeof( FuncInfo ) );
pFuncInfo->pFunc = task;
pFuncInfo->pArgs = pdata;
return ((OS_STK*)pFuncInfo);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -