In article <
58783373-560b-4984...@googlegroups.com>,
Howerd <
how...@yahoo.co.uk> wrote:
>On Tuesday, January 24, 2017 at 1:08:01 PM UTC+1, Albert van der Horst wrot=
<SNIP>
>>=20
>> Now they have a new hurdle, TI wants a crc. They throw a zillion document=
>s
>> at you except the exact definition of which crc.
>>=20
>> If someone can help out with reproducing crc16:
>> crc-16 crc-16 crc-16 crc-16
>> polynomial $8005 $8005 $4C11DB7 $4C11=
>DB7
>> init CRC register $0 $0 $0 $0
>> Final XOR value $0 $0 $0 $0
>> Reflect data (byte) NO YES NO YES
>> Reflect Crc (word) NO YES NO YES
>> input "123456789" "123456789" "123456789" "1234=
>56789"
>> Expected CRC $FEE8 $BB3D $FEE8 $BB3D
>>=20
>> The combination input/output is the only real straw we can grasp.
>>=20
>> The problem is that there is no connection to be found between the terms
>> used by TI in the first column and the general theory.
>>=20
>> <SNIP>
>> >
>> >Best wishes from Germany,
>> >Matthias
>>=20
>> Groetjes Albert
>> --=20
>> Albert van der Horst, UTRECHT,THE NETHERLANDS
>> Economic growth -- being exponential -- ultimately falters.
>> albert@spe&ar&
c.xs4all.nl &=3Dn
http://home.hccnet.nl/a.w.m.van.der.horst
>
>Hi Albert,
>
>It all seems very confusing...
>
>The second column appears to be the Modbus CRC16 with the init and exit XOR=
> values set to 0 instead of -1 .
>
>The Modbus CRC16 code is here :
http://www.inventio.co.uk/crc16modbus.f
>Change the code as follows :
>\ initialises the input CRC value in the required way
>: initIncomingCRC 0 incomingCRC w! ; \ was -1 for Modbus
> =20
>\ initialises the output CRC value in the required way
>: initOutgoingCRC 0 outgoingCRC w! ; \ was -1 for Modbus
>
>This gives $BB3D from "123456789".
>
>Probably column 1 is the same but with all data bit-reversed and the final =
>CRC bit-reversed too.
Once the bit-reversal idea sinks in, I can reproduce column 2 with
\ ---------------------- 8< -----------------------------
"BOUNDS" WANTED
":NONAME" WANTED
1 CELLS 4 <> 1001 ?ERROR
HEX
: reverse-bits
0 SWAP 10 0 DO
DUP 1 AND ROT 1 LSHIFT OR SWAP 1 RSHIFT
LOOP DROP ;
\ REGRESS FF00 reverse-bits S: 00FF
\ REGRESS 8005 reverse-bits S: A001
\ Well the polynomial
8005 reverse-bits CONSTANT CRC16_POLYNOMIAL
\ Auxiliary table with values for single bytes.
:NONAME
100 0 DO I 8 0 DO
DUP >R 1 RSHIFT R> 1 AND IF CRC16_POLYNOMIAL XOR THEN
LOOP 0FFFF AND , LOOP
; CREATE CRCTable EXECUTE
\ For initial CRC and BUFFER COUNT pair, leave the updated CRC
: CRC-MORE BOUNDS DO DUP I C@ XOR 0FF AND CELLS CRCTable + @
SWAP 8 RSHIFT XOR LOOP ;
\ For BUFFER COUNT pair, leave the CRC .
: CRC -1 ROT ROT CRC-MORE INVERT ;
: CRC-16B 0 ROT ROT CRC-MORE ;
\ REGRESS "123456789" CRC-16B S: 0 BB3D
\ ---------------- 8< ---------------------------
Notes:
1. reverse-bits only works reversing 16 bits on a 32 bit Forth.
2. REGRESS signals a test (failure aborts on ciforth)
3. The :NONAME generates the aux table at compile time.
On ciforth this is more elegant, as it can execute loops
in interpret mode.
The compact version for ciforth is a one screener:
\ ---------------------- 8< -----------------------------
WANT BOUNDS HEX
\ Well the polynomial
A001 CONSTANT CRC16_POLYNOMIAL
CREATE CRCTable \ Auxiliary table with values for single bytes.
100 0 DO I 8 0 DO
DUP >R 1 RSHIFT R> 1 AND IF CRC16_POLYNOMIAL XOR THEN
LOOP 0FFFF AND , LOOP
\ For initial CRC and BUFFER COUNT pair, leave the updated CRC
: CRC-MORE BOUNDS DO DUP I C@ XOR 0FF AND CELLS CRCTable + @
SWAP 8 RSHIFT XOR LOOP ;
\ For BUFFER COUNT pair, leave the CRC .
: CRC-16B 0 ROT ROT CRC-MORE ;
DECIMAL
\ ---------------- 8< ---------------------------
>
>Columns 3 and 4 are strange because they seem to have 32 bit polynomials wh=
>ich I have never seen...
I can reproduce column 4 with the built in crc of ciforth.
The polynomial is the bit-reversal of the polynomial represented by
$EDB8,8320
>
>I will post the modified Texas Instruments code, when I get FTP access, her=
>e :=20
Certainly. I hope you like the idea of calculating the table at compile
time, something that you could not do in C.
>
>Best regards
>Howerd