新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > ARM學(xué)習(xí)筆記--初識(shí)uC/OS(一)

ARM學(xué)習(xí)筆記--初識(shí)uC/OS(一)

作者: 時(shí)間:2016-11-10 來源:網(wǎng)絡(luò) 收藏
下面就直接進(jìn)程序看吧,首先看mian函數(shù)

int main(void)
{
INT8U os_err;//OS error
Bsp_init();//Embedded development board Initialization//開發(fā)板初始化
OSInit();//uC/OS initialization//系統(tǒng)初始化
os_err = OSTaskCreateExt((void (*)(void *)) App_Task_LCD,//創(chuàng)建任務(wù)
(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//開始任務(wù)運(yùn)行
}

本文引用地址:http://m.butianyuan.cn/article/201611/317283.htm開發(fā)板的初始化就是對(duì)arm單片機(jī)的引腳和時(shí)鐘等等運(yùn)行必要條件進(jìn)行初始化,這里就不看了,我們來看看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().
× 描述:該函數(shù)用于uC/OS-II系統(tǒng)的內(nèi)部初始化,它必須在調(diào)用uC/OS-II系統(tǒng)的任何創(chuàng)建對(duì)象之前,也必須在
* 函數(shù)OSStart()之前執(zhí)行。也就是說使用uC/OS-II系統(tǒng)的第一步就是執(zhí)行這個(gè)函數(shù),這是一個(gè)約定。
*
* 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
}

我們學(xué)習(xí)的時(shí)候不用管這個(gè)函數(shù)里面到底執(zhí)行了什么,但是我們必須要學(xué)會(huì)一點(diǎn):知道這個(gè)函數(shù)是在使用uC/OS系統(tǒng)前的第一個(gè)需要調(diào)用的函數(shù),只要知道這個(gè)我們就算知道怎么用它了。
看下一個(gè)函數(shù)OSTaskCreateExt

/*
*********************************************************************************************************
* CREATE A TASK (Extended Version)
* 創(chuàng)建任務(wù)(擴(kuò)展版本)
*
* 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.
* 描述: 該函數(shù)用于創(chuàng)建一個(gè) uC/OS-II管理的可執(zhí)行任務(wù).它不是在多個(gè)任務(wù)執(zhí)行前被創(chuàng)建就是在一個(gè)運(yùn)行
* 的任務(wù)中被創(chuàng)建.在中斷服務(wù)函數(shù)中不能創(chuàng)建任務(wù)(也就是說在ISR中不能調(diào)用該函數(shù)). 除了該函數(shù)允許一
*個(gè)任務(wù)的附加信息被列出外,該函數(shù)類同于函數(shù)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 任務(wù)代碼的一個(gè)指針(指向任務(wù)代碼段)
*
* p_arg 當(dāng)task第一次運(yùn)行時(shí),它代表指向一個(gè)用于向task傳遞參數(shù)的可選數(shù)據(jù)區(qū)域的指針.當(dāng)task
* 在連接數(shù)據(jù)的時(shí)候,它被要求和像下面的例子這樣傳遞參數(shù)p_arg :
*
* void Task (void *p_arg)
* {
* for (;;) {
* Task code;
* }
* }
*
* ptos 任務(wù)棧頂指針.假如OS_STK_GROWTH設(shè)置為1,則棧被認(rèn)為是向低地址推移的(即從高存儲(chǔ)到低
* 存儲(chǔ)位置).棧頂指針將指向棧所在的存儲(chǔ)器的最高位置.假如OS_STK_GROWTH設(shè)置為0,則棧被
* 認(rèn)為是向高地址推移的(即從低存儲(chǔ)到高存儲(chǔ)位置).棧頂指針隨內(nèi)存增加增加. ptos一定指向
* 一個(gè)可用的空閑的數(shù)據(jù)項(xiàng).
*
* prio 任務(wù)的優(yōu)先級(jí). 每個(gè)任務(wù)都必須有一個(gè)唯一的優(yōu)先級(jí).優(yōu)先級(jí)數(shù)字越小,優(yōu)先級(jí)別越高.
*
* id 任務(wù)的ID號(hào)(0..65535)
*
* pbos 任務(wù)的棧底指針. 假如OS_STK_GROWTH設(shè)置為1,則棧被認(rèn)為是向低地址推移的(即從高存儲(chǔ)到低
* 存儲(chǔ)位置).棧底指針將指向棧所在的存儲(chǔ)器的最低位置.假如OS_STK_GROWTH設(shè)置為0,則棧被
* 認(rèn)為是向高地址推移的(即從低存儲(chǔ)到高存儲(chǔ)位置).棧底指針隨內(nèi)存增加增加. ptos一定指向
* 一個(gè)可用的空閑的數(shù)據(jù)項(xiàng).
*
* stk_size 棧的長(zhǎng)度.如果OS_STK設(shè)置為INT8U,stk_size則為字節(jié)數(shù)允許.若OS_STK設(shè)置為INT16U,stk_size則為十
* 六位數(shù)允許.最后若OS_STK設(shè)置為INT32U,stk_size則為32位二進(jìn)制允許.(指明棧的寬度)
*
* pext 是一個(gè)指針,指向用戶提供的用于TCB擴(kuò)展部分的內(nèi)存空間. 例如:通過上下文切換用戶存儲(chǔ)器能
* 保持住浮點(diǎn)寄存器的內(nèi)容、每次任務(wù)運(yùn)行的時(shí)間、任務(wù)被開關(guān)的次數(shù)等等.
*
* opt 包含任務(wù)行為的附加信息(或選項(xiàng)).低八位被uC/OS-II系統(tǒng)作為保留字.高八位作為特殊的應(yīng)用.這個(gè)
* 選項(xiàng)的設(shè)置查看uCOS-II.H.中的OS_TASK_OPT_???.當(dāng)前選項(xiàng)是:
* OS_TASK_OPT_STK_CHK 需要進(jìn)行棧的檢查
* OS_TASK_OPT_STK_CLR 任務(wù)創(chuàng)建時(shí)清除棧
* OS_TASK_OPT_SAVE_FP 假如處理器有浮點(diǎn)數(shù)據(jù),保存它們
*
* 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 函數(shù)執(zhí)行成功范圍的內(nèi)容
* OS_PRIO_EXIT 該任務(wù)的優(yōu)先級(jí)已經(jīng)存在返回該值
* (每個(gè)任務(wù)都有一個(gè)唯一的優(yōu)先級(jí)).
* OS_ERR_PRIO_INVALID 設(shè)置的優(yōu)先級(jí)大于最大的優(yōu)先級(jí)別返回該值
* (即 > OS_LOWEST_PRIO)
* OS_ERR_TASK_CREATE_ISR 當(dāng)在ISR中創(chuàng)建任務(wù)時(shí)返回該值(ISR中不允許進(jìn)行任務(wù)創(chuàng)建)
*********************************************************************************************************
*/
/*$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狀態(tài)寄存器的存儲(chǔ) */
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 */
/* 確保優(yōu)先級(jí)在允許的范圍內(nèi) */
return (OS_ERR_PRIO_INVALID);
}
#endif
OS_ENTER_CRITICAL();
if (OSIntNesting > 0) { /* Make sure we dont create the task from within an ISR */
/* 確保不在ISR中創(chuàng)建任務(wù) */
OS_EXIT_CRITICAL();
return (OS_ERR_TASK_CREATE_ISR);
}
if (OSTCBPrioTbl[prio] == (OS_TCB *)0) { /* Make sure task doesnt already exist at this priority */
/* 確保任務(wù)優(yōu)先級(jí)設(shè)置沒有重復(fù) */
OSTCBPrioTbl[prio] = OS_TCB_RESERVED;/* Reserve the priority to prevent others from doing ... */
/* ... the same thing until task is created. */
/* 直到任務(wù)被創(chuàng)建完成,保留優(yōu)先級(jí)確保其他任務(wù)不做同樣的事情 */
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 */
/* 初始化任務(wù)棧 */
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 */
/* 在多任務(wù)開始后發(fā)現(xiàn)HPT */
OS_Sched();
}
} else {
OS_ENTER_CRITICAL();
OSTCBPrioTbl[prio] = (OS_TCB *)0; /* Make this priority avail. to others */
/* 是這個(gè)優(yōu)先級(jí)在其他任務(wù)中可用 */
OS_EXIT_CRITICAL();
}
return (err);
}
OS_EXIT_CRITICAL();
return (OS_ERR_PRIO_EXIST);
}
#endif

學(xué)習(xí)創(chuàng)建任務(wù)這個(gè)函數(shù),我們就要了解的更多,首先必須對(duì)每一個(gè)傳參要有所了解,在注釋中我已經(jīng)寫清楚了;第二要知道我們?cè)趗C/OS中能創(chuàng)建最多64個(gè)進(jìn)程,由于系統(tǒng)占用了4個(gè)還有4個(gè)備用,所以我們能創(chuàng)建的只有56個(gè);第三也是最終要的,在任務(wù)中我們可以創(chuàng)建新的任務(wù),但是在中斷函數(shù)中絕不能創(chuàng)建新的任務(wù),這將會(huì)引起未知錯(cuò)誤。
再來看函數(shù)OSStart()

/*
*********************************************************************************************************
* START MULTITASKING
* 開始多任務(wù)運(yùn)行
*
* 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.
*
×描述:該函數(shù)用于開啟你已經(jīng)在uC/OS-II中創(chuàng)建的多任務(wù)進(jìn)程.在調(diào)用該函數(shù)前,必須已經(jīng)調(diào)用了OSInit()函
* 數(shù)和創(chuàng)建了至少一個(gè)進(jìn)程.
×
* 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() 函數(shù)必須使用:
* a) 然后調(diào)用函數(shù)OSTaskSwHook(),
* b) 設(shè)置 OSRunning為 OS_TRUE.
* c) 加載被OSTCBHighRdy指向的內(nèi)容.
* d_ 運(yùn)行任務(wù)
*********************************************************************************************************
*/

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

這個(gè)函數(shù)的學(xué)習(xí),我們目前需要知道的只有兩點(diǎn):一、它必須在OSInit()和OSTaskCreate()之后,因?yàn)檫@樣才有進(jìn)程開始;二、就是說明里的內(nèi)容,這個(gè)函數(shù)的執(zhí)行是一個(gè)按順序執(zhí)行整個(gè)動(dòng)作?,F(xiàn)在我們還不用明白它干了什么,只要知道怎么用就行了。這里需要注意為什么在系統(tǒng)中時(shí)序,先后執(zhí)行的順序這么重要,因?yàn)閡C/OS系統(tǒng)是一個(gè)搶先式的執(zhí)行系統(tǒng),如果時(shí)序不對(duì),那么就會(huì)產(chǎn)生不一樣的結(jié)果。
這就是我才看uC/OS系統(tǒng)的一些理解,在以后的處理過程中,學(xué)習(xí)到更多的東西,我將會(huì)寫出來。個(gè)人感覺,看注釋其實(shí)是一個(gè)非常好的學(xué)習(xí)方法,雖然注釋是英文很難看懂,但這樣更加準(zhǔn)確,看懂了我們也就理解了。


關(guān)鍵詞: ARMuCO

評(píng)論


技術(shù)專區(qū)

關(guān)閉