SBAA106A June   2020  – August 2021 ADS112C04 , ADS112U04 , ADS114S06 , ADS114S08 , ADS122C04 , ADS122U04 , ADS1235 , ADS1235-Q1 , ADS124S06 , ADS124S08 , ADS1259 , ADS1259-Q1 , ADS125H01 , ADS125H02 , ADS1260 , ADS1260-Q1 , ADS1261 , ADS1262 , ADS1263 , ADS127L01 , ADS131A02 , ADS131A04 , ADS131M04 , ADS131M06 , ADS131M08

 

  1.   Trademarks
  2. 1Introduction
  3. 2Simple Checksum
    1. 2.1 Checksum Code Example
  4. 3CRC
    1. 3.1 CRC Generic Computations
      1. 3.1.1 Using XOR Bitwise Computation
      2. 3.1.2 Using Lookup Tables
        1. 3.1.2.1 Table Initialization
        2. 3.1.2.2 CRC Computation
      3. 3.1.3 CRC Computation Differences Between the ADS122U04 and ADS122C04
        1. 3.1.3.1 Byte Reflection Example
        2. 3.1.3.2 Reassembling Data Using Byte Reflection for CRC Computation
  5. 4Hamming Code
    1. 4.1 Hamming Code Computation
      1. 4.1.1 Hamming Code Computation Example
        1. 4.1.1.1 Counting Bits for Parity and Checksum Computations
          1. 4.1.1.1.1 Example of Counting Set Bits in the Data
          2. 4.1.1.1.2 Example of Counting Set Bits Using a Lookup Table
      2. 4.1.2 Validation of Transmitted Data
        1. 4.1.2.1 Hamming Validation
        2. 4.1.2.2 Checksum Validation
        3. 4.1.2.3 Error Correction
  6. 5Summary
  7. 6References
  8. 7Revision History

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 HAMMING_BIT1_MASK 0x00B66CCC
#define HAMMING_BIT2_MASK 0x0071E3C3
#define HAMMING_BIT3_MASK 0x000FE03F
#define HAMMING_BIT4_MASK 0x00001FFF
#define CHECKSUM_BIT_MASK 0xFFFFFF00
#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));
}