IN2305-II Embedded Programming
description
Transcript of IN2305-II Embedded Programming
![Page 1: IN2305-II Embedded Programming](https://reader036.fdocuments.us/reader036/viewer/2022083006/56813fbf550346895daa9d33/html5/thumbnails/1.jpg)
IN2305-IIEmbedded Programming
Lecture 4:Interrupts
![Page 2: IN2305-II Embedded Programming](https://reader036.fdocuments.us/reader036/viewer/2022083006/56813fbf550346895daa9d33/html5/thumbnails/2.jpg)
in2305-II: L4 2
Interrupts: Principle
CPUdev 1
dev N
IRQcontr
...MOVE R1, (var-addr)MULT R1, 9DIVIDE R1, 5ADD R1, 32...
IRQ #
ISR: PUSH R1...POP R1RET
HW CALL
![Page 3: IN2305-II Embedded Programming](https://reader036.fdocuments.us/reader036/viewer/2022083006/56813fbf550346895daa9d33/html5/thumbnails/3.jpg)
in2305-II: L4 3
X32 Interrupts
IRQ controller preprocesses multiple IRQ’sEach device: (IRQ #, priority)Vectored IRQInterrupts NOT disabled Automatic ISR preemption if prio IRQ > prio current ISRNormal call saves context -> no interrupt keyword
IRQcontr
IRQ #CPU
![Page 4: IN2305-II Embedded Programming](https://reader036.fdocuments.us/reader036/viewer/2022083006/56813fbf550346895daa9d33/html5/thumbnails/4.jpg)
in2305-II: L4 4
Example: ISR version of hello.c#define IRQ_BUTTONS = INTERRUPT_BUTTONS
void isr_buttons(void) {X32_leds = X32_buttons;if (X32_buttons == 0x09) done = 1;
}
void main(void) {SET_INTERRUPT_VECTOR(IRQ_BUTTONS),&isr_buttons);SET_INTERRUPT_PRIORITY(IRQ_BUTTONS),10);ENABLE_INTERRUPT(IRQ_BUTTONS);ENABLE_INTERRUPT(INTERRUPT_GLOBAL);printf(“Hello World!\r\n”);while (! done) X32_disp = X32_clock;DISABLE_INTERRUPTS(INTERRUPT_GLOBAL);
}
![Page 5: IN2305-II Embedded Programming](https://reader036.fdocuments.us/reader036/viewer/2022083006/56813fbf550346895daa9d33/html5/thumbnails/5.jpg)
in2305-II: L4 5
X32: Demo
Demo ..
(x32_projects.tgz, buttons.c, console.c)
![Page 6: IN2305-II Embedded Programming](https://reader036.fdocuments.us/reader036/viewer/2022083006/56813fbf550346895daa9d33/html5/thumbnails/6.jpg)
in2305-II: L4 6
Interrupts: Data Sharing Problem
void isr_read_temps(void) {
iTemp[0] = peripherals[..]; iTemp[1] = peripherals[..];}
void main(void) {
...while (TRUE) {
tmp0 = iTemp[0];tmp1 = iTemp[1];
if (tmp0 != tmp1)panic();
}}
73 74
NOT ATOMIC!
![Page 7: IN2305-II Embedded Programming](https://reader036.fdocuments.us/reader036/viewer/2022083006/56813fbf550346895daa9d33/html5/thumbnails/7.jpg)
in2305-II: L4 7
Interrupts: Data Sharing Problem
single C expressions usually aren’t atomic either ..:
void isr_read_temps(void) {
iTemp[0] = peripherals[..]; iTemp[1] = peripherals[..];}
void main(void) {
...while (TRUE) {
if (iTemp[0] != iTemp[1])panic();
}}
73 74
NOT ATOMIC!
![Page 8: IN2305-II Embedded Programming](https://reader036.fdocuments.us/reader036/viewer/2022083006/56813fbf550346895daa9d33/html5/thumbnails/8.jpg)
in2305-II: L4 8
Solutions (1)
disable interrupts that trigger those ISRs that share the data
...while (TRUE) {
!! DISABLE INTtmp0 = iTemp[0];tmp1 = iTemp[1];!! ENABLE INT
if (tmp0 != tmp1)panic();
}
The critical section is now atomic
use semaphores to protect critical section (discussed in a later lecture)
![Page 9: IN2305-II Embedded Programming](https://reader036.fdocuments.us/reader036/viewer/2022083006/56813fbf550346895daa9d33/html5/thumbnails/9.jpg)
in2305-II: L4 9
Solutions (2)
don’t disable interrupts but write ingenious code, e.g., involving alternating data buffers (Fig. 4.15) or even queues (Fig. 4.16) such that ISR and (main) code never access the same dataproblem: code becomes error-prone and (too) hard to readrule: keep it simple, just disable interrupts, as long as you adhere to:
keep the critical sections SHORT keep the ISRs SHORT (to minimize latency, see later)
![Page 10: IN2305-II Embedded Programming](https://reader036.fdocuments.us/reader036/viewer/2022083006/56813fbf550346895daa9d33/html5/thumbnails/10.jpg)
in2305-II: L4 10
X32: Demo
Demo ..
(x32_projects.tgz, critical.c)
![Page 11: IN2305-II Embedded Programming](https://reader036.fdocuments.us/reader036/viewer/2022083006/56813fbf550346895daa9d33/html5/thumbnails/11.jpg)
in2305-II: L4 11
Interrupt Latency
Quick response to IRQ may be neededDepends on previous rules: worst-case latency = t_disabled + t_higher prio ISRs + t_myISR
main
nw IRQip IRQ
ip ISRnw ISR
DI EI
250 300100
![Page 12: IN2305-II Embedded Programming](https://reader036.fdocuments.us/reader036/viewer/2022083006/56813fbf550346895daa9d33/html5/thumbnails/12.jpg)
in2305-II: L4 12
X32: Demo
Demo ..
(x32_projects.tgz: timing.c)