Engineer To Engineer Note

Similar documents
a Engineer-To-Engineer Note

Engineer-To-Engineer's Note

a Engineer To Engineer Note

Procesory Sygnałowe w aplikacjach przemysłowych

EE-123. An Overview of the ADSP-219x Pipeline Last modified 10/13/00

4 DATA ADDRESS GENERATORS

a Engineer To Engineer Note EE-143

Engineer To Engineer Note

The task of writing device drivers to facilitate booting of the DSP via these interfaces is with the user.

Engineer-to-Engineer Note

GLOSSARY. VisualDSP++ Kernel (VDK) User s Guide B-1

real-time kernel documentation

Engineer-to-Engineer Note

3 PROGRAM SEQUENCER. Overview. Figure 3-0. Table 3-0. Listing 3-0.

Engineer-to-Engineer Note

DSP Platforms Lab (AD-SHARC) Session 05

Engineer To Engineer Note

The SHARC in the C. Mike Smith

The following revision history lists the anomaly list revisions and major changes for each anomaly list revision.

Computer Systems Lecture 9

Grundlagen Microcontroller Interrupts. Günther Gridling Bettina Weiss

INTERRUPTS in microprocessor systems

Programming Model 12.1 OVERVIEW 12 1

EE475 Lab #3 Fall Memory Placement and Interrupts

Writing TMS320C8x PP Code Under the Multitasking Executive

Engineer To Engineer Note

Engineer-to-Engineer Note

Summer 2003 Lecture 14 07/02/03

Q.1 Explain Computer s Basic Elements

Real Time Embedded Systems. Lecture 10 January 31, 2012 Interrupts

Run-Time Data Structures

Newbie s Guide to AVR Interrupts

Lecture test next week

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Interrupt Handling Module No: CS/ES/13 Quadrant 1 e-text

CHAPTER 11 INTERRUPTS PROGRAMMING

FEATURE ARTICLE. Michael Smith

Engineer-to-Engineer Note

Interrupt Basics Karl-Ragmar Riemschneider

G GLOSSARY. Terms. Figure G-0. Table G-0. Listing G-0.

Circular Buffers D 1 D.1 INTRODUCTION D.2 DECLARING CIRCULAR BUFFERS D.3 ACCESSING CIRCULAR BUFFERS

ADSP-21065L EZ-KIT Lite Evaluation System Manual

Introduction ADSP-2100 FAMILY OF PROCESSORS

EEL 4744C: Microprocessor Applications. Lecture 7. Part 1. Interrupt. Dr. Tao Li 1

Reading Assignment. Interrupt. Interrupt. Interrupt. EEL 4744C: Microprocessor Applications. Lecture 7. Part 1

Multics Technical Bulletin MTB-488. To: Distribution From: John J. Bongiovanni Date: 01/20/81 CPU Time Accounting

EE4144: ARM Cortex-M Processor

LOW-COST SIMD. Considerations For Selecting a DSP Processor Why Buy The ADSP-21161?

Interrupts & Interrupt Service Routines (ISRs)

a Engineer To Engineer Note

Engineer To Engineer Note

Chapter 9 :: Subroutines and Control Abstraction

1 INTRODUCTION. Purpose. Audience. Figure 1-0. Table 1-0. Listing 1-0.

ADSP-218x. DSP Instruction Set Reference. First Edition, February Part Number

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

),17(558379(&725 $''5(66(6

Stack Frames. Remind Ourselves why stack-frames are required. Appreciate their role in register-based processors

Single thread Scheduler All processes called once each sample

Today's Topics. CISC 458 Winter J.R. Cordy

Product Update. Errata to Z8 Encore! 8K Series Silicon. Z8 Encore! 8K Series Silicon with Date Codes 0402 and Later

Control Abstraction. Hwansoo Han

embos Real Time Operating System CPU & Compiler specifics for ARM core with ARM RealView Developer Suite 3.0 Document Rev. 1

Section 7 Program Sequencer

GUJARAT TECHNOLOGICAL UNIVERSITY MASTER OF COMPUTER APPLICATION SEMESTER: III

AVR Subroutine Basics

Chapter 7 Subroutines. Richard P. Paul, SPARC Architecture, Assembly Language Programming, and C

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

Engineer To Engineer Note

Interrupts and Low Power Features

Chapter 09. Programming in Assembly

Set No.1. Code No: R

Table of Figures Figure 1. High resolution PWM based DAC...2 Figure 2. Connecting the high resolution buck converter...8

Engineer-to-Engineer Note

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

ATmega Interrupts. Reading. The AVR Microcontroller and Embedded Systems using Assembly and C) by Muhammad Ali Mazidi, Sarmad Naimi, and Sepehr Naimi

Instruction Set Reference

Multiple Choice Questions. Chapter 5

Concepts Introduced in Chapter 7

Digital IP Cell 8-bit Microcontroller PE80

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Instruction Unit Group: Program Sequencer. Vlad Branzoi Jerry Kao

Embedded Operating Systems

1 INTRODUCTION. Figure 1-0. Table 1-0. Listing 1-0.

Engineer-to-Engineer Note

ELEC 242 Time Delay Procedure

Digital Control of Electric Drives

April 4, 2001: Debugging Your C24x DSP Design Using Code Composer Studio Real-Time Monitor

Bare Metal Application Design, Interrupts & Timers

PROGRAM CONTROL UNIT (PCU)


Process Context & Interrupts. New process can mess up information in old process. (i.e. what if they both use the same register?)

1 INTRODUCTION. Purpose. Audience. Figure 1-0. Table 1-0. Listing 1-0.

MPLAB X Debugging Techniques

ADSP EZ-KIT Lite Evaluation System Manual. Part Number: Revision 3.0 January 2003

Homework / Exam. Return and Review Exam #1 Reading. Machine Projects. Labs. S&S Extracts , PIC Data Sheet. Start on mp3 (Due Class 19)

Support for high-level languages

Cooperative Multitasking

Context Switching & Task Scheduling

Corso di Elettronica dei Sistemi Programmabili

EL6483: Brief Overview of C Programming Language

Transcription:

Engineer To Engineer Note EE-134 Phone: (800) ANALOG-D, FAX: (781) 461-3010, EMAIL: dsp.support@analog.com, FTP: ftp.analog.com, WEB: www.analog.com/dsp Copyright 2001, Analog Devices, Inc. All rights reserved. Analog Devices assumes no responsibility for customer product design or the use or application of customers products or for any infringements of patents or rights of others which may result from Analog Devices assistance. All trademarks and logos are property of their respective holders. Information furnished by Analog Devices Applications and Development Tools Engineers is believed to be accurate and reliable, however no responsibility is assumed by Analog Devices regarding the technical accuracy of the content provided in all Analog Devices Engineer-to-Engineer Notes. Writing C Compatible Assembly Code Interrupt Handlers for the SHARC Family Kevin C. Kreitzer last modified 05/10/2001 Introduction Very often, DSP programs will consist of a mix of C code and assembly code. The assembly code is typically used for time critical routines. Interrupt Service Routines (ISRs) can be some of the most time critical elements of the program and, as such, often need to be coded in assembly. This EE-note describes how to write C-compatible assembly code interrupt handlers for the SHARC family. C Interrupt Handlers C provides its own set of interrupt handlers via the interrupt() and signal() functions. Visual DSP has extended the interrupt() function with interruptf() and interrupts() versions. These are summarized for the ADSP-2106x family in Figure 1. Interrupts are enabled at runtime using these library functions as shown in Figure 2. The function interrupt() unmasks the appropriate interrupt in the IMASK register, enables global interrupts in MODE1 register, and maps the specified function as the interrupt service routine. Signal() will enable only one instance of the interrupt. Name Cycle Count C library function What is saved? Regular 128 interrupt() Saves and restores everything. Fast 60 interruptf() Saves the scratch registers only. Super fast 30 interrupts() Alternate register set is used. Nothing is saved. Interrupt nesting is disabled. Figure 1. C interrupt handler table. interrupt (SIG_TMZI, Timer_ISR); Figure 2. C interrupt handler example. Assembly Interrupt Handlers While not inherently inefficient themselves, C interrupt handlers, even if the interrupt service routine is written in assembly, are general purpose and must account for all possible conditions. Writing your own custom assembly interrupt handlers is simple and will greatly speed execution. The interrupt handler consists of three parts the initialization, the interrupt vector, and the interrupt service routine. Initialization performs the tasks previously associated with the C interrupt() function assigning the interrupt service routine to an interrupt vector (dynamic ISR vectors only), unmasking the interrupt, and enabling global interrupts. The interrupt vector routes execution to the appropriate interrupt service routine. a

Interrupt Service Routine Essential to the proper execution of an interrupt service routine is a thorough understanding of the context switch. The context switch must do two things. The first task is to preserve the state of the processor for a seamless return to the main code execution. The second is to put the processor into a known state in which the interrupt service routine may properly operate. There are no scratch registers reserved by C for use in interrupts. Therefore, all registers used in an interrupt service routine must be preserved. One register needing preservation that may not be obvious is the arithmetic status or ASTAT register. Any operations in the interrupt service routine that modify the flags will affect this register. The ASTAT register is pushed onto the hardware status stack implicitly for external interrupts (IRQs), vector interrupts, and timer interrupts. For all other interrupts, it must be pushed and popped explicitly. Once the MODE1 register is pushed, the processor can then be set to a known state for the interrupt service routine. For example, the ADSP-2116x family interrupt service routines will want to guarantee whether the processor is in SIMD or SISD mode. The code in Figure 3 will explicitly push and pop the ASTAT and MODE1 registers. There are two options for performing register preservation of the remaining registers. pop sts; Figure 3. Status stack push/pop example. Alternate Register Set Context Switch The SHARC family provides an alternate register set for the general purpose registers (Rx, Fx), data address generator registers (Ix, Mx, Bx, Lx), and the fixed-point multiplier result register (MR). This is the fastest, but most inflexible method. Interrupt nesting is not allowed since there is only one alternate register set. Additionally, the alternate registers used for interrupts may not be used anywhere else in your code e.g., assembly code subroutines. A compromise is to parse out a subset of alternate registers for use in ISRs and another for use in assembly subroutines. The alternate registers can be selected using bits in the MODE1 register. There is a 1 cycle effect latency for writes to the MODE1 register! Therefore, a NOP or some other instruction not using the alternate registers must follow the write to the MODE1 register. Assuming that the MODE1 register was pushed onto the status stack either implicitly or explicitly, you will not need to reset to the default register sets on exit from the interrupt service routine. The pop of the status stack will do that for you. The example in Figure 4 preserves I0-3, M0-3, L0-3, B0-3, and R0-15. * save environment push sts * restore environment pop sts; rti; Figure 4. Alternate register set context switch example. C Run-Time Stack Context Switch The most flexible method is to push and pop registers using the C run-time stack. While less efficient than the alternate register set method, it is in most cases still considerably faster than using C handlers. This method will allow nesting and will save your alternate registers for fast assembly subroutines. C run-time stack EE-134 Page 2

support is provided in ASM_SPRT.H using the PUTS, GETS, and ALTER macros. Note that these macros will not work on a DAG1 register so those must be saved into a general purpose register first. Since the C run-time stack uses the default I6 and I7 registers, care must be taken if there is any possibility of entering the interrupt service routine with the alternate register set active. For the ADSP-2116x family, the processor must also be in SISD mode. These can be accomplished by clearing the appropriate bits in the MODE1 register after it has been pushed onto the As before, there is a one cycle effect latency and you do not need to reset the MODE1 one back to its previous state on exit the pop of the status stack will do it for you. * save environment bit clr mode1 SRD1H PEYEN; puts = r0; r0 = i0; puts = r0; puts = r1; puts = r2; puts = r3; * restore environment r3 = gets(1); r2 = gets(2); r1 = gets(3); r0 = gets(4); i0 = r0; r0 = gets(5) alter(5); pop sts; rti; Figure 5. C run-time stack context switch example. The example in Figure 5 preserves I0, R0, R1, R2, and R3. Note that the clearing of the PEYEN bit in the MODE1 register is only valid for the ADSP-2116x family. Interrupt Vectors The run-time header file contains the actual interrupt vector locations. These reside at the lowest addressed internal memory of the processor and contain four instruction words per vector. The entire vector table is 256 words long, allowing for a maximum of 64 vectors. Some of these are reserved. A default file called 060_HDR.ASM is used for the C interrupt handlers. In order to use assembly handlers, you will need to provide your own 060_HDR.ASM and include it as a source file in your project. Examples of the vectors are shown in the next two sections. Note that in some of the examples, the vector can not be completed in the allotted four instructions. A secondary vector location must be added to accommodate the additional instructions. These secondary vectors should go at the end of the primary vector locations. Fixed ISR Vectors Here are examples of interrupt vectors for a fixed interrupt service routine. Notice that in order to save cycles, we have moved the first few instructions of the context switch examples given above into the vector itself. In all examples, a nop or other instruction follows a write to the MODE1 register to account for the 1 cycle effect latency. Figures 6a&b show examples for a timer interrupt with an implicit status stack push and an alternate register set context switch. These examples preserve I0-4, M0-4, L0-4, B0-4, and R0-15. EE-134 Page 3

Figure 6a. ADSP-2106x fixed ISR vector example Figure 6b. ADSP-2116x fixed ISR vector example The Figure 7a&b examples are the same as Figure 6a&b, but use the SPORT1 transmit jump _Sport1TXISR (db); Figure 7a. ADSP-2106x fixed ISR vector example jump _Sport1TXISR; Figure 7b. ADSP-2116x fixed ISR vector example Figures 8a&b show examples for a timer interrupt with an implicit status stack push and a C runtime stack context switch. It guarantees that that I6 and I7 (stack frame and pointer) are in the default register set to allow C run-time stack pushes and pops to occur. Note that unlike Figures 6a&b and 7a&b, the register preservation happens in the interrupt service routine itself. bit clr mode1 SRD1H; Figure 8a. ADSP-2106x fixed ISR vector example using C run-time stack and implicit status stack bit clr mode1 SRD1H PEYEN; Figure 8b. ADSP-2116x fixed ISR vector example using C run-time stack and implicit status stack The Figure 9a&b examples are the same as Figure 8a&b, but use the SPORT1 transmit jump _Sport1TxISR (db); bit clr mode1 SRD1H; Figure 9a. ADSP-2106x fixed ISR vector example using C run-time stack and explicit status stack jump _Sport1TxISR (db); bit clr mode1 SRD1H PEYEN; Figure 9b. ADSP-2116x fixed ISR vector example using C run-time stack and explicit status stack Dynamic ISR Vectors There may be instances where you want to dynamically alter which service routine is used for a particular interrupt. The interrupt vector for this type of interrupt service routine must make an indirect jump based on a pointer value rather than a direct jump. Note that the index register used for the indirect jump must be preserved prior to its use. EE-134 Page 4

The Figure 10a&b examples use the alternate register set for the context switch. Registers I8-11, M8-11, L8-11, and B8-11 are preserved. Note that m13 is still in the default register set and is initialized to zero by the C compiler. Figure 10a. ADSP-2106x dynamic ISR vector example jump Vector2TMZHI (db); Vector2TMZHI: Figure 10b. ADSP-2116x dynamic ISR vector example The Figure 11a&b examples are the same as Figure 10a&b, but use the SPORT1 transmit Figure 11a. ADSP-2106x dynamic ISR vector example Figure 11b. ADSP-2116x dynamic ISR vector example The Figure 12a&b examples use the C run-time stack for the context switch. Registers I0-3, M0-3, L0-3, B0-3, I12-15, M12-15, L12-15, and B12-15 are preserved. Note that M13 is in the default register set and was initialized to zero by the C compiler. bit clr mode1 SRD1H SRD2H; jump Vector2TMZHI (db); Vector2TMZHI: Figure 12a. ADSP-2106x dynamic ISR vector example using C run-time stack and implicit status stack bit clr mode1 SRD1H SRD2H PEYEN; jump Vector2TMZHI (db); Vector2TMZHI: Figure 12b. ADSP-2116x dynamic ISR vector example using C run-time stack and implicit status stack The Figure 13a&b examples are the same as Figure 12a&b, but use the SPORT1 transmit EE-134 Page 5

bit clr mode1 SRD1H SRD2H; Figure 13a. ADSP-2106x dynamic ISR vector example using C run-time stack and explicit status stack bit clr mode1 SRD1H SRD2H PEYEN; Figure 13b. ADSP-2116x dynamic ISR vector example using C run-time stack and explicit status stack Initialization void (*timerisr)(); void Timer1ISR(); void Timer2ISR(); // declare ptr // 1st asm isr // 2nd asm isr void main() if (condition) timerisr = Timer1ISR; else timerisr = Timer2ISR; Figure 14. Dynamic ISR vector initialization example. For dynamic ISRs, the interrupt service routine to interrupt vector assignment is performed by declaring a routine pointer in C which will contain the name of the interrupt service routine to be executed. An example of declaring the pointer and its assignment are shown in Figure 14. Care must be used to not enable the interrupt before the pointer is initialized! Unmasking and enabling the interrupt may be done using sysreg_bit_set() statements as shown in Figure 15. #include <sysreg.h> #include <def21160.h> sysreg_bit_set(sysreg_imask, TMZHI); sysreg_bit_set(sysreg_mode1, IRPTEN); Figure 15. Unmasking and enabling interrupt example. Semaphore Passing Often semaphores are passed between interrupt service routines and the main body of code. Whenever this is done, it is critical that the semaphore be declared volatile. In the Figure 16 example, interruptflag is clearly initialized to 0. When control reaches the if statement, the compiler cannot see any reason why interruptflag would have another value, and removes the if statement as unnecessary. Declaring interruptflag as volatile tells the compiler that some other agent -- an interrupt, in this case -- may change the variable unexpectedly. This prevents the compiler from optimizing the code out of existence. volatile int interruptflag; void main() interruptflag = 0; if (interruptflag) Figure 16. Semaphore passing example. EE-134 Page 6

Summary This EE note covers the details of writing assembly code interrupt handlers that are compatible with C. Following the recommendations in this note should allow you to write very fast interrupt handlers that will not interfere with your C-code source. EE-134 Page 7