Exceptions and Interrupts Chap 7, 8 Tae-min Hwang.

Slides:



Advertisements
Similar presentations
I/O Unit.
Advertisements

Processor System Architecture
EET 2261 Unit 9 Interrupts  Read Almy, Chapters 17 – 19.  Homework #9 and Lab #9 due next week.  Quiz next week.
ECE 372 – Microcontroller Design Parallel IO Ports - Interrupts
COMP3221: Microprocessors and Embedded Systems Lecture 15: Interrupts I Lecturer: Hui Wu Session 1, 2005.
Exception Processing ECE511: Digital System & Microprocessor.
1 CSIT431 Introduction to Operating Systems Welcome to CSIT431 Introduction to Operating Systems In this course we learn about the design and structure.
The Cortex-M3 Embedded Systems: The Cortex-M3 Processor Basics
Cortex-M3 Debugging System
Interrupts. 2 Definition: An electrical signal sent to the CPU (at any time) to alert it to the occurrence of some event that needs its attention Purpose:
Exception and Interrupt Handling
Introduction to Embedded Systems
Interrupts. What Are Interrupts? Interrupts alter a program’s flow of control  Behavior is similar to a procedure call »Some significant differences.
Lecture 23: LM3S9B96 Microcontroller - Interrupts.
CORTEX-M0 Structure Discussion 2 – Core Peripherals
Computer System Overview Chapter 1. Operating System Exploits the hardware resources of one or more processors Provides a set of services to system users.
MICROPROCESSOR INPUT/OUTPUT
CHAPTER 3 TOP LEVEL VIEW OF COMPUTER FUNCTION AND INTERCONNECTION
Khaled A. Al-Utaibi  Interrupt-Driven I/O  Hardware Interrupts  Responding to Hardware Interrupts  INTR and NMI  Computing the.
The Functions of Operating Systems Interrupts. Learning Objectives Explain how interrupts are used to obtain processor time. Explain how processing of.
© 2008, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction Purpose  This training course provides an overview of the CPU architecture.
ARM Cortex-M0 August 23, 2012 Paul Nickelsberg Orchid Technologies Engineering and Consulting, Inc. CORTEX-M0 Structure Discussion.
Interrupt driven I/O. MIPS RISC Exception Mechanism The processor operates in The processor operates in user mode user mode kernel mode kernel mode Access.
AT91 Interrupt Handling. 2 Stops the execution of main software Redirects the program flow, based on an event, to execute a different software subroutine.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
CE Operating Systems Lecture 2 Low level hardware support for operating systems.
Interrupt driven I/O Computer Organization and Assembly Language: Module 12.
بسم الله الرحمن الرحيم MEMORY AND I/O.
Structure and Role of a Processor
Memory system Kang Min Ju. 01 Memory map NVIC : Nested Vector Interrupt Controller MPU : Memory Protection Unit * This arrangement allows:
Introduction to Exceptions 1 Introduction to Exceptions ARM Advanced RISC Machines.
Cortex-M3 Exceptions RTLAB. Hyeonggon Jo.  Exceptions Exception types & priority Abort model SVC and PendSV  Interrupt operation Pre-emption & Exit.
Floating Point Operations
Interrupt 마이크로 프로세서 (Micro Processor) 2015년 2학기 충북대학교 전자공학과 박 찬식
1 Computer System Overview Chapter 1. 2 Operating System Exploits the hardware resources of one or more processors Provides a set of services to system.
-Low Power and System Control Features
ARM Cortex M3 & M4 Chapter 4 - Architecture
Interrupts and exceptions
Interrupt and Exception Programming
Interrupts and signals
Microprocessor Systems Design I
Refer to Chapter 7, 8, 9 in the reference book
Mon. Oct 2 Announcements Quiz Postponed to Wednesday – still only on 2.a + 2.b Video lecture for 2.a posted Lab 6 experiment extension You must come to.
Timer and Interrupts.
UNIT – Microcontroller.
ARM Cortex-M3 RTLAB 박 유 진.
Computer Architecture
Interrupts In 8085 and 8086.
Introduction of microprocessor
Chapter 10 And, Finally... The Stack
Computer System Overview
Dr. Michael Nasief Lecture 2
Interrupt and Exception Programming
Interrupt and Exception Programming
An Introduction to Microprocessor Architecture using intel 8085 as a classic processor
Interrupt and Exception Programming
Computer System Overview
Processor Fundamentals
Interrupt and Exception Programming
X1 & X2 These are also called Crystal Input Pins.
Architectural Support for OS
Interrupts.
Md. Mojahidul Islam Lecturer Dept. of Computer Science & Engineering
Md. Mojahidul Islam Lecturer Dept. of Computer Science & Engineering
Interrupt handling Explain how interrupts are used to obtain processor time and how processing of interrupted jobs may later be resumed, (typical.
CORTEX-M0 Structure Discussion 1
Computer System Overview
Architectural Support for OS
COMP3221: Microprocessors and Embedded Systems
Presentation transcript:

Exceptions and Interrupts Chap 7, 8 Tae-min Hwang

Index 1 Overview 2 Exception management 3 Exception processing 4 Interrupt latency and exception handling optimization

Overview 1. The peripheral asserts an interrupt request to the processor 2. The processor suspends the currently executing task 3. The processor executes an Interrupt Service Routine (ISR) to service the peripheral, and optionally clear the interrupt request by software if needed 4. The processor resumes the previously suspended task Typical Interrupt sequence

Overview Number 1 ~ 15 for system exceptions Number 16 ~ for interrupt inputs The exception number is used as the identification for each exception and is used in various places in the ARMv7-M architecture Exception types

Overview Interrupt number(e.g., Interrupt #0) refers to the interrupt inputs to the NVIC on the Cortex-M processor for efficiency in some of API functions(e.g., when setting up priority levels) System exceptions use negative values Note

Exception management There are a number of programmable registers for managing interrupts and exceptions(e.g. NVIC, SCB) Special registers inside the processor core for interrupt masking(e.g. PRIMASK, FAULTMASK, BASEPRI) NVIC and SCB located in SCS address range from 0xE000E000, with size of 4KB (equal to SysTick timer, MPU, debugger register, etc.) Only be accessed by code running in privileged access level except Software Trigger Interrupt Register(STIR) For general application programming, the best practice is to use the CMSIS-Core access functions It is possible to directly access registers in NVIC or SCB if needed, but not recommended because of software portability when porting code from one Cortex-M processor to another with a different processor type Overview

There are a number of registers in the NVIC for interrupt control (exception type 16 up to 255) By default, after a system reset: All interrupts are disabled (enable bit = 0) All interrupts have priority level of 0 (highest programmable level) All interrupt pending statuses are cleared The CMSIS-Core provides the following functions for accessing Interrupt Enable registers: void NVIC_EnableIRQ (IRQn_Type IRQn); // Enable an interrupt void NVIC_DisableIRQ (IRQn_Type IRQn); // Disable an interrupt NVIC registers Exception management

These registers are related to interrupts or exception control The ICSR register can be used by application code to: - Set and clear the pending status of system exceptions including SysTick, PendSV, and NMI. -Determine the currently executing exception/interrupt number by reading VECTACTIVE SCB registers

Exception management All of these registers are special registers PRIMASK is used for temporarily disable all interrupts to carry out some timing critical tasks void __enable_irq(); // Clear PRIMASK void __disable_irq(); // Set PRIMASK void __set_PRIMASK(uint32_t priMask); // Set PRIMASK to value uint32_t __get_PRIMASK(void); // Read the PRIMASK value BASEPRI used for want to disable interrupts with priority lower than a certain level FAULTMASK used for wand to disable interrupts with priority lower than a certain level It is very similar to PRIMASK except that it changes the effective current priority level to -1, so that even the HardFault handler is blocked, so Only the NMI exception handler can be executed when FAULTMASK is set void __enable_fault_irq(void); // Clear FAULTMASK void __disable_fault_irq(void); // Set FAULTMASK to disable interrupts void __set_FAULTMASK(uint32_t faultMask); uint32_t __get_FAULTMASK(void); PRIMASK, FAULTMASK, BASEPRI

Exception management Some of the exceptions (e.g. reset, NMI, and HardFault) have fixed priority levels M3 and M4 supports up to 256 levels of programmable priority(with a maximum of 128 levels of pre-emption, and most Cortex-M3 or Cortex-M4 chips have fewer supported levels - for example, 8, 16, 32, and so on) Reduction in levels is implemented by cutting out the least significant bit (LSB) part of the priority configuration registers Controlled by priority-level registers, with width of 3bits to 8 bits For general application programming, the best practice is to use the CMSIS-Core access function Definitions of priority Bit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1 Bit 0 ImplementedNot Implemented A priority-level register with 3 bits implemented (8 programmable priority levels) Bit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1 Bit 0 ImplementedNot Implemented A priority-level register with 4 bits implemented (16 programmable priority levels)

Exception management #IRQ1 has higher priority level In default(4-bit priority configuration) Bit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1 Bit 0 ImplementedNot Implemented #IRQ00x09 #IRQ10x If LSB removed #IRQ00x04 #IRQ10x #IRQ1 has higher priority level # IRQ0 has higher priority level #IRQ00x01 #IRQ10x If MSB removed

Exception management Using a configuration register in the System Control Block (SCB) called Priority, the priority-level configuration registers for each exception with programmable priority levels is divided into two halves, the group (pre-empt) priority and the lower half (right bits) is the sub-priority The group priority level defines whether an interrupt can take place when the processor is already running another interrupt handler. The sub-priority level value is used only when two exceptions with same group-priority level occur at the same time. In this case, the exception with higher sub-priority (lower value) will be handled first Priority Groups NVIC_DecodePriority are returned by modified values pointed by the pointers uint32_t *pPre emptPriority uint32_t *pSub priority Note

Exception management There are a number of programmable registers for managing interrupts and exceptions(e.g. NVIC, SCB) Special registers inside the processor core for interrupt masking(e.g. PRIMASK, FAULTMASK, BASEPRI) NVIC and SCB located Priority Groups 0(default) Bit [7:1] Bit [7:2] Bit [7:3] Bit [7:4] Bit [7:5] Bit [7:6] Bit [7:7] None Bit [0] Bit [1:0] Bit [2:0] Bit [3:0] Bit [4:0] Bit [5:0] Bit [6:0] Bit [7:0] Priority GroupsPre-empts Priority FieldSub-priority Field

Exception management 3-bit priority-level register with priority group set to 5 Bit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1 Bit 0 Preempt priority Not Implemented Sub- priority 5Bit [7:6]Bit [5:0] Priority GroupsPre-empts Priority FieldSub-priority Field 3-bit priority-level register with priority group set to 1 Bit 7Bit 6Bit 5Bit 4Bit 3Bit 2Bit 1 Bit 0 Preempt priority[5:3]Sub-priority[1:0]Preempt priority[2:0] 1Bit [7:2]Bit [1:0] Priority GroupsPre-empts Priority FieldSub-priority Field Always 0

Exception management Cortex -M processor accepts an exception request, the processor needs to determine the starting address of the exception handler, and this information is stored in the vector table in the memory The vector table is normally defined in the startup codes provided by the microcontroller vendors Vector table Because of possibility that some exception such as NMI could happen as the processor just came out from reset and before any other initialization steps are executed

Exception management The vector tables in the Cortex-M processors are different from the vector tables in traditional ARM processors Vector table relocation Note Traditional ARM processors : Contain instructions such as branch instructions to branch to appropriate handlers Cortex-M processors : Contain the starting addresses of exception handlers It is useful to be able to modify or define exception vectors at run-time, so the Cortex-M3 and Cortex-M4 processors support a feature called Vector Table Relocation Vector Table Relocation feature provides a programmable register called the Vector Table Offset Register (VTOR) The VTOR register has a reset value of zero, and in application programming with a CMSIS-compliant device driver, this register can be accessed as “SCB->VTOR.” This register defines the starting address of the memory being used as the vector table Note VTOR register is slightly different between Cortex-M3 revision r2p0 and revision r2p1 Cortex-M3 r2p0 : the vector table can only be in the CODE region or the SRAM region Cortex-M3 r2p1 and Cortex-M4: The restriction is removed

Exception management Vector table reallocation Bit 31:30Bit 29Bit 28:7Bit 6:0 TBLOFFReserved TBLBASETBLOFFReserved Vector Table Offset Register(VTOR), address 0xE000ED08 TBLOFF : Vector Table Base Offset TBLBASE : Table based in CODE(0) region or SRAM(1) region Reserved : Read as zero, write ignore Cortex-M4 or Cortex-M3 r2p1 Cortex-M3 r0p0 to r2p0 Example code (beginning of SRAM 0x ) #define HW32_REG(ADDRESS) (*((volatile unsigned long *)(ADDRESS))) #define VTOR_NEW_ADDR 0x int i; for(i=0;i<48;i++){// Copy original vector table to SRAM first before programming VTOR HW32_REG((VTOR_NEW_ADDR + (i<<2))) = HW32_REG((i<<2)); // Assume maximum number of exception is 48 } __DMB(); // Data Memory Barrier to ensure write to memory is completed SCB->VTOR = VTOR_NEW_ADDR; // Set VTOR to the new vector table location __DSB(); // Data Synchronization Barrier to ensure all subsequence instructions use the new configuration

Exception management There are various status attributes applicable to each interrupt : - Each interrupt can either be disabled (default) or enabled - Each interrupt can either be pending (a request is waiting to be served) or not pending - Each interrupt can either be in an active (being served) or inactive state To support this, the NVIC contains programmable registers for interrupt enable control, pending status, and read-only active status An interrupt request can be accepted if ① The pending status is set, ② The interrupt is enabled, ③ The priority of the interrupt is higher than the current level(including interrupt masking register configuration) The NVIC is designed to support peripherals that generate pulsed interrupt requests as well as peripherals with high level interrupt request (the pulse must be at least one clock cycle long, and no need to configure any NVIC register to select either interrupt type) For level triggered interrupts, the peripheral requesting service asserts the request signal until it is cleared by an operation inside the ISR The request signals received by the NVIC are active high, although the external interrupt request at the I/O pin level could be active low Interrupt inputs The interrupt request

Exception processing It means it is put into a state of waiting for the processor to serve the interrupt If the processor is already serving another interrupt of higher or equal priority, or if the interrupt is masked by one of the interrupt masking registers, the pended request will remain until the other interrupt handler is finished, or when the interrupt masking is cleared In traditional ARM processors, the devices must be hold the request until they are served Pending behaviors

Exception processing Pending behaviors Cases Interrupt Request Interrupt Pending status Processor mode Pending status cleared by software Interrupt pending status cleared before processor serving the interrupt Interrupt Request Interrupt Pending status Processor mode Pending status cleared by software and re-asserted Cleared before re-asserted due to continuous interrupt request Thread mode Interrupt return Interrupt re-entered Interrupt Request Interrupt Pending status Processor mode Interrupt get its pending status set again if the request is still asserted after exception exit Processor mode Thread mode Handler mode Multiple interrupt pulses before entering ISR Interrupt Request Interrupt Pending status Processor mode Interrupt get its pending Processor mode Thread mode Handler mode

Exception processing The processor accepts an exception if the following conditions are met - The processor is running (not halted or in reset state) -The exception is enabled (with special cases for NMI and HardFault exceptions, which are always enabled) -The exception has higher priority than the current priority level -The exception is not blocked by an exception masking register (e.g., PRIMASK) If the SVC instruction is accidentally used in an exception handler that has the same or higher priority than the SVC exception itself, it will cause the HardFault exception handler to execute An exception entrance sequence contains several operations : -Stacking of a number of registers, including return address to the currently selected stack. This enables an exception handler to be written as a normal C function -Fetching the exception vector (starting address of the exception handler/ISR) -Fetching the instructions for the exception handler to be executed -Update of various NVIC registers and core registers Within the exception handler, you can carry out services for the peripheral that requires service. The processor is in Handler mode when executing an exception handler In ARM Cortex -M processors, the exception return mechanism is triggered using a special return address called EXC_RETURN Exception sequence

Exception processing As the processor enters the exception handler or Interrupt Service Routine (ISR), the value of the Link Register (LR) is updated to a code called EXC_RETURN The value of this code is used to trigger the exception return mechanism when it is loaded into the Program Counter (PC) using BX, POP, or memory load instructions (LDR or LDM) Some bits of the EXC_RETURN code are used to provide additional information about the exception sequence EXC_RETURN

Exception processing C compilers for ARM architecture follow a specification from ARM called the AAPCS According to AAPCS, a C function can modify R0 to R3, R12, R14 (LR), and PSR R0-R3, R12, LR, and PSR(+ S0-S15 in Cortex-M4) are called “caller saved registers” R4-R11(+ S16-S31 in Cortex-M4) are called “callee-saved registers” Typically, a function call uses R0 to R3 as input parameters, and R0 as the return result, and if the return value is 64 bits, R1 will also be used as the return result Exception Handler in C The block of data that are pushed to the stack memory at exception entrance Stack frame

Exception processing When an exception occurs and is accepted by the processor, the stacking sequence starts to push the registers into the stack, forming the stack frame In parallel to the stacking operation (usually on the System bus), the processor can also start the vector fetch(Because of Havard architecture) Exception entrance and stacking At the end of an exception handler, the bit 2 of the EXC_RETURN value generated at the exception entrance is used to decide which stack pointer should be used to extract the stack frame If bit 2 is 0, the processor knows that the main stack was used for stacking To reduce the time required for the unstacking operation, the return address (stacked PC) value is accessed first, so that instruction fetch can start in parallel with the rest of the unstacking operation) Exception return and unstacking

Interrupt latency and exception handling optimization The term interrupt latency refers to the delay from the start of the interrupt request to In the Cortex-M3 and Cortex-M4, if the memory system has zero latency, and provided that the bus system design allows vector fetch and stacking to happen at the same time, the interrupt latency is only 12 clock Some situations can increase the interrupt latency -The processor was serving another exception at the same or higher priority. -Debugger accesses to the memory system. -The processor was carrying out an unaligned transfer. From the processor point of view, this might be a single access, but at the bus level it takes multiple cycles, as the bus interface needs to convert the unaligned transfer into multiple aligned transfers. -The processor was carrying out a write to bit-band alias. The internal bus system converts this into a read-modify-write sequence, which takes at least two cycles Interrupt latency When an exception takes place but the processor is handling another exception of the same or higher priority, the exception will enter the pending state the processor skips the unstacking and stacking steps and enters the exception handler of the pended exception as soon as possible Tail Chaining

Interrupt latency and exception handling optimization During stacking operation another exception of higher priority takes place, the higher priority late arrival exception will be serviced first Late arrival If an exception request arrives during the unstacking process of another exception handler that has just finished, the unstacking operation would be abandoned and the vector fetch and instruction fetch for the next exception service begins Pop preemption