28.03.2015 Views

Task Management and Lab 5

Task Management and Lab 5

Task Management and Lab 5

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

ELET 7404 Embedded & Real Time<br />

Operating Systems<br />

<strong>Task</strong> <strong>Management</strong><br />

<strong>and</strong> <strong>Lab</strong>5<br />

Fall 2007


<strong>Task</strong> Code Rules<br />

• Must be an infinite loop<br />

• for(;;) { }<br />

• while(1) { }<br />

• Must have its OWN STACK space setup in the<br />

beginning of code by the use of OS_STK<br />

• Must call a service at the bottom of the infinite loop


<strong>Task</strong> Structure<br />

Void Your<strong>Task</strong> (void *pdata)<br />

{<br />

for (;;) {<br />

/* USER CODE */<br />

Call one of uC/OS-II’s services:<br />

OSFlagPend();<br />

OSMboxPend();<br />

OSQPend();<br />

OSSemPend();<br />

OS<strong>Task</strong>Suspend(OS_PRIO_SELF);<br />

OSTimeDly();<br />

OSTimeDlyHMSM();<br />

}<br />

/* USER CODE */<br />

}


Creating a <strong>Task</strong><br />

OS<strong>Task</strong>Create ( )<br />

OS<strong>Task</strong>CreateExt ( )


Idle <strong>Task</strong><br />

<br />

Always created<br />

<br />

Created by OSInit()<br />

<br />

Always the lowest priority, OS_LOWEST_PRIO<br />

<br />

The idle task can never be deleted by application<br />

software


OS<strong>Task</strong>Create()<br />

Requires four arguments:<br />

• <strong>Task</strong> : pointer to the task code<br />

• Pdata: pointer to an argument that is passed to<br />

your task when it starts executing<br />

• Ptos: pointer to the top of the stack that is assigned<br />

to the task<br />

• Prio: is the desired task priority


OS<strong>Task</strong>Create(): Example<br />

OS<strong>Task</strong>Create(TestStart, , (void *)0, &TestStartStk[0], 0);<br />

/* Here the stack grows from low to high memory */<br />

OS<strong>Task</strong>Create(TestStart, , (void *)0,<br />

&TestStartStk[TASK_STK_SIZE<br />

TASK_STK_SIZE-1], 0);<br />

/* Here the stack grows from high to low memory */


OS<strong>Task</strong>CreateExt()<br />

• Offers more flexibility but has more overhead<br />

• Requires 9 arguments<br />

• First four, same as OS<strong>Task</strong>Create()<br />

• 5 TH argument: Id – unique identifier<br />

• 6 TH argument: Pbos – pointer to tasks bottom of stack<br />

TH argument: Stk_size – specifies stack size in number<br />

of elements.<br />

• 7 TH<br />

TH argument: Pext – pointer to a user-supplied supplied data<br />

area that can be used to extend the OS_TCB of the task<br />

• 8 TH<br />

TH argument: Opt – specifies whether stack checking is<br />

allowed, whether the stack will be cleared, <strong>and</strong> whether<br />

floating point operations are performed by the task,<br />

among others<br />

• 9 TH<br />

TCB -<strong>Task</strong> Control Block


Note about the TCB (<strong>Task</strong> Control Block)<br />

When a task is created, it is assigned a <strong>Task</strong> Control Block<br />

(OS_TCB) . This TCB is used to maintain the state of a task<br />

when it is preempted.<br />

The TCB resides in RAM<br />

Note about the opt argument<br />

In order to perform stack checking on a task, the<br />

OS_TASK_OPT_STK_CHK flag in the opt argument must<br />

be set<br />

Also the stack must contain zeros, then use the function<br />

OS_TASK_OPT_STK_CLR


OS<strong>Task</strong>CreateExt( ( ): Example<br />

OS<strong>Task</strong>CreateExt(<strong>Task</strong>Start, , (void *)0, ptos, , TASK_START_PRIO,<br />

TASK_START_ID, pbos, , size, (void *)0,<br />

OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);<br />

Note for lab 5:<br />

OS<strong>Task</strong>CreateExt(STKWrit<strong>Task</strong>, , (void *)0, (OS_STK *)&STKWrit<strong>Task</strong>Stk[0],<br />

5, 5, (OS_STK *)&STKWrit<strong>Task</strong>Stk[100-1], 1], 100, (void *)0,<br />

OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);


Notes about <strong>Lab</strong> 5: check stack space on a task<br />

To check space on a task you should:<br />

• Set OS_TASK_CREATE_EXT to 1 in OS_CFG.H<br />

• Set the opt argument in OS<strong>Task</strong>CreateExt( ) to<br />

OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR


Notes about <strong>Lab</strong> 5: check stack space on a task<br />

Stack checking allows to reduce the amount of RAM<br />

needed by the application by not overallocating stack space<br />

OS<strong>Task</strong>StkChk( ) provides this information<br />

• To perform stack checking, microC requires that the<br />

stack be filled with zeros when the stack is created<br />

• Also micro C needs to know the location of the bottom<br />

of the stack <strong>and</strong> the size of the stack you assigned to<br />

the task<br />

• These two values are stored in the task’s OS_TCB<br />

when the task is created but only if the task was created<br />

with OS<strong>Task</strong>CreateExt( )


Functions in OS_TASK.C<br />

<strong>Task</strong>s<br />

• Change Priority of a <strong>Task</strong> [ OSPrioChange() ]<br />

• Create a <strong>Task</strong> [ OS<strong>Task</strong>Create() ]<br />

• Create a <strong>Task</strong> (Extended Version) [ OS<strong>Task</strong>CreateExt() ]<br />

• Delete a <strong>Task</strong> [ OS<strong>Task</strong>Del() ]<br />

• Request that a <strong>Task</strong> Delete Itself [ OS<strong>Task</strong>DelReq() ]<br />

• Resume a Suspended <strong>Task</strong> [ OS<strong>Task</strong>Resume () ]<br />

• Stack Checking [ OS<strong>Task</strong>StkChk() ]<br />

• Suspend a <strong>Task</strong> [ OS<strong>Task</strong>Suspend() ]<br />

• Query a <strong>Task</strong> [ OS<strong>Task</strong>Query() ]


<strong>Task</strong><br />

• Can be created prior to multitasking or created by another<br />

task<br />

• At least one task must be created before multitasking<br />

begins<br />

• <strong>Task</strong> Counter<br />

•OS<strong>Task</strong>Ctr<br />

• Counter is incremented in OS<strong>Task</strong>Create() <strong>and</strong><br />

OS<strong>Task</strong>CreateExt() <strong>and</strong> decremented in<br />

OS<strong>Task</strong>Del()<br />

- To display the #tasks running:<br />

printf("#<strong>Task</strong>s:%2u\n", OS<strong>Task</strong>Ctr);


<strong>Task</strong> Stacks<br />

• Each task must have its own stack space<br />

• Stack must be declared as being of type OS_STK<br />

• Stack must consist of contiguous memory locations<br />

• Allocate stack space statically (compile time) or<br />

dynamically (run-time)


Static stack<br />

Static OS_STK My<strong>Task</strong>Stack[stack_size];<br />

OR<br />

OS_STK My<strong>Task</strong>Stack[stack_size];<br />

• Stack can grow from low to high memory or<br />

vice versa


Configuration File<br />

• You will have to modify a configuration file<br />

to enable or disable features of<br />

MicroC/OS<br />

/OS-II<br />

• OS_CFG.H<br />

• Initially this file is named, os_cfg_r.h while it<br />

isn’t t being used<br />

• Rename os_cfg.r.h to OS_CFG.H to get started


Time <strong>Management</strong><br />

• Time <strong>Management</strong><br />

• Found in OS_TIME.C<br />

• OSTimeDly()<br />

• Enter a delay in clock ticks<br />

• OSTimeDlyHMSM()<br />

• Enter a delay in hours, minutes, seconds,<br />

milliseconds<br />

• OSTimeDlyResume()<br />

• Resumes a task that has been delayed by<br />

OSTimeDly() or OSTimeDlyHMSM()<br />

• OSTimeGet()<br />

• Gets the current value of the system clock<br />

• OSTimeSet()<br />

• Sets the desired value of the system clock in ticks


Kernel Core<br />

•OS_CORE.c<br />

• Priority Resolution Table<br />

• Initialization [ OSInit() ]<br />

• Interrupt Subroutine enter <strong>and</strong> exit<br />

• Scheduler (enable <strong>and</strong> disable)<br />

• Start Multitasking [ OSStart() ]<br />

• System Tick<br />

• Get version [ OSVersion() ]<br />

• Statistics <strong>Task</strong><br />

• Create Idle <strong>Task</strong><br />

• Idle <strong>Task</strong>


About LAB 5:<br />

Stack Space with MicroC/OS-II<br />

• Displaying the actual stack space for a new task<br />

that displays its own stack space<br />

• Create a new task with the lowest priority that<br />

displays the used stack space for this new task


void STKWrit<strong>Task</strong>()<br />

{<br />

char x[2];<br />

INT32U stk_size;<br />

OS_STK_DATA stk_data;<br />

for(;;)<br />

{<br />

OSTimeDly(8000);<br />

// period= 8000mS<br />

// LAB5: Using service to check stack space on this task+ stk_data.OSFree = total<br />

OS<strong>Task</strong>StkChk(OS_STAT_PRIO, &stk_data); stk_size = stk_data.OSUsed ;<br />

itoa(stk_size,x);<br />

DispClrScr();<br />

DispStr(0,x);<br />

}<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!