新闻中心

EEPW首页>嵌入式系统>设计应用> ARM学习笔记--初识uC/OS(一)

ARM学习笔记--初识uC/OS(一)

作者: 时间:2016-11-10 来源:网络 收藏
下面就直接进程序看吧,首先看mian函数

        

int main(void)
{
INT8U os_err;//OSerror
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//开始任务运行
}

本文引用地址: //m.amcfsurvey.com/article/201611/317283.htm开发板的初始化就是对 arm单片机的引脚和时钟等等运行必要条件进行初始化,这里就不看了,我们来看看OSInit()

        

/*
*********************************************************************************************************
* INITIALIZATION
* 初始化
*
* Description: This function is used to initialize the internals of uC/OS-II and MUST be calledprior 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
}

我们学习的时候不用管这个函数里面到底执行了什么,但是我们必须要学会一点:知道这个函数是在使用uC/OS系统前的第一个需要调用的函数,只要知道这个我们就算知道怎么用它了。
看下一个函数OSTaskCreateExt

        

/*
*********************************************************************************************************
* 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 tasks 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 tasks 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 tasks priority. A unique priority MUST be assigned to each task and the
* lower the number, the higher the priority.
*
* id is the tasks ID (0..65535)
*
* pbos is a pointer to the tasks 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 dont 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 doesnt 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 tasks 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

学习创建任务这个函数,我们就要了解的更多,首先必须对每一个传参要有所了解,在注释中我已经写清楚了;第二要知道我们在uC/OS中能创建最多64个进程,由于系统占用了4个还有4个备用,所以我们能创建的只有56个;第三也是最终要的,在任务中我们可以创建新的任务,但是在中断函数中绝不能创建新的任务,这将会引起未知错误。
再来看函数OSStart()

        

/*
*********************************************************************************************************
* 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 prioritys 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 */
}
}

这个函数的学习,我们目前需要知道的只有两点:一、它必须在OSInit()和OSTaskCreate()之后,因为这样才有进程开始;二、就是说明里的内容,这个函数的执行是一个按顺序执行整个动作。现在我们还不用明白它干了什么,只要知道怎么用就行了。这里需要注意为什么在系统中时序,先后执行的顺序这么重要,因为uC/OS系统是一个抢先式的执行系统,如果时序不对,那么就会产生不一样的结果。
这就是我才看uC/OS系统的一些理解,在以后的处理过程中,学习到更多的东西,我将会写出来。个人感觉,看注释其实是一个非常好的学习方法,虽然注释是英文很难看懂,但这样更加准确,看懂了我们也就理解了。


关键词:ARMuCO

评论


技术专区

关闭