STM32的CRC32 实现代码 -- Ether

uint32_t reverse_32( uint32_t data )
{
  asm("rbit r0,r0");
  return data;
}
;

uint32_t crc32_ether( char *buf, int len, int clear )
{
  uint32_t *p = (uint32_t*) buf;
  uint32_t crc, crc_reg;
  if ( clear )
    CRC_ResetDR( );
  while ( len >= 4 )
  {
    crc_reg = CRC_CalcCRC( reverse_32( *p++ ) );
    len -= 4;
  }
  crc = reverse_32( crc_reg );
  if ( len )
  {
    CRC_CalcCRC( crc_reg );
    switch ( len )
    {
      case 1:
        crc_reg = CRC_CalcCRC( reverse_32( ( *p & 0xFF ) ^ crc ) >> 24 );
        crc = ( crc >> 8 ) ^ reverse_32( crc_reg );
        break;
      case 2:
        crc_reg = CRC_CalcCRC( reverse_32( ( *p & 0xFFFF ) ^ crc ) >> 16 );
        crc = ( crc >> 16 ) ^ reverse_32( crc_reg );
        break;
      case 3:
        crc_reg = CRC_CalcCRC( reverse_32( ( *p & 0xFFFFFF ) ^ crc ) >> 8 );
        crc = ( crc >> 24 ) ^ reverse_32( crc_reg );
        break;
    }
  }
  return ~crc;
}

 

你可能感兴趣的:(stm32)