generated from hulk/gd32e23x_template
Revert "更改协议处理部分,但是串口没反应?"
This reverts commit 705cb687647e05a290d148b914362a30ae59c610.
This commit is contained in:
parent
705cb68764
commit
9f615ba3a3
@ -13,12 +13,13 @@
|
|||||||
#include "fwdgt.h"
|
#include "fwdgt.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "ultrasonic_analog.h"
|
#include "ultrasonic_analog.h"
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
|
#define RX_BUFFER_SIZE 32
|
||||||
|
|
||||||
#define PROTOCOL_PACKAGE_HEADER 0xD5
|
#define PROTOCOL_PACKAGE_HEADER 0xD5
|
||||||
#define PROTOCOL_BOARD_TYPE 0x04
|
#define PROTOCOL_BOARD_TYPE 0x04
|
||||||
#define PROTOCOL_PACKAGE_LENGTH 0x02
|
#define PROTOCOL_PACKAGE_LENGTH 0x02
|
||||||
@ -42,16 +43,10 @@ typedef enum
|
|||||||
PS_PAYLOAD,
|
PS_PAYLOAD,
|
||||||
PS_CRC,
|
PS_CRC,
|
||||||
PS_NULL
|
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(uint8_t* cmd, size_t length);
|
||||||
void process_command(void);
|
void process_command(void);
|
||||||
|
|
||||||
|
21
inc/usart.h
21
inc/usart.h
@ -8,29 +8,8 @@
|
|||||||
#include "gd32e23x.h"
|
#include "gd32e23x.h"
|
||||||
#include "board_config.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 usart_config(void);
|
||||||
|
|
||||||
void rs485_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
|
#endif //USART_H
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/* memory map */
|
/* memory map */
|
||||||
MEMORY
|
MEMORY
|
||||||
{
|
{
|
||||||
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 16K
|
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 64K
|
||||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 8K
|
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 8K
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +35,6 @@ OF SUCH DAMAGE.
|
|||||||
#include "gd32e23x_it.h"
|
#include "gd32e23x_it.h"
|
||||||
|
|
||||||
extern uint16_t g_capture_value;
|
extern uint16_t g_capture_value;
|
||||||
ring_buffer rx_buffer = {{0}, 0, 0}; // ring buffer for USART0
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\brief this function handles NMI exception
|
\brief this function handles NMI exception
|
||||||
@ -159,32 +158,25 @@ void EXTI0_1_IRQHandler(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void USART0_IRQHandler(void) {
|
void USART0_IRQHandler(void) {
|
||||||
// static uint8_t rx_index = 0;
|
static uint8_t rx_index = 0;
|
||||||
// static uint8_t rx_buffer[RX_BUFFER_SIZE];
|
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;
|
|
||||||
if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
|
if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
|
||||||
// usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
|
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
|
||||||
data = usart_data_receive(USART0);
|
uint8_t received_data = (uint8_t) usart_data_receive(USART0);
|
||||||
store_char(data, &rx_buffer);
|
|
||||||
// process_command(&data, 1);
|
// 将接收到的数据存储到缓冲区
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -41,7 +41,6 @@ int main(void)
|
|||||||
// gd60914_get_object_tempture();
|
// gd60914_get_object_tempture();
|
||||||
|
|
||||||
delay_ms(50);
|
delay_ms(50);
|
||||||
printf("hello");
|
|
||||||
|
|
||||||
// ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES);
|
// ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES);
|
||||||
|
|
||||||
|
@ -5,24 +5,11 @@
|
|||||||
#include "rs485_protocol.h"
|
#include "rs485_protocol.h"
|
||||||
|
|
||||||
extern uint8_t g_temperature_uint8[2];
|
extern uint8_t g_temperature_uint8[2];
|
||||||
static int buffer_index_r = 0;
|
|
||||||
static int buffer_index_w = 0;
|
static uint8_t buffer_length = 0;
|
||||||
static int buffer_length = 0;
|
|
||||||
static char cmd_buffer[BUF_SIZE][MAX_CMD_SIZE];
|
|
||||||
static int serial_count = 0;
|
static int serial_count = 0;
|
||||||
static uint8_t serial_char = 0x00;
|
static uint8_t serial_char = 0x00;
|
||||||
static packet_state_t packet_state = PS_NULL;
|
static packet_state 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));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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];
|
||||||
@ -147,91 +134,19 @@ void process_printer(void) {
|
|||||||
}
|
}
|
||||||
if(buffer_length) {
|
if(buffer_length) {
|
||||||
process_command();
|
process_command();
|
||||||
buffer_length = (buffer_length -1);
|
|
||||||
buffer_index_r = (buffer_index_r + 1) % BUF_SIZE;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_command (void) {
|
void get_command (void) {
|
||||||
|
static uint8_t testchar=0;
|
||||||
static uint8_t check_sum = 0;
|
static uint8_t check_sum = 0;
|
||||||
static uint8_t packet_len ;
|
static uint8_t packet_len ;
|
||||||
static uint8_t packet_type;
|
static uint8_t packet_type;
|
||||||
packet_state = PS_NULL;
|
state = PS_NULL;
|
||||||
serial_count = 0;
|
serial_count = 0;
|
||||||
serial_char=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) {
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
34
src/usart.c
34
src/usart.c
@ -4,8 +4,6 @@
|
|||||||
|
|
||||||
#include "usart.h"
|
#include "usart.h"
|
||||||
|
|
||||||
extern ring_buffer rx_buffer; // ring buffer for USART0
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief configure the USART
|
* @brief configure the USART
|
||||||
* @param none
|
* @param none
|
||||||
@ -29,14 +27,11 @@ void usart_config(void)
|
|||||||
usart_receive_config(USART_PHY, USART_RECEIVE_ENABLE);
|
usart_receive_config(USART_PHY, USART_RECEIVE_ENABLE);
|
||||||
usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE);
|
usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE);
|
||||||
|
|
||||||
/* USART configure */
|
usart_enable(USART_PHY);
|
||||||
// usart_deinit(USART_PHY);
|
|
||||||
// usart_baudrate_set(USART_PHY, 115200U);
|
nvic_irq_enable(USART0_IRQn, 0);
|
||||||
// usart_receive_config(USART_PHY, USART_RECEIVE_ENABLE);
|
|
||||||
// usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE);
|
|
||||||
usart_interrupt_enable(USART_PHY, USART_INT_RBNE);
|
usart_interrupt_enable(USART_PHY, USART_INT_RBNE);
|
||||||
usart_interrupt_enable(USART_PHY, USART_INT_IDLE);
|
usart_interrupt_enable(USART_PHY, USART_INT_IDLE);
|
||||||
usart_enable(USART_PHY);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -53,26 +48,3 @@ void rs485_config(void)
|
|||||||
|
|
||||||
gpio_bit_write(RS485_EN_PORT, RS485_EN_PIN, SET); //auto dircetion control
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user