Lab4 μC/OS - rswiki.csie.orgrswiki.csie.org/dokuwiki/_media/courses:105_1:csl_lab4_ucos.pdf · The...

14
Lab 4 Department of Computer Science and Information Engineering National Taiwan University Lab4 – μC/OS 2016/10/18 / 13 1

Transcript of Lab4 μC/OS - rswiki.csie.orgrswiki.csie.org/dokuwiki/_media/courses:105_1:csl_lab4_ucos.pdf · The...

Page 1: Lab4 μC/OS - rswiki.csie.orgrswiki.csie.org/dokuwiki/_media/courses:105_1:csl_lab4_ucos.pdf · The main kernel The user tasks The ... (TCB) is a data structure used to maintain the

Lab 4 Department of Computer Science and Information Engineering

National Taiwan University

Lab4 – μC/OS

2016/10/18 / 13 1

Page 2: Lab4 μC/OS - rswiki.csie.orgrswiki.csie.org/dokuwiki/_media/courses:105_1:csl_lab4_ucos.pdf · The main kernel The user tasks The ... (TCB) is a data structure used to maintain the

Lab 4 Department of Computer Science and Information Engineering

National Taiwan University

Practice real-time programming on μC/OS-II.

2016/10/18 / 13 2

Page 3: Lab4 μC/OS - rswiki.csie.orgrswiki.csie.org/dokuwiki/_media/courses:105_1:csl_lab4_ucos.pdf · The main kernel The user tasks The ... (TCB) is a data structure used to maintain the

Lab 4 Department of Computer Science and Information Engineering

National Taiwan University

Host System

Windows XP

Build System

IAR Embedded Workbench

Target System

PTK development board (STM32F207)

Software

The source codes of μC/OS-II.

You can find all software on HERE.

2016/10/18 / 13 3

Page 4: Lab4 μC/OS - rswiki.csie.orgrswiki.csie.org/dokuwiki/_media/courses:105_1:csl_lab4_ucos.pdf · The main kernel The user tasks The ... (TCB) is a data structure used to maintain the

Lab 4 Department of Computer Science and Information Engineering

National Taiwan University

μC/OS-II is a real-time operating system with the following features:

Highly portable

It is written in ANSI C, with target-specific code written in assembly language.

It support various platforms, such as x86, MIPS, ARM.

ROMable

You can embed μC/OS-II as part of a product with the proper tool chain.

Very scalable

Simply specify which features to use through #define constant.

Preemptive real-time

It always runs the highest priority task that is ready.

Multitasking

It can manage up to 64 tasks, including 8 reserved tasks for μC/OS-II.

The source codes can be downloaded from the official website freely.

It is neither freeware nor open source code.

You are required to purchase a license for use in any commercial application.

2016/10/18 / 13 4

Page 5: Lab4 μC/OS - rswiki.csie.orgrswiki.csie.org/dokuwiki/_media/courses:105_1:csl_lab4_ucos.pdf · The main kernel The user tasks The ... (TCB) is a data structure used to maintain the

Lab 4 Department of Computer Science and Information Engineering

National Taiwan University 2016/10/18 / 13 5

The definition of features in this application

The main kernel

The user tasks

The functionalities of board support package (BSP)

The functions of tick interrupt handler and context switch

Page 6: Lab4 μC/OS - rswiki.csie.orgrswiki.csie.org/dokuwiki/_media/courses:105_1:csl_lab4_ucos.pdf · The main kernel The user tasks The ... (TCB) is a data structure used to maintain the

Lab 4 Department of Computer Science and Information Engineering

National Taiwan University

It is a multitasking and preemptive kernel with a priority-driven real-time scheduling.

Nested interrupts could go up to 256 levels.

There are 64 priorities, 0 is the highest, and 63 is the lowest.

Each task must have a unique priority in the application.

μC/OS-III supports more tasks having the same priority with round-robin scheduling, while it is not supported in μC/OS-II.

2016/10/18 / 13 6

Non-preemptive kernel Preemptive kernel

Page 7: Lab4 μC/OS - rswiki.csie.orgrswiki.csie.org/dokuwiki/_media/courses:105_1:csl_lab4_ucos.pdf · The main kernel The user tasks The ... (TCB) is a data structure used to maintain the

Lab 4 Department of Computer Science and Information Engineering

National Taiwan University

A task control block (TCB) is a data structure used to maintain the state of a task when it is preempted.

All valid TCB’s are doubly linked.

Free TCB’s are linked in a free list.

The contents of a TCB is saved/restored when a context switch occurs.

The stack, priority, time delay, etc. of a task.

CPU registers are stored in the stack rather than in the TCB.

2016/10/18 / 13 7

0OSTCBFreeList OSTCBNext OSTCBNext OSTCBNext OSTCBNext

OSTCBTbl[0] OSTCBTbl[1] OSTCBTbl[2]

OSTCBTbl[OS_MAX_TASKS+OS_N_SYS_TASKS-1]

Page 8: Lab4 μC/OS - rswiki.csie.orgrswiki.csie.org/dokuwiki/_media/courses:105_1:csl_lab4_ucos.pdf · The main kernel The user tasks The ... (TCB) is a data structure used to maintain the

Lab 4 Department of Computer Science and Information Engineering

National Taiwan University

If there is no user task ready, it will start the idle task (the priority is 63).

In os_core.c:

void OS_Sched (void) {

...

OS_ENTER_CRITICAL();

if (OSIntNesting == 0u) {

if (OSLockNesting == 0u) {

OS_SchedNew();

OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];

if (OSPrioHighRdy != OSPrioCur) {

#if OS_TASK_PROFILE_EN > 0u

OSTCBHighRdy->OSTCBCtxSwCtr++;

#endif

OSCtxSwCtr++;

OS_TASK_SW();

}

}

}

OS_EXIT_CRITICAL();

}

2016/10/18 / 13 8

Find the highest ready task.

Perform a context switch.

Page 9: Lab4 μC/OS - rswiki.csie.orgrswiki.csie.org/dokuwiki/_media/courses:105_1:csl_lab4_ucos.pdf · The main kernel The user tasks The ... (TCB) is a data structure used to maintain the

Lab 4 Department of Computer Science and Information Engineering

National Taiwan University

app.c:

void main(void) {

OSInit();

OSTaskCreate(

((void (*)(void *)) App_TaskStart,

(void *) 0,

(OS_STK *) &App_TaskStartStk[APP_TASK_START_STK_SIZE - 1],

(INT8U) APP_TASK_START_PRIO);

OSStart();

}

void App_TaskStart(void *pdata) {

for (;;) {

/* do something ... */

OSTimeDly(100);

}

}

2016/10/18 / 13 9

Each task performs an infinite loop.

Initialize task ready list, priority table, TCBs, and free pool.

Use OSTaskCreate() or OSTaskCreateExt() to create a task.

Tasks become “ready” after they are created.

Start multitasking of μC/OS-II (and never return).

function pointer of a user task

the pointer to the task's top of stack

the task's priority

task-specified data

Use OSTimeDly() or OSTimeDlyHMSM() to do time delay.

It allows other tasks to execute.

Page 10: Lab4 μC/OS - rswiki.csie.orgrswiki.csie.org/dokuwiki/_media/courses:105_1:csl_lab4_ucos.pdf · The main kernel The user tasks The ... (TCB) is a data structure used to maintain the

Lab 4 Department of Computer Science and Information Engineering

National Taiwan University

Step 1: download the PTK_Examples, provided in Lab 3.

Step 2: download and extract the μC/OS-II source codes.

Step 3: copy the folder Software in Micrium to PTK_Examples/ePBB/Libraries/OS_uCOS-II/Micrium-V29x.

Step 4: connect the target system STM32F207 to Windows XP.

2016/10/18 / 13 10

Page 11: Lab4 μC/OS - rswiki.csie.orgrswiki.csie.org/dokuwiki/_media/courses:105_1:csl_lab4_ucos.pdf · The main kernel The user tasks The ... (TCB) is a data structure used to maintain the

Lab 4 Department of Computer Science and Information Engineering

National Taiwan University

Step 1: open the serial console.

Please set baud rate to 115200bps.

Step 2: open the following workspace in IAR Embedded Workbench IDE.

PTK_Examples/ePBB/Applications/Projects/PTK-STM32F207/EWARM-

V6/OS_uCOS-II/base_uart/demo.eww

Step 3: compile the project and download the program to the target system.

Step 4: click the button “Go” or press the key “F5” to continue the execution.

You will see the message “Hello World!!!” on the serial console.

2016/10/18 / 13 11

Page 12: Lab4 μC/OS - rswiki.csie.orgrswiki.csie.org/dokuwiki/_media/courses:105_1:csl_lab4_ucos.pdf · The main kernel The user tasks The ... (TCB) is a data structure used to maintain the

Lab 4 Department of Computer Science and Information Engineering

National Taiwan University

A counter recording the number of OS-ticks since the system’s startup.

OS_TICKS_PER_SEC ticks per second. Originally configured to be 1000.

typedef unsigned int INT32U

0~4294967295

Other than %d, You may use %u in the format string for printing.

2016/10/18 / 13 12

Page 13: Lab4 μC/OS - rswiki.csie.orgrswiki.csie.org/dokuwiki/_media/courses:105_1:csl_lab4_ucos.pdf · The main kernel The user tasks The ... (TCB) is a data structure used to maintain the

Lab 4 Department of Computer Science and Information Engineering

National Taiwan University

Create several tasks in μC/OS-II to complete the followings:

Regularly read the value of temperature.

Regularly read the value of luminous flux.

Set up a key handler to display message to LCD screen when a key button is pressed.

Tips:

You need to initialize the LCD device by invoking “ili9325_gui_config()” in the function “platform_board_init_hook()”.

OSTimeDly(#OS-tick) v.s. VK_DELAY_MS(#ms);

OSTimeDlyHMSM(H, M, S, M)

If confronted by the problem of displaying things on LCD screen, try to adjust the size of stack of a task.

2016/10/18 / 13 13

Page 14: Lab4 μC/OS - rswiki.csie.orgrswiki.csie.org/dokuwiki/_media/courses:105_1:csl_lab4_ucos.pdf · The main kernel The user tasks The ... (TCB) is a data structure used to maintain the

Lab 4 Department of Computer Science and Information Engineering

National Taiwan University

Show that you can display the information of current temperature and luminous flux on LCD screen by pressing key buttons.

According to OSTimeDly() and OSTime, calculate the amount of time between each change of temperature (and/or luminous flux) while displaying it on LCD screen.

See how you can make the resolution higher and explain it in the report.

(Hint: you may read the implementation of some functions in os_time.c)

2016/10/18 / 13 14