Write My Paper Button

WhatsApp Widget

GY 6483

联系我们: 手动添加方式: 微信>添加朋友>企业微信联系人>13262280223 或者 QQ: 1483266981

INTERRUPTS EL-GY 6483 Real Time Embedded Systems 2I/O MODELS POLLING I/O 3 PROBLEMS WITH POLLING Suppose a serial port operates at 57600 baud, processor at 18 MHz. for(i = 0; i ICER[0] = (1<MCR |= 0x01; // Arm (enable interrupt) LPC_TIM3->MCR &= ~(0x02); // Don’t reset timer after LPC_TIM3->MCR |= 0x04; // Stop incrementing after // Set priority to 0 (highest) NVIC->IPR[1] &= ~(0xFF); // Enable at interrupt controller NVIC->ISER[0] = (1<AIC_SVR[AT91C_ID_TC1] = (unsigned long)Timer1ISR; … } DEFINING THE ISR CHECK THE TRIGGER Check which peripheral triggered interrupt and/or what kind of

interrupt was triggered. // Check peripheral interrupt register // on timer 3 if (LPC_TIM3->IR & 0x1) { … } If you have multiple sources for this IRQ, don’t stop there; there

may be more than one hit. 28 GENERAL RULES Keep it short. Typical use of interrupts is to set a flag. Avoid calling functions Use volatile on global shared variables Disable other interrupts when possible Know what your platform does automatically and what you

must do in software What happens if you have a long ISR, don’t disable interrupts, and a

frequent trigger 29 REENTRANT FUNCTIONS ISRs should only call reentrant functions: functions that can be

safely called multiple times while it is running. Functions that write to static or global variables are

nonreentrant. Many standard library calls are nonreentrant: malloc printf (most I/O or file function) 30 31 Not reentrant: int t; void swap(int* x, int* y) { t = *x; *x = *y; // hardware interrupt might invoke isr() here while // the function has been called from main or other // non-interrupt code *y = t; } void isr { int x=1, y=2; swap(&x, &y); } REENTRANT FUNCTIONS 32 Be careful about atomicity. Consider this on 8-bit MCU: volatile uint32_t timerCount = 0; void countDown(void) { if (timerCount != 0) { timerCount–; } } int main(void) { timerCount = 2000; SysTickPeriodSet(SysCtlClockGet() / 1000); SysTickIntRegister(&countDown); SysTickEnable(); SysTickIntEnable(); while(timerCount != 0) { … code to run for 2 seconds … } } ATOMICITY 33 EXAMPLE: TIMER3 ISR // global variable set by the interrupt // handler, which is cleared by normal code // when event handled volatile tBoolean gYellowTimeout = FALSE; void TIMER3_IRQHandler(void){ if (LPC_TIM3->IR & 0x1) { __disable_irq(); gYellowTimeout = TRUE; // on some processors, need to manually // clear interrupt flag __enable_irq(); } } 34 PRIORITY AND NESTED INTERRUPTS INTERRUPT CONTROLLER 35 PRIORITY LEVELS Many processor have multiple priority levels for interrupts. On our board: Main priority: lowest priority takes precedence Also define sub priority 36 NESTED INTERRUPTS Some processors have nested interrupts, where an interrupt with

high priority can supersede the one currently running. 37 NON-MASKABLE INTERRUPTS Some processors have non-maskable interrupts that cannot be

disabled. 38 39 PERFORMANCE LATENCY The time it takes between the IRQ and the start of ISR is the

processor’s interrupt latency (usually given in cycles) The system latency is the worst-case latency from when an

interrupt event happens and start of ISR. (Includes interrupt latency

and maximum amount of time that interrupts may be disabled.) The largest contributor to system latency may be time spent in

longest interrupt! 40 LATENCY 41 LATENCY 100 Hz processor, 10 cycle interrupt latency, and timer

interrupt every second: 10% lost to context switching 30 MHz processor, 10 cycle interrupt latency, and

audio interrupts at 44.1 kHz: 1.47% lost to context

switching In the latter example, if processing involves 10

function calls at 10 cycles of overhead each, and 275

cycles of actual work, no interrupt will be handled for

at least 385 cycles, and system spends 50% of time in

interrupt! 42 COST OF INTERRUPTS Interrupts have (latency) overhead Interrupts make system less deterministic Interrupts make debugging harder 43 WHEN TO USE INTERRUPTS System is time-critical Event happens rarely and is expensive to check for 44 45 MODELING EVENT-DRIVEN SYSTEMS WITH FSM FINITE STATE MACHINE Models system as: States (initial state, current state) Transitions Sometimes inputs and outputs Use a state diagram to show relationships. 46 EXAMPLE: TURNSTILE 47 EXAMPLE: TRAFFIC LIGHT States: red, yellow, green Transitions: Go message, Stop message, Timeout Desired behavior: When the light is red and gets message to go, it turns light green. When in green state and gets a message to stop, it turns yellow for a

while, then red (after timeout). Otherwise, stay in same state. Model this with: pseudocode (use switch/case) state diagram 48

GY 6483最先出现在KJESSAY历史案例。