Skip to Main Content
建立嵌入式系統--傑出軟體的設計模式
book

建立嵌入式系統--傑出軟體的設計模式

by Elecia White
October 2012
Intermediate to advanced content levelIntermediate to advanced
328 pages
7h 19m
Chinese
GoTop Information, Inc.
Content preview from 建立嵌入式系統--傑出軟體的設計模式
週邊通訊
|
177
接下來介紹這種編碼方式的餘數運算(modulo),以下是
cb->write (5)
cb-
>read (6)
cb->size (8)
做環型相減時的步驟:
(cb->write - cb->read) & (cb->size - 1)
= ( w - t ) & ( 8 - 1)
= ( -1 ) & ( 7 )
= ( 1111 1111 ) & ( 0000 0111 )
= 0000 0111 = 7
二的補數法讓位元 AND 運算就能夠去除正負號資訊,得到正確的答案。
空緩衝區傳回長度 0,滿的緩衝區長度值為
(cb->size-1)
,記得這點就能輕鬆完成將資
料放入緩衝區的函數:
enum eError CBWrite(struct sCircularBuffer *cb, tElement data){
if (CBLengthData(cb) == (cb->size-1)) {return eErrorBufferFull;}
cb->buf[cb->write] = data;
cb->write = (cb->write + 1) 7 (cb->size -1); // must be atomic
}
最後一行修改
write
變數值的程式碼必須是單元指令,但這單指設定變數值的指令,之
前的準備(
(write+1)
(size-1)
)並不要求單元指令,這些變數都宣告為
uint16_t
型,因此執行在 16 位元處理器時,能以單元指令設定
write
變數值(但在
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

HBASE技術手冊

HBASE技術手冊

Lars George
ANGULARJS建置與執行

ANGULARJS建置與執行

Shyam Seshadri, Brad Green
高效能PYTHON程式設計

高效能PYTHON程式設計

Micha Gorelick, Ian Ozsvald

Publisher Resources

ISBN: 9789862765784