generated from hulk/gd32e23x_template
暂存
This commit is contained in:
parent
7ffb8c0612
commit
a5c1c857a9
@ -13,17 +13,19 @@
|
||||
#include "fwdgt.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "ultrasonic_analog.h"
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define RX_BUFFER_SIZE 32
|
||||
|
||||
#define PROTOCOL_PACKAGE_HEADER 0xD5
|
||||
#define PROTOCOL_BOARD_TYPE 0x04
|
||||
#define PROTOCOL_PACKAGE_LENGTH 0x02
|
||||
|
||||
#define MAX_SERIAL_CMD_SIZE 16
|
||||
#define MAX_SERIAL_CMD_COUNT 8
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
typedef enum
|
||||
@ -35,9 +37,21 @@ typedef enum
|
||||
VALIDATION_LENGTH_ERROR = 8
|
||||
} validation_result_t;
|
||||
|
||||
typedef enum {
|
||||
PS_LEN,
|
||||
PS_TYPE,
|
||||
PS_PAYLOAD,
|
||||
PS_CRC,
|
||||
PS_NULL
|
||||
}packet_state_t;
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
void process_command(uint8_t* cmd, size_t length);
|
||||
// void process_command(uint8_t* cmd, size_t length);
|
||||
|
||||
bool code_seen(char code);
|
||||
|
||||
float code_value(void);
|
||||
|
||||
uint8_t calculate_crc(uint8_t data[], uint8_t data_length);
|
||||
|
||||
@ -53,4 +67,10 @@ void gd60914_tempture_report(void);
|
||||
|
||||
void ultrasonic_distance_report(void);
|
||||
|
||||
void start_communication(void);
|
||||
|
||||
void get_command(void);
|
||||
|
||||
void prcess_command(void);
|
||||
|
||||
#endif //RS485_PROTOCOL_H
|
||||
|
17
inc/usart.h
17
inc/usart.h
@ -8,8 +8,25 @@
|
||||
#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_t;
|
||||
|
||||
static ring_buffer_t rx_buffer = {{0}, 0, 0}; // ring buffer for USART0
|
||||
|
||||
void usart_config(void);
|
||||
|
||||
void rs485_config(void);
|
||||
|
||||
void store_char(unsigned char data, ring_buffer_t *rx_buf);
|
||||
|
||||
uint16_t uart_available(void);
|
||||
|
||||
int uart_read(void);
|
||||
|
||||
#endif //USART_H
|
||||
|
@ -35,6 +35,7 @@ OF SUCH DAMAGE.
|
||||
#include "gd32e23x_it.h"
|
||||
|
||||
extern uint16_t g_capture_value;
|
||||
extern ring_buffer_t rx_buffer;
|
||||
|
||||
/*!
|
||||
\brief this function handles NMI exception
|
||||
@ -162,21 +163,22 @@ void USART0_IRQHandler(void) {
|
||||
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);
|
||||
// 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;
|
||||
// // 将接收到的数据存储到缓冲区
|
||||
// if (rx_index < RX_BUFFER_SIZE - 1) {
|
||||
// rx_buffer[rx_index++] = received_data;
|
||||
// }
|
||||
store_char(received_data, &rx_buffer);
|
||||
}
|
||||
//
|
||||
// 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;
|
||||
// }
|
||||
}
|
@ -38,11 +38,12 @@ int main(void)
|
||||
ultrasonic_config();
|
||||
|
||||
while(1){
|
||||
gd60914_get_object_tempture();
|
||||
// gd60914_get_object_tempture();
|
||||
|
||||
delay_ms(50);
|
||||
|
||||
ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES);
|
||||
|
||||
// ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES);
|
||||
|
||||
watchdog_reload();
|
||||
}
|
||||
|
@ -6,49 +6,70 @@
|
||||
|
||||
extern uint8_t g_temperature_uint8[2];
|
||||
|
||||
void process_command(uint8_t *cmd, size_t length) {
|
||||
char combined_str[3];
|
||||
validation_result_t validate = VALIDATION_SUCCESS;
|
||||
static int buf_index_r = 0;
|
||||
static int buf_index_w = 0;
|
||||
static int buf_length = 0;
|
||||
|
||||
validate = (validate_package_header(cmd) |
|
||||
validate_package_type(cmd) |
|
||||
validate_data_length(cmd) |
|
||||
validate_package_crc(cmd, length));
|
||||
static char cmd_buffer[MAX_SERIAL_CMD_COUNT][MAX_SERIAL_CMD_SIZE];
|
||||
static char serial_char = 0x00;
|
||||
|
||||
switch (validate) {
|
||||
case VALIDATION_SUCCESS:
|
||||
// printf("%d", length);
|
||||
sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
|
||||
if (strcmp(combined_str, "M1") == 0) {
|
||||
ultrasonic_distance_report();
|
||||
} else if (strcmp(combined_str, "M2") == 0) {
|
||||
gd60914_tempture_report();
|
||||
} else if (strcmp(combined_str, "M3") == 0)
|
||||
{
|
||||
printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC);
|
||||
fwdgt_reset_mcu();
|
||||
} else {
|
||||
printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
|
||||
return;
|
||||
}
|
||||
break;
|
||||
case VALIDATION_CRC_ERROR:
|
||||
printf("%c%c%c%c%c%c%c", 0xB5, 0xF1, 0x03, 0x65, 0x72, 0x72, 0x3D);
|
||||
break;
|
||||
case VALIDATION_HEADER_ERROR:
|
||||
printf("%c%c%c%c%c%c%c", 0xB5, 0xF2, 0x03, 0x65, 0x72, 0x72, 0x3E);
|
||||
break;
|
||||
case VALIDATION_TYPE_ERROR:
|
||||
printf("%c%c%c%c%c%c%c", 0xB5, 0xF3, 0x03, 0x65, 0x72, 0x72, 0x3F);
|
||||
break;
|
||||
case VALIDATION_LENGTH_ERROR:
|
||||
printf("%c%c%c%c%c%c%c", 0xB5, 0xF4, 0x03, 0x65, 0x72, 0x72, 0x40);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
static int serial_count = 0;
|
||||
static char *strchr_pointer = NULL;
|
||||
|
||||
static packet_state_t packet_state = PS_LEN;
|
||||
|
||||
bool code_seen(char code) {
|
||||
strchr_pointer = strchr(cmd_buffer[buf_index_r], code);
|
||||
return (strchr_pointer != NULL); //Return True if a character was found
|
||||
}
|
||||
|
||||
float code_value(void) {
|
||||
return (strtod(&cmd_buffer[buf_index_r][strchr_pointer - cmd_buffer[buf_index_r] + 1], NULL));
|
||||
}
|
||||
|
||||
// void process_command(uint8_t *cmd, size_t length) {
|
||||
// char combined_str[3];
|
||||
// validation_result_t validate = VALIDATION_SUCCESS;
|
||||
//
|
||||
// validate = (validate_package_header(cmd) |
|
||||
// validate_package_type(cmd) |
|
||||
// validate_data_length(cmd) |
|
||||
// validate_package_crc(cmd, length));
|
||||
//
|
||||
// switch (validate) {
|
||||
// case VALIDATION_SUCCESS:
|
||||
// // printf("%d", length);
|
||||
// sprintf(combined_str, "%c%c", cmd[3], cmd[4]);
|
||||
// if (strcmp(combined_str, "M1") == 0) {
|
||||
// ultrasonic_distance_report();
|
||||
// } else if (strcmp(combined_str, "M2") == 0) {
|
||||
// gd60914_tempture_report();
|
||||
// } else if (strcmp(combined_str, "M3") == 0)
|
||||
// {
|
||||
// printf("%c%c%c%c%c%c", 0xB5, 0xF1, 0x02, 0x6F, 0x6B, 0xCC);
|
||||
// fwdgt_reset_mcu();
|
||||
// } else {
|
||||
// printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
|
||||
// return;
|
||||
// }
|
||||
// break;
|
||||
// case VALIDATION_CRC_ERROR:
|
||||
// printf("%c%c%c%c%c%c%c", 0xB5, 0xF1, 0x03, 0x65, 0x72, 0x72, 0x3D);
|
||||
// break;
|
||||
// case VALIDATION_HEADER_ERROR:
|
||||
// printf("%c%c%c%c%c%c%c", 0xB5, 0xF2, 0x03, 0x65, 0x72, 0x72, 0x3E);
|
||||
// break;
|
||||
// case VALIDATION_TYPE_ERROR:
|
||||
// printf("%c%c%c%c%c%c%c", 0xB5, 0xF3, 0x03, 0x65, 0x72, 0x72, 0x3F);
|
||||
// break;
|
||||
// case VALIDATION_LENGTH_ERROR:
|
||||
// printf("%c%c%c%c%c%c%c", 0xB5, 0xF4, 0x03, 0x65, 0x72, 0x72, 0x40);
|
||||
// break;
|
||||
// default:
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
|
||||
uint8_t calculate_crc(uint8_t data[], uint8_t data_length) {
|
||||
uint8_t crc = 0;
|
||||
|
||||
@ -121,3 +142,25 @@ void ultrasonic_distance_report(void) {
|
||||
printf("%c%c", package_data[0], package_data[1]);
|
||||
printf("%c", calculate_crc(combined_data, 6));
|
||||
}
|
||||
|
||||
void start_communication(void) {
|
||||
|
||||
}
|
||||
|
||||
void get_command(void) {
|
||||
static uint8_t check_sum = 0;
|
||||
static uint8_t packet_len ;
|
||||
static uint8_t packet_type;
|
||||
packet_state = PS_NULL;
|
||||
serial_count = 0;
|
||||
serial_char=0;
|
||||
|
||||
if (uart_available() > 0)
|
||||
delay_ms(5);
|
||||
while (uart_available() > 0 && buf_length < BUFSIZ)
|
||||
|
||||
}
|
||||
|
||||
void prcess_command(void) {
|
||||
|
||||
}
|
||||
|
32
src/usart.c
32
src/usart.c
@ -27,11 +27,14 @@ void usart_config(void)
|
||||
usart_receive_config(USART_PHY, USART_RECEIVE_ENABLE);
|
||||
usart_transmit_config(USART_PHY, USART_TRANSMIT_ENABLE);
|
||||
|
||||
usart_enable(USART_PHY);
|
||||
|
||||
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);
|
||||
nvic_irq_enable(USART0_IRQn, 0);
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -48,3 +51,26 @@ void rs485_config(void)
|
||||
|
||||
gpio_bit_write(RS485_EN_PORT, RS485_EN_PIN, SET); //auto dircetion control
|
||||
}
|
||||
|
||||
void store_char(unsigned char data, ring_buffer_t *rx_buf) {
|
||||
uint16_t i = (unsigned int)(rx_buf->head + 1) % RX_BUFFER_SIZE;
|
||||
|
||||
if (i != rx_buf->tail) {
|
||||
rx_buf->buffer[rx_buf->head] = data;
|
||||
rx_buf->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