Functions

crc7.h File Reference

implement CRC-7 as used in SD-Card command packets More...

#include <stdint.h>

Go to the source code of this file.

Functions

uint8_t _crc7_update (uint8_t crc, uint8_t data)
 CRC-7 calculation as used in SD-Card command packets.

Detailed Description

implement CRC-7 as used in SD-Card command packets


Function Documentation

uint8_t _crc7_update ( uint8_t  crc,
uint8_t  data 
) [inline]

CRC-7 calculation as used in SD-Card command packets.

Parameters:
crc calculated CRC from bytes read so far (or 0 for first byte)
data byte to put into CRC
Returns:
CRC-7 in upper 7 bits

returning CRC in upper bits:

  • saves one left shift in using function
  • saves one left shift at start of function but costs one at end
  • converts one 3-bit shift into a 4-bit shift which can be better optimized

{
#if 0
  /* bit-wise algorithm */
  int i;
  for (i = 8; i > 0; i--) {
    uint8_t b = (data >> 7) ^ ((crc & 0x80) >> 7);
    crc = (crc ^ b ^ (b << 3)) << 1;
    crc &= 0xfe;
    data <<= 1;
  }
  return crc;
#else
  /* no need to do &= 0xfe, as that bit will not be used anyway */
  /*crc &= 0x7f;*/
  data ^= crc;
  crc = data >> 4;
  data ^= crc ^ (crc >> 3);
  return /*0xfe & */((uint8_t)(data<<1) ^ (uint8_t)(data << 4));
#endif
}