crc.c 1.36 KB
#include "osint.h"
#include "controller.h"

#define CRC5GEN		21	/* CRC5 generator = x^5 + x^4 + x^2 + 1 (-x^5) */
#define ADDR_LEN	11	/* address length in bits */
#define CRC8GEN		133	/* CRC8 generator = x^8 + x^7 + x^2 + 1 (-x^8) */
#define DATA_LEN	32	/* data length in bytes */

u8 __osContAddressCrc(u16 addr)
/* calculate CRC5 by long division algorithm */
/* bufp: pointer to address buffer */
/* return: 5-bit crc value */
{
  u32	temp = 0;
  u32	i = 0x400;

  do {
    temp <<= 1;
    if ((u32)addr & i) {
      if (temp & 0x20) {
	temp ^= 0x14;
      } else {
	temp++;
      }
    } else {
      if (temp & 0x20) temp ^= 0x15;
    }
    i >>= 1;
  } while (i != 0) ;

  i = 5;
  do {
    temp <<= 1;
    if (temp & 0x20) temp ^= 0x15;
  } while (--i != 0);

  return( (u8)(0x1f & temp));
}

u8 __osContDataCrc(u8 *data)
/* calculate CRC8 for the data buffer of length DATA_LEN bytes */
/* 	using table-driven algorithm */
/* bufp: pointer to data buffer */
/* return: 8-bit crc value */
{
  u32	temp=0;
  u32	i, j;

  for( i = 32 ; i != 0 ; i-- ) {
    for( j = 0x80 ; j != 0 ; j >>= 1) {
      temp <<= 1;
      if ((u32)(*data) & j) {
	if (temp & 0x100) {
	  temp ^= 0x84;
	} else{
	  temp++;
	}
      } else {
	if (temp & 0x100) temp ^= 0x85;
      }
    }
    data++;
  }
  do {
    temp <<= 1;
    if (temp & 0x100) temp ^= 0x85;
  } while ( ++i < 8 ) ;

  return( (u8)temp );
}