generated from hulk/gd32e23x_template
add calculate CRC function
This commit is contained in:
parent
2ff2916ed6
commit
21a5a2e783
@ -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
|
||||
|
15
src/rs485.c
15
src/rs485.c
@ -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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user