23 / 71
Checksum calculation
This checksum calculation is the same as the checksum
used in the IP protocol.
Definition: sum of 1’s complement of all 16 bits words of whole
message (fiveco’s packet) except checksum bytes.
Code structure: (all values are unsigned)
Clear accumulator
Loop
x.
Only if last word is not made of two bytes, the
data byte is the upper byte (big endian)
1.
Compute 1’s of each 16bits word, result is 16bits
2.
Convert last result from 16 bits to 32 bits, result is
32bits: last result
3.
Add last result to the 32 bits accumulator.
Try the Loop
4.
Convert accumulator in two 16bits words
5.
Add this two 16bits words, result is 16bits word.
6.
If an overflow occurs with the last addition
(Carry), add 1 to the last result.
7.
Last result is the final result
Example (in hexadecimal) for this packet:
0x0021
Æ
0x0000FFDE (Read)
+0x1234
Æ
0x0001EDA9 (TransID)
+0x0003
Æ
0x0002EDA5 (3 reg to read)
+0x0A10
Æ
0x0003E394 (reg 0A,10,02)
+0x02(00)
Æ
0x0004E193
Note that in this case a last 00 is implicitly used.
(02
Æ
02 00).
0xE193 = 0xE197, (carry=0)
carry = 0xE197
Checksum = 0xE197
FMod-IPAXESCTRL User Manual v.1.1