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
3. 3.1.3 CRC Computation Differences Between the ADS122U04 and ADS122C04
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.2.3 Error Correction

After using the Hamming and checksum validation, it is possible to correct single bit error. The data validation and correction takes place first and the bit is corrected if possible. If the data are restored, the checksum is reevaluated. If a multiple bit error or checksum error occurs, the transmission is invalid.

uint32_t fixResults[] = {
0,        // datablock OK
1 << 3,   // no error in data (H0)
1 << 4,   // no error in data (H1)
1u << 31, // data error
1 << 5,   // no error in data (H2)
1 << 30,  // data error
1 << 29,  // data error
1 << 28,  // data error
1 << 6,   // no error in data (H3)
1 << 27,  // data error
1 << 26,  // data error
1 << 25,  // data error
1 << 24,  // data error
1 << 23,  // data error
1 << 22,  // data error
1 << 21,  // data error
1 << 7,   // no error in data (H4)
1 << 20,  // data error
1 << 19,  // data error
1 << 18,  // data error
1 << 17,  // data error
1 << 16,  // data error
1 << 15,  // data error
1 << 14,  // data error
1 << 13,  // data error
1 << 12,  // data error
1 << 11,  // data error
1 << 10,  // data error
1 << 9,   // data error
1 << 8,   // data error
// not correctable 30
// not correctable 31
// As H0 covers 14 bits and H1-H4 cover 13 bits a
//   multi-bit error will occur if hamming is 30
//   or 31 and cannot be corrected
};
/**
* Data validation and repair of single-bit error.
*
* \details Validation and restoration of data using the Hamming bit
* information to correct for single-bit error.  The 'in' value is checked
* first and if the XOR returns a 0, then the data contents are validated relative
* to the Hamming bits and then are checked for multi-bit error using the checksum.
* If the Hamming bits do not match the data, then the Hamming bits are used to
* correct the data if possible by using an XOR of the computed Hamming to the transmitted
* Hamming.  Using a lookup table based on the correction factors and the XOR value the
* data is restored and then a checksum is computed to make sure there were no multi-bit
* errors.
*
* \param    uint32_t in of the value to be computed and compared.
*
* \returns  uint32_t res returns 0 if the data was correctable or valid.
*                        A non-zero is invalid data.
*/
uint32_t fixHamming(uint32_t in)
{
uint32_t res;
uint32_t fix;
// checkHamming will return the 5 hamming bits from the data 'in'
// When using an assignment in the conditional statement, the conditional evaluates the
//    assignment.
// If checkHamming returns something other than '0', the result is attempted to be fixed
if(res = checkHamming(in))
{
// Based on the results of the hamming code not '0' the result is corrected from the
// lookup by adjusting the 32 bit value from 'in' from the table of 2^5 (or 32
// possible combinations). In summary, 24 values out of 32 are for 1-bit correction,
// 5 values for no error in data (1bit error in Hamming Code), 1 value for no error
// in both data and Hamming code and 2 values for more than 1 bit error in either
// data or Hamming Code or both. Since only two values are assigned for more than
// 1 bit error cases, there are high probabilities that more than 1 bit-error will
// be mistakenly treated as 1-bit error by the algorithm. To reduce such
// possibilities, 2 bit checksum are added for the error detection in addition to
// the Hamming Detection.

// Quick check for multi bit error in hamming value
fix = res >> 3;
// Then XOR computed hamming bits with 'in' value hamming bits
fix = fix ^ ((in & 0xFF) >> 3);
// If there is a known multi-bit error then fail the fix
if (fix > 29) return HC_FIX_FAIL;
else
{
// Bit error occurred and attempting to fix
fix = (fixResults[fix]);
// Identified error could be either hamming or data error where 'in' value is
//   XORed with the fix value
res = in ^ fix;
}
}
else
{
res = in;
}
// res is now containing either the original or the fixed value of data which is
// data + hamming following the hamming check and data correction, multi-bit
// errors are checked using the checksum bits checkSum will exclude the hamming bits
// in the count. If the checkSum returns something other than 0, the
// checkSum fails, otherwise return 0
if(checkSum(res))
{
return HC_FIX_FAIL;
}
return res;
}