# Interrupt、Exception & Timer ###### tags: `EOS` ## Interrupt ### external - 硬體併發 設備收到嵌入式上的處理器的工作,和處理器併行處理,完成工作後送出外部中斷。 - 服務請求管理: 包含處理器外部的計時器、網路介面設備,送出完成工作的外部中斷。 ### Signal > s/w interrupt 、 asynchronous asynchronous signal routine (ASR) ![](https://hackmd.io/_uploads/H1QJDfXLn.png) ![](https://hackmd.io/_uploads/BJcCDX7In.png) Signal Vector Table - Element in vector table is pointer or offset to ASR - NULL if no handler is assigned to signal - CATCH signal: Writing pointer or offset to ASR into table ![](https://hackmd.io/_uploads/SJekO7Q8n.png) #### Programmable interrupt controller (PIC) example: - Four interrupt sources - Airbag sensor - Break sensor - Fuel-level sensor - Real-time clock - interrupt table * Priority: higher priority interrupt source can preempt the processing of a lower priority interrupt * Max Freq. : process time constraint placed on all ISRs that have the smallest impact on the overall system * Vector Addr. : memory address that the ISR must be installed * IRQ: Interrupt number ## Exception `(p.91)` 1. Asynchronous 同步: 來自內部依序執行的程式碼。 除以零、存取無效的記憶體位置 非同步: 來自外部硬體。 中斷執行中的執行緒,並執行相應的非同步例外處理程式碼。 2. Maskable 可以通過軟體操作來阻塞 (屏蔽)或啟用這些例外。這意味著當這些例外發生時,系統可以選擇是否要處理它們。 non-maskable interrupt (NMI) 非可屏蔽中斷 3. Precise program counter 程式計數器按照寫入順序指向有問題的指令。 | Priority | Type | | | -------- | -------- | -------- | |Highest |Asynchronous| Non-maskable| |High |Synchronous |Precise| |Mid |Synchronous |Imprecise| |Lowest |Asynchronous |Maskable| ![](https://hackmd.io/_uploads/H1zTG9gU3.png) ### Loading Exception Vector 同步例外:向量表檢索相應的ESR 非同步例外:中斷控制器(PIC)必須確定該中斷是否被禁用(或屏蔽)。 如果中斷被屏蔽,PIC會忽略中斷,並且處理器的狀態不受影響。 如果中斷沒有被屏蔽,PIC會引發中斷信號給處理器。 ![](https://hackmd.io/_uploads/S1y3fa-L2.png) ### ESR(Exception Service Routine)& ISR(Interrupt Service Routine)差別 控制權:ESR通常無法阻止其他例外發生,而ISR可以阻止相同或優先級低於它的中斷發生。 處理器狀態:ESR需要保存額外的處理器狀態信息,以便在例外處理期間進行恢復。這包括保存和恢復暫存器的值、程式計數器(PC)和其他相關狀態。 > IMR(Interrupt Mask Register):IMR僅適用於可屏蔽的非同步例外,並且不會被同步例外例程保存。 ### Exception Timing ![](https://hackmd.io/_uploads/BkOrNRZIh.png) * 中斷延遲 Interrupt latency (TB)是指從中斷引發的時刻到中斷服務例程(ISR)開始執行的時刻之間的時間間隔。不可預測和不可控制的,因為它受到多個因素的影響。這意味著中斷延遲的時間可以是不受限制的,而且ISR無法控制。 * 處理時間 Processing time (TC)取決於ISR的具體實現方式,即ISR執行所需的時間。這取決於ISR內部的代碼和操作,以及處理器的性能。 * 響應時間 Response time (TD)是指從中斷引發的時刻到ISR執行完畢的時刻之間的時間間隔。響應時間等於中斷延遲加上處理時間,即TD = TB + TC。 ![](https://hackmd.io/_uploads/rk56SC-8n.png) 法二: ISR 分兩部分執行 1. interrupt context 1. daemon task context 優點: 1. 減少中斷上下文的處理時間 1. 同時允許其他較高優先級的任務在受到較小影響的情況下完成執行。 ## Stack Overflow 中斷和例外都會發生堆疊溢位 ![](https://hackmd.io/_uploads/HyTsNpZU2.png) task 2 放在 application data 被三個中斷之後 task 3 發生堆疊溢位 ### Switching SP to Exception Frame ![](https://hackmd.io/_uploads/ryE4wpbL2.png) ## Timer Programmable interval timer (PIT): timer chip: 每隔固定時間送一次計時中斷 Timer interrupt-rate register (TINTR) is the most important register: 計算多少時間後要送一次中斷 - TINTR = F(x) - where x = frequency of the input crystal (石英震盪器) ![](https://hackmd.io/_uploads/B1FGjQXU3.png) ![](https://hackmd.io/_uploads/r1-lhm78h.png) ![](https://hackmd.io/_uploads/BJEgn7XIn.png) 中斷間隔時間:1次 / 100 ms app1:200 ms / count_down=2 app2:300 ms / count_down=3 app3:500 ms / count_down=5 經過兩次中斷之後 app1:200 ms / count_down=0 到達送出 app2:300 ms / count_down=1 app3:500 ms / count_down=3 ![](https://hackmd.io/_uploads/r1efhmQU3.png)