generated from hulk/gd32e23x_template
107 lines
3.6 KiB
C
107 lines
3.6 KiB
C
//
|
|
// Created by dell on 24-12-3.
|
|
//
|
|
|
|
#include "rs485.h"
|
|
|
|
extern uint32_t g_temperature_uint32;
|
|
extern uint32_t g_eddy_current_value_uint32;
|
|
|
|
uint8_t package_header[3] = {0xB5, 0x00, 0x04};
|
|
uint8_t package_data[4] = {0};
|
|
|
|
void rs485_config(void) {
|
|
rcu_periph_clock_enable(RS485_GPIO_RCU);
|
|
rcu_periph_clock_enable(RS485_RCU);
|
|
|
|
gpio_af_set(RS485_GPIO_PORT, GPIO_AF_1, GPIO_PIN_2 | GPIO_PIN_3);
|
|
|
|
/* configure USART Tx&Rx as alternate function push-pull */
|
|
gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, RS485_TX_PIN | RS485_RX_PIN);
|
|
gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, RS485_TX_PIN | RS485_RX_PIN);
|
|
|
|
/* configure RS485 EN Pin */
|
|
gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, RS485_EN_PIN);
|
|
gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN);
|
|
gpio_bit_write(RS485_GPIO_PORT, RS485_EN_PIN, SET);
|
|
|
|
/* USART configure */
|
|
usart_deinit(RS485_PHY);
|
|
usart_baudrate_set(RS485_PHY, RS485_BAUDRATE);
|
|
usart_receive_config(RS485_PHY, USART_RECEIVE_ENABLE);
|
|
usart_transmit_config(RS485_PHY, USART_TRANSMIT_ENABLE);
|
|
|
|
usart_enable(RS485_PHY);
|
|
|
|
nvic_irq_enable(USART0_IRQn, 0);
|
|
usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);
|
|
}
|
|
|
|
void process_command(char *cmd) {
|
|
|
|
|
|
|
|
|
|
|
|
if (strncmp(cmd, "M12", 3) == 0) {
|
|
// printf("M1 -=-=- OK!\r\n");
|
|
eddy_current_value_report();
|
|
} else if (strncmp(cmd, "M2", 2) == 0) {
|
|
// printf("M2 -=-=- OK!\r\n");
|
|
tempture_value_report();
|
|
// } else if (strncmp(cmd, "M3", 2) == 0) {
|
|
// char *param_str = cmd + 2; // Skip "M3"
|
|
// int param = atoi(param_str + 1); // Skip "S" and convert to integer
|
|
// if (param >= 0 && param <= 100) {
|
|
// printf("M3 with parameter %d -=-=- OK!\r\n", param);
|
|
// } else {
|
|
// printf("Invalid parameter for M3 command!\r\n");
|
|
// }
|
|
} else {
|
|
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);
|
|
}
|
|
|
|
void eddy_current_value_report(void) {
|
|
package_header[1] = 0xF0; //eddy current
|
|
|
|
package_data[0] = (g_eddy_current_value_uint32 >> 24) & 0xFF;
|
|
package_data[1] = (g_eddy_current_value_uint32 >> 16) & 0xFF;
|
|
package_data[2] = (g_eddy_current_value_uint32 >> 8) & 0xFF;
|
|
package_data[3] = g_eddy_current_value_uint32 & 0xFF;
|
|
|
|
// printf("EddyCurrent: %x\r\n", g_eddy_current_value_uint32);
|
|
printf("%c%c%c", package_header[0], package_header[1], package_header[2]);
|
|
printf("%c%c%c%c", package_data[0], package_data[1], package_data[2], package_data[3]);
|
|
printf("%c\r\n", calculate_crc(package_header, package_data, 4));
|
|
}
|
|
|
|
void tempture_value_report(void) {
|
|
package_header[1] = 0xF1; //temperature
|
|
|
|
package_data[0] = (g_temperature_uint32 >> 24) & 0xFF;
|
|
package_data[1] = (g_temperature_uint32 >> 16) & 0xFF;
|
|
package_data[2] = (g_temperature_uint32 >> 8) & 0xFF;
|
|
package_data[3] = g_temperature_uint32 & 0xFF;
|
|
|
|
// printf("Temperature: %x\r\n", g_temperature_uint32);
|
|
printf("%c%c%c", package_header[0], package_header[1], package_header[2]);
|
|
printf("%c%c%c%c", package_data[0], package_data[1], package_data[2], package_data[3]);
|
|
printf("%c\r\n", calculate_crc(package_header, package_data, 4));
|
|
}
|