cancel
Showing results for 
Search instead for 
Did you mean: 

CRC

lmullane
Associate II
Posted on August 12, 2010 at 12:52

CRC

8 REPLIES 8
swhite2
Associate III
Posted on May 17, 2011 at 14:01

The first function will perform a CRC computation over a block of memory, assumed to be a multiple of 4 bytes (32-bits)

The second function simulates the action of the STM32 hardware register, and functions in a bit wise fashion.

//****************************************************************************

DWORD CRC32WideFast(DWORD Crc, DWORD Size, BYTE *Buffer)

{

    Size = Size >> 2; // /4  Size passed in as a byte count, assumed to be a multiple of 4

  while(Size--)

  {

      static const DWORD CrcTable[16] = { // Nibble lookup table for 0x04C11DB7 polynomial

        0x00000000,0x04C11DB7,0x09823B6E,0x0D4326D9,0x130476DC,0x17C56B6B,0x1A864DB2,0x1E475005,

        0x2608EDB8,0x22C9F00F,0x2F8AD6D6,0x2B4BCB61,0x350C9B64,0x31CD86D3,0x3C8EA00A,0x384FBDBD };

    Crc = Crc ^ *((DWORD *)Buffer); // Apply all 32-bits

        Buffer += 4;

      // Process 32-bits, 4 at a time, or 8 rounds

      Crc = (Crc << 4) ^ CrcTable[Crc >> 28]; // Assumes 32-bit reg, masking index to 4-bits

      Crc = (Crc << 4) ^ CrcTable[Crc >> 28]; //  0x04C11DB7 Polynomial used in STM32

      Crc = (Crc << 4) ^ CrcTable[Crc >> 28];

      Crc = (Crc << 4) ^ CrcTable[Crc >> 28];

      Crc = (Crc << 4) ^ CrcTable[Crc >> 28];

      Crc = (Crc << 4) ^ CrcTable[Crc >> 28];

      Crc = (Crc << 4) ^ CrcTable[Crc >> 28];

      Crc = (Crc << 4) ^ CrcTable[Crc >> 28];

    }

  return(Crc);

}

//****************************************************************************

DWORD CrcSTM32(DWORD Crc, DWORD Data)

{

  int i;

  Crc = Crc ^ Data;

  for(i=0; i<32; i++)

    if (Crc & 0x80000000)

      Crc = (Crc << 1) ^ 0x04C11DB7; // Polynomial used in STM32

    else

      Crc = (Crc << 1);

  return(Crc);

}

//****************************************************************************

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
John F.
Senior
Posted on May 17, 2011 at 14:01

Is it necessary to call your functions with CRC initialised to 0xFFFFFFFF

eg as

CRC = CRC32WideFast(0xFFFFFFFF, 400, Buffer);

and (initial call)

CRC = CrcSTM32(0xFFFFFFFF, Data)

Posted on May 17, 2011 at 14:01

It depends on your initial conditions, and whether you are chaining together multiple regions, sectors or blocks. The hardware only resets to 0xFFFFFFFF when you direct it too, the register itself is a linear feedback construct and accumulative in nature (actually more like a remainder from a long division), it will clear if you feedback the current value on itself.

crc = 0xFFFFFFFF; // Initial reset state, may be something else

crc = CrcSTM32(crc, Data[0]);

crc = CrcSTM32(crc, Data[1]);

..

crc = 0xFFFFFFFF; // Initial reset state, may be something else

crc = CRC32WideFast(crc, 512, Block0);

crc = CRC32WideFast(crc, 512, Block1);

..

For two iterations of the block crc, you get the value for 1024 bytes total. You could do it as 8 blocks of 128 bytes too, or 1 block of 1024 bytes, the value computed should be the same.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
John F.
Senior
Posted on May 17, 2011 at 14:02

OK, understood. Thankyou.

Poonam Deoghare
Associate III
Posted on May 22, 2018 at 12:54

Hi All,

I am trying to use CRC component of STM32F4.

Procedure I followed:-

For STM32F413, Selected uart2,uart6 and CRC from stm32Cubemx.

I am using below function to calculate CRC value.

HAL_CRC_Calculate(CRC_HandleTypeDef *hcrc, uint32_t pBuffer[], uint32_t BufferLength);

HAL_CRC_Calculate(&hcrc,(uint32_t *)Data,Length);

where Data = 'START'

Lenght = 5;

It is giving me some CRC computed value ...0x200004f0.

If I create other project with same above setting and do the the same procedure, I am getting different CRC computed value even if I pass the same data.

The CRC computed value coming for data 'START' should be same in both the project if I am not wrong.

Please provide your inputs if I am doing something wrong.

Can I directly use HAL_CRC_Calculate() function in my project or not ?

Thanks,

Poonam

Posted on May 22, 2018 at 13:41

You're passing a 32-bit pointer, I'd assume the length is in words, not bytes. Check function usage expectations.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on May 23, 2018 at 15:39

Thanks Clive..