Skip to main content
lowpowermcu
Associate III
January 12, 2011
Question

CRC computation

  • January 12, 2011
  • 45 replies
  • 12131 views
Posted on January 12, 2011 at 13:46

CRC computation

#stm-crc32 #stm32f2-crc
This topic has been closed for replies.

45 replies

zabel
Associate III
June 17, 2011
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

Tesla DeLorean
Guru
June 17, 2011
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 VenmoUp vote any posts that you find helpful, it shows what's working..
zabel
Associate III
June 17, 2011
Posted on June 17, 2011 at 13:43

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

Regards

  Dirk

lubos
Associate
June 29, 2011
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.

Tesla DeLorean
Guru
June 29, 2011
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 VenmoUp vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
June 29, 2011
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 VenmoUp vote any posts that you find helpful, it shows what's working..
esiqueira
Associate III
June 15, 2012
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!!!!

Tesla DeLorean
Guru
June 15, 2012
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 VenmoUp vote any posts that you find helpful, it shows what's working..
tsuneo
Associate II
June 17, 2012
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

Tesla DeLorean
Guru
June 17, 2012
Posted on June 17, 2012 at 15:13

// 16-Bit CRC of Right Shifting MODBUS Polynomial 0xA001
 #include <
windows.h
>
#include <
stdio.h
>
WORD Crc16Modbus(WORD Crc, BYTE Data)
{
int i;
Crc = Crc ^ Data;
for(i=0; i<
8
; i++)
if (Crc & 0x0001)
Crc = (Crc >> 1) ^ 0xA001; // Polynomial used by MODBUS
else
Crc = (Crc >> 1); // Right shifting algorithm
return(Crc);
}
int main(int argc, char **argv)
{
BYTE TestData[] = { 0x01,0x04,0x75,0x54,0x00,0x02 };
WORD crc;
int i;
crc = 0xFFFF; // Initial state
for(i=0; i<sizeof(TestData); i++)
crc = Crc16Modbus(crc, TestData[i]);
printf(''CRC = %04
X'', crc);
return(1);
}

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..