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
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));
}