25.7.8.10 Checksum Calculations
An IP header checksum is a simple checksum used to provide basic protection against bit corruption during transmission. The header is calculated by dividing the data stream into half-words (16-bits) and adding them together. Any overflow is also added back into the sum. The result is the one’s complement of the calculated sum. The following code shows the details on how a check-sum is calculated.
The CRCDAT value is loaded with the expected check sum value prior to the start of the block transfer, at the end of the block transfer the CRCDAT will read back 0xFFFF if successful. Note, that checksum in CRC-DAT is the non-inverted value if CRCXOR=0. To read back the 1s-complement value of the checksum, set CRCXOR=1 prior to performing the calculation.
To perform IP Header checksums, configure the channel as follows:
- Configure the block transfer options to read the IP Header in memory.
- Set the CHCTRLBk.CASTEN=1.
- Configure CHCTRLBk.CRCEN to enable CRC/Checksum for the channel.
- Set CHCTRLCRCk.CRCMD to 7 to select IP Header Checksum mode.
- Set CHCRCDATk.CRCDAT to 0x0000.
- Set CHCTRLCRCk.CRCXOR=1.
- Set CHCTRLAk.SWFRC to 1 to initiate the transfer.
- When the transfer completes CRCDAT register reports the IP Header checksum.
Consider an IP header frame byte stream
(MSBit first, little endian ordering):
0F 2B 3C 4D 5E 6F 78 9A
0 1 2 3 4 5 6 7 (address)
The checksum is calculated by splitting
the stream into 16-bit words and summing
them as follows:
2B0F
+4D3C
----
784B
+6F5E
----
E7A9
+9A78
----
18221 <- Note the overflow!
0001 <- Overflow added
----
8222 <- MSB added back into sum
8222 is the sum of the IP Header. The
checksum is the ones-complement of the
sum: 7DDD = ~8222