generated from hulk/gd32e23x_template
	Compare commits
	
		
			3 Commits
		
	
	
		
			7ffb8c0612
			...
			a546f7bf83
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| a546f7bf83 | |||
| 39d800cb1b | |||
| a5c1c857a9 | 
@@ -91,15 +91,15 @@ set(TARGET_CFLAGS_HARDWARE "-mcpu=cortex-m23 -mfloat-abi=soft -mthumb -mthumb-in
 | 
			
		||||
 | 
			
		||||
# Conditional flags
 | 
			
		||||
# DEBUG
 | 
			
		||||
#set(CMAKE_C_FLAGS_DEBUG     "-DDEBUG=0 -O0 -g")
 | 
			
		||||
#set(CMAKE_CXX_FLAGS_DEBUG   "-DDEBUG=0 -O0 -g")
 | 
			
		||||
#set(CMAKE_ASM_FLAGS_DEBUG   "-DDEBUG=0 -O0 -g")
 | 
			
		||||
set(CMAKE_C_FLAGS_DEBUG     "-DDEBUG=0 -O0 -g")
 | 
			
		||||
set(CMAKE_CXX_FLAGS_DEBUG   "-DDEBUG=0 -O0 -g")
 | 
			
		||||
set(CMAKE_ASM_FLAGS_DEBUG   "-DDEBUG=0 -O0 -g")
 | 
			
		||||
#set(CMAKE_C_FLAGS_DEBUG     "-DDEBUG=0 -O2 -g")
 | 
			
		||||
#set(CMAKE_CXX_FLAGS_DEBUG   "-DDEBUG=0 -O2 -g")
 | 
			
		||||
#set(CMAKE_ASM_FLAGS_DEBUG   "-DDEBUG=0 -O2 -g")
 | 
			
		||||
set(CMAKE_C_FLAGS_DEBUG     "-DDEBUG=0 -Os -g")
 | 
			
		||||
set(CMAKE_CXX_FLAGS_DEBUG   "-DDEBUG=0 -Os -g")
 | 
			
		||||
set(CMAKE_ASM_FLAGS_DEBUG   "-DDEBUG=0 -Os -g")
 | 
			
		||||
#set(CMAKE_C_FLAGS_DEBUG     "-DDEBUG=0 -Os -g")
 | 
			
		||||
#set(CMAKE_CXX_FLAGS_DEBUG   "-DDEBUG=0 -Os -g")
 | 
			
		||||
#set(CMAKE_ASM_FLAGS_DEBUG   "-DDEBUG=0 -Os -g")
 | 
			
		||||
 | 
			
		||||
# RELEASE
 | 
			
		||||
set(CMAKE_C_FLAGS_RELEASE        "-DNDEBUG -O3") #  -flto
 | 
			
		||||
 
 | 
			
		||||
@@ -41,12 +41,12 @@
 | 
			
		||||
/******************************************************************************/
 | 
			
		||||
 | 
			
		||||
#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 RS485_EN_PORT        GPIOA
 | 
			
		||||
#define RS485_EN_PIN         GPIO_PIN_4
 | 
			
		||||
 
 | 
			
		||||
@@ -13,17 +13,21 @@
 | 
			
		||||
#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 PACKET_START_BYTE         0xD5
 | 
			
		||||
 | 
			
		||||
#define MAX_SERIAL_CMD_SIZE       16
 | 
			
		||||
#define BUF_SIZE      8
 | 
			
		||||
 | 
			
		||||
/******************************************************************************/
 | 
			
		||||
 | 
			
		||||
typedef enum
 | 
			
		||||
@@ -35,9 +39,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 +69,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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,7 @@ void watchdog_init(void) {
 | 
			
		||||
    rcu_osci_stab_wait(RCU_IRC40K);
 | 
			
		||||
 | 
			
		||||
    /* Configure FWDGT counter clock: 40KHz(IRC40K) / 64 = 0.625 KHz */
 | 
			
		||||
    fwdgt_config(625, FWDGT_PSC_DIV64); // Set timeout to 1 seconds (625 / 0.625 KHz)
 | 
			
		||||
    fwdgt_config(6250, FWDGT_PSC_DIV64); // Set timeout to 1 seconds (625 / 0.625 KHz)
 | 
			
		||||
 | 
			
		||||
    /* Enable FWDGT */
 | 
			
		||||
    fwdgt_enable();
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
@@ -117,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;
 | 
			
		||||
    }
 | 
			
		||||
@@ -157,7 +158,7 @@ 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];
 | 
			
		||||
 | 
			
		||||
@@ -165,18 +166,19 @@ void USART0_IRQHandler(void) {
 | 
			
		||||
        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;
 | 
			
		||||
    // }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										28
									
								
								src/main.c
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								src/main.c
									
									
									
									
									
								
							@@ -25,34 +25,36 @@ 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");
 | 
			
		||||
 | 
			
		||||
    ultrasonic_config();
 | 
			
		||||
    // ultrasonic_config();
 | 
			
		||||
 | 
			
		||||
    while(1){
 | 
			
		||||
        gd60914_get_object_tempture();
 | 
			
		||||
        // gd60914_get_object_tempture();
 | 
			
		||||
 | 
			
		||||
        delay_ms(50);
 | 
			
		||||
        delay_ms(500);
 | 
			
		||||
        start_communication();
 | 
			
		||||
        printf("hello world!\r\n");
 | 
			
		||||
 | 
			
		||||
        ultrasonic_pwm_out_cycles(ULTRASONIC_TX_CYCLES);
 | 
			
		||||
        // 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;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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[BUF_SIZE][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,88 @@ 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) {
 | 
			
		||||
    if (buf_length < (BUF_SIZE - 1)) {
 | 
			
		||||
        get_command();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (buf_length) {
 | 
			
		||||
        prcess_command();
 | 
			
		||||
        buf_length--;
 | 
			
		||||
        buf_index_r = (buf_index_r + 1) % BUF_SIZE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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 < 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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										34
									
								
								src/usart.c
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								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);
 | 
			
		||||
 | 
			
		||||
    nvic_irq_enable(USART0_IRQn, 0);
 | 
			
		||||
    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(USART1_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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user