線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2178
推到 Plurk!
推到 Facebook!

BCB 使用 crc 計算程式

答題得分者是:taishyang
dafeng0908
一般會員


發表:4
回覆:2
積分:1
註冊:2015-09-21

發送簡訊給我
#1 引用回覆 回覆 發表時間:2015-10-19 13:28:48 IP:60.251.xxx.xxx 訂閱
今天看到一段用於燒錄程式的原代碼叫用了一個func計算crc
CalcCodeChecksum(ProgramBuf, 65535 ); // programbuf is *.hex of keil
完整程序如下,請問這段是根據什麼寫出來的?要怎麼理解它呢
--------------------------------------------------------------------------------
CalcCodeChecksum(u8 *buf, u32 len)
{

u16 crc_result, crc_bit0, crc_bit1, crc_bit2, crc_bit14_3, crc_bit15;
u8 data, shift, loop;


crc_result = 0xffff;
crc_bit0 = 0;

for(int i=0; i<(int)len; i ){
data = buf[i];
shift = 15;

for(loop=0; loop<8; loop ){
crc_bit0 = crc_result^(data< crc_bit0 >>= 15;
crc_bit0 &= 0x01; //new_crc_result_bit0

crc_bit15 = crc_result^(crc_bit0<<14); //crc_result_b14^crc_bit0_b0
crc_bit15 <<= 1;
crc_bit15 &= 0x8000; //new_crc_result_b15

crc_bit14_3 = crc_result&0x3ffc;
crc_bit14_3 <<= 1;
crc_bit14_3 &= 0x7ff8; //new_crc_result_b14-b3

crc_bit2 = crc_result^(crc_bit0<<1); //crc_result_b1^crc_bit0_b0
crc_bit2 <<= 1;
crc_bit2 &= 0x0004; //new_crc_result_b2

crc_bit1 = crc_result<<1;
crc_bit1 &= 0x0002; //new_crc_result_b1

crc_result = (crc_bit15|crc_bit14_3|crc_bit2|crc_bit1|crc_bit0); //b15 b14-b3 b2 b1 b0
shift--;
}
}
return crc_result;
}
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2015-10-19 16:44:14 IP:59.127.xxx.xxx 訂閱
很熟悉的感覺^^
應該可以不用刻意理解裡面的運作,這function算出來的CRC是用來跟JTAG燒入後,用JTAG讀出來的結果作比對
若相同表示JTAG燒入OK


===================引 用 dafeng0908 文 章===================
今天看到一段用於燒錄程式的原代碼叫用了一個func計算crc
CalcCodeChecksum(ProgramBuf, 65535 ); // programbuf is *.hex of keil
完整程序如下,請問這段是根據什麼寫出來的?要怎麼理解它呢
--------------------------------------------------------------------------------
CalcCodeChecksum(u8 *buf, u32 len)
{

u16 crc_result, crc_bit0, crc_bit1, crc_bit2, crc_bit14_3, crc_bit15;
u8 data, shift, loop;


crc_result = 0xffff;
crc_bit0 = 0;

for(int i=0; i<(int)len; i ){
data = buf[i];
shift = 15;

for(loop=0; loop<8; loop ){
crc_bit0 = crc_result^(data< crc_bit0 >>= 15;
crc_bit0 &= 0x01; //new_crc_result_bit0

crc_bit15 = crc_result^(crc_bit0<<14); //crc_result_b14^crc_bit0_b0
crc_bit15 <<= 1;
crc_bit15 &= 0x8000; //new_crc_result_b15

crc_bit14_3 = crc_result&0x3ffc;
crc_bit14_3 <<= 1;
crc_bit14_3 &= 0x7ff8; //new_crc_result_b14-b3

crc_bit2 = crc_result^(crc_bit0<<1); //crc_result_b1^crc_bit0_b0
crc_bit2 <<= 1;
crc_bit2 &= 0x0004; //new_crc_result_b2

crc_bit1 = crc_result<<1;
crc_bit1 &= 0x0002; //new_crc_result_b1

crc_result = (crc_bit15|crc_bit14_3|crc_bit2|crc_bit1|crc_bit0); //b15 b14-b3 b2 b1 b0
shift--;
}
}
return crc_result;
}
系統時間:2024-03-29 17:22:43
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!