generated from hulk/gd32e23x_template
Compare commits
6 Commits
705cb68764
...
feature-pr
Author | SHA1 | Date | |
---|---|---|---|
c11db2c9bf | |||
11cf546438 | |||
1dacace57a | |||
a546f7bf83 | |||
39d800cb1b | |||
a5c1c857a9 |
@@ -41,13 +41,14 @@
|
||||
/******************************************************************************/
|
||||
|
||||
#define USART_GPIO_RCU RCU_GPIOA
|
||||
#define USART_RCU RCU_USART0
|
||||
#define USART_RCU RCU_USART1
|
||||
#define USART_GPIO_PORT GPIOA
|
||||
#define USART_GPIO_AF GPIO_AF_1
|
||||
#define USART_TX_PIN GPIO_PIN_2
|
||||
#define USART_RX_PIN GPIO_PIN_3
|
||||
#define USART_PHY USART0
|
||||
#define USART_PHY USART1
|
||||
#define USART_PHY_BAUDRATE 115200U
|
||||
#define USART_PHY_IRQ USART1_IRQn
|
||||
#define RS485_EN_PORT GPIOA
|
||||
#define RS485_EN_PIN GPIO_PIN_4
|
||||
|
||||
|
@@ -23,6 +23,11 @@
|
||||
#define PROTOCOL_BOARD_TYPE 0x04
|
||||
#define PROTOCOL_PACKAGE_LENGTH 0x02
|
||||
|
||||
#define PACKET_START_BYTE 0xD5
|
||||
|
||||
#define MAX_SERIAL_CMD_SIZE 16
|
||||
#define BUF_SIZE 8
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
typedef enum
|
||||
@@ -34,10 +39,8 @@ typedef enum
|
||||
VALIDATION_LENGTH_ERROR = 8
|
||||
} validation_result_t;
|
||||
|
||||
|
||||
typedef enum
|
||||
{
|
||||
PS_LEN = 0,
|
||||
typedef enum {
|
||||
PS_LEN,
|
||||
PS_TYPE,
|
||||
PS_PAYLOAD,
|
||||
PS_CRC,
|
||||
@@ -46,14 +49,11 @@ typedef enum
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
#define BUF_SIZE 8
|
||||
#define MAX_CMD_SIZE 16
|
||||
#define PACKET_START_BYTE 0xD5
|
||||
|
||||
// void process_command(uint8_t* cmd, size_t length);
|
||||
|
||||
bool code_seen(char code);
|
||||
// void process_command(uint8_t* cmd, size_t length);
|
||||
void process_command(void);
|
||||
|
||||
float code_value(void);
|
||||
|
||||
uint8_t calculate_crc(uint8_t data[], uint8_t data_length);
|
||||
|
||||
@@ -69,8 +69,10 @@ void gd60914_tempture_report(void);
|
||||
|
||||
void ultrasonic_distance_report(void);
|
||||
|
||||
void process_printer(void);
|
||||
void start_communication(void);
|
||||
|
||||
void get_command(void);
|
||||
|
||||
void prcess_command(void);
|
||||
|
||||
#endif //RS485_PROTOCOL_H
|
||||
|
12
inc/usart.h
12
inc/usart.h
@@ -15,19 +15,15 @@ typedef struct
|
||||
unsigned char buffer[RX_BUFFER_SIZE];
|
||||
volatile unsigned int head;
|
||||
volatile unsigned int tail;
|
||||
}ring_buffer;
|
||||
}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 *rx_buffer);
|
||||
|
||||
uint16_t uart_available(void);
|
||||
|
||||
int uart_read(void);
|
||||
|
||||
void store_char(unsigned char data, ring_buffer *rx_buffer);
|
||||
void store_char(unsigned char data, ring_buffer_t *rx_buf);
|
||||
|
||||
uint16_t uart_available(void);
|
||||
|
||||
|
@@ -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
|
||||
}
|
||||
|
||||
|
@@ -35,7 +35,7 @@ OF SUCH DAMAGE.
|
||||
#include "gd32e23x_it.h"
|
||||
|
||||
extern uint16_t g_capture_value;
|
||||
ring_buffer rx_buffer = {{0}, 0, 0}; // ring buffer for USART0
|
||||
extern ring_buffer_t rx_buffer;
|
||||
|
||||
/*!
|
||||
\brief this function handles NMI exception
|
||||
@@ -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;
|
||||
}
|
||||
@@ -158,33 +158,28 @@ void EXTI0_1_IRQHandler(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void USART0_IRQHandler(void) {
|
||||
void USART1_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 (RESET != usart_interrupt_flag_get(USART_PHY, USART_INT_FLAG_RBNE)) {
|
||||
// usart_interrupt_flag_clear(USART_PHY, USART_INT_FLAG_RBNE);
|
||||
uint8_t received_data = (uint8_t) usart_data_receive(USART_PHY);
|
||||
|
||||
// printf("%c", received_data);
|
||||
// 将接收到的数据存储到缓冲区
|
||||
// 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); // 处理指令
|
||||
// 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)) {
|
||||
// usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
|
||||
data = usart_data_receive(USART0);
|
||||
store_char(data, &rx_buffer);
|
||||
// process_command(&data, 1);
|
||||
}
|
||||
}
|
25
src/main.c
25
src/main.c
@@ -6,6 +6,8 @@
|
||||
*/
|
||||
#include "main.h"
|
||||
|
||||
#include "newlib.h"
|
||||
|
||||
volatile uint8_t g_temperature_uint8[2] = {0};
|
||||
volatile uint16_t g_capture_value;
|
||||
|
||||
@@ -25,13 +27,13 @@ int main(void)
|
||||
/* configure LED */
|
||||
led_blink_config();
|
||||
/* configure FWDGT */
|
||||
watchdog_init();
|
||||
// watchdog_init();
|
||||
|
||||
#ifdef SOFTWARE_IIC
|
||||
soft_i2c_config();
|
||||
#else
|
||||
i2c_config();
|
||||
#endif
|
||||
// #ifdef SOFTWARE_IIC
|
||||
// soft_i2c_config();
|
||||
// #else
|
||||
// i2c_config();
|
||||
// #endif
|
||||
|
||||
printf("system start!\r\n");
|
||||
|
||||
@@ -40,20 +42,21 @@ int main(void)
|
||||
while(1){
|
||||
// gd60914_get_object_tempture();
|
||||
|
||||
delay_ms(50);
|
||||
printf("hello");
|
||||
delay_ms(500);
|
||||
start_communication();
|
||||
printf("hello world!\r\n");
|
||||
|
||||
// ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES);
|
||||
|
||||
watchdog_reload();
|
||||
// watchdog_reload();
|
||||
}
|
||||
}
|
||||
|
||||
/* retarget the C library printf function to the USART */
|
||||
int _write(int fd, char *pBuffer, int size) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
usart_data_transmit(USART0, (uint8_t) pBuffer[i]);
|
||||
while (RESET == usart_flag_get(USART0, USART_FLAG_TBE));
|
||||
usart_data_transmit(USART1, (uint8_t) pBuffer[i]);
|
||||
while (RESET == usart_flag_get(USART1, USART_FLAG_TBE));
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
@@ -5,23 +5,37 @@
|
||||
#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 int buf_index_r = 0;
|
||||
static int buf_index_w = 0;
|
||||
static int buf_length = 0;
|
||||
|
||||
static char cmd_buffer[BUF_SIZE][MAX_SERIAL_CMD_SIZE];
|
||||
static char serial_char = 0x00;
|
||||
|
||||
static int serial_count = 0;
|
||||
static uint8_t serial_char = 0x00;
|
||||
static packet_state_t packet_state = PS_NULL;
|
||||
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
|
||||
// }
|
||||
|
||||
bool code_seen(char code) {
|
||||
strchr_pointer = strchr(cmd_buffer[buffer_index_r], code);
|
||||
strchr_pointer = NULL;
|
||||
for (int i = 0; cmd_buffer[buf_index_r][i] != '\0'; i++) {
|
||||
if (cmd_buffer[buf_index_r][i] == code) {
|
||||
strchr_pointer = &cmd_buffer[buf_index_r][i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
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));
|
||||
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) {
|
||||
@@ -140,16 +154,18 @@ void ultrasonic_distance_report(void) {
|
||||
printf("%c", calculate_crc(combined_data, 6));
|
||||
}
|
||||
|
||||
|
||||
void process_printer(void) {
|
||||
if(buffer_length < (RX_BUFFER_SIZE - 1)) {
|
||||
void start_communication(void) {
|
||||
if (buf_length < (BUF_SIZE - 1)) {
|
||||
get_command();
|
||||
}
|
||||
if(buffer_length) {
|
||||
process_command();
|
||||
buffer_length = (buffer_length -1);
|
||||
buffer_index_r = (buffer_index_r + 1) % BUF_SIZE;
|
||||
|
||||
if (buf_length) {
|
||||
printf("DONE!\r\n");
|
||||
prcess_command();
|
||||
buf_length--;
|
||||
buf_index_r = (buf_index_r + 1) % BUF_SIZE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void get_command(void) {
|
||||
@@ -162,18 +178,16 @@ void get_command (void) {
|
||||
|
||||
if (uart_available() > 0)
|
||||
delay_ms(5);
|
||||
while(uart_available() > 0 && buffer_length < BUF_SIZE) {
|
||||
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;
|
||||
packet_state = PS_TYPE;
|
||||
} else
|
||||
{
|
||||
} else {
|
||||
serial_count = 0;
|
||||
}
|
||||
break;
|
||||
@@ -183,53 +197,45 @@ void get_command (void) {
|
||||
packet_state = PS_LEN;
|
||||
break;
|
||||
case PS_LEN:
|
||||
packet_len = serial_char;
|
||||
check_sum += serial_char;
|
||||
packet_len = 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)
|
||||
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)
|
||||
{
|
||||
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;
|
||||
cmd_buffer[buf_index_w][serial_count] = 0;
|
||||
buf_index_w = (buf_index_w + 1) % BUF_SIZE;
|
||||
buf_length++;
|
||||
serial_count = 0;
|
||||
break;
|
||||
default:
|
||||
packet_state = PS_NULL;
|
||||
serial_count = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void process_command(void) {
|
||||
void prcess_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);
|
||||
printf("M1");
|
||||
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();
|
||||
printf("M2");
|
||||
break;
|
||||
default:
|
||||
printf("%c%c%c%c%c%c%c", 0xB5, 0xF0, 0x03, 0x65, 0x72, 0x72, 0x3C);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
20
src/usart.c
20
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,13 +27,9 @@ 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);
|
||||
nvic_irq_enable(USART_PHY_IRQ, 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);
|
||||
}
|
||||
|
||||
@@ -54,12 +48,12 @@ 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 *rx_buffer) {
|
||||
uint16_t i = (unsigned int)(rx_buffer->head + 1) % RX_BUFFER_SIZE;
|
||||
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_buffer->tail) {
|
||||
rx_buffer->buffer[rx_buffer->head] = data;
|
||||
rx_buffer->head = i;
|
||||
if (i != rx_buf->tail) {
|
||||
rx_buf->buffer[rx_buf->head] = data;
|
||||
rx_buf->head = i;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user