From b0b2966efcd383797ead2a0b2fcce229384d9a69 Mon Sep 17 00:00:00 2001 From: yelvlab Date: Mon, 20 Jan 2025 15:25:48 +0800 Subject: [PATCH] =?UTF-8?q?Revert=20"=E6=9B=B4=E6=94=B9=E5=8D=8F=E8=AE=AE?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=83=A8=E5=88=86=EF=BC=8C=E4=BD=86=E6=98=AF?= =?UTF-8?q?=E4=B8=B2=E5=8F=A3=E6=B2=A1=E5=8F=8D=E5=BA=94=EF=BC=9F"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 705cb687647e05a290d148b914362a30ae59c610. --- inc/rs485_protocol.h | 11 ++--- inc/usart.h | 21 ---------- ld/gd32e23x_gcc.ld | 2 +- src/gd32e23x_it.c | 46 +++++++++------------ src/main.c | 1 - src/rs485_protocol.c | 95 +++----------------------------------------- src/usart.c | 34 ++-------------- 7 files changed, 31 insertions(+), 179 deletions(-) diff --git a/inc/rs485_protocol.h b/inc/rs485_protocol.h index cb2ad65..6080a0c 100644 --- a/inc/rs485_protocol.h +++ b/inc/rs485_protocol.h @@ -13,12 +13,13 @@ #include "fwdgt.h" #include #include -#include #include #include "ultrasonic_analog.h" /******************************************************************************/ +#define RX_BUFFER_SIZE 32 + #define PROTOCOL_PACKAGE_HEADER 0xD5 #define PROTOCOL_BOARD_TYPE 0x04 #define PROTOCOL_PACKAGE_LENGTH 0x02 @@ -42,16 +43,10 @@ typedef enum PS_PAYLOAD, PS_CRC, PS_NULL -} packet_state_t; +} packet_state; /******************************************************************************/ -#define BUF_SIZE 8 -#define MAX_CMD_SIZE 16 -#define PACKET_START_BYTE 0xD5 - - -bool code_seen(char code); // void process_command(uint8_t* cmd, size_t length); void process_command(void); diff --git a/inc/usart.h b/inc/usart.h index d9d7afa..2c523d0 100644 --- a/inc/usart.h +++ b/inc/usart.h @@ -8,29 +8,8 @@ #include "gd32e23x.h" #include "board_config.h" -#define RX_BUFFER_SIZE 64 - -typedef struct -{ - unsigned char buffer[RX_BUFFER_SIZE]; - volatile unsigned int head; - volatile unsigned int tail; -}ring_buffer; - void usart_config(void); void rs485_config(void); -void store_char(unsigned char data, ring_buffer *rx_buffer); - -uint16_t uart_available(void); - -int uart_read(void); - -void store_char(unsigned char data, ring_buffer *rx_buffer); - -uint16_t uart_available(void); - -int uart_read(void); - #endif //USART_H diff --git a/ld/gd32e23x_gcc.ld b/ld/gd32e23x_gcc.ld index 3ed20a8..4773fab 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 = 8K } diff --git a/src/gd32e23x_it.c b/src/gd32e23x_it.c index 8d42803..bccf806 100644 --- a/src/gd32e23x_it.c +++ b/src/gd32e23x_it.c @@ -35,7 +35,6 @@ OF SUCH DAMAGE. #include "gd32e23x_it.h" extern uint16_t g_capture_value; -ring_buffer rx_buffer = {{0}, 0, 0}; // ring buffer for USART0 /*! \brief this function handles NMI exception @@ -159,32 +158,25 @@ void EXTI0_1_IRQHandler(void) { } void USART0_IRQHandler(void) { - // static uint8_t rx_index = 0; - // static uint8_t rx_buffer[RX_BUFFER_SIZE]; - // - // if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) { - // usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE); - // uint8_t received_data = (uint8_t) usart_data_receive(USART0); - // - // // 将接收到的数据存储到缓冲区 - // if (rx_index < RX_BUFFER_SIZE - 1) { - // rx_buffer[rx_index++] = received_data; - // } - // } - // - // if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) { - // usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE); - // - // // process_command(rx_buffer, rx_index); // 处理指令 - // - // rx_index = 0; // 重置缓冲区索引 - // return; - // } - static uint8_t data; + static uint8_t rx_index = 0; + static uint8_t rx_buffer[RX_BUFFER_SIZE]; + if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) { - // usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE); - data = usart_data_receive(USART0); - store_char(data, &rx_buffer); - // process_command(&data, 1); + usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE); + uint8_t received_data = (uint8_t) usart_data_receive(USART0); + + // 将接收到的数据存储到缓冲区 + if (rx_index < RX_BUFFER_SIZE - 1) { + rx_buffer[rx_index++] = received_data; + } + } + + if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) { + usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE); + + // process_command(rx_buffer, rx_index); // 处理指令 + + rx_index = 0; // 重置缓冲区索引 + return; } } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 3a2fded..a8bb1ba 100644 --- a/src/main.c +++ b/src/main.c @@ -41,7 +41,6 @@ int main(void) // gd60914_get_object_tempture(); delay_ms(50); - printf("hello"); // ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES); diff --git a/src/rs485_protocol.c b/src/rs485_protocol.c index 3e2ee2c..9658c95 100644 --- a/src/rs485_protocol.c +++ b/src/rs485_protocol.c @@ -5,24 +5,11 @@ #include "rs485_protocol.h" extern uint8_t g_temperature_uint8[2]; -static int buffer_index_r = 0; -static int buffer_index_w = 0; -static int buffer_length = 0; -static char cmd_buffer[BUF_SIZE][MAX_CMD_SIZE]; + +static uint8_t buffer_length = 0; static int serial_count = 0; static uint8_t serial_char = 0x00; -static packet_state_t packet_state = PS_NULL; -static char *strchr_pointer = NULL; - -bool code_seen(char code) { - strchr_pointer = strchr(cmd_buffer[buffer_index_r], code); - return (strchr_pointer != NULL); //Return True if a character was found -} - -float code_value(void) -{ - return (strtod(&cmd_buffer[buffer_index_r][strchr_pointer - cmd_buffer[buffer_index_r] + 1], NULL)); -} +static packet_state state = PS_NULL; // void process_command(uint8_t *cmd, size_t length) { // char combined_str[3]; @@ -147,91 +134,19 @@ void process_printer(void) { } if(buffer_length) { process_command(); - buffer_length = (buffer_length -1); - buffer_index_r = (buffer_index_r + 1) % BUF_SIZE; } } void get_command (void) { + static uint8_t testchar=0; static uint8_t check_sum = 0; static uint8_t packet_len ; static uint8_t packet_type; - packet_state = PS_NULL; + state = PS_NULL; serial_count = 0; serial_char=0; - - if(uart_available() > 0) - delay_ms(5); - while(uart_available() > 0 && buffer_length < BUF_SIZE) { - delay_us(100); - serial_char = uart_read(); - - switch (packet_state) { - case PS_NULL: - if (serial_char == PACKET_START_BYTE) { - serial_count = 0; - check_sum = 0; - packet_state = PS_TYPE; - } else - { - serial_count = 0; - } - break; - case PS_TYPE: - packet_type = serial_char; - check_sum += serial_char; - packet_state = PS_LEN; - break; - case PS_LEN: - packet_len = serial_char; - check_sum += serial_char; - packet_state = PS_PAYLOAD; - break; - case PS_PAYLOAD: - check_sum += serial_char; - cmd_buffer[buffer_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[buffer_index_w][serial_count] = 0; // 终止字符串 - buffer_index_w = (buffer_index_w + 1) % BUF_SIZE; - buffer_length += 1; - serial_count = 0; - break; - default: - packet_state = PS_NULL; - serial_count = 0; - break; - } - } } void process_command(void) { - if (code_seen('M')) { - switch ((int) code_value()) { - case 1: - // ultrasonic_distance_report(); - printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xF1); - break; - case 2: - // gd60914_tempture_report(); - printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xF2); - break; - case 3: - printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC); - fwdgt_reset_mcu(); - break; - default: - printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C); - break; - } - } } \ No newline at end of file diff --git a/src/usart.c b/src/usart.c index 7ed36bd..4416f9e 100644 --- a/src/usart.c +++ b/src/usart.c @@ -4,8 +4,6 @@ #include "usart.h" -extern ring_buffer rx_buffer; // ring buffer for USART0 - /** * @brief configure the USART * @param none @@ -29,14 +27,11 @@ void usart_config(void) usart_receive_config(USART_PHY, USART_RECEIVE_ENABLE); usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE); - /* USART configure */ - // usart_deinit(USART_PHY); - // usart_baudrate_set(USART_PHY, 115200U); - // usart_receive_config(USART_PHY, USART_RECEIVE_ENABLE); - // usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE); + usart_enable(USART_PHY); + + nvic_irq_enable(USART0_IRQn, 0); usart_interrupt_enable(USART_PHY, USART_INT_RBNE); usart_interrupt_enable(USART_PHY, USART_INT_IDLE); - usart_enable(USART_PHY); } /** @@ -52,27 +47,4 @@ void rs485_config(void) gpio_output_options_set(RS485_EN_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN); gpio_bit_write(RS485_EN_PORT, RS485_EN_PIN, SET); //auto dircetion control -} - -void store_char(unsigned char data, ring_buffer *rx_buffer) { - uint16_t i = (unsigned int)(rx_buffer->head + 1) % RX_BUFFER_SIZE; - - if (i != rx_buffer->tail) { - rx_buffer->buffer[rx_buffer->head] = data; - rx_buffer->head = i; - } -} - -uint16_t uart_available(void) { - return (unsigned int)(RX_BUFFER_SIZE + rx_buffer.head - rx_buffer.tail) % RX_BUFFER_SIZE; -} - -int uart_read(void) { - if (rx_buffer.head == rx_buffer.tail) { - return -1; - } else { - unsigned char c = rx_buffer.buffer[rx_buffer.tail]; - rx_buffer.tail = (unsigned int)(rx_buffer.tail + 1) % RX_BUFFER_SIZE; - return c; - } } \ No newline at end of file