generated from hulk/gd32e23x_template_cmake_vscode
update come code
This commit is contained in:
parent
ddb2dfbd2a
commit
9b3d19cffa
@ -28,6 +28,8 @@
|
|||||||
#define PROTOCOL_BOARD_TYPE 0x03
|
#define PROTOCOL_BOARD_TYPE 0x03
|
||||||
#define PROTOCOL_PACKAGE_LENGTH 0x02
|
#define PROTOCOL_PACKAGE_LENGTH 0x02
|
||||||
|
|
||||||
|
#define PACKAGE_MIN_LENGTH 6
|
||||||
|
|
||||||
/* 可选的应答类型定义(与示例保持一致,可按需扩展) */
|
/* 可选的应答类型定义(与示例保持一致,可按需扩展) */
|
||||||
#define RESP_HEADER 0xB5
|
#define RESP_HEADER 0xB5
|
||||||
#define RESP_TYPE_OK 0xF0
|
#define RESP_TYPE_OK 0xF0
|
||||||
@ -42,7 +44,10 @@
|
|||||||
static uint8_t proto_sum_crc(const uint8_t *data, uint8_t len)
|
static uint8_t proto_sum_crc(const uint8_t *data, uint8_t len)
|
||||||
{
|
{
|
||||||
uint8_t crc = 0;
|
uint8_t crc = 0;
|
||||||
if (len < 3) return 0; // 最少应为 header + X + crc
|
// 仅在满足协议最小帧长时计算(header + type + len + payload + crc)
|
||||||
|
if (len < PACKAGE_MIN_LENGTH) return 0;
|
||||||
|
|
||||||
|
// 累加从索引 1 到 len-2 的字节(不含 header 和 crc 字节)
|
||||||
for (uint8_t i = 1; i < (uint8_t)(len - 1); i++) {
|
for (uint8_t i = 1; i < (uint8_t)(len - 1); i++) {
|
||||||
crc += data[i];
|
crc += data[i];
|
||||||
}
|
}
|
||||||
@ -69,7 +74,8 @@ static void send_response(uint8_t type, const uint8_t *payload, uint8_t len)
|
|||||||
|
|
||||||
void handle_command(const uint8_t *cmd, uint8_t len) {
|
void handle_command(const uint8_t *cmd, uint8_t len) {
|
||||||
// 期望帧:D5 03 02 'M' '1' CRC(或 'M2'/'M3')
|
// 期望帧:D5 03 02 'M' '1' CRC(或 'M2'/'M3')
|
||||||
if (len < 6) return; // 最小长度 3+2+1
|
if (len < PACKAGE_MIN_LENGTH) return; // 最小长度 3+2+1
|
||||||
|
|
||||||
uint8_t payload_len = cmd[2];
|
uint8_t payload_len = cmd[2];
|
||||||
if (payload_len < 2) return; // 我们期望 2 字节命令
|
if (payload_len < 2) return; // 我们期望 2 字节命令
|
||||||
|
|
||||||
@ -102,8 +108,8 @@ void command_process(void) {
|
|||||||
static uint8_t cmd_len = 0;
|
static uint8_t cmd_len = 0;
|
||||||
static uint8_t expected_total = 0; // 0 表示尚未确定总长度
|
static uint8_t expected_total = 0; // 0 表示尚未确定总长度
|
||||||
|
|
||||||
while (uart_rx_available() > 0) {
|
while (uart_ring_buffer_available() > 0) {
|
||||||
int byte = uart_rx_get();
|
int byte = uart_ring_buffer_get();
|
||||||
if (byte < 0) break;
|
if (byte < 0) break;
|
||||||
|
|
||||||
if (cmd_len == 0) {
|
if (cmd_len == 0) {
|
||||||
|
@ -101,14 +101,6 @@ void SysTick_Handler(void) {
|
|||||||
void USART0_IRQHandler(void) {
|
void USART0_IRQHandler(void) {
|
||||||
if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
|
if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) {
|
||||||
uint8_t data = usart_data_receive(USART0);
|
uint8_t data = usart_data_receive(USART0);
|
||||||
uart_rx_put(data);
|
(void)uart_ring_buffer_put(data); // 缓冲满时丢弃,返回值可用于统计
|
||||||
}
|
}
|
||||||
/* 处理 USART0 中断
|
|
||||||
** USART_INT_FLAG_RBNE 读缓冲区非空中断标志
|
|
||||||
** 即:当 USART0 的接收缓冲区有数据时,触发该中断
|
|
||||||
** 该中断为只读中断,不需要手动清除
|
|
||||||
**
|
|
||||||
**
|
|
||||||
**
|
|
||||||
*/
|
|
||||||
}
|
}
|
@ -36,6 +36,7 @@ OF SUCH DAMAGE.
|
|||||||
#include "systick.h"
|
#include "systick.h"
|
||||||
#include "uart.h"
|
#include "uart.h"
|
||||||
#include "led.h"
|
#include "led.h"
|
||||||
|
#include "command.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
Loading…
x
Reference in New Issue
Block a user