您的位置 首页 软件

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

下面就直接进程序看吧,首先看mian函数intmain(void){INT8Uos_err;//OSerrorBsp_init();//EmbeddeddevelopmentboardInitiali

下面就直接进程序看吧,首要看mian函数


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//开端使命运转
}

开发板的初始化便是对arm单片机的引脚和时钟等等运转必要条件进行初始化,这儿就不看了,咱们来看看OSInit()


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

咱们学习的时分不必管这个函数里边究竟履行了什么,可是咱们必需求学会一点:知道这个函数是在运用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体系的一些了解,在今后的处理过程中,学习到更多的东西,我将会写出来。个人感觉,看注释其实是一个非常好的学习方法,尽管注释是英文很难看懂,但这样愈加精确,看懂了咱们也就了解了。

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qianrushi/ruanjian/275735.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部