cancel
Showing results for 
Search instead for 
Did you mean: 

CRC computation

lowpowermcu
Associate II
Posted on January 12, 2011 at 13:46

CRC computation

#stm-crc32 #stm32f2-crc
45 REPLIES 45
Posted on May 17, 2011 at 14:21

The bit reverses on the data and the CRC register above relate to making the STM32's CRC perform a computation which matches the polynomial, and inverse conditions of the one used by PKZIP.

The issues are related to endian, and shift direction. In my opinion the choices by ST were particularly bad.

The code snippet from the second post to this thread will allow you to validate the 32-bit computation used by the STM32. It will work on small-endian machines, and does validate the numbers you were seeing.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
zabel
Associate III
Posted on June 17, 2011 at 11:13

CRC computation does not work!

Hi,

I am completely stuck -- the STM32 does not compute any CRC value for me -- I always read zero from the CRC_DR register.

I just used the standard peripheral library V3.5.0. My test code is:

#include ''stm32f10x_crc.h''

uint32_t crc;

const uint32_t crctestdata[] =

{    0x01020304,    0x05060708,    0x090a0b0c,    0x0d0e0f00};

void test(void)

{

 CRC_ResetDR();

  crc = CRC_CalcBlockCRC(crctestdata, 4);

}

I am using the STM32F10ZGT on an STM3210E-EVAL board.

Regards

  Dirk

Posted on June 17, 2011 at 12:14

the STM32 does not compute any CRC value for me -- I always read zero from the CRC_DR register.

 

Yeah, that sounds like the clock has not been enabled.

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
zabel
Associate III
Posted on June 17, 2011 at 13:43

Thanks a lot, after enabling the clock crc generation works as expected.

Regards

  Dirk

lubos
Associate
Posted on June 29, 2011 at 13:44

The function uint32_t revbit(uint32_t data) doesnt work for me.

I get the same data as were inserted.

I was trying debug it, Assembler part -  asm(''rbit r0,r0''); - works but function doesnt return register R0.

Thanks.

Posted on June 29, 2011 at 18:15

The functionuint32_t revbit(uint32_t data) doesnt work for me.

I get the same data as were inserted.

I was trying debug it, Assembler part -asm(''rbit r0,r0''); -works but function doesnt return register R0.

It is a cheap hack. I'd probably write the whole thing in assembler myself, and in-line, the code was provided as a quick demonstration / proof of concept.

The assembler I'd expect for the function would be

revbit:

rbit r0,r0

bx lr

Or you could just implement it in C

u32 revbit(u32 data)

{

int i;

u32 rev = 0;

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

rev |= ((data >> i) & 1) << (31 - i);

return rev;

};

What tool chain are you using?

This was based on the IAR code provided

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/CRC calculation in software&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&currentviews=2175

, it probably will require attention with Keil or GNU.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 29, 2011 at 20:42

In Keil

__asm u32 revbit(u32 data)

{

  rbit r0, r0

  bx lr

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
esiqueira
Associate II
Posted on June 15, 2012 at 19:21

If I have for example an a vector with the following datas

[0x01][0x04][0x75][0x54][0x00][0x02]

I know that the CRC with 6bytes result

[0x2A][0x17]

How can I convert this data for to use this function????

DWORD Crc32(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);

}

THANKS!!!!

Posted on June 15, 2012 at 20:28

If I have for example an a vector with the following datas [0x01][0x04][0x75][0x54][0x00][0x02]

 

 

I know that the CRC with 6bytes result [0x2A][0x17]

 

Looks like you have a 16-bit CRC, not sure the 32-bit method is applicable.

Any hint on the protocol or bus being used here?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
tsuneo
Senior
Posted on June 17, 2012 at 06:51

I typed in ''01 04 75 54 00 02'' (Hex) to this on-line CRC calculator,

http://www.lammertbies.nl/comm/info/crc-calculation.htm

CRC-16 (Modbus) results in 0x172A

Of course, double check is required.

Tsuneo