generated from hulk/gd32e23x_template
format code
This commit is contained in:
parent
fca25e1981
commit
eca38a53b8
74
src/rs485.c
74
src/rs485.c
@ -10,8 +10,7 @@ extern uint32_t g_eddy_current_value_uint32;
|
|||||||
uint8_t package_header[3] = {0xB5, 0xF0, 0x04};
|
uint8_t package_header[3] = {0xB5, 0xF0, 0x04};
|
||||||
uint8_t package_data[4] = {0};
|
uint8_t package_data[4] = {0};
|
||||||
|
|
||||||
void rs485_config(void)
|
void rs485_config(void) {
|
||||||
{
|
|
||||||
rcu_periph_clock_enable(RS485_GPIO_RCU);
|
rcu_periph_clock_enable(RS485_GPIO_RCU);
|
||||||
rcu_periph_clock_enable(RS485_RCU);
|
rcu_periph_clock_enable(RS485_RCU);
|
||||||
|
|
||||||
@ -39,8 +38,7 @@ void rs485_config(void)
|
|||||||
usart_interrupt_enable(RS485_PHY, USART_INT_IDLE);
|
usart_interrupt_enable(RS485_PHY, USART_INT_IDLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void process_command(uint8_t* cmd, size_t length)
|
void process_command(uint8_t *cmd, size_t length) {
|
||||||
{
|
|
||||||
char combined_str[3];
|
char combined_str[3];
|
||||||
validation_result_t validate = VALIDATION_SUCCESS;
|
validation_result_t validate = VALIDATION_SUCCESS;
|
||||||
|
|
||||||
@ -49,21 +47,15 @@ void process_command(uint8_t* cmd, size_t length)
|
|||||||
validate_data_length(cmd) |
|
validate_data_length(cmd) |
|
||||||
validate_package_crc(cmd, length));
|
validate_package_crc(cmd, length));
|
||||||
|
|
||||||
switch (validate)
|
switch (validate) {
|
||||||
{
|
|
||||||
case VALIDATION_SUCCESS:
|
case VALIDATION_SUCCESS:
|
||||||
// printf("%d", length);
|
// printf("%d", length);
|
||||||
sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
|
sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
|
||||||
if (strcmp(combined_str, "M1") == 0)
|
if (strcmp(combined_str, "M1") == 0) {
|
||||||
{
|
|
||||||
eddy_current_value_report();
|
eddy_current_value_report();
|
||||||
}
|
} else if (strcmp(combined_str, "M2") == 0) {
|
||||||
else if (strcmp(combined_str, "M2") == 0)
|
|
||||||
{
|
|
||||||
tempture_value_report();
|
tempture_value_report();
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
|
printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -85,68 +77,49 @@ void process_command(uint8_t* cmd, size_t length)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t calculate_crc(uint8_t data[], uint8_t data_length)
|
uint8_t calculate_crc(uint8_t data[], uint8_t data_length) {
|
||||||
{
|
|
||||||
uint8_t crc = 0;
|
uint8_t crc = 0;
|
||||||
|
|
||||||
for (uint8_t i = 1; i < data_length - 1; i++)
|
for (uint8_t i = 1; i < data_length - 1; i++) {
|
||||||
{
|
|
||||||
crc += data[i];
|
crc += data[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return (uint8_t)(crc & 0xFF);
|
return (uint8_t) (crc & 0xFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
validation_result_t validate_package_crc(uint8_t* data, uint8_t data_length)
|
validation_result_t validate_package_crc(uint8_t *data, uint8_t data_length) {
|
||||||
{
|
if (data[data_length - 1] == calculate_crc(data, data_length) && data_length == 3 + data[2] + 1) {
|
||||||
if (data[data_length - 1] == calculate_crc(data, data_length) && data_length == 3 + data[2] + 1)
|
|
||||||
{
|
|
||||||
return VALIDATION_SUCCESS;
|
return VALIDATION_SUCCESS;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return VALIDATION_CRC_ERROR;
|
return VALIDATION_CRC_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
validation_result_t validate_package_header(uint8_t* data)
|
validation_result_t validate_package_header(uint8_t *data) {
|
||||||
{
|
if (data[0] == LDC1612_PACKAGE_HEADER) {
|
||||||
if (data[0] == LDC1612_PACKAGE_HEADER)
|
|
||||||
{
|
|
||||||
return VALIDATION_SUCCESS;
|
return VALIDATION_SUCCESS;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return VALIDATION_HEADER_ERROR;
|
return VALIDATION_HEADER_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
validation_result_t validate_package_type(uint8_t* data)
|
validation_result_t validate_package_type(uint8_t *data) {
|
||||||
{
|
if (data[1] == LDC1612_BOARD_TYPE) {
|
||||||
if (data[1] == LDC1612_BOARD_TYPE)
|
|
||||||
{
|
|
||||||
return VALIDATION_SUCCESS;
|
return VALIDATION_SUCCESS;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return VALIDATION_TYPE_ERROR;
|
return VALIDATION_TYPE_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
validation_result_t validate_data_length(uint8_t* data)
|
validation_result_t validate_data_length(uint8_t *data) {
|
||||||
{
|
if (data[2] == LDC1612_PACKAGE_LENGTH) {
|
||||||
if (data[2] == LDC1612_PACKAGE_LENGTH)
|
|
||||||
{
|
|
||||||
return VALIDATION_SUCCESS;
|
return VALIDATION_SUCCESS;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
return VALIDATION_LENGTH_ERROR;
|
return VALIDATION_LENGTH_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void eddy_current_value_report(void)
|
void eddy_current_value_report(void) {
|
||||||
{
|
|
||||||
package_data[0] = (g_eddy_current_value_uint32 >> 24) & 0xFF;
|
package_data[0] = (g_eddy_current_value_uint32 >> 24) & 0xFF;
|
||||||
package_data[1] = (g_eddy_current_value_uint32 >> 16) & 0xFF;
|
package_data[1] = (g_eddy_current_value_uint32 >> 16) & 0xFF;
|
||||||
package_data[2] = (g_eddy_current_value_uint32 >> 8) & 0xFF;
|
package_data[2] = (g_eddy_current_value_uint32 >> 8) & 0xFF;
|
||||||
@ -161,8 +134,7 @@ void eddy_current_value_report(void)
|
|||||||
printf("%c", calculate_crc(combined_data, 7));
|
printf("%c", calculate_crc(combined_data, 7));
|
||||||
}
|
}
|
||||||
|
|
||||||
void tempture_value_report(void)
|
void tempture_value_report(void) {
|
||||||
{
|
|
||||||
package_data[0] = (g_temperature_uint32 >> 24) & 0xFF;
|
package_data[0] = (g_temperature_uint32 >> 24) & 0xFF;
|
||||||
package_data[1] = (g_temperature_uint32 >> 16) & 0xFF;
|
package_data[1] = (g_temperature_uint32 >> 16) & 0xFF;
|
||||||
package_data[2] = (g_temperature_uint32 >> 8) & 0xFF;
|
package_data[2] = (g_temperature_uint32 >> 8) & 0xFF;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user