SBAA106A June   2020  – August 2021

2. 1Introduction
3. 2Simple Checksum
4. 3CRC
1. 3.1 CRC Generic Computations
1. 3.1.1 Using XOR Bitwise Computation
2. 3.1.2 Using Lookup Tables
5. 4Hamming Code
1. 4.1 Hamming Code Computation
1. 4.1.1 Hamming Code Computation Example
2. 4.1.2 Validation of Transmitted Data
6. 5Summary
7. 6References
8. 7Revision History

### 4.1.1 Hamming Code Computation Example

The Hamming code computation can be used for both the computation of data to be transmitted as well as the verification of data received. The Hamming bit mask is applied to the data for each of the five Hamming bits. The checksum is also computed and appended to the result. The value returned is the 24-bit data and Hamming byte as an unsigned 32-bit integer.

#define HAMMING_BIT0_MASK 0x00DAB555
#define HC_FIX_FAIL 0xFFFFFFFF
/**
* Computation of the Hamming bits for the 24-bit data that is input.
*
* \details Computation of Hamming bits using a bit mask for each Hamming bit then
* counting of the number of bits set in the masked value.  This is completed 5 times
* once for each Hamming bit. The checksum is also computed returning the complete byte
* that is appended to the 24-bit data. The 'in' value used for the computation assumes
* the data transferred is not including a hamming byte. When the computation is made the
* Hamming byte is added to the input value which is accomplished by shifting the 24-bit
* value left by 8 bits.
*
* \param    uint32_t in of the value to be computed.
*
* \returns  uint32_t out containing the 24-bit data appended with the Hamming byte value.
*/
uint32_t calcHamming(uint32_t in)
{
// Take the integer value passed and convert to a format that will include the hamming
//    byte when passed back
uint32_t out = in << 8;
// The 5 hamming bits are computed by taking the input value, and ANDing with the
//   mask value followed by counting the number of bits with the result ANDed with
//   0x01. If the count is odd, then the hamming bit is set to make the count even.
//   If the count is even, then hamming bit is not set.
uint32_t hamming =
((countBits(HAMMING_BIT0_MASK & in) & 0x01)     ) |
((countBits(HAMMING_BIT1_MASK & in) & 0x01) << 1) |
((countBits(HAMMING_BIT2_MASK & in) & 0x01) << 2) |
((countBits(HAMMING_BIT3_MASK & in) & 0x01) << 3) |
((countBits(HAMMING_BIT4_MASK & in) & 0x01) << 4) ;
// The returned result is the data shifted left by 8, ORed with the hamming bits
//   shifted left by 3, ORed with the checksum of the data ANDed with 2-bits (0x03)
//   which is shifted left by 1 with the LSB as 0 for a total of 5 hamming,
//   2 checksum and a '0' for a total of 8 bits.
return    (out | (hamming << 3) | ((countBits(in) & 0x03) << 1));
}