The Interrupt Program Status Register (IPSR) Contains the Exception Type Number of the Current ISR
Total Page:16
File Type:pdf, Size:1020Kb
Lab #4: POLLING & INTERRUPTS CENG 255 - iHaz - 2016 Lab4 Objectives Recognize different kinds of exceptions. Understand the behavior of interrupts. Realize the advantages and disadvantages of interrupts relative to polling. Explain what polling is and write code for it. Explain what an Interrupt Service Routine (ISR) is and write code for it. Must be Done Individually Before the Lab. Pre-Lab4 Questions Describe the major difference between polling 1 and interrupt. 2 Explain what APSR, IPSR and EPSR are. 3 Explain how PRIMASK is used. 4 Explain what an exception vector table is. 5 Explain what PORT Control registers are. Must be Done Individually Before the Lab. Polling vs. Interrupt Describe the major difference between polling and interrupt. Polling Interrupt • In polling, the processor continuously • An interrupt is a signal to the microprocessor polls or tests a given device as to whether from a device that requires attention. Upon it requires attention. The polling is receiving an interrupt signal, the microcontroller carried out by a polling program that interrupts whatever it is doing and serves the shares processing time with the currently device. The program which is associated with the running task. A device indicates it interrupt is called the interrupt service routine requires attention by setting a bit in its (ISR) or interrupt handler. device status register. • When ISR is done, the microprocessor continues • Polling the device usually means reading with its original task as if it had never been its status register every so often until the interrupted. This is achieved by pushing the device's status changes to indicate that it contents of all of its internal registers on the has completed the request. This is an stack. The registers will then be pop(ed) from the inefficient method and much of the stack on completion of the interrupt call, processors time is wasted on unnecessary allowing the microprocessor to resume its polls. original task. Polling vs. Interrupt A) Polling: FaceBook with notifications Disabled B) Interrupt: FaceBook with notifications Enabled. Example: Detecting a pulse lasting for 1ms, which appears once in 10s at random timing. Polling method: Check every 500us for example for this pulse so we don’t miss it. Interrupt detection ISR: Triggers itself and executes this only when pulse occurs (much more efficient). Polling is good for operations that are not dependent on exact timings. APSR, IPSR, EPSR and PRIMASK Explain what APSR, IPSR and EPSR are. Program status register The Program Status Register (PSR) combines: ● Application program status register (APSR): Contains the current state of The condition flags from previous instruction executions. ● Interrupt program status register (IPSR): contains the exception type number of the current ISR. Bit[31:6] of IPSR are reserved while Bit[5:0] are applied to the current ISR number. ● Execution program status register (EPSR): The EPSR contains the Thumb state bit. Explain how PRIMASK is used. The priority mask register (PRIMASK) prevents activation of all exceptions with configurable priority. Bit[31:1] of PRIMASK are reserved and there is no effect when Bit[0]=0, but it prevents the activation of all exceptions with configurable priority when Bit[0]=1. Exception Vector Table Explain what an exception vector table is. ARM provides the Cortex Microcontroller Software Interface Standard (CMSIS) for programming the Cortex-M0 microcontroller. The CMSIS defines a standard way to access peripheral registers and to define exception vectors, the names of the registers of the core peripherals and the core exception vectors. The entry for IRQ0 is located at 0x00000040 because the IRQ0 is located at position 16 in the vector table. STM32F051 Microcontroller GPIO Explain what PORT Control registers are. STM32F051 microcontroller has five general-purpose input/output (GPIO) ports, Port A, B, C, D and F. Each port can have up to 16 pins, and each port has associated with it the following set of registers: • GPIO port mode register (GPIOx_MODER) • GPIO port output register (GPIOx_OTYPER) • GPIO port output speed register (GPIOx_OSPEEDR) • GPIO port pull-up/pull-down register (GPIOx_PUPDR) • GPIO port input data register (GPIOx_IDR) • GPIO port output data register (GPIOx_ODR) • GPIO port bit set/reset register (GPIOx_BSRR) • GPIO port configuration lock register (GPIOx_LCKR) • GPIO alternate function low register (GPIOx_AFRL) • GPIO alternate function high register (GPIOx_AFRH) • GPIO port bit reset register (GPIOx_BRR) Lab Hardware (Boards) DigiLab DIO1 Board DIO1 board features include • A four digit seven-segment LED display; • 8 individual LEDs; • A 3-bit VGA port; • A four digit seven-segment LED display; • 5 momentary pushbuttons; • 8 slide switches; • A PS2 mouse/keyboard port. http://www.ece.uvic.ca/~ceng255/lab/lab_files/interrupt.zip DigiLab DIO1 Board Diagram Digilab DIO1 Board Diagram Abbreviation Definition A1 - A4 Seven-segment anode connection CA - CG Seven-segment cathode connection BTN1-BTN4, BTN5 Push button connection SW1 - SW8 Slide switch connection LD1 - LD8 Light Emitting Diode connection CLK1 Clock connection Digilab DIO1 Board Diagram Digilab DIO1 Board Diagram Digilab DIO1 Board (PINs) DIO1B Pin Signal DIO1B Pin Signal DIO1B Pin Signal DIO1B Pin Signal A1 A21 B1 CA B21 A1 A2 A22 B2 SW1 B22 LD1 A3 A23 B3 CB B23 A2 A4 A24 B4 SW2 B24 LD2 A5 A25 B5 CC B25 A3 A6 A26 B6 SW3 B26 LD3 A7 A27 B7 CD B27 A4 A8 A28 B8 SW4 B28 LD4 A9 A29 B9 CE B29 A10 A30 B10 SW5 B30 LD5 A11 A31 BLU B11 CF B31 A12 A32 PS2D B12 SW6 B32 LD6 A13 A33 GRN B13 CG B33 A14 A34 PS2C B14 SW7 B34 LD7 A15 A35 RED B15 DP B35 BTN5 A16 A36 HS B16 SW8 B36 LD8 A17 A37 VCC B17 BTN2 B37 VCC A18 A38 VS B18 BTN1 B38 LDG A19 A39 GND B19 BTN4 B39 GND A20 A40 VU B20 BTN3 B40 VU The Lab Task Polling Given Required The following program tests the status of a) Revise the program in such a way that another Port PA4, and uses a button to change the button (btn2) can do the same operation. status of port PA4. b) Consider having two buttons, modify the program so that the LEDs toggle only if you push In this experiment, initially all LEDs on the buttons in the following order: Pushing the board are off. button number one (btn1) twice and button number two (btn2) once. Pressing the button (btn1) makes one Include a well-commented listing of your LED toggle. program. For this part of the lab, you are required Explain the changes that you have made in the to change the program as follows: provided program and the http://www.ece.uvic.ca/~ceng255/lab/lab_files/polling.zip The Lab Task Interrupt Given Required The provided program in Figure 9 uses a a) Revise the program in such a way that another button to send an interrupt to the button (btn2) generates another interrupt and processor. increments the content of the counter. The program is running a loop until it b) Consider having two buttons, modify the receives an interrupt. Having received program so that the LEDs toggle only if you the interrupt, the processor completes the receive interrupts in the following order: execution of the current instruction and Pushing button one (btn1) three times and jumps to the interrupt handler. button two (btn2) twice. We increment the contents of a counter Include a well-commented listing of your by one inside the interrupt handler and program. display its contents on the LEDs You are required to change the program Explain the changes that you have made in the to add the following functionality. provided program and the http://www.ece.uvic.ca/~ceng255/lab/lab_files/interrupt.zip What is an Exception Exception and interrupt change the control or execution flow of a program when certain conditions occur. They are designed to be special events which occurrence cannot be predicted precisely. Exceptions occur as a result of an event during a program’s execution. While an interrupt is a hardware event that is triggered by an external source An exception is an automatic procedure call initiated by some events during the execution of a program. What is an Exception - Example If the divisor of a division is equal to zero, a divided- by-zero trap will occur. As a result, the control flow is transferred to some fixed memory location instead of continuing in sequence in the currently executing program. At the fixed location, there exists a branch instruction to a routine called the exception handler, which performs some appropriate actions. The essential point about an exception is that it is synchronous with program execution, meaning that it happens as a result of executing some instructions. Some common exceptions are undefined op-codes, privileged instructions, virtual memory page faults, and system calls (i.e., operating system service request). Interrupts An interrupt is a hardware event that triggers the processor asynchronously to branch from its current PC to a specific location in the memory. After the interrupt stops the currently executing program, it transfers control to a fixed address that calls the appropriate Interrupt Service Routine (ISR), which performs some needed actions. The control will return to the interrupted program when the handler completes the required actions. The external interrupt handler’s address in ARM depends on an 8-bit vector number, which is supplied by the interrupt controller module. It is crucial to return to the interrupted program in the same state as processor was in when the interrupt occurred ARM Interrupt Structure The processor and the Nested Vectored Interrupt Controller (NVIC) prioritize and handle all exceptions that change the normal flow of software control. The NVIC supports up to 32 individual interrupts, each of which has 4 priority levels (higher 0-3 lower).