From 39d800cb1b244a65b76ef235241e2892eb7fd0da Mon Sep 17 00:00:00 2001 From: yelvlab Date: Mon, 20 Jan 2025 11:12:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/rs485_protocol.h | 4 ++- ld/gd32e23x_gcc.ld | 2 +- src/gd32e23x_it.c | 2 +- src/main.c | 3 +- src/rs485_protocol.c | 69 ++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 73 insertions(+), 7 deletions(-) diff --git a/inc/rs485_protocol.h b/inc/rs485_protocol.h index 8c0a153..6c835f6 100644 --- a/inc/rs485_protocol.h +++ b/inc/rs485_protocol.h @@ -23,8 +23,10 @@ #define PROTOCOL_BOARD_TYPE 0x04 #define PROTOCOL_PACKAGE_LENGTH 0x02 +#define PACKET_START_BYTE 0xD5 + #define MAX_SERIAL_CMD_SIZE 16 -#define MAX_SERIAL_CMD_COUNT 8 +#define BUF_SIZE 8 /******************************************************************************/ diff --git a/ld/gd32e23x_gcc.ld b/ld/gd32e23x_gcc.ld index a811994..65afe02 100644 --- a/ld/gd32e23x_gcc.ld +++ b/ld/gd32e23x_gcc.ld @@ -1,7 +1,7 @@ /* memory map */ MEMORY { - FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 16K + FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 64K RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 4K } diff --git a/src/gd32e23x_it.c b/src/gd32e23x_it.c index 492ffe1..a79d490 100644 --- a/src/gd32e23x_it.c +++ b/src/gd32e23x_it.c @@ -118,7 +118,7 @@ void TIMER5_IRQHandler(void) { } else { //! turn off led & reconfig timer13 period to 1000(100ms) gpio_bit_write(LED_PORT, LED_PIN, SET); - timer_autoreload_value_config(LED_BLINK_TIMER, 800); + timer_autoreload_value_config(LED_BLINK_TIMER, 8000); } led_status = !led_status; } diff --git a/src/main.c b/src/main.c index f3d562b..309d554 100644 --- a/src/main.c +++ b/src/main.c @@ -40,7 +40,8 @@ int main(void) while(1){ // gd60914_get_object_tempture(); - delay_ms(50); + delay_ms(500); + // start_communication(); // ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES); diff --git a/src/rs485_protocol.c b/src/rs485_protocol.c index cd9dffd..c81a9b9 100644 --- a/src/rs485_protocol.c +++ b/src/rs485_protocol.c @@ -10,7 +10,7 @@ static int buf_index_r = 0; static int buf_index_w = 0; static int buf_length = 0; -static char cmd_buffer[MAX_SERIAL_CMD_COUNT][MAX_SERIAL_CMD_SIZE]; +static char cmd_buffer[BUF_SIZE][MAX_SERIAL_CMD_SIZE]; static char serial_char = 0x00; static int serial_count = 0; @@ -144,6 +144,15 @@ void ultrasonic_distance_report(void) { } void start_communication(void) { + if (buf_length < (BUF_SIZE - 1)) { + get_command(); + } + + if (buf_length) { + prcess_command(); + buf_length--; + buf_index_r = (buf_index_r + 1) % BUF_SIZE; + } } @@ -157,10 +166,64 @@ void get_command(void) { if (uart_available() > 0) delay_ms(5); - while (uart_available() > 0 && buf_length < BUFSIZ) - + while (uart_available() > 0 && buf_length < BUF_SIZE) { + delay_us(100); + serial_char = uart_read(); + switch (packet_state) { + case PS_NULL: + if (serial_char == PACKET_START_BYTE) { + packet_state = PS_TYPE; + serial_count = 0; + check_sum = 0; + } else { + serial_count = 0; + } + break; + case PS_TYPE: + packet_type = serial_char; + check_sum += serial_char; + packet_state = PS_LEN; + break; + case PS_LEN: + check_sum += serial_char; + packet_len = serial_char; + packet_state = PS_PAYLOAD; + break; + case PS_PAYLOAD: + check_sum += serial_char; + cmd_buffer[buf_index_w][serial_count++] = serial_char; + if (serial_count >= packet_len) { + packet_state = PS_CRC; + } + break; + case PS_CRC: + packet_state = PS_NULL; + if (!serial_count || check_sum != serial_char) { + serial_count = 0; + return; + } + cmd_buffer[buf_index_w][serial_count] = 0; + buf_index_w = (buf_index_w + 1) % BUF_SIZE; + buf_length++; + serial_count = 0; + } + } } void prcess_command(void) { + if (code_seen('M')) { + switch ((int)code_value()) { + case 1: + // ultrasonic_distance_report(); + printf("M1"); + break; + case 2: + // gd60914_tempture_report(); + printf("M2"); + break; + default: + break; + } + } }