3.27_433:实现并验证RF433模块接收相应指令:新增UART路由核心模块,使程序能响应RF433/RS485指令,并向UART2输出LOG(RS485由于硬件原因未验证)
This commit is contained in:
@ -3,13 +3,16 @@
|
||||
* @file cmd_parser.h
|
||||
* @brief ASCII指令解析模块头文件
|
||||
* @author Application Layer
|
||||
* @version 1.0
|
||||
* @version 1.2
|
||||
******************************************************************************
|
||||
* @attention
|
||||
* 本模块实现ASCII文本指令的解析和处理
|
||||
* 指令格式: $CMD,param1,param2*CS\r\n
|
||||
* 支持异或校验,FF为调试特权后门
|
||||
* 包含完善的安全防护机制
|
||||
*
|
||||
* 修订历史:
|
||||
* v1.2 - 增加响应回调机制,支持多端口路由
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
@ -41,67 +44,31 @@ typedef struct {
|
||||
char param2[PARAM_MAX_LEN];
|
||||
uint8_t received_cs;
|
||||
uint8_t calculated_cs;
|
||||
uint8_t source_port;
|
||||
bool valid;
|
||||
bool skip_checksum;
|
||||
} cmd_frame_t;
|
||||
|
||||
/**
|
||||
* @brief 初始化指令解析模块
|
||||
* @note 重置解析状态机,清空缓冲区
|
||||
* @param 无
|
||||
* @retval 无
|
||||
*/
|
||||
typedef void (*cmd_response_callback_t)(uint8_t source_port, const char *response);
|
||||
|
||||
void CmdParser_Init(void);
|
||||
|
||||
/**
|
||||
* @brief 指令解析任务处理函数
|
||||
* @note 在主循环中调用,处理已接收的完整指令帧
|
||||
* @param 无
|
||||
* @retval 无
|
||||
*/
|
||||
void CmdParser_Task(void);
|
||||
|
||||
/**
|
||||
* @brief 喂入单字节数据到解析器
|
||||
* @note 通常在UART接收中断中调用
|
||||
* @param byte: 接收到的字节数据
|
||||
* @param current_tick: 当前系统tick(用于超时检测)
|
||||
* @retval 无
|
||||
*/
|
||||
void CmdParser_FeedByte(uint8_t byte, uint32_t current_tick);
|
||||
|
||||
/**
|
||||
* @brief 检查是否有完整的指令帧
|
||||
* @note 用于查询解析状态
|
||||
* @param frame: 输出参数,存储解析结果
|
||||
* @retval true: 有完整帧, false: 无
|
||||
*/
|
||||
bool CmdParser_HasCompleteFrame(cmd_frame_t *frame);
|
||||
|
||||
/**
|
||||
* @brief 确认指令已处理
|
||||
* @note 处理完指令后调用,重置解析器
|
||||
* @param 无
|
||||
* @retval 无
|
||||
*/
|
||||
void CmdParser_Acknowledge(void);
|
||||
|
||||
/**
|
||||
* @brief 获取解析错误计数
|
||||
* @note 用于调试和诊断
|
||||
* @param 无
|
||||
* @retval 错误计数
|
||||
*/
|
||||
uint32_t CmdParser_GetErrorCount(void);
|
||||
|
||||
/**
|
||||
* @brief 获取接收到的有效指令计数
|
||||
* @note 用于调试和诊断
|
||||
* @param 无
|
||||
* @retval 有效指令计数
|
||||
*/
|
||||
uint32_t CmdParser_GetValidCount(void);
|
||||
|
||||
void CmdParser_SetResponseCallback(cmd_response_callback_t callback);
|
||||
|
||||
void CmdParser_SetSourcePort(uint8_t port_id);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
51
Core/Inc/cmd_router.h
Normal file
51
Core/Inc/cmd_router.h
Normal file
@ -0,0 +1,51 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file cmd_router.h
|
||||
* @brief 指令路由与响应分发模块头文件
|
||||
* @author Application Layer
|
||||
* @version 1.0
|
||||
******************************************************************************
|
||||
* @attention
|
||||
* 本模块实现指令路由与响应分发功能
|
||||
* 设计依据:多通信接口统一指令处理系统开发计划 第3.2节
|
||||
*
|
||||
* 核心职责:
|
||||
* - 从各UART端口读取数据并喂入解析器
|
||||
* - 根据指令来源端口路由响应
|
||||
* - 管理响应路由表
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef __CMD_ROUTER_H
|
||||
#define __CMD_ROUTER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "multi_uart_router.h"
|
||||
#include "cmd_parser.h"
|
||||
|
||||
typedef void (*cmd_response_handler_t)(port_id_t port, const char *response, uint16_t len);
|
||||
|
||||
void CmdRouter_Init(void);
|
||||
|
||||
void CmdRouter_Task(void);
|
||||
|
||||
void CmdRouter_SetResponseHandler(cmd_response_handler_t handler);
|
||||
|
||||
void CmdRouter_SendResponse(port_id_t port, const char *response, uint16_t len);
|
||||
|
||||
void CmdRouter_BroadcastResponse(const char *response, uint16_t len);
|
||||
|
||||
uint32_t CmdRouter_GetProcessedCount(void);
|
||||
|
||||
uint32_t CmdRouter_GetRoutedCount(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
61
Core/Inc/debug_log.h
Normal file
61
Core/Inc/debug_log.h
Normal file
@ -0,0 +1,61 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file debug_log.h
|
||||
* @brief 增强型调试日志系统头文件
|
||||
* @author Application Layer
|
||||
* @version 1.0
|
||||
******************************************************************************
|
||||
* @attention
|
||||
* 本模块实现增强型调试日志系统
|
||||
* 设计依据:多通信接口统一指令处理系统开发计划 第3.4节
|
||||
*
|
||||
* 日志格式:[LEVEL][MODULE] message
|
||||
* 示例:[INFO][CMD] Command received: RL
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef __DEBUG_LOG_H
|
||||
#define __DEBUG_LOG_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef enum {
|
||||
LOG_LEVEL_DEBUG = 0,
|
||||
LOG_LEVEL_INFO = 1,
|
||||
LOG_LEVEL_WARN = 2,
|
||||
LOG_LEVEL_ERROR = 3,
|
||||
LOG_LEVEL_NONE = 4
|
||||
} log_level_t;
|
||||
|
||||
void DebugLog_Init(void);
|
||||
|
||||
void DebugLog_SetLevel(log_level_t level);
|
||||
|
||||
log_level_t DebugLog_GetLevel(void);
|
||||
|
||||
void DebugLog_EnableModule(const char *module, bool enable);
|
||||
|
||||
void DebugLog_Output(log_level_t level, const char *module, const char *fmt, ...);
|
||||
|
||||
#define LOG_DEBUG(module, fmt, ...) \
|
||||
DebugLog_Output(LOG_LEVEL_DEBUG, module, fmt, ##__VA_ARGS__)
|
||||
|
||||
#define LOG_INFO(module, fmt, ...) \
|
||||
DebugLog_Output(LOG_LEVEL_INFO, module, fmt, ##__VA_ARGS__)
|
||||
|
||||
#define LOG_WARN(module, fmt, ...) \
|
||||
DebugLog_Output(LOG_LEVEL_WARN, module, fmt, ##__VA_ARGS__)
|
||||
|
||||
#define LOG_ERROR(module, fmt, ...) \
|
||||
DebugLog_Output(LOG_LEVEL_ERROR, module, fmt, ##__VA_ARGS__)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@ -3,12 +3,15 @@
|
||||
* @file io_monitor.h
|
||||
* @brief IO状态监控模块头文件
|
||||
* @author Application Layer
|
||||
* @version 1.0
|
||||
* @version 1.1
|
||||
******************************************************************************
|
||||
* @attention
|
||||
* 本模块实现四路数字输入(DI1-DI4)的状态监控
|
||||
* 采用定时扫描+软件去抖方式检测IO状态变化
|
||||
* 状态变化时通过UART2_Print上报
|
||||
* 状态变化时通过回调函数上报,支持多端口路由
|
||||
*
|
||||
* 修订历史:
|
||||
* v1.1 - 增加事件回调机制,支持多端口路由
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
@ -26,6 +29,15 @@ extern "C" {
|
||||
#define IO_SCAN_PERIOD_MS 10
|
||||
#define IO_DEBOUNCE_COUNT 3
|
||||
|
||||
/**
|
||||
* @brief IO事件回调函数类型
|
||||
* @note 当IO状态变化时调用,用于事件路由
|
||||
* @param channel: 通道号(0-3对应DI1-DI4)
|
||||
* @param state: 当前状态(0=LOW, 1=HIGH)
|
||||
* @param event_msg: 事件消息字符串
|
||||
*/
|
||||
typedef void (*io_event_callback_t)(uint8_t channel, uint8_t state, const char *event_msg);
|
||||
|
||||
/**
|
||||
* @brief 初始化IO监控模块
|
||||
* @note 初始化各通道状态,读取初始IO电平
|
||||
@ -75,6 +87,14 @@ void IO_Monitor_EnableReport(bool enable);
|
||||
*/
|
||||
uint32_t IO_Monitor_GetChangeCount(uint8_t channel);
|
||||
|
||||
/**
|
||||
* @brief 设置IO事件回调函数
|
||||
* @note 设置后,IO状态变化将通过回调函数上报
|
||||
* @param callback: 回调函数指针,NULL则使用默认UART2输出
|
||||
* @retval 无
|
||||
*/
|
||||
void IO_Monitor_SetEventCallback(io_event_callback_t callback);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
97
Core/Inc/multi_uart_router.h
Normal file
97
Core/Inc/multi_uart_router.h
Normal file
@ -0,0 +1,97 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file multi_uart_router.h
|
||||
* @brief 多UART统一路由核心模块头文件
|
||||
* @author Application Layer
|
||||
* @version 1.0
|
||||
******************************************************************************
|
||||
* @attention
|
||||
* 本模块实现多UART端口的统一管理,包括:
|
||||
* - 接收环形缓冲区
|
||||
* - 发送环形缓冲区
|
||||
* - 端口上下文管理
|
||||
* - 响应路由表
|
||||
*
|
||||
* 设计依据:多通信接口统一指令处理系统开发计划 第3.1、3.3、3.5节及附录A
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
#ifndef __MULTI_UART_ROUTER_H
|
||||
#define __MULTI_UART_ROUTER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "usart.h"
|
||||
|
||||
#define UART_RX_BUFFER_SIZE 128
|
||||
#define UART_TX_BUFFER_SIZE 256
|
||||
|
||||
typedef enum {
|
||||
PORT_UART1 = 0,
|
||||
PORT_UART2 = 1,
|
||||
PORT_UART3 = 2,
|
||||
PORT_COUNT
|
||||
} port_id_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t buffer[UART_RX_BUFFER_SIZE];
|
||||
volatile uint16_t head;
|
||||
volatile uint16_t tail;
|
||||
volatile uint16_t count;
|
||||
volatile uint16_t overflow_count;
|
||||
} uart_rx_ring_t;
|
||||
|
||||
typedef struct {
|
||||
uint8_t buffer[UART_TX_BUFFER_SIZE];
|
||||
volatile uint16_t head;
|
||||
volatile uint16_t tail;
|
||||
volatile uint16_t count;
|
||||
volatile bool is_sending;
|
||||
volatile uint16_t overflow_count;
|
||||
} uart_tx_ring_t;
|
||||
|
||||
typedef struct {
|
||||
UART_HandleTypeDef *huart;
|
||||
const char *name;
|
||||
uart_rx_ring_t rx_ring;
|
||||
uart_tx_ring_t tx_ring;
|
||||
uint8_t rx_tmp;
|
||||
uint32_t rx_count;
|
||||
uint32_t tx_count;
|
||||
uint32_t error_count;
|
||||
bool initialized;
|
||||
} uart_port_context_t;
|
||||
|
||||
void MultiUART_Init(void);
|
||||
|
||||
void MultiUART_FeedByte(port_id_t port_id, uint8_t byte);
|
||||
|
||||
void MultiUART_Task(void);
|
||||
|
||||
void MultiUART_Send(port_id_t port_id, const uint8_t *data, uint16_t len);
|
||||
|
||||
void MultiUART_SendString(port_id_t port_id, const char *str);
|
||||
|
||||
void MultiUART_SendFmt(port_id_t port_id, const char *fmt, ...);
|
||||
|
||||
void MultiUART_TxCpltCallback(port_id_t port_id);
|
||||
|
||||
const char *MultiUART_GetPortName(port_id_t port_id);
|
||||
|
||||
uint16_t MultiUART_GetRxCount(port_id_t port_id);
|
||||
|
||||
uint16_t MultiUART_ReadByte(port_id_t port_id, uint8_t *byte);
|
||||
|
||||
uint16_t MultiUART_GetTxAvailable(port_id_t port_id);
|
||||
|
||||
uint32_t MultiUART_GetOverflowCount(port_id_t port_id);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@ -3,11 +3,14 @@
|
||||
* @file relay_control.h
|
||||
* @brief 继电器控制模块头文件
|
||||
* @author Application Layer
|
||||
* @version 1.0
|
||||
* @version 2.0
|
||||
******************************************************************************
|
||||
* @attention
|
||||
* 本模块提供继电器的安全控制接口
|
||||
* 当前硬件配置:PA15连接继电器控制端
|
||||
* 本模块提供单路继电器的控制接口
|
||||
* 硬件配置:PA15连接继电器控制端
|
||||
*
|
||||
* 修订历史:
|
||||
* v2.0 - 精简为单路继电器控制,移除冗余功能
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
@ -21,7 +24,6 @@ extern "C" {
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define RELAY_COUNT 1
|
||||
#define RELAY_MIN_INTERVAL 100
|
||||
|
||||
/**
|
||||
@ -35,35 +37,18 @@ void Relay_Init(void);
|
||||
/**
|
||||
* @brief 设置继电器状态
|
||||
* @note 带最小间隔保护,防止频繁切换损坏继电器
|
||||
* @param relay_id: 继电器编号(1-4),当前硬件只有1
|
||||
* @param state: true=打开, false=关闭
|
||||
* @retval 无
|
||||
*/
|
||||
void Relay_SetState(uint8_t relay_id, bool state);
|
||||
void Relay_SetState(bool state);
|
||||
|
||||
/**
|
||||
* @brief 获取继电器当前状态
|
||||
* @note 读取GPIO输出状态
|
||||
* @param relay_id: 继电器编号(1-4)
|
||||
* @note 读取软件记录的状态
|
||||
* @param 无
|
||||
* @retval true=打开, false=关闭
|
||||
*/
|
||||
bool Relay_GetState(uint8_t relay_id);
|
||||
|
||||
/**
|
||||
* @brief 翻转继电器状态
|
||||
* @note 带最小间隔保护
|
||||
* @param relay_id: 继电器编号(1-4)
|
||||
* @retval 无
|
||||
*/
|
||||
void Relay_Toggle(uint8_t relay_id);
|
||||
|
||||
/**
|
||||
* @brief 获取继电器切换次数
|
||||
* @note 用于调试和诊断
|
||||
* @param 无
|
||||
* @retval 切换次数
|
||||
*/
|
||||
uint32_t Relay_GetToggleCount(void);
|
||||
bool Relay_GetState(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user