Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf ·...

37
Functions, Subroutines & Interrupts Lecture 4

Transcript of Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf ·...

Page 1: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Functions, Subroutines & Interrupts

Lecture 4

Page 2: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

• A well-structured program should be divided into separate modules—functions in C subroutines in assembly language.

• Interrupts are a major feature of most embedded software. They are vaguely like functions that are called by hardware rather than software. The distinction sounds trivial but it makes them much harder to handle because the processor must be able to return correctly to its activity before it was interrupted.

Page 3: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

“950=0x03B6”

Page 4: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

When DelayLoop sub is called

Page 5: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in
Page 6: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

The return address is stored in the stack, which grows down from the highest address in RAM. The CPU keeps track of the stack through the stack pointer (SP) register .The vital point is that the stack pointer must be initialized to the first address beyond the RAM before any subroutine is called (it is decremented before it is first used).

Page 7: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in
Page 8: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in
Page 9: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Interrupt vector for reset 0xFFFE@0xFFFE = 0x3100

Page 10: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

When chip is resetted

Page 11: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in
Page 12: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Storage for Local Variables

• CPU registers are simple and fast. The convention is that registers R4–R11 may be used and their values should be preserved. This is done by pushing their values on to the stack.

• A second approach is to use a fixed location in RAM. There are two serious disadvantages to this approach. The first is that the space in RAM is reserved permanently, even when the function is not being called, which is wasteful. Second, the function is not reentrant. This means that thefunction cannot call a second copy of itself, either directly or with nested functions between. Both copies would try to use the same variable and interfere with each other, unless the intention was for them to share the variable. This would obviously wreck a software delay loop, for instance.

• The third approach is to allocate variables on the stack and is generally used when a program has run out of CPU registers. This can be slow in older designs of processors, but the MSP430 can address variables on the stack with its general indexed and indirect modes. Of course it is still faster to use registers in the CPU when these are available.

Page 13: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Before: SP:0x3B4R4:0x6975After push.wSP:0x3B2@0x3B2: 0x6975

save and restore R4 correctly

Page 14: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Before: SP:0x3B2R4:0x0000After pop.wSP:0x3B4R4:0x6975

Page 15: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

delay loop uses two localvariables on the stack

Page 16: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in
Page 17: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Passing Parameters to a Subroutineand Returning a Result

@6(SP) = 0x000A parameter passed@4(SP) return address@2(SP)=0x0082 Big loop counter@0(SP)=0x0064 Little loop counter

Page 18: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in
Page 19: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

•Use R4-R11 for local vars•Use R12-R15 for passing args. Further arguments are pushed on to the stack, which is also used to return a large value, typically a structure (which must be very large if it does not fit into four registers).

Page 20: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Interrupts

• They are like functions but with the critical distinction that they are requested by hardware at unpredictable times rather than called by software in an orderly manner.

Page 21: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Interrupts

• Interrupts are commonly used for a range of applications:• Urgent tasks that must be executed promptly at higher priority than

the main code.• However, it is even faster to execute a task directly by hardware if

this is possible.• Infrequent tasks, such as handling slow input from humans. This

saves the overhead of regular polling.• Waking the CPU from sleep. This is particularly important in the

MSP430, which typically spends much of its time in a low-power mode and can be awakened only by an interrupt.

• Calls to an operating system. These are often processed through a trap or software interrupt instruction but the MSP430 does not have one.

Page 22: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

ISR

• The code to handle an interrupt is called an interrupt handler or interrupt service routine(ISR).

Page 23: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

flags

• Interrupts can be requested by most peripheral modules and some in the core of the MCU, such as the clock generator. Each interrupt has a flag, which is raised (set) when the condition for the interrupt occurs.

• For example, Timer_A sets the TAIFG flag in the TACTL register when the counter TAR returns to 0

Page 24: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Maskable (non-Maskable)

• Most interrupts are maskable, which means that they are effective only if the general interrupt enable (GIE) bit is set in the status register (SR).

• They are ignored if GIE is clear.

• The (non)maskable interrupts cannot be suppressed by clearing GIE. The reason for the parentheses around (non) is that these interrupts also require bits to be set in special function or peripheral registers to enable them.

Page 25: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

MSP430 uses vectored interrupts

• A Vectored interrupt means the address of each ISR—its vector—is stored in a vector table at a defined address in memory.

• In most cases each vector is associated with a unique interrupt but some sources share a vector. The ISR itself must locate the source of interrupts that share vectors.

• For example, TAIFG shares a vector with the capture/compare interrupts for all channels of Timer_A other than 0. Channel 0 has its own interrupt flag TACCR0 CCIFG and separate vector.

Page 26: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Priorities

• Each interrupt vector has a distinct priority, which is used to select which vector is taken if more than one interrupt is active when the vector is fetched.

• The priorities are fixed in hardware and cannot be changed by the user.

• They are given simply by the address of the vector: A higher address means a higher priority. The reset vector has address 0xFFFE, which gives it the top priority, followed by 0xFFFC for the single nonmaskable interrupt vector. The vectors for the maskable interrupts depend on the peripherals in a particular device and are listed in a table of Interrupt Vector Addresses in the data sheet.

Page 27: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Handling Interrupts

Interrupts must be handled in such a way that the code that was interrupted can be resumed without error. This means in particular that the values in the CPU registers must be restored.

1. Copies of all the registers are saved on the stack automatically as part of the process for entering an interrupt.

2. The opposite approach is for the hardware to save only the absolute minimum, which is the return address in the PC as in a subroutine. This is much faster but it is up to the user to save and restore values of the critical registers, notably the status register.

• The MSP430 is close to the second extreme but stacks both the return address and the status register.

Page 28: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

What Happens when an Interrupt Is Requested?

• It starts when a flag bit is set in the module when the condition for an interrupt occurs. – For example, TAIFG is set when the counter TAR

returns to 0. This is passed to the logic that controls interrupts if the corresponding enable bit is also set, TAIE in this case.

• The request for an interrupt is finally passed to the CPU if the GIE bit is set.

• Hardware then performs the following steps to launch the ISR:

Page 29: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Steps before ISR1. Any currently executing instruction is completed if the CPU was active

when the interrupt was requested. MCLK is started if the CPU was off.

2. The PC, which points to the next instruction, is pushed onto the stack.

3. The SR is pushed onto the stack.

4. The interrupt with the highest priority is selected if multiple interrupts are

waiting for service.

5. The interrupt request flag is cleared automatically for vectors that have a

single source. Flags remain set for servicing by software if the vector has

multiple sources, which applies to the example of TAIFG.

6. The SR is cleared, which has two effects. First, further maskable interrupts

are disabled because the GIE bit is cleared; nonmaskable interrupts remain

active. Second it terminates any low-power mode.

7. The interrupt vector is loaded into the PC and the CPU starts to execute the

interrupt service routine at that address., it terminates any low-power mode.

Page 30: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

This sequence takes six clock cycles in the

MSP430 before the ISR commences.

Page 31: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Latency

• The delay between an interrupt being requested and the start of the ISR is called the latency. If the CPU is already running it is given by the time to execute the current instruction, which might only just have started when the interrupt was requested, plus the six cycles needed to execute the launch sequence. This should be calculated for the slowest instruction to get the worst case. Format I instructions take up to 6 clock cycles so the overall latency is 12 cycles.

Page 32: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Returning from InterruptAn interrupt service routine must always finish with the special

return from interrupt instruction reti, which has the following

actions:

1. The SR pops from the stack. All previous settings of GIE and the mode control bits are now in effect, regardless of the settings used during the interrupt service routine. In particular, this reenables maskable interrupts and restores the previous low-power mode of operation if there was one.

2. The PC pops from the stack and execution resumes at the point where it was interrupted. Alternatively, the CPU stops and the device reverts to its low-power mode before the interrupt.

Page 33: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Interrupt Service Routines(in Assembly)

An ISR looks almost identical to a subroutine but

with two distinctions:

• The address of the subroutine, for which we can use its name (a label on its first line), must be stored in the appropriate interrupt vector.

• The routine must end with reti rather than ret so that the correct sequence of actions takes place when it returns.

Page 34: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in
Page 35: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Look them up in the header file!

#define TASSEL_2 (2*0x100u) /* Timer A clock source select: 2 -SMCLK */

#define TA0CCR0 TACCR0 /* Timer A Capture/Compare 0 */#define TA0CCTL0 TACCTL0 /* Timer A Capture/Compare Control

0 */#define TACLR (0x0004) /* Timer A counter clear */#define TAIE (0x0002) /* Timer A counter interrupt enable */#define TAIFG (0x0001) /* Timer A counter interrupt flag */#define MC_1 (1*0x10u) /* Timer A mode control: 1 - Up to

CCR0 */#define ID_3 (3*0x40u) /* Timer A input divider: 3 - /8 */#define CCIE (0x0010) /* Capture/compare interrupt enable */

Page 36: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Refer page 105-107

Page 37: Functions, Subroutines & Interruptscse.yeditepe.edu.tr/~sgoren/fall2013/CSE421/web_lect4.pdf · •A well-structured program should be divided into separate modules—functions in

Another Example: Toggle the LED state with push button ISR