+ All Categories
Home > Documents > RTOS Working

RTOS Working

Date post: 20-Jul-2016
Category:
Upload: nguyen-quan
View: 35 times
Download: 7 times
Share this document with a friend
34
How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do Introduction to Free-RTOS Deepak D’Souza Department of Computer Science and Automation Indian Institute of Science, Bangalore. 23 August 2011
Transcript
Page 1: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Introduction to Free-RTOS

Deepak D’Souza

Department of Computer Science and AutomationIndian Institute of Science, Bangalore.

23 August 2011

Page 2: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Outline

1 How a program runs on the ARM processor

2 How an application uses RTOS

3 What RTOS is expected to do

Page 3: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

ARM processor architecture

R0

R1

(SP) R13

(LR) R14

(PC) R15

CPSR

SPSR

IVT Code

ISR Code

App Code

Stack/HeapRegisters

Memory

Page 4: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

ARM processor architecture: SP and PC Registers

R0

R1

(SP) R13

(LR) R14

(PC) R15

CPSR

SPSR

IVT Code

ISR Code

App Code

Stack/HeapRegisters

Memory

Normal execution: fetch instruction pointed to by PC and execute.

Page 5: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Using the stack: calling foo

R0

R1

(SP) R13

(LR) R14

(PC) R15

CPSR

SPSR

IVT Code

ISR Code

Stack/HeapRegisters

Memory

BL foo

1020

xy

main

foo

main pushes arguments to foo on the stack before calling foo.

Page 6: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Using the stack: in foo

R0

R1

(SP) R13

(LR) R14

(PC) R15

CPSR

SPSR

IVT Code

ISR Code

Stack/HeapRegisters

Memory

BL foo

1020

xy

main

foo

ab

00010004

foo creates space for local vars, saves regs it uses; accesses args.

Page 7: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Using the stack: returning from foo

00010004

R0

R1

(SP) R13

(LR) R14

(PC) R15

CPSR

SPSR

IVT Code

ISR Code

Stack/HeapRegisters

Memory

BL foo

1020

xy

main

foo

30

foo puts return value in register, restores regs and stack pointer.

Page 8: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

How interrupts are handled

Save currentPC in LR’,CPSR inCPSR’.

Change to“super” mode.

Disable lowerpriorityinterrupts.

Branch toappropriateIVT entry.

R0

R1

(SP) R13

(LR) R14

(PC) R15

CPSR

SPSR

IVT Code

ISR Code

App Code

Stack/HeapRegisters

Memory

Page 9: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Current Program Status Register (CPSR)

Arithmetic instructions set bits [31:27] (overflow, carry, etcconditions).

Normal programs execute in “User” mode.

Restricted access to memory and co-processor functions.Cannot change “mode” (except by calling SWI).

Page 10: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Interrupts (Exceptions)

Various kinds of interrupts may be generated (hardware eg.timer, software, instruction exceptions).

Corresponding mode bits are set in CPSR[4:0] (Eg. 10011 forSWI).

Exception Resulting Mode IVT address

Priority

Reset Supervisor 0x00000000

1

Undefined Inst. Undef 0x00000004

6

Software Interrupt Supervisor 0x00000008

6

Abort prefetch Abort 0x0000000C

2

Abort data Abort 0x00000010

2

IRQ IRQ 0x00000018

4

FIQ FIQ 0x0000001C

3

Page 11: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Interrupts (Exceptions)

Various kinds of interrupts may be generated (hardware eg.timer, software, instruction exceptions).

Corresponding mode bits are set in CPSR[4:0] (Eg. 10011 forSWI).

Exception Resulting Mode IVT address PriorityReset Supervisor 0x00000000 1Undefined Inst. Undef 0x00000004 6Software Interrupt Supervisor 0x00000008 6Abort prefetch Abort 0x0000000C 2Abort data Abort 0x00000010 2IRQ IRQ 0x00000018 4FIQ FIQ 0x0000001C 3

Page 12: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Disabling interrupts

Setting ‘I’ bit (CPSR[7]) disables IRQ interrupts.

Setting ‘F’ bit (CPSR[6]) disables FIQ interrupts.

Page 13: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

What the ARM processor does on an interrupt

When interrupt (exception) occurs do [ARM]

R14_<exception_mode> = return link

SPSR_<exception_mode> = CPSR

CPSR[4:0] = exception mode number

CPSR[5] = 0 /* Execute in ARM state */

if <exception_mode> == Reset or FIQ then

CPSR[6] = 1 /* Disable fast interrupts */

CPSR[7] = 1 /* Disable normal interrupts */

if <exception_mode> != UNDEF or SWI then

CPSR[8] = 1 /* Disable imprecise aborts (v6 only) */

CPSR[9] = CP15_reg1_EEbit /* Endianness on exception entry */

PC = exception vector address

ARM has many instructions that facilitate a quick save/restore forentering/exiting an ISR.

Page 14: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

What the ARM processor does on an s/w interrupt

When SWI instruction is executed do [ARM]

R14_svc = address of next instruction after the SWI instruction

SPSR_svc = CPSR

CPSR[4:0] = 0b10011 /* Enter Supervisor mode */

CPSR[5] = 0 /* Execute in ARM state */

CPSR[7] = 1 /* Disable normal interrupts */

CPSR[9] = CP15_reg1_EEbit /* Endianness on exception entry */

if high vectors configured then

PC = 0xFFFF0008

else

PC = 0x00000008

To return after handling: R14 = R14svc , CPSR = SPSRsvc , returnto the instruction following SWI.

To return after handling [ARM]

MOVS PC,R14

Page 15: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

What RTOS provides the programmer

Ways to:

Create and manage multiple tasks.

Schedule tasks based on priority-based pre-emption.

Let tasks communicate (via message queues, semaphores,etc).

Let tasks delay and timeout on blocking operations.

Page 16: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Example application that uses RTOS

Sample RTOS application

int main(void){

xTaskCreate(foo, "Task 1", 1000, NULL, 1, NULL);

xTaskCreate(bar, "Task 2", 1000, NULL, 2, NULL);

vTaskStartScheduler();

}

void foo(void* params){

for(;;);

}

void bar(void* params){

for(;;){

vTaskDelay(2);

}

}

Page 17: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Task execution in example application

Task 1

Task 2

Time (tick interrupts)

t1 t2 t3 t4 t5

Page 18: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Example application: execution sequence

mainvtaskCreate(1)vtaskCreate(2)vtaskStartScheduler()

create Idle tasktask2

vtaskdelay()yield()

task1timer interrupttimer interrupt

task2

ISR Code

Memory

R0

R1

(SP) R13

(LR) R14

(PC) R15

CPSR

SPSR

Registers

Scheduler

startscheduler

taskcreate

App Codemain

foo

Stack/Heap

IVT Code branch instr

TCB2

Stack2

TCB1

Stack1

bar

Page 19: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Example 2: using RTOS inter-task communication

RTOS application (Example 2)int main(void){

xTaskCreate(vTask1,"Task1",configMINIMAL_STACK_SIZE,NULL,2,&xTask1Handle);

xTaskCreate(vTask2,"Task2",configMINIMAL_STACK_SIZE,NULL,4,&xTask2Handle);

vTaskStartScheduler();

}

void vTask1(void *pvParameters){

xQueueReceive(xQueue,&lGlobalData,0);

vTaskPrioritySet(NULL,0);

for(;;);

}

void vTask2(void *pvParameters){

long lData = 1;

xQueue = xQueueCreate(2,sizeof(long));

xQueueSendToBack(xQueue,&lData,0);

lData = 2;

xQueueSendToBack(xQueue,&lData,0);

lData = 3;

xQueueSendToBack(xQueue,&lData,1000);

vTaskPrioritySet(NULL,0);

for(;;);

}

Page 20: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Task execution in example application

Time (tick interrupts)

Task 2

Task 1

t1 t2

Page 21: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Example 2: Data-structures maintained by RTOS

Initially:

1

0

2

NULL

NULL

currentTask

pxReadyTasksLists

NULLn−1

3

4

NULLdelayedTasks

NULL

NULL

NULL

Page 22: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Example 2: Data-structures maintained by RTOS

After TaskCreate(1):

Task1

1

0

2

NULL

NULL

currentTask

pxReadyTasksLists

NULLn−1

3

4

NULLdelayedTasks

NULL

NULL

Page 23: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Example 2: Data-structures maintained by RTOS

After TaskCreate(2):

Task1

Task2

1

0

2

NULL

NULL

currentTask

pxReadyTasksLists

NULLn−1

3

4

NULLdelayedTasks

NULL

Page 24: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Example 2: Data-structures maintained by RTOS

After TaskStartScheduler():

Task1

Task2

idle

1

0

2

NULL

currentTask

pxReadyTasksLists

NULLn−1

3

4

NULLdelayedTasks

NULL

NULL

Page 25: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Example 2: Data-structures maintained by RTOS

After QueueCreate:

Task1

Task2

idle

NULL

1

0

2

NULL

currentTask

pxReadyTasksLists

n−1

3

4

NULLdelayedTasks

NULL

xQueue

waitingToSnd

waitingToRcv

NULL

NULL

NULL

Page 26: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Example 2: Data-structures maintained by RTOS

After QueueSend(1):

Task1

Task2

idle

NULL

1

1

0

2

NULL

currentTask

pxReadyTasksLists

n−1

3

4

NULLdelayedTasks

NULL

xQueue

waitingToSnd

waitingToRcv

NULL

NULL

NULL

Page 27: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Example 2: Data-structures maintained by RTOS

After QueueSend(2):

Task1

Task2

idle

NULL

1

0

2

NULL

currentTask

pxReadyTasksLists

n−1

3

4

NULLdelayedTasks

NULL

xQueue

waitingToSnd

waitingToRcv

NULL

NULL

NULL

21

Page 28: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Example 2: Data-structures maintained by RTOS

After QueueSend(3)b:

Task2

Task2

Task1

idle

NULL

21

1

0

2

NULL

currentTask

pxReadyTasksLists

n−1

3

4

delayedTasks

NULL

xQueue

waitingToSnd

waitingToRcv NULL

NULL

NULL

Page 29: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Example 2: Data-structures maintained by RTOS

After QueueReceive(1):

Task2

Task1

idle

NULL

1

0

2

NULL

currentTask

pxReadyTasksLists

n−1

3

4

delayedTasks

NULL

xQueue

waitingToSnd

waitingToRcv

NULL

NULL

NULL

NULL

2

Page 30: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Example 2: Data-structures maintained by RTOS

After QueueReceive(1):

Task1

Task2

idle

NULL

1

0

2

NULL

currentTask

pxReadyTasksLists

n−1

3

4

delayedTasks

NULL

xQueue

waitingToSnd

waitingToRcv

NULL

NULL

NULL

NULL

2

Page 31: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Example 2: Data-structures maintained by RTOS

After QueueSend(3)e :

Task1

Task2

idle

NULL

1

0

2

NULL

currentTask

pxReadyTasksLists

n−1

3

4

delayedTasks

NULL

xQueue

waitingToSnd

waitingToRcv

NULL

NULL

NULL

NULL

2 3

Page 32: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Main functionality of RTOS

What do we expect RTOS to do?

Implement its stated scheduling policy (fixed prioritypre-emptive scheduling).

Trap SWI interrupts

Find highest priority ready task to run.Save context of yielding task.Restore context of new task.

Trap timer IRQ interrupt

Update tickcount,Check delayed tasks, and move to ready if required,Switch context if required.

Provide Application Programmer Interface (API’s) for:

Task creation, deletion, set priority, etc.Inter-task communication through queues, semaphores, andmutexes.Heap memory management (malloc, free).

Page 33: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

Main functionality of RTOS

What do we expect RTOS to do?

Implement its stated scheduling policy (fixed prioritypre-emptive scheduling).

Trap SWI interrupts

Find highest priority ready task to run.Save context of yielding task.Restore context of new task.

Trap timer IRQ interrupt

Update tickcount,Check delayed tasks, and move to ready if required,Switch context if required.

Provide Application Programmer Interface (API’s) for:

Task creation, deletion, set priority, etc.Inter-task communication through queues, semaphores, andmutexes.Heap memory management (malloc, free).

Page 34: RTOS Working

How a program runs on the ARM processor How an application uses RTOS What RTOS is expected to do

References

ARM Architecture Reference Manual

RTOS User Guide, http://www.freertos.org.


Recommended