1 | function crcOut = calcCRC32(thePacketBytes) |
---|
2 | %Based on the C code example at: |
---|
3 | %http://www.netrino.com/Embedded-Systems/How-To/CRC-Calculation-C-Code |
---|
4 | %Uses slightly modified version of CRC-32: |
---|
5 | % No output XOR |
---|
6 | % No bit-order swapping on message or digest bytes |
---|
7 | |
---|
8 | CRCPolynomial = hex2dec('04c11db7'); |
---|
9 | CRC_Table = CRC_table_gen(CRCPolynomial, 32); |
---|
10 | |
---|
11 | init_crc = hex2dec('ffffffff'); |
---|
12 | myData = thePacketBytes; |
---|
13 | %de2bi(49,'left-msb',8)) |
---|
14 | crc_accum = init_crc; |
---|
15 | for n=1:length(myData) |
---|
16 | x = bitshift(crc_accum,-24,32); |
---|
17 | %CRC32 would swap bit order here: |
---|
18 | % x = bitxor(x, bi2de( de2bi(myData(n),'left-msb',8))); |
---|
19 | x = bitxor(x, myData(n)); |
---|
20 | x = bitand(x,hex2dec('ff')); |
---|
21 | crc_accum = bitxor(bitshift(crc_accum,8,32),CRC_Table(x+1)); |
---|
22 | crc_tracking(n) = crc_accum; |
---|
23 | end |
---|
24 | |
---|
25 | CRC32 = crc_accum; |
---|
26 | %CRC32 would XOR with all ones, then reorder bits here: |
---|
27 | %CRC32 = bitxor(CRC32, hex2dec('ffffffff')); |
---|
28 | %CRC32 = bi2de(de2bi(CRC32,'left-msb',32)); |
---|
29 | |
---|
30 | CRC32_b3 = bitand(bitshift(CRC32,-24,32),hex2dec('ff')); |
---|
31 | CRC32_b2 = bitand(bitshift(CRC32,-16,32),hex2dec('ff')); |
---|
32 | CRC32_b1 = bitand(bitshift(CRC32,-8,32),hex2dec('ff')); |
---|
33 | CRC32_b0 = bitand(CRC32,hex2dec('ff')); |
---|
34 | |
---|
35 | crcOut = [CRC32_b3 CRC32_b2 CRC32_b1 CRC32_b0]; |
---|