add calculate CRC function

This commit is contained in:
yelvlab 2024-12-20 16:56:05 +08:00
parent 2ff2916ed6
commit 21a5a2e783
2 changed files with 16 additions and 0 deletions

View File

@ -12,6 +12,7 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#define RS485_RCU RCU_USART0
#define RS485_GPIO_RCU RCU_GPIOA

View File

@ -53,3 +53,18 @@ void process_command(char *cmd) {
printf("Invalid Command!\r\n");
}
}
uint8_t calculate_crc(uint8_t package_header[3], uint8_t package_data[], size_t data_length) {
uint8_t crc = 0;
/* Calculate CRC for header */
for (int i = 1; i < 3; i++) {
crc += package_header[i];
}
for (size_t i = 0; i < data_length; i++) {
crc += package_data[i];
}
return (uint8_t)(crc & 0xFF);
}