Emlid.WindowsIot.Hardware.Components.Ms5611.Ms5611PromData.Validate C# (CSharp) Method

Validate() public static method

Validates a PROM data set (bytes read from all coefficients/memory).
public static Validate ( byte buffer ) : bool
buffer byte PROM data buffer to validate.
return bool
        public static bool Validate(byte[] buffer)
        {
            // Get hardware calculated 4 bit CRC from buffer
            var crc = buffer[C7SerialCrcOffset + 1] & 0x0f;

            // Re-calculate CRC based on data in buffer
            var remainder = 0;
            for (var bufferIndex = 0; bufferIndex < MemorySize; bufferIndex++)
            {
                // Get data byte in 16 bit form
                var data = (int)buffer[bufferIndex];

                // Mask byte containing CRC (from the CRC check itself)
                // Note the whole byte is masked not just the 4 CRC bits,
                // i.e. 4 bits of serial are excluded too
                if (bufferIndex == C7SerialCrcOffset + 1)
                    data &= 0xff00;

                // XOR data
                remainder ^= data;

                // XOR bits
                for (var bits = 8; bits > 0; bits--)
                {
                    if ((remainder & 0x8000) != 0)
                        remainder = (remainder << 1) ^ 0x3000;
                    else
                        remainder = remainder << 1;
                }
            }

            // Final shift down to 4 bits produces CRC result
            var crcCheck = 0x000f & (remainder >> 12);

            // Return successful when CRC matches hardware
            return crc == crcCheck;
        }