工程師應知道的0x10個問題(11): 中斷

MuLong PuYang
2 min readMar 12, 2022

--

英文參考網址

A ‘C’ Test: The 0x10 Best Questions for Would-be Embedded Programmers

中文參考網址

C語言測試 應知道的0x10個基本問題

原文翻譯

11. Interrupts are an important part of embedded systems. Consequently, many compiler vendors offer an extension to standard C to support interrupts. Typically, this new key word is __interrupt. The following code uses __interrupt to define an interrupt service routine. Comment on the code.

中斷是嵌入式系統很重要的一部分。因此,很多編譯器供應商提供一個標準C的擴展來支持中斷。典型的,這個新的key word是__interrupt。以下的程式碼使用__interrupt來定義一個中斷service routine。評論這個程式碼

__interrupt double compute_area(double radius) {

double area = PI * radius * radius;

printf(“nArea = %f”, area);

return area;

}

This function has so much wrong with it, it’s almost tough to know where to start.

這個函數有很多錯誤在裡面,幾乎很難知道要從哪邊開始

(a) Interrupt service routines cannot return a value. If you don’t understand this, then you aren’t hired.

(a) 中斷service routines無法回傳數值。如果你不知道這個,你無法被聘僱。

(b) ISR’s cannot be passed parameters. See item (a) for your employment prospects if you missed this.

(b) ISR無法傳遞參數。如果你錯過了這個,你的聘僱期望會跟(a)一樣

(c) On many processors / compilers, floating point operations are not necessarily re-entrant. In some cases one needs to stack additional registers, in other cases, one simply cannot do floating point in an ISR. Furthermore, given that a general rule of thumb is that ISRs should be short and sweet, one wonders about the wisdom of doing floating point math here.

(c) 在很多處理器/編譯器,浮點數操作是不必要可重入的。在一些案例中需要堆積額外的暫存器,在其他案例中,就簡單的不能在ISR中做浮點數。而且,在這裡一個人不知道關於做浮點數數學的智慧,鑒於一個通用的經驗法則來說,ISR應該要是短而甜的,

注:
(1)關於可重入大家可以參考以下兩篇文章
可重入函數 程式設計規範
可重入的危機頁面
(2) In some cases one needs to stack additional registerss,這裡我是直接照原文翻: 在一些案例中需要堆積額外的暫存器,中文來源是翻成: 有些處理器/編譯器需要讓多餘的暫存器入棧(PUSH入堆疊)(3)Furthermore, given that a general rule of thumb is that ISRs should be short and sweet,這裡原文是寫短而甜的,我參照另外一個英文來原
The 0x10 Best Questions for Would-be Embedded Programmers
這邊我就直接照著原文翻譯,如果有讀者認為有更好的翻法歡迎留言跟我說,另外在中文來源的話這裡是翻成: ISR應該是短而有效率的
(4)one wonders about the wisdom of doing floating point math here.這句話我自認為我翻得不好,如果有網友有更好的翻好,歡迎留言跟我說,在中文來源裡,是翻成: 在ISR中做浮點運算是不明智的。而我這邊是認為,wonder有想知道與疑惑、不知道的意味在,所以這邊given that a general rule of thumb is that ISRs should be short and sweet,我覺得英文應該是要表達這個意思:一個人不知道...,鑒於...的情況下,所以我這裡整句就會翻成像以下這樣
原文:
Furthermore, given that a general rule of thumb is that ISRs should be short and sweet, one wonders about the wisdom of doing floating point math here.
翻譯:
而且,在這裡一個人不知道關於做浮點數數學的智慧,鑒於一個通用的經驗法則來說,ISR應該要是短而甜的,

(d) In a similar vein to point (c), printf() often has problems with reentrancy and performance. If you missed points (c)& (d) then I wouldn’t be too hard on you. Needless to say, if you got these two points, then your employment prospects are looking better and better.

(d) 從(c)一脈相承過來,printf()通常在可重入與效能有問題。如果你錯過了(c)與(d)的點,那我不會太為難你。另外不用多說,如果你得到了這兩點,那麼你獲得聘僱的預期將越來越高

--

--