2022-07-28 11:57 PM
Hi. i'm working with L9369 H-bridge driver and i want to know how to calculate CRC code in C programming
Do you have any documentation regarding CRC implementation in C code?
thank you.
2022-07-29 12:27 AM
Would need an actual Data Sheet, not the Data Brief to determine this
2022-07-29 12:41 AM
thanks for your replying.
i have L9369 Datasheet and it says it use X^5+X^2+1 as polynomial. and it use "11111(0x1F)" as initial value.
i don't know how to implement CRC Table and get CRC value by C programing. please help me.
thank you.
2022-07-29 12:56 AM
A table may or may not be appropriate, depends on how the data needs to be processed.
Could you just use a simple bit-wise application of the polynomial?
0x25 (0x05)
ie something like, assuming a right shift, MSB fed
if (crc & 0x10)
crc = (crc << 1) ^ 0x25; // this has the secondary effect of clearing the high order bit
else
crc <<=1;
Do you have some example words / test patterns?
Send me the full data sheet or attach.
sourcer32@gmail.com
2022-07-29 01:01 AM
Found this https://www.st.com/resource/en/datasheet/l5965.pdf probably consistent
2022-07-29 01:27 AM
If you read out the current registers you can generate other examples and test patterns.
I could build a table driven variant, fund work via https://paypal.me/cliveone
// CRC5 0x05/0x25 Polynomial Example - sourcer32@gmail.com
// Copyright (C) 2022, C Turvey, All Rights Reserved
// ST PMIC CRC Generation Test, L9369 via L5965 docs
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
/*
https://www.st.com/resource/en/datasheet/l5965.pdf
SPI frame CRC generator
The SPI protocol is defined by frames of 32 bits with 5 bits of CRC (Cyclic
Redundancy Check) in both input and output directions. The polynomial
calculation implemented is: g(x)= x^5 + x^2 + 1 the structure of CRC generator
is shown in Figure 25. Structure of CRC generator.
Here are the rules:
1. For DI, CNT=DI[21] is ignored when calculating CRC, it means
only {DI[31:22],DI[20:5]} is used to calculate CRC. For example,
if DI[31:5]=27'b1000_0010_1011_1111_1111_1111_111, the CRC[4:0]= 5'b0_0011
2. For DO, DO[21] is ignored when calculating CRC, it means only
{DO[31:22],DO[20:5]} is used to calculate CRC.
3. The initial value of CRC generator is 5'b1_1111.
4. MSB (DI[31]) is shift in CRC generator at first.
*/
int main(int argc, char **argv) // Copyright (C) 2022 sourcer32@gmail.com
{
int i;
uint32_t test = 0x82BFFFE0; // 0x03
uint32_t crc;
crc = 0x1F << 27; // Initial, aligned high-order to match data
crc = crc ^ test; // Apply data
for(i=0; i<27; i++) // Consume data
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 (03?) -> %08X\n", crc, test ^ crc);
return(1);
}
2022-07-29 02:44 AM
thank you very much for specific sample code.
i have a doubt. The data bit 21 is ignored. so i think we have to compute CRC based on the data
b0100_0001_0101_1111_1111_1111_111+'0_0000'(0x415FFFE0)(delete 21's bit)
not b1000_0010_1011_1111_1111_1111_111+'0_0000'(0x82BFFFE0)
when i calculate CRC with 0x415FFFE0. the result crc is 0x15.
is above logic wrong?
thank you.
2022-07-29 03:50 AM
Don't know, I don't have the data sheet for the part, the parts, or means to get example test patterns to check / validate.
The example they give has a valid response for the polynomial and methods, and then has other rules that seem at odds with how CRCs are implemented ..
2022-07-29 04:56 AM
>>when i calculate CRC with 0x415FFFE0. the result crc is 0x15. is above logic wrong?
The result is wrong.
CRC:03 -> 82BFFFE3
CRC:1D -> 7654321D
CRC:1F -> AABBCC1F
2022-07-30 12:49 AM
Hi. I found why 0x415FFFE0's result crc is 0x15. and wrong.
the most high byte is "4", so MSB's bit position is 27, not 28. so shift operation need to be 27->26. and in this case, CRC is 03.
thank you for your help.
thank you.