Файл: Embedded system development and labs for ARM (R. Muresan, 2005).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 13.06.2025

Просмотров: 2329

Скачиваний: 3

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Embedded Systems Development and Labs; The English Edition

#

AND

r4, r4, #0xFFFFFF20

#

ORR

r4, r4, #0x13

MSR

SPSR_cxsf, r4

@ YYY+

LDMFD sp!, {r4}

@ YYY+

#

AND

r4, r4, #0xFFFFFF20

#

ORR

r4, r4, #0x13

MSR

CPSR_cxsf, r4

@ YYY+

ldr

r0,=0x4000000

BL

SysENInterrupt

LDMFD sp!, {r0-r12, lr, pc} @ YYY+

Exercises

(1)Expand the function of uC/OS-II. Add time calculation of task switching.

(2)Trace OsTickISR() function. Watch the task switching process in timer pacing.

7.2 uC/OS Application Lab

7.2.1 Purpose

Get familiar with the uC/OS-II boot flow.

Get familiar with the uC/OS-II task management.

Learn how to use the inter-task communication, synchronization and memory management functions provided by uC/OS-II.

7.1.2 Lab Equipment

● Hardware: Embest S3CEV40 hardware platform, Embest Standard/Power Emulator, PC. ● Software: Embest IDE 2003, Windows 98/2000/NT/XP operation system.

7.1.3 Content of the Lab

Write a program that creates 3 tasks for 8-SEG LED displaying, LED lights flashing, and sending data to the serial port.

7.1.4 Principles of the Lab

1. The Boot Process of the uC/OS-II Kernel

The uC/OS-II booting follows the following steps flow:

(1)Assign task stack in the programs. The purpose of assigning stack is to provide a space for stack and variables of the running task. The task stack is initialized by defining array unsigned int StackX[STACKSIZE] and transfer the pointer to this array when task is booted.

(2)Establish Task Function Body. The function body includes variable definitions and initializations, functions or instructions, time interval settings of suspended task.

275


Embedded Systems Development and Labs; The English Edition

(3)Describes boot task. Transfer the address of task function, task stack and task priority.

(4)The boot process is done by function main(). This function includes hardware initialization before running tasks, operation system initialization, start timer interrupt, boot tasks, etc.

2. uC/OS-II Task Managment

uC/OS provides the following functions for task management:

OSTaskCreate ()

create a task

OSTaskCreateExt()

extension version of create a task

OSTaskDel()

delete a task

OSTaskDelReq()

request for a task delete

OSTaskChangePrio()

change task priority

OSTaskSuspend()

suspend a task

OSTaskResume()

resume a task

OSTaskStkChk()

stack check

OSTaskQuery()

get information of task

3. uC/OS-II System Calls

1) Inter-task Communication and Synchronization – Semaphore, Mailbox and Message Queues

(1) Seaphore

OSSemCreate()

create a semaphore

SSemPend()

wait for a semaphore

OSSemPost()

send a semaphore

OSSemAccept()

no waiting request a semaphore

OSSemQuery()

query the current status of a semaphore

(2) Mailbox

OSMboxCreate()

create a mailbox

OSMboxPend()

suspend a mailbox

OSMboxPost()

send a message to mailbox

OSMboxAccept()

no waiting get a message from mailbox

OSMboxQuery()

query status of a mailbox

3) Message Queue

OSQCreate()

create a message queue

OSQPend()

suspend a message queue

OSQPost()

send a message to message queue

OSQAccept()

no waiting get a message from message queue

OSQFlush()

clear a message queue

OSQuery()

query status of a mailbox

2) Other System Calls – Time, Memory Management

(1) Time Management

OSTimeDly()

task delay function

OSTimeDlyHMSM()

time delay by second, minutes, or hours

OSTimeDlyResume()

stop delay when a task is in delay

276


Embedded Systems Development and Labs; The English Edition

OSTimeGet()

get system time

OSTimeSet()

set system time

(2) Memory Management

OSMemCreate()

create a memory partition

OSMemGet()

assign a memory block

OSMemPut()

release a memory block

OSMemQuery()

query the status of a memory block

7.2.5 Sample Programs

void Task1(void *Id)

{

/* print task's id */ OSSemPend(UART_sem, 0, &err);

uHALr_printf(" Task%c Called.\n", *(char *)Id); OSSemPost(UART_sem);

while(1)

{

led1_on(); // lit the led led2_off(); OSTimeDly(800); // delay led1_off();

led2_on(); OSTimeDly(800);

}

}

void Task4(void *Id)

{

int i;

INT32U NowTime; /* print task's id */

OSSemPend(UART_sem, 0, &err); uHALr_printf(" Task%c Called.\n", *(char *)Id); OSSemPost(UART_sem);

while(1)

{

for(i=0; i<16; i++)

{

OSSemPend(UART_sem, 0, &err); NowTime=OSTimeGet(); //»ñȡʱ¼ä //uHALr_printf("Run Times at:%d\r", NowTime);

277


Embedded Systems Development and Labs; The English Edition

OSSemPost(UART_sem); OSTimeDly(180);

}

}

}

void Task3 (void *Id)

{

char *Msg; int i=0;

/* print task's id */ OSSemPend(UART_sem, 0, &err);

uHALr_printf(" Task%c Called.\n", *(char *)Id); OSSemPost(UART_sem);

while(1)

{

OSTimeDly(900); OSSemPend(UART_sem, 0, &err); EV40_rtc_Disp(); OSSemPost(UART_sem);

}

}

void Task2 (void *Id)

{

int value; char *Msg;

/* print task's id */ OSSemPend(UART_sem, 0, &err);

uHALr_printf(" Task%c Called.\n\n", *(char *)Id); OSSemPost(UART_sem);

while(1)

{

value = key_read();

// display in 8-segment LED if(value > -1)

{

Digit_Led_Symbol(value); OSTimeDly(90);

}

OSTimeDly(90);

}

278

Embedded Systems Development and Labs; The English Edition

}

void TaskStart (void *i)

{

char Id1 = '1'; char Id2 = '2'; char Id3 = '3'; char Id4 = '4'; /*

*create the first Semaphore in the pipeline with 1

*to get the task started.

*/

UART_sem = OSSemCreate(1);

uHALr_InitTimers(); // enable timer counter interrupt /*

*create the tasks in uC/OS and assign decreasing

*priority to them

*/

OSTaskCreate(Task1, (void *)&Id1, &Stack1[STACKSIZE - 1], 2); OSTaskCreate(Task2, (void *)&Id2, &Stack2[STACKSIZE - 1], 3); OSTaskCreate(Task3, (void *)&Id3, &Stack3[STACKSIZE - 1], 4); OSTaskCreate(Task4, (void *)&Id4, &Stack4[STACKSIZE - 1], 5); ARMTargetStart();

// Delete current task OSTaskDel(OS_PRIO_SELF);

}

void Main(void)//int argc, char **argv

{

char Id0 = '4';

ARMTargetInit();

//hardware initialization

/* needed by uC/OS */

OSInit();

//uC/OS initialization

OSTimeSet(0);

// timer setting

/* create the start task */

OSTaskCreate(TaskStart,(void *)0, &StackMain[STACKSIZE - 1], 0); /* start the operating system */

ARMTargetStart();

//enable timer interrupt

OSStart();

//start the OS

}

279


Embedded Systems Development and Labs; The English Edition

7.2.6 Exercises

Improve the program by implementing inter-task communication and synchronization such that every time when the 8-SEG LED displays a character the serial port also outputs the same character.

7.3 uC/OS Application Lab

7.3.1 Content of the Lab

Write a start-stop watch program that uses the uC/OS-II kernel. The program is a simple one-button stopwatch that displays minutes, seconds, and tenths of seconds in the following format: 99:59.9

The stopwatch has a single button that cycles the watch through three modes: CLEAR -> COUNT -> STOP -> CLEAR …

7.3.2 Stopwatch Tasks

There are five tasks for the complete program, including the start-up task. The priorities assigned to each task follow the rate monotonic scheduling rule. Following are the task execution rates and the assigned priorities:

Task

Task Period

Priority

StartTask()

One time only

4*

UpdateTimeTsk()

1ms

6

ScanSwTsk()

10ms

8

DispTimeTsk()

100ms

10

TimerModeTsk

1/keypress

12

* Required to be the highest priority.

The following describes briefly the tasks functions:

(1)StartTask(): This task starts by initializing the kernel timer with OSTTickInit(). It then initializes the LCD and creates the rest of the tasks. Once the rest of the tasks are complete, the start-up task suspends itself indefinitely.

(2)UpdateTimeTsk(): This is the primary time keeping task. It has the highest priority to keep the stopwatch accuracy within 1ms. The task increments a global variable called msCntr every millisecond.

(3)ScanSw(): This is the switch-scanning and debouncing task. The main requirement is that it has to run with a period that is at least one-half the switch bounce time. Since the task period is 10ms, it is designed for switch bounce times less than 20ms. It also rejects noise pulses up to 10ms wide. Notice that the task period does not have to be exactly 10ms. It can vary as much as 20% without causing significant errors. When a valid keypress is accepted, ScanSw() signals a semaphore event flag, SwFlag. This flag can than be used by other tasks to service a keypress. In this application the timer mode task changes the mode each time the key is pressed.

(4)TimerModeTsk(). This task is a simple state machine that controls the mode of the stopwatch. Each time a key is pressed, the SwFlag semaphore is signaled by the switch-scanning task. When SwFlag is

280