2023-11-02 04:58 AM - edited 2023-11-02 04:59 AM
I am sending/receiving data using UART between STM32G4 board and other board. While I am sending the data I am adding the CRC and transmitting it through UART to other board. While receiving how do I check the CRC correct or not.
For Eg: From other board to my STM32G4 board I recivedata frame is as below
header | Length | Command | Data | CRC | EOF |
0x32 | 0x05 | 0x17 | 0x00/0x01/0x02 |
| 0x89 |
how do I check the CRC is correct or not in the coding?
Do I need to calculate crc for all 3 possible data values and compare the possible crc with received crc?
is this correct way. Can anyone suggest how to check the CRC.
Thanks
Solved! Go to Solution.
2023-11-02 06:54 AM
You'd need the details of the polynomial used, align your frames as received and compute the correct value for the data received, and compare that.
Write a CRC computation that can take the data a byte at a time, the packet format looks to have flexibility, so all packets are not the same size.
Link to documentation for the protocol. Find a number of valid examples for packets so you can valid the computation based on polynomial, shift direction, initial values, etc.
2023-11-02 05:16 AM
Transmitter and Receiver should use the same algorithm for calculating the CRC, only that the receiver compares the result to the CRC sent by the transmitter.
2023-11-02 06:54 AM
You'd need the details of the polynomial used, align your frames as received and compute the correct value for the data received, and compare that.
Write a CRC computation that can take the data a byte at a time, the packet format looks to have flexibility, so all packets are not the same size.
Link to documentation for the protocol. Find a number of valid examples for packets so you can valid the computation based on polynomial, shift direction, initial values, etc.
2023-11-02 09:17 AM
Here is code that I use for a simple CRC. You'll have to pass the Command, Data and CRC when validating and just Command and Data when calculating the CRC.
/*
* Description: Validate checksum using modulo 256
* Input: the data array and the length of array including the checksum
* Output: true if valid checksum else false
*
*/
bool ValidateChkSum(uint8_t *data, uint8_t len){
uint32_t chksum = 0;
uint32_t i = 0;
for(i = 0; i < len - 1; i++){
chksum += *data++;
}
if((chksum % 256) != *data){
return false;
}
return true;
}
/*
* Description: Calculate checksum using modulo 256 and adds checksum to array at last index + 1
* Input: The data array and the length of array minus the checksum index
* Output:
*
*/
void CalculateChkSum(uint8_t *data, uint8_t len){
uint32_t chksum = 0;
uint32_t i = 0;
for (i = 0; i < len - 1; i++)
{
chksum += *data++;
}
*data = (chksum % 256); // copy checksum to last index
}
2023-11-02 10:52 AM
>>Do I need to calculate crc for all 3 possible data values and compare the possible crc with received crc?
No you'd compute on-the-fly, otherwise the table of possible values for 3 bytes would run to 16 million..
Typically it would include the length and command fields, so you can fully validate the whole content of the messages/packet.
Provide a cite to the specific protocol you are using here.
>>Can anyone suggest how to check the CRC.
Have some documentation showing known working examples, or collect some valid / good data from the serial port to inspect and test against.