EECS 373 Design of Microprocessor-Based Systems Mark Brehob University of Michigan

44
1 EECS 373 Design of Microprocessor-Based Systems Mark Brehob University of Michigan Lecture 6: Interrupts January 30 th Slides developed in part by Prof. Dutta

description

EECS 373 Design of Microprocessor-Based Systems Mark Brehob University of Michigan Lecture 6 : Interrupts January 30 th. Slides developed in part by Prof. Dutta. Announcements. Behind due to snow…. Outline. APB review Interrupts Background - PowerPoint PPT Presentation

Transcript of EECS 373 Design of Microprocessor-Based Systems Mark Brehob University of Michigan

Page 1: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

1

EECS 373Design of Microprocessor-Based Systems

Mark BrehobUniversity of Michigan

Lecture 6: InterruptsJanuary 30th

Slides developed in part by Prof. Dutta

Page 2: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Announcements

• Behind due to snow…

2

Page 3: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

3

Outline

• APB review

• Interrupts– Background– Our processor (Cortex M3 and the SmartFusion

implementation)

Page 4: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

So what’s happening here?Read? Write? Wait states?

Page 5: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

And here?

5

The key thing here is that each slave device has its own read data (PRDATA) bus!

Page 6: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

6

And this?

Setup phase beginswith this rising edge

SetupPhase

Access

Phase

WaitState

WaitState

Page 7: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Verilog!

7

/*** APB3 BUS INTERFACE ***/ input PCLK, // clock input PRESERN, // system reset input PSEL, // peripheral select input PENABLE, // distinguishes access phase output wire PREADY, // peripheral ready signal output wire PSLVERR, // error signal input PWRITE, // distinguishes read and write cycles input [31:0] PADDR, // I/O address input wire [31:0] PWDATA, // data from processor to I/O device (32 bits) output reg [31:0] PRDATA, // data to processor from I/O device (32-bits) /*** I/O PORTS DECLARATION ***/ output reg LEDOUT, // port to LED input SW // port to switch ); assign PSLVERR = 0; assign PREADY = 1;

Page 8: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

8

APB state machine

• IDLE– Default APB state

• SETUP– When transfer required– PSELx is asserted– Only one cycle

• ACCESS– PENABLE is asserted– Addr, write, select, and

write data remain stable

– Stay if PREADY = L– Goto IDLE if PREADY =

H and no more data– Goto SETUP is PREADY

= H and more data pending

We’ll spend a bit more time on this next week…

Page 9: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Interrupts

Merriam-Webster: – “to break the uniformity or continuity of”

• Informs a program of some external events• Breaks execution flow

Key questions:• Where do interrupts come from?• How do we save state for later

continuation?• How can we ignore interrupts?• How can we prioritize interrupts?• How can we share interrupts?

9

Page 10: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

I/O Data Transfer

Two key questions to determine how data is transferred to/from a non-trivial I/O device:

1. How does the CPU know when data is available?a. Pollingb. Interrupts

2. How is data transferred into and out of the device?a. Programmed I/Ob. Direct Memory Access (DMA)

Page 11: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Interrupts

Interrupt (a.k.a. exception or trap): • An event that causes the CPU to stop executing the current

program and begin executing a special piece of code called an interrupt handler or interrupt service routine (ISR). Typically, the ISR does some work and then resumes the interrupted program.

Interrupts are really glorified procedure calls, except that they:• can occur between any two instructions• are transparent to the running program (usually)• are not explicitly requested by the program (typically)• call a procedure at an address determined by the type of

interrupt, not the program

Page 12: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Two basic types of interrupts(1/2)

• Those caused by an instruction– Examples:

• TLB miss• Illegal/unimplemented instruction• div by 0• Trap instruction

– Names:• Trap, exception, software interrupt

Page 13: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Two basic types of interrupts(2/2)

• Those caused by events external to the core.– External device– Reset button– Timer expires– Power failure– System error

• Names:– interrupt, external interrupt, hardware

interrupt

Page 14: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

How it works

• Something tells the processor core there is an interrupt

• Core transfers control to code that needs to be executed

• Said code “returns” to old program• Much harder then it looks.

– Why?

Page 15: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

… is in the details

• How do you figure out where to branch to?

• How to you ensure that you can get back to where you started?

• Don’t we have a pipeline? What about partially executed instructions?

• What if we get an interrupt while we are processing our interrupt?

• What if we are in a “critical section?”

Page 16: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Where

• If you know what caused the interrupt then you want to jump to the code that handles that interrupt.– If you number the possible interrupt

cases, and an interrupt comes in, you can just branch to a location, using that number as an offset (this is a branch table)

– If you don’t have the number, you need to poll all possible sources of the interrupt to see who caused it.• Then you branch to the right code

Page 17: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Get back to where you once belonged

• Need to store the return address somewhere.– Stack might be a scary place.

• That would involve a load/store and might cause an interrupt (page fault)!

– So a dedicated register seems like a good choice• But that might cause problems later…

Page 18: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Snazzy architectures

• Be aware that processors sometimes “cheat”– Execute instructions out-of-order.– Can happen even in a (mostly) “in-order”

processor due to variable execution latencies.

• So when an interrupt occurs, need to get things in order.– Could be a delay.

Page 19: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Nested interrupts

• If we get one interrupt while handling another what to do?– Just handle it

• But what about that dedicated register?

• What if I’m doing something that can’t be stopped?

– Ignore it• But what if it is important?

– Prioritize• Take those interrupts you care about.

Ignore the rest• Still have dedicated register problems.

Page 20: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Critical section

• We probably need to ignore some interrupts but take others.– Probably should be sure our code can’t cause an

exception.– Use same prioritization as before.

Page 21: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

21

Outline

• Interrupts– Background– Our processor (Cortex M3 and the SmartFusion

implementation)

Page 22: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

22

Page 23: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

SmartFusion interrupt sources

23

Page 24: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

And the interrupt vectors (in startup_a2fxxxm3.s found in CMSIS, startup_gcc)g_pfnVectors:

.word _estack .word Reset_Handler .word NMI_Handler .word HardFault_Handler .word MemManage_Handler .word BusFault_Handler .word UsageFault_Handler .word 0 .word 0 .word 0 .word 0 .word SVC_Handler .word DebugMon_Handler .word 0 .word PendSV_Handler .word SysTick_Handler .word WdogWakeup_IRQHandler .word BrownOut_1_5V_IRQHandler .word BrownOut_3_3V_IRQHandler.............. (they continue)

24

Page 25: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Interrupt handlers

25

Page 26: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Pending interrupts

26

The normal case. Once Interrupt request is seen, processor puts it in “pending” state even if hardware drops the request. IPS is cleared by the hardware once we jump to the ISR.

This figure and those following are from The Definitive Guide to the ARM Cortex-M3, Section 7.4

Page 27: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

27

In this case, the processor never took the interrupt because we cleared the IPS by hand (via a memory-mapped I/O register)

Page 28: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

28

Page 29: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

29

Page 30: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Answer

30

Page 31: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Interrupt pulses before entering ISR

31

Page 32: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Answer

32

Page 33: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

33

Page 34: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

34

Page 35: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

35

Page 36: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

36

Page 37: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

37

Page 38: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

38

Page 39: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

39

Page 40: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

40

Page 41: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Masking

41

Page 42: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

42

Page 43: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

Example of Complexity: The Reset Interrupt

1) No power2) System is held in RESET as long as VCC15 < 0.8V

a) In reset: registers forced to defaultb) RC-Osc begins to oscillatec) MSS_CCC drives RC-Osc/4 into FCLKd) PORESET_N is held low

3) Once VCC15GOOD, PORESET_N goes higha) MSS reads from eNVM address 0x0 and 0x4

43

Page 44: EECS 373 Design of Microprocessor-Based Systems Mark  Brehob University of Michigan

The xPSR register layout

44