int main(void)
{
INT8U os_err;//OS error
Bsp_init();//Embedded development board Initialization//开发板初始化
OSInit();//uC/OS initialization//系统初始化
os_err = OSTaskCreateExt((void (*)(void *)) App_Task_LCD,//创建任务
(void * ) 0,
(OS_STK * )&App_TaskLCDStk[APP_TASK_LCD_STK_SIZE-1],
(INT8U ) APP_TASK_LCD_PRIO,
(INT16U ) APP_TASK_LCD_PRIO,
(OS_STK * )&App_TaskLCDStk[0],
(INT32U ) APP_TASK_LCD_STK_SIZE,
(void * ) 0,
(INT16U )(OS_TASK_OPT_STK_CLR | OS_TASK_OPT_STK_CHK));
OSStart(); //uC/OS start multitasking//开始任务运行
}
/*
*********************************************************************************************************
* INITIALIZATION
* 初始化
*
* Description: This function is used to initialize the internals of uC/OS-II and MUST be called prior to
* creating any uC/OS-II object and, prior to calling OSStart().
× 描述:该函数用于uC/OS-II系统的内部初始化,它必须在调用uC/OS-II系统的任何创建对象之前,也必须在
* 函数OSStart()之前执行。也就是说使用uC/OS-II系统的第一步就是执行这个函数,这是一个约定。
*
* Arguments : none
× 传参 : 无
*
* Returns : none
× 返回值 : 无
*********************************************************************************************************
*/
void OSInit (void)
{
OSInitHookBegin(); /* Call port specific initialization code */
OS_InitMisc(); /* Initialize miscellaneous variables */
OS_InitRdyList(); /* Initialize the Ready List */
OS_InitTCBList(); /* Initialize the free list of OS_TCBs */
OS_InitEventList(); /* Initialize the free list of OS_EVENTs */
#if (OS_FLAG_EN > 0) && (OS_MAX_FLAGS > 0)
OS_FlagInit(); /* Initialize the event flag structures */
#endif
#if (OS_MEM_EN > 0) && (OS_MAX_MEM_PART > 0)
OS_MemInit(); /* Initialize the memory manager */
#endif
#if (OS_Q_EN > 0) && (OS_MAX_QS > 0)
OS_QInit(); /* Initialize the message queue structures */
#endif
OS_InitTaskIdle(); /* Create the Idle Task */
#if OS_TASK_STAT_EN > 0
OS_InitTaskStat(); /* Create the Statistic Task */
#endif
#if OS_TMR_EN > 0
OSTmr_Init(); /* Initialize the Timer Manager */
#endif
OSInitHookEnd(); /* Call port specific init. code */
#if OS_DEBUG_EN > 0
OSDebugInit();
#endif
}
/*
*********************************************************************************************************
* CREATE A TASK (Extended Version)
* 创建任务(扩展版本)
*
* Description: This function is used to have uC/OS-II manage the execution of a task. Tasks can either
* be created prior to the start of multitasking or by a running task. A task cannot be
* created by an ISR. This function is similar to OSTaskCreate() except that it allows
* additional information about a task to be specified.
* 描述: 该函数用于创建一个 uC/OS-II管理的可执行任务.它不是在多个任务执行前被创建就是在一个运行
* 的任务中被创建.在中断服务函数中不能创建任务(也就是说在ISR中不能调用该函数). 除了该函数允许一
*个任务的附加信息被列出外,该函数类同于函数OSTaskCreate().
*
* Arguments : task is a pointer to the task's code
*
* p_arg is a pointer to an optional data area which can be used to pass parameters to
* the task when the task first executes. Where the task is concerned it thinks
* it was invoked and passed the argument 'p_arg' as follows:
*
* void Task (void *p_arg)
* {
* for (;;) {
* Task code;
* }
* }
*
* ptos is a pointer to the task's top of stack. If the configuration constant
* OS_STK_GROWTH is set to 1, the stack is assumed to grow downward (i.e. from high
* memory to low memory). 'ptos' will thus point to the highest (valid) memory
* location of the stack. If OS_STK_GROWTH is set to 0, 'ptos' will point to the
* lowest memory location of the stack and the stack will grow with increasing
* memory locations. 'ptos' MUST point to a valid 'free' data item.
*
* prio is the task's priority. A unique priority MUST be assigned to each task and the
* lower the number, the higher the priority.
*
* id is the task's ID (0..65535)
*
* pbos is a pointer to the task's bottom of stack. If the configuration constant
* OS_STK_GROWTH is set to 1, the stack is assumed to grow downward (i.e. from high
* memory to low memory). 'pbos' will thus point to the LOWEST (valid) memory
* location of the stack. If OS_STK_GROWTH is set to 0, 'pbos' will point to the
* HIGHEST memory location of the stack and the stack will grow with increasing
* memory locations. 'pbos' MUST point to a valid 'free' data item.
*
* stk_size is the size of the stack in number of elements. If OS_STK is set to INT8U,
* 'stk_size' corresponds to the number of bytes available. If OS_STK is set to
* INT16U, 'stk_size' contains the number of 16-bit entries available. Finally, if
* OS_STK is set to INT32U, 'stk_size' contains the number of 32-bit entries
* available on the stack.
*
* pext is a pointer to a user supplied memory location which is used as a TCB extension.
* For example, this user memory can hold the contents of floating-point registers
* during a context switch, the time each task takes to execute, the number of times
* the task has been switched-in, etc.
*
* opt contains additional information (or options) about the behavior of the task. The
* LOWER 8-bits are reserved by uC/OS-II while the upper 8 bits can be application
* specific. See OS_TASK_OPT_??? in uCOS-II.H. Current choices are:
*
* OS_TASK_OPT_STK_CHK Stack checking to be allowed for the task
* OS_TASK_OPT_STK_CLR Clear the stack when the task is created
* OS_TASK_OPT_SAVE_FP If the CPU has floating-point registers, save them
* during a context switch.
* 传参:task 任务代码的一个指针(指向任务代码段)
*
* p_arg 当task第一次运行时,它代表指向一个用于向task传递参数的可选数据区域的指针.当task
* 在连接数据的时候,它被要求和像下面的例子这样传递参数'p_arg' :
*
* void Task (void *p_arg)
* {
* for (;;) {
* Task code;
* }
* }
*
* ptos 任务栈顶指针.假如OS_STK_GROWTH设置为'1',则栈被认为是向低地址推移的(即从高存储到低
* 存储位置).栈顶指针将指向栈所在的存储器的最高位置.假如OS_STK_GROWTH设置为'0',则栈被
* 认为是向高地址推移的(即从低存储到高存储位置).栈顶指针随内存增加增加. 'ptos'一定指向
* 一个可用的'空闲'的数据项.
*
* prio 任务的优先级. 每个任务都必须有一个唯一的优先级.优先级数字越小,优先级别越高.
*
* id 任务的ID号(0..65535)
*
* pbos 任务的栈底指针. 假如OS_STK_GROWTH设置为'1',则栈被认为是向低地址推移的(即从高存储到低
* 存储位置).栈底指针将指向栈所在的存储器的最低位置.假如OS_STK_GROWTH设置为'0',则栈被
* 认为是向高地址推移的(即从低存储到高存储位置).栈底指针随内存增加增加. 'ptos'一定指向
* 一个可用的'空闲'的数据项.
*
* stk_size 栈的长度.如果OS_STK设置为INT8U,stk_size则为字节数允许.若OS_STK设置为INT16U,stk_size则为十
* 六位数允许.最后若OS_STK设置为INT32U,stk_size则为32位二进制允许.(指明栈的宽度)
*
* pext 是一个指针,指向用户提供的用于TCB扩展部分的内存空间. 例如:通过上下文切换用户存储器能
* 保持住浮点寄存器的内容、每次任务运行的时间、任务被开关的次数等等.
*
* opt 包含任务行为的附加信息(或选项).低八位被uC/OS-II系统作为保留字.高八位作为特殊的应用.这个
* 选项的设置查看uCOS-II.H.中的OS_TASK_OPT_???.当前选项是:
* OS_TASK_OPT_STK_CHK 需要进行栈的检查
* OS_TASK_OPT_STK_CLR 任务创建时清除栈
* OS_TASK_OPT_SAVE_FP 假如处理器有浮点数据,保存它们
*
* Returns : OS_ERR_NONE if the function was successful.
* OS_PRIO_EXIT if the task priority already exist
* (each task MUST have a unique priority).
* OS_ERR_PRIO_INVALID if the priority you specify is higher that the maximum allowed
* (i.e. > OS_LOWEST_PRIO)
* OS_ERR_TASK_CREATE_ISR if you tried to create a task from an ISR.
*
* 返回值 : OS_ERR_NONE 函数执行成功范围的内容
* OS_PRIO_EXIT 该任务的优先级已经存在返回该值
* (每个任务都有一个唯一的优先级).
* OS_ERR_PRIO_INVALID 设置的优先级大于最大的优先级别返回该值
* (即 > OS_LOWEST_PRIO)
* OS_ERR_TASK_CREATE_ISR 当在ISR中创建任务时返回该值(ISR中不允许进行任务创建)
*********************************************************************************************************
*/
/*$PAGE*/
#if OS_TASK_CREATE_EXT_EN > 0
INT8U OSTaskCreateExt (void (*task)(void *p_arg),
void *p_arg,
OS_STK *ptos,
INT8U prio,
INT16U id,
OS_STK *pbos,
INT32U stk_size,
void *pext,
INT16U opt)
{
OS_STK *psp;
INT8U err;
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
/* 分配CPU状态寄存器的存储 */
OS_CPU_SR cpu_sr = 0;
#endif
#if OS_ARG_CHK_EN > 0
if (prio > OS_LOWEST_PRIO) { /* Make sure priority is within allowable range */
/* 确保优先级在允许的范围内 */
return (OS_ERR_PRIO_INVALID);
}
#endif
OS_ENTER_CRITICAL();
if (OSIntNesting > 0) { /* Make sure we don't create the task from within an ISR */
/* 确保不在ISR中创建任务 */
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_CREATE_ISR);
}
if (OSTCBPrioTbl[prio] == (OS_TCB *)0) { /* Make sure task doesn't already exist at this priority */
/* 确保任务优先级设置没有重复 */
OSTCBPrioTbl[prio] = OS_TCB_RESERVED;/* Reserve the priority to prevent others from doing ... */
/* ... the same thing until task is created. */
/* 直到任务被创建完成,保留优先级确保其他任务不做同样的事情 */
OS_EXIT_CRITICAL();
#if (OS_TASK_STAT_STK_CHK_EN > 0)
OS_TaskStkClr(pbos, stk_size, opt); /* Clear the task stack (if needed) */
/* 清栈 (如需) */
#endif
psp = OSTaskStkInit(task, p_arg, ptos, opt); /* Initialize the task's stack */
/* 初始化任务栈 */
err = OS_TCBInit(prio, psp, pbos, id, stk_size, pext, opt);
if (err == OS_ERR_NONE) {
if (OSRunning == OS_TRUE) { /* Find HPT if multitasking has started */
/* 在多任务开始后发现HPT */
OS_Sched();
}
} else {
OS_ENTER_CRITICAL();
OSTCBPrioTbl[prio] = (OS_TCB *)0; /* Make this priority avail. to others */
/* 是这个优先级在其他任务中可用 */
OS_EXIT_CRITICAL();
}
return (err);
}
OS_EXIT_CRITICAL();
return (OS_ERR_PRIO_EXIST);
}
#endif
/*
*********************************************************************************************************
* START MULTITASKING
* 开始多任务运行
*
* Description: This function is used to start the multitasking process which lets uC/OS-II manages the
* task that you have created. Before you can call OSStart(), you MUST have called OSInit()
* and you MUST have created at least one task.
*
×描述:该函数用于开启你已经在uC/OS-II中创建的多任务进程.在调用该函数前,必须已经调用了OSInit()函
* 数和创建了至少一个进程.
×
* Arguments : none
*传参 :无
*
* Returns : none
*返回 : 无
*
* Note : OSStartHighRdy() MUST:
* a) Call OSTaskSwHook() then,
* b) Set OSRunning to OS_TRUE.
* c) Load the context of the task pointed to by OSTCBHighRdy.
* d_ Execute the task.
* 说明 : OSStartHighRdy() 函数必须使用:
* a) 然后调用函数OSTaskSwHook(),
* b) 设置 OSRunning为 OS_TRUE.
* c) 加载被OSTCBHighRdy指向的内容.
* d_ 运行任务
*********************************************************************************************************
*/
void OSStart (void)
{
if (OSRunning == OS_FALSE) {
OS_SchedNew(); /* Find highest priority's task priority number */
OSPrioCur = OSPrioHighRdy;
OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy]; /* Point to highest priority task ready to run */
OSTCBCur = OSTCBHighRdy;
OSStartHighRdy(); /* Execute target specific code to start task */
}
}
上一篇:ARM学习笔记--LPC1788引脚分析
下一篇:ARM学习笔记--GPIO接口
推荐阅读最新更新时间:2024-03-16 15:00