real-time executive (rex) user guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1...

54
Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted Distribution: This document contains critical information about QUALCOMM products and may not be distributed to anyone that is not an employee of QUALCOMM without the approval of Configuration Management. QUALCOMM Incorporated 5775 Morehouse Dr. San Diego, CA 92121-1714 U.S.A.

Upload: others

Post on 26-May-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX)User Guide

80-24897-1 Rev. CSeptember 6, 2001

Restricted Distribution: This document contains critical information about QUALCOMM products and may not be distributed to anyone that is not an employee of QUALCOMM without the approval of Configuration Management.

QUALCOMM Incorporated5775 Morehouse Dr.

San Diego, CA 92121-1714U.S.A.

Page 2: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

ii QUALCOMM Proprietary 80-24897-1 Rev. C

QUALCOMM Incorporated 5775 Morehouse Dr.

San Diego, CA 92121-1714 U.S.A.

Copyright © 1999, 2001 QUALCOMM Incorporated. All rights reserved. Printed in the United States of America.

All data and information contained in or disclosed by this document is confidential and proprietary information of QUALCOMM Incorporated, and all rights therein are expressly reserved. By accepting this material, the recipient agrees that this material and the information contained therein is held in confidence and in trust and will not be used, copied, reproduced in whole or in part, nor its contents revealed in any manner to others without the express written permission of QUALCOMM Incorporated.

Export of this technology may be controlled by the United States Government. Diversion contrary to U.S. law prohibited.

Restricted Distribution: This document contains critical information about QUALCOMM products and may not be distributed to anyone that is not an employee of QUALCOMM without the approval of Configuration Management.

QUALCOMM is a registered trademark and registered service mark of QUALCOMM Incorporated. Other product and brand names may be trademarks or registered trademarks of their respective owners.

Information in this document is preliminary and subject to change and does not represent a commitment on the part of QUALCOMM Incorporated.

Real-Time Executive (REX) User Guide

80-24897-1 Rev. C

September 6, 2001

Page 3: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

80-24897-1 Rev. C QUALCOMM Proprietary iii

Contents

1 Introduction........................................................................................ 1-1 1.1 Purpose ................................................................................................................1-1 1.2 Scope ...................................................................................................................1-1 1.3 Organization ........................................................................................................1-1 1.4 Conventions.........................................................................................................1-1 1.5 Revision history...................................................................................................1-2 1.6 References ...........................................................................................................1-2 1.7 Technical assistance ............................................................................................1-2 1.8 Terms and definition ...........................................................................................1-3

2 Overview of REX................................................................................ 2-1 2.1 Tasks....................................................................................................................2-1 2.2 Stacks ..................................................................................................................2-1 2.3 Priorities and scheduling .....................................................................................2-2 2.4 Interrupts .............................................................................................................2-2 2.5 Mutual exclusion .................................................................................................2-2 2.6 Suspending with interrupts disabled....................................................................2-2 2.7 Signals .................................................................................................................2-3 2.8 Timers..................................................................................................................2-3 2.9 Extended interface ...............................................................................................2-3

3 Tasks .................................................................................................. 3-1 3.1 Task creation .......................................................................................................3-1

3.1.1 Scheduler ................................................................................................3-2 3.1.2 Task specifics for ARM platforms .........................................................3-3

4 Interrupt Handling ............................................................................. 4-1

5 Signals and Timers............................................................................ 5-1 5.1 Signals .................................................................................................................5-1 5.2 Timers..................................................................................................................5-2

Page 4: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Contents Real-Time Executive (REX)

iv QUALCOMM Proprietary 80-24897-1 Rev. C

6 Features.............................................................................................. 6-1

7 Inside REX .......................................................................................... 7-1 7.1 Task control block............................................................................................... 7-1 7.2 Current task......................................................................................................... 7-1 7.3 Best task.............................................................................................................. 7-1 7.4 Task list............................................................................................................... 7-2

8 Programming Reference ................................................................... 8-1 8.1 rex_init( ) ............................................................................................................ 8-2 8.2 rex_def_task( ) .................................................................................................... 8-4 8.3 rex_set_sigs( ) ..................................................................................................... 8-6 8.4 rex_clr_sigs( ) ..................................................................................................... 8-7 8.5 rex_get_sigs( )..................................................................................................... 8-8 8.6 rex_wait( )........................................................................................................... 8-9 8.7 rex_def_timer( ) ................................................................................................ 8-10 8.8 rex_set_timer( ) ................................................................................................. 8-11 8.9 rex_get_timer( ) ................................................................................................ 8-12 8.10 rex_clr_timer( ) ............................................................................................... 8-13 8.11 rex_pause_timer( ) .......................................................................................... 8-14 8.12 rex_resume_timer( )........................................................................................ 8-15 8.13 rex_tick( )........................................................................................................ 8-16 8.14 rex_timed_wait( )............................................................................................ 8-17 8.15 rex_self( ) ........................................................................................................ 8-18 8.16 rex_get_pri( ) .................................................................................................. 8-19 8.17 rex_set_pri( )................................................................................................... 8-20 8.18 rex_task_pri( )................................................................................................. 8-21 8.19 rex_set_interrupt_vector( ).............................................................................. 8-22 8.20 rex_enable_interrupt( ).................................................................................... 8-23 8.21 rex_disable_interrupt( )................................................................................... 8-24 8.22 rex_init_crit_sect( ) ......................................................................................... 8-25 8.23 rex_enter_crit_sect( ) ...................................................................................... 8-26 8.24 rex_leave_crit_sect( )...................................................................................... 8-27

Page 5: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX) Contents

80-24897-1 Rev. C QUALCOMM Proprietary v

Figures

Figure 3–1 Context frame of a suspended task on ARM................................................3-3

Tables Table 1-1 Revision history..............................................................................................1-2 Table 1-2 Reference documents and standards...............................................................1-2

Page 6: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Contents Real-Time Executive (REX)

vi QUALCOMM Proprietary 80-24897-1 Rev. C

Page 7: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

80-24897-1 Rev. C QUALCOMM Proprietary 1-1

1 Introduction

1.1 Purpose 1

This document describes the features and operation of the Real-Time Executive (REX) operating 2

system for ARM®-processor-based systems. 3

REX is a simple, efficient, preemptible, multitasking real-time operating system (RTOS) designed for 4

small embedded systems. It was initially designed for use on the Intel® 80186 processor. 5

Subsequently, it was ported to the ARM microprocessor. This document provides a guide to REX. 6

See Chapter 8 for a programming reference. 7

1.2 Scope 8

This guide is intended for engineers who need to write applications to run in REX on an ARM 9

processor. 10

1.3 Organization 11

This document is organized as follows: 12

Chapter 2 – provides an overview of REX 13

Chapters 3 through 7 – provide a more detailed look at REX’s features and inner workings 14

Chapter 8 – provides a programming reference 15

1.4 Conventions 16

Function declarations, function names, type declarations, and code samples appear in a different font. 17

For example: #include 18

Code variables appear in angle brackets. For example: <number> 19

20

Page 8: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Introduction Real-Time Executive (REX)

1-2 QUALCOMM Proprietary 80-24897-1 Rev. C

1.5 Revision history 1

The revision history for this document is shown in Table 1-1. 2

Table 1-1 Revision history 3

Version Date Description

Rev. — Jan 1999 Initial release Rev. A Mar 1999 Updated content Rev. B Jun 2001 Deleted obsolete 80186-related content. Updated REX API. Updated section

on interrupt handling. Rev. C Sep 2001 Deleted profiling information.

4

1.6 References 5

Reference documents, which may include QUALCOMM, standards, and resource documents, are 6

listed in Table 1-2. 7

Table 1-2 Reference documents and standards 8

Ref. Document

QUALCOMM

1 REX Portation User Guide 80-24880-1 X1 Mar 1999 2 REX++ - REX Extensions Users Guide 80-V3083-1 X1 Apr 2001

9

1.7 Technical assistance 10

For assistance or clarification on information in this guide, email QUALCOMM CDMA 11

Technologies at [email protected]. 12

13

Page 9: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX) Introduction

80-24897-1 Rev. C QUALCOMM Proprietary 1-3

1.8 Terms and definition 1

The following terms are used throughout this document: 2

APCS ARM procedure call standard. In order to support flexible mixing of routines generated by different compilers and written in assembly language, ARM Limited has defined this set of rules for procedure entry and exit, and for register usage.

API Application programming interface

ARM Advanced RISC Machines Limited (ARM)®. The manufacturer of the ARM7TDMI processor. In common use the company name, ARM, is also used as the name of the processor.

ARM7TDMI Official name of the ARM processor

CPSR Current Program Status Register. This register holds the flags, interrupt control bits, and the current operating mode of the processor.

critical section Block of code that accesses a shared resource

FIQ One of two interrupts used by the ARM processor. FIQ is also used to denote the mode of the processor when the FIQ interrupt is being serviced.

IRQ One of two interrupts used by the ARM processor. IRQ is also used to denote the mode of the processor when the IRQ interrupt is being serviced.

ISR Interrupt service routine, also called interrupt trampoline function

Idle Task This is the lowest priority task in the system. It has a priority of zero and runs when there is no other task ready to run. It sits in a tight loop doing nothing and loses control of the CPU only due to an interrupt.

PC Program counter register

PSR Program status register

REX QUALCOMM’s real-time executive operating system

RTOS Real-time operating system

SPSR Saved program status register. This is banked for each processor mode. The SPSR for a mode holds the CPSR value at the time the processor changed to that mode.

TCB Task control block. This is a REX internal data structure that holds task-specific information.

timer block Another name for a timer

trampoline function Function outside REX that services interrupts, also called ISR 3

4

Page 10: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Introduction Real-Time Executive (REX)

1-4 QUALCOMM Proprietary 80-24897-1 Rev. C

1

Page 11: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

80-24897-1 Rev. C QUALCOMM Proprietary 2-1

2 Overview of REX

REX is a preemptive, multitasking RTOS that provides APIs for task control, task synchronization, 1

mutual exclusion, timers, and interrupt control. All of its functions are executed in the context of the 2

calling task. 3

2.1 Tasks 4

REX treats tasks as independently handled entities, each having its own stack and priority that 5

collectively constitute the context of the task. Each task has an associated data structure, called a task 6

control block (TCB), which REX uses to keep track of the task’s context. 7

REX allows an arbitrary number of tasks to be created dynamically at any time during execution. 8

Realistically, however, REX’s performance degrades slightly with each added task since it must 9

traverse a longer list as more tasks are created. Care must be taken to keep the number of tasks to a 10

minimum. 11

Ultimately, the task load that REX can support is dependent upon the processor chosen, the processor 12

clock speed, and the interrupt characteristics of the particular system. 13

2.2 Stacks 14

As mentioned above, each task has its own stack that is active whenever the task is running. When a 15

task is suspended, its context is saved in a context frame on top of its stack, and the context frame’s 16

stack pointer is saved in the task’s TCB. An example of a suspended task is one that is blocked, 17

waiting for a signal, or waiting for an interrupt service. 18

When the task is again selected for running, the stack pointer is restored from the task’s TCB, the 19

task’s context frame is popped off its stack, and task execution resumes where it left off. The process 20

of task switching is virtually invisible to tasks. 21

22

Page 12: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Overview of REX Real-Time Executive (REX)

2-2 QUALCOMM Proprietary 80-24897-1 Rev. C

2.3 Priorities and scheduling 1

Each task has a priority associated with it that is stored in its TCB. Priority can be any nonzero, 2

positive 32-bit integer with lower values indicating lower priorities. REX reserves priority zero for 3

use with the Idle task. Older versions of REX had the restriction that each task must be assigned a 4

unique priority. This restriction has now been eliminated. 5

When scheduling, REX always chooses the highest priority ready task—the highest priority task not 6

waiting on some event. If this task is not unique, REX may choose any one of the highest priority 7

tasks. The chosen task will continue to execute until it voluntarily suspends or until an interrupt 8

occurs that reactivates a higher priority task. 9

When the wait condition for a suspended task is satisfied, that task becomes ready. In cases where all 10

tasks are suspended, the Idle task is executed. 11

REX also provides a mechanism by which a task can dynamically raise or lower its own or another 12

task’s priority. 13

2.4 Interrupts 14

REX implements a preemptible kernel. On returning from an interrupt, control is passed to the highest 15

priority ready task and not necessarily to the task that was interrupted. 16

2.5 Mutual exclusion 17

When two tasks share a common resource, a method may be required to make their accesses to that 18

resource mutually exclusive. The block of code that accesses a shared resource is called a critical 19

section. 20

Locking interrupts is one way that REX facilitates critical sections. REX also provides a less drastic 21

method to lock-shared resources by exporting functions that can be used to lock and free common 22

resources. 23

2.6 Suspending with interrupts disabled 24

Disabling interrupts is valid for the current task only. If a task suspends itself with interrupts disabled, 25

the interrupt state is saved with the task, which will be restored when the task becomes active again. 26

Interrupts are then potentially enabled for the next active task. This behavior may change in the 27

future, so the programmer should not depend on its availability. 28

29

Page 13: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX) Overview of REX

80-24897-1 Rev. C QUALCOMM Proprietary 2-3

2.7 Signals 1

A set of general-purpose signals is associated with each task. These general-purpose signals are kept 2

as part of the task’s context in its TCB, and they are used to notify the task that some kind of event 3

has occurred. A task’s signals may be set or cleared by any task or interrupt handler. 4

A task can poll the current state of its signals at any time. Moreover, a task can suspend its own 5

execution pending the setting of one or more of its signals. However, it can neither poll nor wait for 6

signals of another task. 7

Important points to keep in mind about signals are: 8

Signals are general-purpose flags whose meanings may be defined in any way by the user. 9

A task may optionally suspend execution pending the setting of particular signals, presumably by 10

another task or an interrupt handler. The emphasis here should be optionally, since setting a 11

signal for a task that is not suspended on that signal does not directly affect the execution state of 12

that task. 13

2.8 Timers 14

Timers are used to perform software interval timing. When using a timer, a task specifies the interval 15

to be timed and a signal-mask to be set when the timer expires. The task may then poll to determine 16

when the signal mask is set, or it may suspend until the signal mask is set. 17

Timers may be used in conjunction with other events that set signals, making it possible to perform 18

timeout detection for those events. REX maintains a timer list that contains all active (that is, 19

unexpired) timers. An arbitrary number of timers may be created although, as with tasks, performance 20

deteriorates slightly with each active timer since a longer list must be traversed. There is no overhead 21

cost for timers that have expired. 22

2.9 Extended interface 23

REX optionally provides an extended interface that exports functionality such as asynchronous 24

procedure calls, deferred procedure calls, and dynamic memory management. Refer to the REX++ – 25

REX Extensions Users Guide (80-V3083-1) for details. 26

27

Page 14: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Overview of REX Real-Time Executive (REX)

2-4 QUALCOMM Proprietary 80-24897-1 Rev. C

1

Page 15: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

80-24897-1 Rev. C QUALCOMM Proprietary 3-1

3 Tasks

This chapter presents a detailed discussion of REX’s scheduling and context-switching mechanism. 1

3.1 Task creation 2

Tasks are created by rex_def_task(). REX does not allocate space for the task stack or its TCB. 3

These have to be provided by the calling function. But REX does require that its users avoid direct 4

manipulation of the TCB structure. 5

The task-creating function, rex_def_task(), does the following: 6

1. Handcrafts a context frame into the stack. 7

2. Sets up the TCB. 8

3. Places this task in the task list. 9

4. Calls the scheduler. 10

If the newly created task has a higher priority than the current task, then it gets control. Otherwise, the 11

scheduler returns without performing a task switch. This task will get its chance to run when it 12

becomes the highest priority ready task. 13

The initial stack frame is set up such that when a task is first given control, it always starts at 14

rex_task_preamble, which in turn calls the task entry point, as specified in rex_def_task(). 15

A task is never supposed to return. So rex_task_preamble() has a call to rex_kill_task() 16

after the call to the task entry function. This will kill the task if there is an erroneous return. 17

18

Page 16: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Tasks Real-Time Executive (REX)

3-2 QUALCOMM Proprietary 80-24897-1 Rev. C

3.1.1 Scheduler 1

The scheduler is implemented as a function, rex_sched(), which should not be called by 2

applications directly. It is internal to REX and should remain so. It is executed with interrupts 3

disabled. The calling function must set the global variable rex_best_task before calling 4

rex_sched(). This variable should be set to point to the TCB of the highest priority ready task in the 5

system. 6

The basic algorithm of the scheduler is: 7

1. The scheduler checks to see if rex_best_task is the same as the current task, 8

rex_curr_task. 9

2. If rex_best_task and rex_curr_task are the same, the scheduler simply returns. 10

3. If rex_best_task and rex_curr_task are different, rex_curr_task is assigned 11

rex_best_task. Now, the scheduler checks to see if the system is currently servicing interrupts. 12

If it is servicing interrupts, task switch is not performed and the scheduler returns. 13

4. Once the above tests pass, the scheduler performs the context switch. This involves pushing the 14

current context onto the stack and storing the resultant stack pointer in that task’s TCB. 15

Thereafter, the stack pointer is restored from the new rex_curr_task TCB, and the context is 16

restored from that stack. 17

18

Page 17: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX) Tasks

80-24897-1 Rev. C QUALCOMM Proprietary 3-3

3.1.2 Task specifics for ARM platforms 1

ARM is a 32-bit processor and follows the flat memory model. The stack pointer in this case is only 2

one field, sp, and is stored in the TCB. 3

Context frame 4

The context frame contains registers r0-r12, lr, pc, and CPSR. The stack of a suspended task is 5

shown in Figure 3–1. 6

Program StatusRegister

r0-r12

Link Register

ReturnAddress

Application StackFrame

Top of Stack(stored in TCB)

7

Figure 3–1 Context frame of a suspended task on ARM 8

Remember also that rex_sched() runs with interrupts disabled. On the ARM, rex_sched() has 9

to do these two actions itself. The Program Status Register stored in the context is the value of CPSR 10

on entry into this function. Thus, the interrupt state is being stored with the task context. 11

The context is restored by these actions: 12

1. Stack pointer is loaded from the TCB that is pointed to by rex_curr_task. 13

2. The saved PSR value is copied into the SPSR register. 14

3. Then an ldmfd with a ^ (caret) does the following: 15

a. Restores registers r0-r12, the link register, and the program counter. 16

b. Switches the mode back to Thumb mode. 17

c. Returns to the task. 18

19

NOTE The caret is required with ldmfd because the PC and CPSR need to be updated at the same time. 20

Page 18: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Tasks Real-Time Executive (REX)

3-4 QUALCOMM Proprietary 80-24897-1 Rev. C

1

Page 19: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

80-24897-1 Rev. C QUALCOMM Proprietary 4-1

4 Interrupt Handling

The actions performed by REX on entering and leaving an interrupt are discussed in this chapter. 1

The ARM has two interrupts, FIQ and IRQ. The discussion in this chapter pertains to handling the 2

IRQ interrupt on ARM. The handling of the FIQ interrupt is much simpler, since currently context 3

switches are not allowed in a handler that services an FIQ interrupt. 4

A function programmed into the exception vector table—IRQ_Handler(), written in ARM 5

assembly—gets control as soon as the interrupt is asserted. This function then invokes the interrupt 6

trampoline function. The trampoline function, which is defined outside of REX, typically detects 7

which interrupt source has asserted an interrupt and calls the corresponding service routine. It is 8

installed using rex_set_interrupt_vector() (see Section 8.19 for a detailed description). When 9

an interrupt occurs, the ARM processor changes mode to IRQ and switches to a different bank of 10

registers. The following actions are performed in IRQ_Handler: 11

1. Some registers will be trashed by the interrupt trampoline function. Save the registers that might 12

be overwritten by the trampoline function on the interrupt stack. Since this function is a C routine, 13

the registers saved are r0-r3, r10, r12, and r14. Save the SPSR as well since this register 14

may be trashed by a subsequent nested interrupt. 15

2. Increment the interrupt nesting level counter. 16

3. Switch to System mode and save the System mode link register, which gets overwritten later. 17

4. Call the registered trampoline function. 18

5. On return from the trampoline function, restore the System mode link register and switch back to 19

IRQ mode. 20

6. Decrement the interrupt nesting level counter. 21

7. Determine whether a task swap is required. A swap is needed only if the nesting level counter is 22

zero and rex_best_task is not the same as rex_curr_task. 23

8. If a task swap is required, do the following: 24

a. Restore the SPSR and saved registers from the IRQ mode stack. 25

b. Switch to Supervisor mode. 26

c. Save the context of the current task on its stack. 27

Page 20: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Interrupt Handling Real-Time Executive (REX)

4-2 QUALCOMM Proprietary 80-24897-1 Rev. C

d. Save the Supervisor mode stack pointer in the task’s TCB. 1

e. Set rex_curr_task to rex_best_task. 2

f. Restore the context of rex_curr_task. 3

9. If a task swap is not required, restore the SPSR from the interrupt stack, restore the other registers 4

saved on the interrupt stack, and return, switching back to Supervisor mode in the process. 5

6

NOTE In some versions of IRQ_Handler, there is no switch to System mode before calling the trampoline function. Versions of IRQ_Handler that do not support nested interrupts do not keep track of the interrupt nesting level.

7

This sequence of actions achieves preemption since control is returned to the highest priority ready 8

task rather than the task that was running when the interrupt occurred. 9

10

Page 21: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

80-24897-1 Rev. C QUALCOMM Proprietary 5-1

5 Signals and Timers

Signals and timers are mechanisms through which REX tasks can interact and perform interval 1

timing. 2

5.1 Signals 3

REX provides signals as an intertask communication mechanism. A set of signals is associated with 4

each task. It is possible for any task to read, set, and clear any other task’s signals using 5

rex_get_sigs(), rex_set_sigs(), and rex_clr_sigs(). 6

A task can wait on any subset of its own signals to be set using rex_wait(). It cannot wait on some 7

other task’s signals. The OR semantics are used, which means that the task will be woken up when 8

any of the flags in its wait pattern is set. If a task requests to wait when a signal from its wait mask is 9

already set, the wait will return immediately without suspending the task. 10

Signals are implemented as integers. The ARM provides a 32-bit mask, which means that 32 different 11

signals can be set simultaneously for a task. 12

13

Page 22: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Signals and Timers Real-Time Executive (REX)

5-2 QUALCOMM Proprietary 80-24897-1 Rev. C

5.2 Timers 1

A task can define a new timer using rex_def_timer(). Once defined, a timer (also called a timer 2

block) can be used by tasks to perform software interval timing: 3

Use rex_set_timer() to start a timer. 4

Use rex_get_timer() to read the time remaining in a timer. A remaining time of zero implies 5

that the timer is not active. 6

rex_clr_timer() clears a timer. The timer becomes inactive and its count is set to zero. 7

rex_pause_timer() pauses a timer. The timer becomes inactive, but its current count is 8

retained. 9

Use rex_resume_timer() to restart a paused timer. 10

A task can use rex_timed_wait() to do timeout detection by waiting for a signal with a timer. 11

Once the timer expires, it sets the signals specified in its block. 12

13

NOTE It is important to note that the task should be made to wait on a superset of the signals that the timer is going to set. This care is required because the timer block is usually initialized at the beginning of the program, when its signal pattern is set. While using rex_timed_wait(), a task specifies the signals it wants to wait on as a separate parameter, which, if the programmer is not careful, may not have anything in common with the signals in the timer block.

14

Time is specified in milliseconds. The minimum resolution possible is 1 ms. The actual resolution 15

depends upon the platform and is equal to the number of milliseconds per clock-tick interrupt. 16

Time is advanced at each clock tick by the function rex_tick(), which must be called with the 17

number of milliseconds per tick by the clock-tick ISR. The signals specified in the timer block are 18

then set in the specified TCB when the timer expires. 19

REX maintains a list of unexpired timers. Once a timer expires, however, it is removed from that list 20

and REX has no further knowledge of the structure’s existence. In other words, timer structures are 21

allocated and maintained only by tasks. 22

23

NOTE rex_def_timer() can be called as many times as desired for a timer block. However, users must be very careful not to call this function on an active timer. Doing so will corrupt the timer list as well as a portion of the allocated memory—an action which is fatal to task execution.

24

25

Page 23: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

80-24897-1 Rev. C QUALCOMM Proprietary 6-1

6 Features

A brief explanation of REX features will be provided in this chapter in a future revision of this 1

document. 2

3

Page 24: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Features Real-Time Executive (REX)

6-2 QUALCOMM Proprietary 80-24897-1 Rev. C

1

Page 25: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

80-24897-1 Rev. C QUALCOMM Proprietary 7-1

7 Inside REX

Information on some of REX’s internal data structures and variables is provided in this chapter. These 1

should never be manipulated directly by applications, although they are accessible. Knowing about 2

them may help when debugging programs. 3

7.1 Task control block 4

Each task has a TCB that keeps track of the task context. The fields of interest in the TCB are: 5

sp – This field stores the pointer to the top of stack. It is meaningful only for a suspended task. 6

stack_limit – This field stores the stack limit for this task’s stack. The stack pointer should never 7

go below this value. 8

sigs – This field holds the signals currently set for this task. 9

wait – This field holds the signals on which this task is waiting. A nonzero value here implies that 10

the task is suspended, and a zero value implies that the task is ready to run or is running. 11

pri – This field holds the task’s priority. 12

7.2 Current task 13

The global variable rex_curr_task contains a pointer to the TCB of the currently running task. 14

7.3 Best task 15

The global variable rex_best_task contains a pointer to the TCB of the highest priority runnable 16

task. 17

18

Page 26: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Inside REX Real-Time Executive (REX)

7-2 QUALCOMM Proprietary 80-24897-1 Rev. C

7.4 Task list 1

All TCBs are kept in a queue that is sorted by priority. The queue itself is a doubly linked list with 2

next_ptr and prev_ptr holding the next and previous pointers. The global structure 3

rex_task_list is the headnode of this queue. Its next_ptr field points to the highest priority task 4

in the system (although that task need not be ready to run). The highest priority ready task is found by 5

traversing this queue from the head to the tail looking for a TCB with a wait field equal to zero. 6

This search is guaranteed to succeed because the Idle Task is always at the end of this queue (since it 7

has the lowest priority, zero) and it is always ready to run. 8

9

NOTE As mentioned earlier, the priority value of zero is reserved and cannot be used by application tasks. 10

11

Page 27: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

80-24897-1 Rev. C QUALCOMM Proprietary 8-1

8 Programming Reference

A programming reference for the REX API is provided in this chapter. Programmers should use these 1

functions only according to the interface specifications, since REX does not check any parameter for 2

validity. Functions include: 3

rex_init( ) 4

rex_def_task( ) 5

rex_set_sigs( ) 6

rex_clr_sigs( ) 7

rex_get_sigs( ) 8

rex_wait( ) 9

rex_def_timer( ) 10

rex_set_timer( ) 11

rex_get_timer( ) 12

rex_clr_timer( ) 13

rex_pause_timer( ) 14

rex_resume_timer( ) 15

rex_tick( ) 16

rex_timed_wait( ) 17

rex_self( ) 18

rex_get_pri( ) 19

rex_set_pri( ) 20

rex_task_pri( ) 21

rex_set_interrupt_vector( ) 22

rex_enable_interrupt( ) 23

rex_disable_interrupt( ) 24

rex_init_crit_sect( ) (not available with older versions) 25

rex_enter_crit_sect( ) (not available with older versions) 26

rex_leave_crit_sect( ) (not available with older versions) 27

Page 28: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Programming Reference Real-Time Executive (REX)

8-2 QUALCOMM Proprietary 80-24897-1 Rev. C

8.1 rex_init( ) 1

Synopsis 2

extern void rex_init ( 3

void * p_istack, /* interrupt stack */ 4

rex_stack_word_type p_istksiz, /* interrupt stack size */ 5

rex_tcb_type *p_tcb, /* task control block */ 6

void * p_stack, /* stack */ 7

rex_stack_word_type p_stksiz, /* stack size */ 8

rex_priority_type p_pri, /* task priority */ 9

void (*p_task)( dword ), /* task function */ 10

dword p_param /* task parameter */ 11

); 12

13

Description 14

This function initializes REX. It should be called once and only once during the power-up processing, 15

before using any REX API. Interrupts should be enabled in the system only after this initialization. 16

This function: 17

1. Creates the first user task, p_task, with: 18

Stack p_stack of size p_stksiz (16-bit words) 19

Priority of p_pri 20

TCB p_tcb 21

2. Calls the scheduler, which gives control to this newly created task. 22

The first two parameters of this function, p_istack and p_istksiz, are present for backward 23

compatibility and are no longer used. 24

Side effects 25

None 26

Return value 27

Not applicable 28

29

Page 29: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX) Programming Reference

80-24897-1 Rev. C QUALCOMM Proprietary 8-3

Usage 1

#include “rex.h”

#define FIRST_STACK_SIZE 512 /* 1024 bytes */

#define FIRST_TASK_PRI 200 /* Some non-zero unique value */

#define FIRST_TASK_PARAM 101 /* Some value understood by the new task */

rex_tcb_type first_task_tcb; 2

int first_task_stack[FIRST_STACK_SIZE]; 3

4

void first_task(dword p) 5

{ 6

/* 7

* This is the first task in the system. 8

* When control comes here … 9

* - p has been set to FIRST_TASK_PARAM by REX 10

* - first_task_stack is in use 11

* - this function is not supposed to return. 12

*/ 13

} 14

15

During power-up processing 16

rex_init ( 17

NULL, 18

0, 19

&first_task_tcb, 20

first_task_stack, 21

FIRST_STACK_SIZE * sizeof (int) / sizeof (word), 22

FIRST_TASK_PRI, 23

first_task, 24

FIRST_TASK_PARAM 25

); 26

27

28

Page 30: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Programming Reference Real-Time Executive (REX)

8-4 QUALCOMM Proprietary 80-24897-1 Rev. C

8.2 rex_def_task( ) 1

Synopsis 2

extern void rex_def_task ( 3

rex_tcb_type *p_tcb, /* valid TCB for new task */ 4

void * p_stack, /* stack for new task */ 5

rex_stack_word_type p_stksiz, /* stack size for new task */ 6

rex_priority_type p_pri, /* priority for new task */ 7

void (*p_task)(dword), /* task startup function */ 8

dword p_param /* parameter for new task */ 9

); 10

11

Description 12

This function creates a new REX task. The TCB, pointed to by p_tcb, should have been allocated by 13

the calling function. The new task gets: 14

Its own stack, p_stack, which is p_stksiz words long (16-bit words) 15

Priority of p_pri and its entry point is p_task – This entry point will get the parameter 16

p_param, when it first runs. This parameter value is not interpreted by REX; it is simply passed 17

to the task. Applications can attach any meaning to this value. 18

The scheduler is called as soon as REX defines this new task. However, there is no guarantee that this 19

task will get control immediately as a result of this API call. That will depend solely upon its priority 20

relative to other ready tasks in the system at the time it is called. This task will be created ready-to-21

run. 22

Side effects 23

This function can cause a task switch. 24

Return value 25

None 26

27

Page 31: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX) Programming Reference

80-24897-1 Rev. C QUALCOMM Proprietary 8-5

Usage 1

#include “rex.h”

#define NEW_STACK_SIZE 512 /* 1024 bytes */

#define NEW_TASK_PRI 200 /* Some non-zero unique value */

#define NEW_TASK_PARAM 101 /* Some value understood by the new task */

rex_tcb_type new_task_tcb; 2

int new_task_stack[NEW_STACK_SIZE]; 3

4

void new_task(dword p) 5

{ 6

/* 7

* When control comes here … 8

* - p has been set to NEW_TASK_PARAM by REX 9

* - new_task_stack is in use 10

* - this function is not supposed to return. 11

*/ 12

} 13

14

void existing_task(dword p) 15

{ 16

… 17

rex_def_task ( &new_task_tcb, 18

new_task_stack, 19

NEW_STACK_SIZE * sizeof (int) / sizeof (word) , 20

NEW_TASK_PRI, 21

new_task, 22

NEW_TASK_PARAM 23

); 24

… 25

} 26

27

28

Page 32: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Programming Reference Real-Time Executive (REX)

8-6 QUALCOMM Proprietary 80-24897-1 Rev. C

8.3 rex_set_sigs( ) 1

Synopsis 2

extern rex_sigs_type rex_set_sigs ( 3

rex_tcb_type *p_tcb, /* TCB for which the sigs will be set */ 4

rex_sigs_type p_sigs /* the sigs to set */ 5

); 6

7

Description 8

This function sets a specified mask of signals (p_sigs) for the specified task (p_tcb). The signals, 9

p_sigs, are bitwise-ORed with the signals already set for the task. If that task is waiting on any of the 10

signals set, it will again be eligible for execution. 11

Side effects 12

This function can cause a task switch. 13

Return value 14

The previous value of the signal mask of the task. 15

Usage 16

#include “rex.h”

#define NEW_SIGNALS 0x30

/* Some signals. */

extern rex_tcb_type target_task; 17

void signal_setting_task( dword p) 18

{ 19

rex_sigs_type old_sigs; 20

old_sigs = rex_set_sigs( &target_task, (rex_sigs_type)NEW_SIGNALS); 21

} 22

23

24

Page 33: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX) Programming Reference

80-24897-1 Rev. C QUALCOMM Proprietary 8-7

8.4 rex_clr_sigs( ) 1

Synopsis 2

extern rex_sigs_type rex_clr_sigs ( 3

rex_tcb_type *p_tcb, /* TCB for which the signals will be cleared */ 4

rex_sigs_type p_sigs /* which signals to clear */ 5

); 6

7

Description 8

This function clears the specified signals (p_sigs) for the specified task (p_tcb). The scheduler is 9

not called. 10

Return value 11

The previous value of the signal mask of the task. 12

Usage 13

#include “rex.h”

#define CLEAR_THESE 0x30 /* Some signals. */

extern rex_tcb_type target_task; 14

void signal_clearing_task( dword p) 15

{ 16

rex_sigs_type old_sigs; 17

old_sigs = rex_clr_sigs(&target_task, (rex_sigs_type)CLEAR_THESE); 18

} 19

20

21

Page 34: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Programming Reference Real-Time Executive (REX)

8-8 QUALCOMM Proprietary 80-24897-1 Rev. C

8.5 rex_get_sigs( ) 1

Synopsis 2

extern rex_sigs_type rex_get_sigs ( 3

rex_tcb_type *p_tcb /* TCB for which sigs will be returned */ 4

); 5

6

Description 7

This function reads the state of the signals for a specified task (p_tcb). 8

Return value 9

The present value of the signal mask of the task. 10

Usage 11

#include “rex.h” 12

extern rex_tcb_type target_task; 13

void signal_reading_task( dword p) 14

{ 15

rex_sigs_type present_sigs; 16

present_sigs = rex_get_sigs( &target_task ); 17

} 18

19

20

Page 35: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX) Programming Reference

80-24897-1 Rev. C QUALCOMM Proprietary 8-9

8.6 rex_wait( ) 1

Synopsis 2

extern rex_sigs_type rex_wait ( 3

rex_sigs_type p_sigs /* signals to wait on */ 4

); 5

6

Description 7

This function suspends the calling task until at least one signal of a specified signal mask (p_sigs) is 8

set for the task. If any signal of the signal mask is already set prior to calling rex_wait(), an 9

immediate return is made from the function and the calling task continues to execute. 10

A task can wait only on its own signals; it may not wait on any other task’s signals. 11

Side effects 12

This function can cause a task switch. 13

Return value 14

The present value of the signal mask of the task after its waiting is over. 15

Usage 16

#include “rex.h”

#define WAIT_FOR_THESE 0x50 /* Some signals. */

void wanting_to_wait_task( dword p) 17

{ 18

rex_sigs_type got_these_sigs; 19

got_these_sigs = rex_wait((rex_sigs_type)WAIT_FOR_THESE); 20

} 21

22

23

Page 36: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Programming Reference Real-Time Executive (REX)

8-10 QUALCOMM Proprietary 80-24897-1 Rev. C

8.7 rex_def_timer( ) 1

Synopsis 2

extern void rex_def_timer( 3

rex_timer_type *p_timer, /* pointer to a valid timer structure */ 4

rex_tcb_type *tcb_ptr, /* TCB to associate with the timer */ 5

rex_sigs_type sigs /* sigs to set upon timer expiration */ 6

); 7

8

Description 9

This function: 10

Initializes a specified timer block for use as an interval timer 11

Sets up: 12

Which task (tcb_ptr) to notify when the timer expires 13

Which signal mask (sigs) to set 14

REX assumes that space for the timer block has already been allocated by the calling function. 15

This function must be called at least once before the timer block can be used. It must not be called on 16

a timer block if that block is currently an active timer. 17

Return value 18

None 19

Usage 20

#include “rex.h” 21

#define SET_THESE 0x12 /* Some signals. */ 22

extern rex_tcb_type target_task; 23

rex_timer_type new_timer; 24

void timer_defining_task( dword p) 25

{ 26

rex_def_timer(&new_timer, &target_task, (rex_sigs_type)SET_THESE); 27

} 28

29

30

Page 37: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX) Programming Reference

80-24897-1 Rev. C QUALCOMM Proprietary 8-11

8.8 rex_set_timer( ) 1

Synopsis 2

extern rex_timer_cnt_type rex_set_timer( 3

rex_timer_type *p_timer, /* pointer to timer to set */ 4

rex_timer_cnt_type msecs /* value in milliseconds */ 5

); 6

7

Description 8

This function starts a timer (p_timer) for a specified number of milliseconds (msecs). The signal 9

mask specified by the timer is cleared for the task associated with the timer and will be set later when 10

the timer expires. 11

The timer block pointed to by p_timer must have been previously initialized using 12

rex_def_timer(). If this timer is already active and running, then it is restarted with the new count. 13

Side effects 14

This function can cause a task switch. 15

Return value 16

The timer count before setting it to the new value. 17

Usage 18

#include “rex.h”

#define TIMER_VALUE 20 /* 20-millisecond timer */

extern rex_timer_type existing_timer; 19

void timer_setting_task( dword p) 20

{ 21

rex_timer_cnt_type old_timer_count; 22

old_timer_count = rex_set_timer(&existing_timer, TIMER_VALUE); 23

} 24

25

26

Page 38: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Programming Reference Real-Time Executive (REX)

8-12 QUALCOMM Proprietary 80-24897-1 Rev. C

8.9 rex_get_timer( ) 1

Synopsis 2

extern rex_timer_cnt_type rex_get_timer( 3

rex_timer_type *p_timer /* pointer to the timer to get */ 4

); 5

6

Description 7

This function reads out the current counter value for a specified timer (p_timer). A value of zero 8

indicates that this timer is currently inactive. 9

Return value 10

The current counter value of the specified timer. 11

Usage 12

#include “rex.h” 13

extern rex_timer_type existing_timer; 14

void timer_reading_task( dword p) 15

{ 16

rex_timer_cnt_type timer_count; 17

timer_count = rex_get_timer(&existing_timer); 18

} 19

20

21

Page 39: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX) Programming Reference

80-24897-1 Rev. C QUALCOMM Proprietary 8-13

8.10 rex_clr_timer( ) 1

Synopsis 2

extern rex_timer_cnt_type rex_clr_timer( 3

rex_timer_type *p_timer /* pointer to timer to clear */ 4

); 5

6

Description 7

This function sets the counter value for a specified timer to zero. This, in effect, stops the timer and 8

removes it from the list of active timers. 9

Return value 10

The previous counter value of the specified timer. 11

Usage 12

#include “rex.h” 13

extern rex_timer_type existing_timer; 14

void timer_clearing_task( dword p) 15

{ 16

rex_timer_cnt_type old_timer_count; 17

old_timer_count = rex_clr_timer(&existing_timer); 18

} 19

20

21

Page 40: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Programming Reference Real-Time Executive (REX)

8-14 QUALCOMM Proprietary 80-24897-1 Rev. C

8.11 rex_pause_timer( ) 1

Synopsis 2

extern void rex_pause_timer( 3

rex_timer_type *p_timer /* pointer to timer to pause */ 4

); 5

6

Description 7

This function removes a specified timer from the list of active timers, which prevents the timer’s 8

count from being decremented further. This function differs from rex_clr_timer() in that the 9

count is not set to zero before the timer is removed from the active list. 10

Return value 11

None 12

Usage 13

#include “rex.h” 14

extern rex_timer_type existing_timer; 15

void timer_pausing_task( dword p) 16

{ 17

rex_pause_timer(&existing_timer); 18

} 19

20

21

Page 41: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX) Programming Reference

80-24897-1 Rev. C QUALCOMM Proprietary 8-15

8.12 rex_resume_timer( ) 1

Synopsis 2

extern void rex_resume_timer( 3

rex_timer_type *p_timer /* pointer to timer to resume */ 4

); 5

6

Description 7

This function resumes a paused timer by restoring it to the active timer list. This function differs from 8

rex_set_timer() in that the starting count of the timer is not specified explicitly (the existing value 9

is used instead). 10

Return value 11

None 12

Usage 13

#include “rex.h” 14

extern rex_timer_type paused_timer; 15

void timer_resuming_task( dword p) 16

{ 17

rex_resume_timer(&paused_timer); 18

} 19

20

21

Page 42: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Programming Reference Real-Time Executive (REX)

8-16 QUALCOMM Proprietary 80-24897-1 Rev. C

8.13 rex_tick( ) 1

Synopsis 2

extern void rex_tick( 3

rex_timer_cnt_type msecs /* number of milliseconds elapsed */ 4

); 5

6

Description 7

This function is not for use by application tasks. It should be called by the clock-tick ISR for each 8

interrupt. The parameter msecs should be the number of milliseconds per clock tick. This value is 9

hardware-dependent. 10

This function runs through the list of unexpired timers and decrements their current count. If any 11

timer’s count reaches zero, that timer is removed from the list of active timers and the signals 12

specified in its block are set in the specified task. 13

Side effects 14

This function can cause a task switch. 15

Return value 16

None 17

Usage 18

#include “rex.h” 19

#define MS_PER_CLK_INTERRUPT 5 20

void clock_tick_isr() 21

{ 22

rex_tick(MS_PER_CLK_INTERRUPT); 23

} 24

25

26

Page 43: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX) Programming Reference

80-24897-1 Rev. C QUALCOMM Proprietary 8-17

8.14 rex_timed_wait( ) 1

Synopsis 2

extern rex_timer_cnt_type rex_timed_wait( 3

rex_sigs_type p_sigs, /* sigs to wait on */ 4

rex_timer_type *p_timer, /* timer to set and wait on */ 5

rex_timer_cnt_type p_cnt /* timer to wait */ 6

); 7

8

Description 9

This function is exactly the same as: 10

rex_set_timer(p_timer, p_cnt); 11

return rex_wait(p_sigs); 12

The calling task becomes ready to run when one of the signals is set, either by some other task or by 13

the expired timer. 14

Users must ensure that the signals (p_sigs) are a superset of the signals held in the timer block 15

(p_timer->sigs). 16

Side effects 17

This function can cause a task switch. 18

Return value 19

The present value of the signal mask of the task after its waiting is over. 20

Usage 21

#include “rex.h”

#define TIMER_VALUE 20 /* 20-millisecond timer */

#define WAIT_FOR_THESE 0x30 /* Some signals. */

extern rex_timer_type existing_timer; 22

void timed_waiting_task( dword p) 23

{ 24

rex_sigs_type got_these_sigs; 25

got_these_sigs = rex_timed_wait ( (rex_sigs_type)WAIT_FOR_THESE), 26

&existing_timer, 27

TIMER_VALUE 28

); 29

} 30

31

Page 44: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Programming Reference Real-Time Executive (REX)

8-18 QUALCOMM Proprietary 80-24897-1 Rev. C

8.15 rex_self( ) 1

Synopsis 2

extern rex_tcb_type* rex_self ( void ); 3

4

Description 5

This function returns a pointer to the TCB of the calling task. This allows a task to obtain the address 6

of its own TCB. 7

Return value 8

Pointer to the TCB of the calling task. 9

Usage 10

#include “rex.h” 11

void dont_know_my_own_name_task( dword p) 12

{ 13

rex_tcb_type * my_tcb; 14

my_tcb = rex_self(); 15

} 16

17

18

Page 45: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX) Programming Reference

80-24897-1 Rev. C QUALCOMM Proprietary 8-19

8.16 rex_get_pri( ) 1

Synopsis 2

extern rex_priority_type rex_get_pri( void ); 3

4

Description 5

This function returns the current priority of the calling task. 6

Return value 7

Current priority of the calling task. 8

Usage 9

#include “rex.h” 10

void dont_know_my_priority_task(dword p) 11

{ 12

rex_priority_type my_priority; 13

my_priority = rex_get_pri(); 14

} 15

16

17

Page 46: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Programming Reference Real-Time Executive (REX)

8-20 QUALCOMM Proprietary 80-24897-1 Rev. C

8.17 rex_set_pri( ) 1

Synopsis 2

extern rex_priority_type rex_set_pri( 3

rex_priority_type p_pri /* the new priority */ 4

); 5

6

Description 7

This function allows the calling task to change its own priority to that specified in p_pri. 8

Side effects 9

This function can cause a task switch. 10

Return value 11

Previous priority value. 12

Usage 13

#include “rex.h” 14

void nice_task(dword p) 15

{ 16

rex_priority_type old_priority 17

18

/* I am nice. I reduce my priority by 10. */ 19

old_priority = rex_set_pri( rex_get_pri() – 10); 20

} 21

22

23

Page 47: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX) Programming Reference

80-24897-1 Rev. C QUALCOMM Proprietary 8-21

8.18 rex_task_pri( ) 1

Synopsis 2

extern rex_priority_type rex_task_pri( 3

rex_tcb_type *p_tcb, /* TCB to set priority on */ 4

rex_priority_type p_pri /* the new priority */ 5

); 6

7

Description 8

This function sets the priority of the specified task (p_tcb) to the specified value (p_pri). 9

Side effects 10

This function can cause a task switch. 11

Return value 12

Previous priority value of that task. 13

Usage 14

#include “rex.h” 15

extern rex_tcb_type some_other_task; 16

void extremely_nice_task(dword p) 17

{ 18

rex_priority_type old_priority; 19

20

/* I am extremely nice. I set this task’s priority to a 1000 21

* above my own priority. 22

*/ 23

old_priority = rex_task_pri( 24

&some_other_task, 25

rex_get_pri() + 1000 26

); 27

} 28

29

30

Page 48: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Programming Reference Real-Time Executive (REX)

8-22 QUALCOMM Proprietary 80-24897-1 Rev. C

8.19 rex_set_interrupt_vector( ) 1

Synopsis 2

void rex_set_interrupt_vector( 3

rex_vect_type v, 4

void (*fnc_ptr)( void ) 5

); 6

7

Description 8

REX maintains a software equivalent of an interrupt table. The parameter v signifies entries in this 9

table. This function installs handlers for IRQ and FIQ interrupts in this table. 10

Side effects 11

None 12

Return value 13

None 14

Usage 15

#include “arm.h” 16

#include “rex.h” 17

18

void irq_isr ( void ); 19

20

void irq_handler_setting_function( ) 21

{ 22

rex_set_interrupt_vector(P_IRQ_VECT, irq_isr); 23

} 24

25

26

Page 49: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX) Programming Reference

80-24897-1 Rev. C QUALCOMM Proprietary 8-23

8.20 rex_enable_interrupt( ) 1

Synopsis 2

void rex_enable_interrupt ( 3

rex_vect_type v 4

); 5

6

Description 7

REX maintains a software equivalent of an interrupt table. The parameter v signifies entries in this 8

table. This function enables IRQ and FIQ interrupts by setting the appropriate bits in CPSR. 9

Side effects 10

None 11

Return value 12

None 13

Usage 14

#include “arm.h” 15

#include “rex.h” 16

17

void irq_enabling_function( ) 18

{ 19

rex_enable_interrupt (P_IRQ_VECT); 20

} 21

22

23

Page 50: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Programming Reference Real-Time Executive (REX)

8-24 QUALCOMM Proprietary 80-24897-1 Rev. C

8.21 rex_disable_interrupt( ) 1

Synopsis 2

void rex_disable_interrupt ( 3

rex_vect_type v 4

); 5

6

Description 7

REX maintains a software equivalent of an interrupt table. The parameter v signifies entries in this 8

table. This function disables IRQ and FIQ interrupts by setting the appropriate bits in CPSR. 9

Side effects 10

None 11

Return value 12

None 13

Usage 14

#include “arm.h” 15

#include “rex.h” 16

17

void fiq_disabling_function( ) 18

{ 19

rex_disable_interrupt (P_FIQ_VECT); 20

} 21

22

23

Page 51: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX) Programming Reference

80-24897-1 Rev. C QUALCOMM Proprietary 8-25

8.22 rex_init_crit_sect( ) 1

Synopsis 2

extern void rex_init_crit_sect ( 3

rex_crit_sect_type *crit_sect /* Pointer to critical section variable */ 4

); 5

6

Description 7

Initializes a critical section. All critical section variables must be initialized prior to use. 8

Side effects 9

None 10

Return value 11

None 12

Usage 13

#include “rex.h” 14

15

rex_crit_sect_type sample_cs; 16

17

void cs_initializing_function( ) 18

{ 19

rex_init_crit_sect(&sample_cs); 20

} 21

22

NOTE This function is not available with some versions of REX. 23

24

Page 52: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Programming Reference Real-Time Executive (REX)

8-26 QUALCOMM Proprietary 80-24897-1 Rev. C

8.23 rex_enter_crit_sect( ) 1

Synopsis 2

extern void rex_enter_crit_sect ( 3

rex_crit_sect_type *crit_sect /* Pointer to critical section variable */ 4

); 5

6

Description 7

Acquires a critical section and proceeds. If the critical section is locked by another task, puts the 8

calling task onto a queue to be woken up later when the owning task releases it. If the critical section 9

cannot be acquired immediately, the calling task blocks. 10

Side effects 11

The calling task will be suspended if the critical section is not available. 12

Return value 13

None 14

Usage 15

#include “rex.h” 16

17

extern rex_crit_sect_type initialized_cs; 18

19

void cs_entering_function( ) 20

{ 21

rex_enter_crit_sect(&initialized_cs); 22

} 23

24

NOTE This function is not available with some versions of REX. 25

26

Page 53: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Real-Time Executive (REX) Programming Reference

80-24897-1 Rev. C QUALCOMM Proprietary 8-27

8.24 rex_leave_crit_sect( ) 1

Synopsis 2

extern void rex_leave_crit_sect ( 3

rex_crit_sect_type *crit_sect /* Pointer to critical section variable */ 4

); 5

6

Description 7

Allows a task to release a critical section that it is holding. The critical section then becomes available 8

for acquisition by another task. If a higher priority task is waiting on the critical section, then when 9

this task releases the critical section, the higher priority task will be swapped in. 10

Side effects 11

A context switch might occur. 12

Return value 13

None 14

Usage 15

#include “rex.h” 16

17

extern rex_crit_sect_type acquired_cs; 18

19

void cs_leaving_function( ) 20

{ 21

rex_leave_crit_sect(&acquired_cs); 22

} 23

24

NOTE This function is not available with some versions of REX. 25

26

Page 54: Real-Time Executive (REX) User Guideread.pudn.com/downloads159/doc/comm/709860/80-24897-1 Real-T… · Real-Time Executive (REX) User Guide 80-24897-1 Rev. C September 6, 2001 Restricted

Programming Reference Real-Time Executive (REX)

8-28 QUALCOMM Proprietary 80-24897-1 Rev. C

1