cancel
Showing results for 
Search instead for 
Did you mean: 

CRC5 creation method and example.

ldong.1
Associate III

Hi. I'm working with L9369 H-bridge driver and i want to know how to calculate CRC code in C programming.

it's polynomial is X^5+X^2+1 and initial value is 0x1F(1 1111)

  1. Do you have any documentation regarding CRC implementation in C code?

i got CRC documentable attched document, but i think it's 26 bit and responding CRC value is not matched.

2.i want to know X^5+X^2+1 polynomial's CRC Lookup Table and how to use lookup table in C code. i calculated it by manual, and have some result. i want to know i calculate it correctly.

0->0x0

1-> 0x5

2->0xa

3->0xf

4->0x14

5->0x11

.....

255-> 0x14 

3. i want to know how to use lookup table below 2 cases. and sample code.

when i try to calucate CRC value with lookup-table, it's result is different to the value calculated by manual.

(please let me know how to calculate in case of CRC initial(0x1F) value apply, not apply case)

0x1234 ( 0001 0010 0011 0100)

0x2D40CFF(0010 1101 0100 0000 1100 1111 1111)

4. I attached excel file that i calculate CRC value by manual.

atttched document says 26 bit stream is  "0 0000:0011 0 0000:0000:0000:0000" and CRC value is 0x0B. My CRC value is 0x15(with initial CRC), 0x1A(with out initial CRC)

i don't know why the result is different to me. please help to solve it.

thank you.

thank you.

1 ACCEPTED SOLUTION

Accepted Solutions

I'm computing the CRC over 26 bits, when completed the high order 5-bits are the remainder, then I pull them back into the low order[4..0]

crc = (crc >> 27) & 0x1F; // Recover, align, mask computed value

Now if I appended the 5-bit CRC and ran those 31 bits, the remainder for the correct CRC would drop to zero.

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

View solution in original post

11 REPLIES 11

Prior thread https://community.st.com/s/question/0D53W00001gxfOrSAI/reqeust-for-l9369-crc-calculation-documentation

TX: [0 0000:0011 0 0 0000:0000:0000:0000 01011]
0180000B (32) [0B]
CRC:0B -> 0180000B
 
TX: [0 0000:0010 0 1 0000:0000:0000:0000 11110]
0120001E (32) [1E]
CRC:1E -> 0120001E
 
TX: [0 0000:0100 0 0 0000:0000:0000:0000 01111]
0200000F (32) [0F]
CRC:0F -> 0200000F
 
0 10 0000:0011 0000:0101:0111:0000 0:1111
4060AE0F (32) [0F]
CRC:0F -> 4060AE0F
 
RX: [0 00 0000:0000 0000:0000:0000:0000 10001]
00000011 (32) [11]
CRC:11 -> 00000011
 
RX: [0 10 0000:0011 0000:0101:0111:0000 01111]
4060AE0F (32) [0F]
CRC:0F -> 4060AE0F
 
RX: [0 00 0000:0010 0000:1001:0011:0011 01111] Table 6c, seems inconsistent
0041266F (32) [0F]
CRC:0A -> 0041266A
 
RX: [0 00 0000:0011 0000:0101:0111:0000 00110]
0060AE06 (32) [06]
CRC:06 -> 0060AE06
 
RX: [1000:1000:1111:1111:1111:1111:1111:0010]
88FFFFF2 (32) [12]
CRC:12 -> 88FFFFF2
 
RX: [0 00 1111:1101 0000:0001:0001:1111 00000]
1FA023E0 (32) [00]
CRC:00 -> 1FA023E0
 
RX: [0 00 0000:0011 0000:0101:0111:0000 00110]
0060AE06 (32) [06]
CRC:06 -> 0060AE06

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

Table 9 test values seem good

Table 9
TX: [0 1001:0000 0 0 0000:0000:0000:0000 01111]
4800000F (32) [0F]
CRC:0F -> 4800000F
 
RX: [0 00 0000:0000 0000:0000:0000:0000 10001]
00000011 (32) [11]
CRC:11 -> 00000011
 
TX: [0 0000:0011 0 1 0000:0000:0000:0000 01011]
01A0000B (32) [0B]
CRC:0B -> 01A0000B
 
RX: [1000:0000:1111:1111:1111:1111:1110:0101]
80FFFFE5 (32) [05]
CRC:05 -> 80FFFFE5
 
TX: [0 0000:0011 0 0 0000:0000:0000:0000 01011]
0180000B (32) [0B]
CRC:0B -> 0180000B
 
RX: [0 00 0000:0011 0000:0101:0111:0010 01100]
0060AE4C (32) [0C]
CRC:0C -> 0060AE4C
 
TX: [0 0000:0011 0 0 0000:0000:0000:0000 01011]
0180000B (32) [0B]
CRC:0B -> 0180000B
 
RX: [0 10 0000:0011 0000:0101:0111:0010 00101]
4060AE45 (32) [05]
CRC:05 -> 4060AE45
 
TX: [0 1111:1101 0 1 0000:0000:0000:0000 00000]
7EA00000 (32) [00]
CRC:00 -> 7EA00000
 
RX: [0 00 0000:0011 0000:0101:0111:0000 00110]
0060AE06 (32) [06]
CRC:06 -> 0060AE06
 
TX: [0 0000:0011 0 0 0000:0000:0000:0000 01011]
0180000B (32) [0B]
CRC:0B -> 0180000B
 
RX: [0 00 1111:1101 0000:0000:0001:1111 11100]
1FA003FC (32) [1C]
CRC:1C -> 1FA003FC

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

hello. thank for reply. i'm modifying your example code for calculate data input with polynomial '100101', it's hard to implement. please help me to make getting CRC code function.

and i calculated your example, it seem that CRC code is wrong.

please check belows.

  1. i calculated first Tx message's CRC, but i can't get CRC 0x01011 with initial value 0x1F. i think this example don't care CRC initial value.

TX: [0 0000:0011 0 0 0000:0000:0000:0000 01011]

-> payload is 0x60000, CRC is 0x0B(without initial value)

2.i calculated sencond Tx Message

TX: [0 0000:0010 0 1 0000:0000:0000:0000 11110]

->payload is 0x40000, CRC is 0x0F(without initial value)

void rule21a(uint32_t data)
{
  int i;
  uint32_t test = (data & 0xFFC00000) | ((data & 0x1FFFE0) << 1); // Remove bit 20, high-order align
  uint32_t crc;
 
  crc = 0x1F << 27; // Initial, aligned high-order to match data
  crc = crc ^ test; // Apply data
  for(i=0; i<26; i++) // Consume data (shortened)
    if (crc & 0x80000000)
      crc = (crc << 1) ^ (0x05 << 27); // Bitwise application
    else
      crc <<= 1;
 
  crc = (crc >> 27) & 0x1F; // Recover, align, mask computed value
 
  printf("CRC:%02X -> %08X %s\n\n", crc, (data & 0xFFFFFFE0) ^ crc, ((data & 0x1F) != crc) ? "???" : "");
} // sourcer32@gmail.com

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

My 8-bit table ended up like this

  static const uint8_t crctbl[] = { // CRC-5 8-bit table, sourcer32@gmail.com
    0x00,0x28,0x50,0x78,0xA0,0x88,0xF0,0xD8,0x68,0x40,0x38,0x10,0xC8,0xE0,0x98,0xB0,
    0xD0,0xF8,0x80,0xA8,0x70,0x58,0x20,0x08,0xB8,0x90,0xE8,0xC0,0x18,0x30,0x48,0x60,
    0x88,0xA0,0xD8,0xF0,0x28,0x00,0x78,0x50,0xE0,0xC8,0xB0,0x98,0x40,0x68,0x10,0x38,
    0x58,0x70,0x08,0x20,0xF8,0xD0,0xA8,0x80,0x30,0x18,0x60,0x48,0x90,0xB8,0xC0,0xE8,
    0x38,0x10,0x68,0x40,0x98,0xB0,0xC8,0xE0,0x50,0x78,0x00,0x28,0xF0,0xD8,0xA0,0x88,
    0xE8,0xC0,0xB8,0x90,0x48,0x60,0x18,0x30,0x80,0xA8,0xD0,0xF8,0x20,0x08,0x70,0x58,
    0xB0,0x98,0xE0,0xC8,0x10,0x38,0x40,0x68,0xD8,0xF0,0x88,0xA0,0x78,0x50,0x28,0x00,
    0x60,0x48,0x30,0x18,0xC0,0xE8,0x90,0xB8,0x08,0x20,0x58,0x70,0xA8,0x80,0xF8,0xD0,
    0x70,0x58,0x20,0x08,0xD0,0xF8,0x80,0xA8,0x18,0x30,0x48,0x60,0xB8,0x90,0xE8,0xC0,
    0xA0,0x88,0xF0,0xD8,0x00,0x28,0x50,0x78,0xC8,0xE0,0x98,0xB0,0x68,0x40,0x38,0x10,
    0xF8,0xD0,0xA8,0x80,0x58,0x70,0x08,0x20,0x90,0xB8,0xC0,0xE8,0x30,0x18,0x60,0x48,
    0x28,0x00,0x78,0x50,0x88,0xA0,0xD8,0xF0,0x40,0x68,0x10,0x38,0xE0,0xC8,0xB0,0x98,
    0x48,0x60,0x18,0x30,0xE8,0xC0,0xB8,0x90,0x20,0x08,0x70,0x58,0x80,0xA8,0xD0,0xF8,
    0x98,0xB0,0xC8,0xE0,0x38,0x10,0x68,0x40,0xF0,0xD8,0xA0,0x88,0x50,0x78,0x00,0x28,
    0xC0,0xE8,0x90,0xB8,0x60,0x48,0x30,0x18,0xA8,0x80,0xF8,0xD0,0x08,0x20,0x58,0x70,
    0x10,0x38,0x40,0x68,0xB0,0x98,0xE0,0xC8,0x78,0x50,0x28,0x00,0xD8,0xF0,0x88,0xA0 };

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

I can make the Programmable HW CRC peripheral process it too, but the value/utility is questionable.

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

Hello. thank you very much for replying to my refit repetitive question.

i know above source code how to get CRC. but i have one question.

if data is not 32bit data, for example, 0x40000000, in this case, below initial value shifting seems wrong.

" crc = 0x1F << 27; // Initial, aligned high-order to match data"

i think above code works propely only when in case MSB(31bit) exist.

correct?

Hello. thank you very much for replying to my refit repetitive question.

i have 2 question.

  1. Do you use polynomial X^5+X^2+1? it seems wrong, for second data of CRCtable, crctbl[1] = 0x28.

but the value i calculate is 0x05. how to you get 0x28?

  1. i want to know how to use look up table. i already know how to use CRC-8 lookup table, but when i try it with CRC-5 table, it's result was wrong. do you know how to use CRC-5 lookuptable? and have any sourcecode?

thank you.

I am left aligning in the register for computational efficiency, allowing me to process 1, 2, 4 or 8-bits via tables

For the 26-bit value the least significant 6-bits (right) are zero, [5..0]=0, so the data exists in [31..6]

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