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

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

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

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

Добавлен: 13.06.2025

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

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

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

Embedded Systems Development and Labs; The English Edition

6.3.6 Exercises

(1)Write a program that implements the function of adjusting the voice volume via button.

(2)Write a program that implements the recording function.

268

Embedded Systems Development and Labs; The English Edition

Chapter7 Real Time Operation System Labs

7.1 uC/OS Porting Lab

6.3.1 Purpose

Get familiar with the uC/OS-II porting conditions and uC/OS-II kernel basic architecture

Understand the steps of porting the uC/OS-II kernel to the ARM processor.

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

Learn how to port the uC/OS-II kernel to the S3C44B0 ARM processor. Test its functionality using the Embest IDE.

7.1.4 Principles of the Lab

1. uC-OS-II File System

The file system of the uC/OS-II real time kernel is shown in Figure 7-1. The application software layer is the code based on the uC/OS-II kernel. The uC/OS-II includes the following three parts:

Kernel Code: This part has no relationship with the microprocessor. The kernel code includes 7 source files and 1 header file. The 7 source files are responsible for tasks such as: kernel management, event management, message queue management, memory management, message management, semaphore management, task scheduling and timer management.

Configuration Code: This part includes 2 header files for configuring the number of events per control block and it includes message management code, etc.

Processor Related Code: Includes 1 header file, 1 assembly file and 1 C file. In the process of porting the uC/OS-II kernel the users need to consider these files.

Application Software

Kernel Code (CPU independent)

Configuration Code (Application Related)

Oscore.c

Os_mbox.c

Os_mem.c

Os_cfg.h

Os_q.c

Includes.h

Os_sem.c

Os_task.c

Os_time.c

Ucos_ii.h

269


Embedded Systems Development and Labs; The English Edition

Porting Code (Microprocessor Related)

Os_cup.h

Os_cpu_a.asm

Os_cup_c.c

Figure 7-1 uC/OS-II File System

2. uC/OS-II Porting Conditions

Porting the uC/OS-II to the ARM processor requires the following conditions:

1) The C Compiler Targeting the Microprocessor Can Generate Reentry Code

Reentry code means that a piece of code can be used by more than one task without fear of data corruption. In another words, this code can be recalled after it was interrupted during the processing.

The following are two examples of non-reentrant and reentrant functions: Int temp;

Void swap (int *x, int *y)

{

temp=*x;

*X=*Y; *y=Temp;

}

void swap(int *x, int *y)

{

int temp; temp=*x; *X=*Y; *y=Temp;

}

The difference between these two functions is that the place for storing the variable temp is different. In the first function, “temp” is a global variable. In the second function, “temp” is a local variable. As a result, the upper function is not reentrant function. The lower function is a reentrant function.

2) Use C Language to Enable/Disable Interrupts

This can be done through the CPSR register within the ARM processor. The CPSR register has a global interrupt disable bit. Controlling this bit can enable/disable interrupts.

3) Microprocessor Supports Interrupts and Supports Timer Interrupts (Ticks)

All of the ARM processor cores support interrupts and they can generate timer interrupts.

4) Microprocessor Provide Hardware Support for Stack Control

270


Embedded Systems Development and Labs; The English Edition

For the 8-bit microprocessors that have only 10 address lines, the chip can only access a maximum of 1Kb memory. For these processors it is difficult to port the uC/OS-II kernel.

5) Microprocessor has Stack Pointer and Other Instructions for Reading Registers and Store the

Contents of Register to Memory or Stack.

The ARM processor has STMFD instruction for pushing the content of registers to stack, LDMFD instruction for pulling the register contents back from stack..

3. uC/OS-II Porting Steps

1) Basic Configuration and Definition

All the basic configurations and definitions are in 0s_cup.h.

Defines the data type related to compiler. In order to port uC/OS-II applications, there should be no int, unsigned int, etc definitions in the program. UC/OS has its own data type such as INT16U which represents 16-bit unsigned integer. For a 32-bit ARM processor, the INT16U is unsigned short. For a 16-bit ARM processor, the INT16U is unsigned int.

Defines interrupt enable or disable.

Defines stack growing direction. After defining the growing direction of stack, the value of OS_STK_GROWTH is defined.

Define the micro OS_TASK_SW. OS_TASK_SW is a called when a uc/OS-II lower priority task is switched with higher priority task. There are two ways to define it. One way is by using software interrupt and make the interrupt vector to point to the OSCtxSw() function. Another way is to call the OSCrxSw() function directly.

2) Porting OS_CPU_A.ASM Assembly File

In the OS_CPU_A.ASM, there are four functions that need to be ported.

(1)OSStartHighRdy() function. This function is called by OSStart() function to start the highest priority task ready to run. OSStart() is responsible for setting the tasks in the ready status. The functions of this routine are described in the MicroC/OS-II book using pseudocode language. This pseudocode must be converted in ARM assembly language. OSStartHighRdy() function loads the stack pointer of the CPU with the top-of-stack pointer of the highest priority task. Restore all processor registers from the new task’s stack. Execute a return from interrupt instruction. Note that OSStartHighRdy() never returns to OSStart().

(2)OSCtxSw() function. This function is responsible for the context switch. OSCtxSw() is generally written in assembly language because most C compilers cannot manipulate CPU registers directly from C. This function is responsible for pushing the registers of the current task on the stack; changing the SP to the new stack value; restore the registers of the new task; execute and return from the interrupt instruction. This function is called by OS_TASK_SW which in turn is called by the OSSched() function. OSSched() function is responsible for scheduling the tasks.

(3)OSIntCtxSw() function. This function is called by OSIntExit() function to perform a context switch from an ISR. OSIntExit is called by OSTickISR() function. Because OSIntCtxSw() is called from an ISR, it is assumed that all the processor registers are already properly saved onto the interrupted task’s stack. OSIntCtxSw() function responds for switching the tasks in timer interruptions. The OSCtxSw() function and OSIntCtxSw() function are responsible for the switching between tasks. OSIntCtxSw() function is responsible for saving the current task pointer and recover the register values from the stack.

271


Embedded Systems Development and Labs; The English Edition

(4) OSTickISR() function is a time tick function generated by the timer interrupt. OSTickISR() is responsible for saving the microprocessor registers and recovering the registers when the task switching is finished.

3) Porting OS_CPU_C.C File

The third step of porting the uC/OS-II kernel is to port the OS_CPU_C.C file. There are 6 functions in this file that need to be ported.

OSTaskStkInit()

OSTaskCreateHook()

OSTaskDelHook()

OSTaskSwHook()

OSTaskStatHook()

OSTaskTickHook()

The last 5 functions are called hook functions and are used mainly for extending the functions of uC/OS-II. Note that these functions don’t have to contain code.

The only function that really needs to be ported is the OSTTaskStkInit(). This function is called when the task is created. This function is responsible for initializing the stack architecture for tasks. This function can be in the same form for porting to most of the ARM processors.

Please refer to the following sample programs.

7.1.5 Sample Programs

1. OSStartHighRdy

OSStartHighRdy:

BL OSTaskSwHook

MOV R0,#1

LDR R1,=OSRunning

STRB R0,[R1]

LDR

r4, addr_OSTCBCur

@ Get current task TCB address

LDR

r5, addr_OSTCBHighRdy

@ Get highest priority task TCB address

LDR

r5, [r5]

@ get stack pointer

LDR

sp, [r5]

@ switch to the new stack

STR r5, [r4]

@ set new current task TCB address

LDMFD sp!, {r4}

@ YYY

MSR

SPSR_cxsf, r4

LDMFD

sp!, {r4}

@ get new state from top of the stack

MSR

CPSR_cxsf, r4

@ CPSR should be SVC32Mode

LDMFD sp!, {r0-r12, lr, pc }@ start the new task

2. OS_Task_Sw

OS_TASK_SW:

272


Embedded Systems Development and Labs; The English Edition

MRS

lr, SPSR

AND

lr, lr, #0xFFFFFFE0

ORR

lr, lr, #0x13

MSR

CPSR_cxsf, lr

#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#Now

Supervisor mode

#;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

STR

r12, [sp, #-8]

@ saved r12

LDR

r12, SAVED_LR @LDR r12, [pc, #SAVED_LR-.-8]

STMFD

sp!, {r12}

@ r12 that PC of task

SUB

sp, sp, #4

@ inclease stack point

LDMIA

sp!, {r12}

@ restore r12

STMFD

sp!, {lr}

@ save lr

STMFD

sp!, {r0-r12} @ save register file and ret address

MRS

r4, CPSR

STMFD

sp!, {r4}

@ save current PSR

MRS

r4, SPSR

@ YYY+

STMFD

sp!, {r4}

@ YYY+ save SPSR

# OSPrioCur = OSPrioHighRdy

LDR

r4, addr_OSPrioCur

LDR

r5, addr_OSPrioHighRdy

LDRB

r6, [r5]

STRB

r6, [r4]

# Get current task TCB address

LDR

r4, addr_OSTCBCur

LDR

r5, [r4]

STR sp, [r5]

@ store sp in preempted tasks's TCB

# Get highest priority task TCB address

LDR

r6, addr_OSTCBHighRdy

LDR

r6, [r6]

LDR

sp, [r6]

@ get new task's stack pointer

# OSTCBCur = OSTCBHighRdy

STR r6, [r4]

@ set new current task TCB address

LDMFD sp!, {r4}

@ YYY+

274