Compare commits

..

2 Commits

11 changed files with 103 additions and 214 deletions

View File

@ -73,7 +73,7 @@ extern "C" {
🚀 核心身份标识:烧录不同设备时,请务必修改这个数字!
比如设备A烧录时改为 0x01设备B烧录时改为 0x02
========================================================= */
#define MY_DEVICE_ID 0x01
#define MY_DEVICE_ID 0x02
/* USER CODE END EM */
/* Exported functions prototypes ---------------------------------------------*/

View File

@ -64,6 +64,8 @@ typedef struct {
uint32_t tx_count;
uint32_t error_count;
bool initialized;
volatile bool csma_backoff_active;
volatile uint32_t csma_backoff_until;
} uart_port_context_t;
void MultiUART_Init(void);

View File

@ -112,146 +112,6 @@ void DebugLog_EnableModule(const char *module, bool enable)
}
}
void DebugLog_Output(log_level_t level, const char *module, const char *fmt, ...)
{
if (level < g_current_level) {
return;
}
if (!is_module_enabled(module)) {
return;
}
char buffer[256];
int len = 0;
len += snprintf(buffer + len, sizeof(buffer) - len, "[%s][%s] ",
g_level_str[level], module ? module : "MAIN");
if (len < (int)sizeof(buffer) - 1) {
va_list args;
va_start(args, fmt);
len += vsnprintf(buffer + len, sizeof(buffer) - len, fmt, args);
va_end(args);
}
if (len > 0 && len < (int)sizeof(buffer)) {
buffer[len++] = '\r';
if (len < (int)sizeof(buffer)) {
/**
******************************************************************************
* @file debug_log.c
* @brief 增强型调试日志系统实现
* @author Application Layer
* @version 1.0
******************************************************************************
* @attention
* 本模块实现增强型调试日志系统
* 设计依据:多通信接口统一指令处理系统开发计划 第3.4节
*
* 日志格式:[LEVEL][MODULE] message
* 示例:[INFO][CMD] Command received: RL
******************************************************************************
*/
#include "debug_log.h"
#include "uart2_print.h"
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include "multi_uart_router.h"
#define MAX_MODULES 16
#define MODULE_NAME_LEN 16
typedef struct {
char name[MODULE_NAME_LEN];
bool enabled;
} module_config_t;
static log_level_t g_current_level = LOG_LEVEL_DEBUG;
static module_config_t g_modules[MAX_MODULES];
static bool g_modules_initialized = false;
static const char *const g_level_str[] = {
[LOG_LEVEL_DEBUG] = "DEBUG",
[LOG_LEVEL_INFO] = "INFO ",
[LOG_LEVEL_WARN] = "WARN ",
[LOG_LEVEL_ERROR] = "ERROR",
};
static void init_modules(void)
{
if (g_modules_initialized) {
return;
}
for (int i = 0; i < MAX_MODULES; i++) {
g_modules[i].name[0] = '\0';
g_modules[i].enabled = false;
}
g_modules_initialized = true;
}
static bool is_module_enabled(const char *module)
{
if (module == NULL || module[0] == '\0') {
return true;
}
init_modules();
for (int i = 0; i < MAX_MODULES; i++) {
if (g_modules[i].name[0] != '\0' &&
strncmp(g_modules[i].name, module, MODULE_NAME_LEN - 1) == 0) {
return g_modules[i].enabled;
}
}
return true;
}
void DebugLog_Init(void)
{
g_current_level = LOG_LEVEL_DEBUG;
init_modules();
UART2_Print_String("[LOG] Debug log system initialized\r\n");
}
void DebugLog_SetLevel(log_level_t level)
{
g_current_level = level;
}
log_level_t DebugLog_GetLevel(void)
{
return g_current_level;
}
void DebugLog_EnableModule(const char *module, bool enable)
{
if (module == NULL || module[0] == '\0') {
return;
}
init_modules();
for (int i = 0; i < MAX_MODULES; i++) {
if (g_modules[i].name[0] == '\0') {
strncpy(g_modules[i].name, module, MODULE_NAME_LEN - 1);
g_modules[i].name[MODULE_NAME_LEN - 1] = '\0';
g_modules[i].enabled = enable;
return;
}
if (strncmp(g_modules[i].name, module, MODULE_NAME_LEN - 1) == 0) {
g_modules[i].enabled = enable;
return;
}
}
}
void DebugLog_Output(log_level_t level, const char *module, const char *fmt, ...)
{
if (level < g_current_level) {

View File

@ -127,7 +127,7 @@ void RF433_SendPacket(uint8_t type, const uint8_t *payload, uint8_t len)
frame[frame_idx++] = PROTO_START_BYTE; // AA
frame[frame_idx++] = type; // TYPE
frame[frame_idx++] = (uint8_t)(len + 1); // LEN (ID + Payload)
frame[frame_idx++] = (uint8_t)(len + 1 + 1); // LEN (ID + Payload + SUM)
frame[frame_idx++] = MY_DEVICE_ID; // ID
if (len > 0 && payload != NULL) {
@ -396,6 +396,7 @@ int main(void)
/* USER CODE END WHILE */
}
}
/* USER CODE END 3 */
/**
* @brief System Clock Configuration

View File

@ -42,6 +42,16 @@
#define DEBUG_LOG(fmt, ...)
#endif
/*==============================================================================
* CSMA/CA 随机退避相关
*============================================================================*/
static uint32_t csma_rand(uint32_t seed)
{
// static uint32_t seed = 12345;
seed = seed * 1103515245 + 12345;
return (seed >> 16) & 0x7FFF;
}
/*==============================================================================
* 全局变量定义
*============================================================================*/
@ -235,16 +245,23 @@ static void tx_kickoff(port_id_t port_id)
__disable_irq();
if (!ring->is_sending && ring->count > 0) {
/* --- 核心修复:针对 RF433 (UART1) 执行包级 LBT 检查 --- */
if (port_id == PORT_433) {
// 如果 AUX 为低电平,说明信道忙(接收中或正在发送前一包),暂缓起步
if (ctx->csma_backoff_active) {
if ((int32_t)(HAL_GetTick() - ctx->csma_backoff_until) < 0) {
__enable_irq();
return;
}
ctx->csma_backoff_active = false;
}
if (HAL_GPIO_ReadPin(AUX_GPIO_Port, AUX_Pin) == GPIO_PIN_RESET) {
ctx->csma_backoff_active = true;
ctx->csma_backoff_until = HAL_GetTick() + (csma_rand(HAL_GetTick()) % 1000) + 1;
__enable_irq();
return;
}
}
/* 取出下一个待发送字节 */
byte = ring->buffer[ring->tail];
ring->tail = (ring->tail + 1) % UART_TX_BUFFER_SIZE;
ring->count--;
@ -297,6 +314,10 @@ void MultiUART_Init(void)
ctx->tx_count = 0;
ctx->error_count = 0;
ctx->initialized = true;
/* 初始化 CSMA/CA 随机退避状态 */
ctx->csma_backoff_active = false;
ctx->csma_backoff_until = 0;
}
DEBUG_LOG("Init OK, %d ports configured", PORT_COUNT);
@ -681,7 +702,7 @@ bool MultiUART_IsBusy(uint8_t port_id)
if (port_id >= PORT_COUNT) return false;
// 调试串口特殊处理
if (port_id == PORT_UART2) {
if (port_id == PORT_DEBUG) {
// UART2_Print 使用单独的标志位,这里简单兼容
return false;
}

View File

@ -462,7 +462,7 @@ int fputc(int ch, FILE *f)
{
(void)f;
UART2_Print_Send((uint8_t *)&ch, 1);
MultiUART_Send(PORT_UART3, (uint8_t *)&ch, 1); /* 增加:同时发给 UART3 */
MultiUART_Send(PORT_RS485, (uint8_t *)&ch, 1); /* 增加:同时发给 UART3 */
return ch;
}
#endif
@ -471,7 +471,7 @@ int fputc(int ch, FILE *f)
int __io_putchar(int ch)
{
UART2_Print_Send((uint8_t *)&ch, 1);
MultiUART_Send(PORT_UART3, (uint8_t *)&ch, 1); /* 增加:同时发给 UART3 */
MultiUART_Send(PORT_RS485, (uint8_t *)&ch, 1); /* 增加:同时发给 UART3 */
return ch;
}
@ -479,7 +479,7 @@ int _write(int file, char *ptr, int len)
{
(void)file;
UART2_Print_Send((uint8_t *)ptr, len);
MultiUART_Send(PORT_UART3, (uint8_t *)ptr, len); /* 增加:同时发给 UART3 */
MultiUART_Send(PORT_RS485, (uint8_t *)ptr, len); /* 增加:同时发给 UART3 */
return len;
}
#endif

View File

@ -208,7 +208,7 @@ void Passthrough_Task(void)
}
uint8_t byte = node->data[node->offset++];
MultiUART_Send(PORT_UART1, &byte, 1);
MultiUART_Send(PORT_433, &byte, 1);
ctx->queue.pending_count--;
ctx->stats.total_bytes_sent++;
@ -232,7 +232,7 @@ void Passthrough_OnTxComplete(void)
*/
bool Passthrough_CanSend(void)
{
return (MultiUART_GetTxAvailable(PORT_UART1) > 0) &&
return (MultiUART_GetTxAvailable(PORT_433) > 0) &&
(g_passthrough_ctx.queue.pending_count > 0);
}

View File

@ -2,6 +2,7 @@
#include "wiz_platform.h"
#include "wizchip_conf.h"
#include "dhcp.h"
#include "main.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -93,15 +94,18 @@ void wiz_delete_timer(void (*func)(void))
}
/**
* @brief wiz timer event handler
* @brief wiz timer event handler (using HAL_GetTick)
*
* You must add this function to your 1ms timer interrupt
*
*/
void wiz_timer_handler(void)
{
static uint32_t last_tick = 0;
uint32_t current_tick = HAL_GetTick();
wiz_delay_ms_count++;
if (current_tick != last_tick) {
last_tick = current_tick;
struct wiz_timer *temp = wiz_timer_head;
while (temp != NULL)
{
@ -114,36 +118,36 @@ void wiz_timer_handler(void)
temp = temp->next;
}
}
}
/**
* @brief Delay function in milliseconds
* @brief Delay function in milliseconds (using HAL_GetTick)
* @param nms :Delay Time
*/
void wiz_user_delay_ms(uint32_t nms)
{
wiz_delay_ms_count = 0;
while (wiz_delay_ms_count < nms)
{
uint32_t start = HAL_GetTick();
while ((HAL_GetTick() - start) < nms) {
}
}
/**
* @brief Check the WIZCHIP version
* @brief Check the WIZCHIP version (with timeout)
*/
void wizchip_version_check(void)
{
uint8_t error_count = 0;
while (1)
uint32_t start_tick = HAL_GetTick();
while ((HAL_GetTick() - start_tick) < 5000)
{
wiz_user_delay_ms(1000);
wiz_user_delay_ms(100);
if (getVERSIONR() != W5500_VERSION)
{
error_count++;
if (error_count > 5)
{
printf("error, W5500 version is 0x%02x, but read W5500 version value = 0x%02x\r\n", W5500_VERSION, getVERSIONR());
while (1)
;
printf("WARN: W5500 version check failed, SPI may be disconnected\r\n");
break;
}
}
else
@ -165,25 +169,26 @@ void wiz_print_phy_info(void)
}
/**
* @brief Ethernet Link Detection
* @brief Ethernet Link Detection (with timeout)
*/
void wiz_phy_link_check(void)
{
uint8_t phy_link_status;
do
uint32_t start_tick = HAL_GetTick();
while ((HAL_GetTick() - start_tick) < 10000)
{
wiz_user_delay_ms(1000);
wiz_user_delay_ms(500);
ctlwizchip(CW_GET_PHYLINK, (void *)&phy_link_status);
if (phy_link_status == PHY_LINK_ON)
{
printf("PHY link\r\n");
wiz_print_phy_info();
return;
}
else
{
printf("PHY no link\r\n");
}
} while (phy_link_status == PHY_LINK_OFF);
printf("WARN: PHY link timeout, using static config\r\n");
}
/**

View File

@ -138,7 +138,7 @@
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART3) {
MultiUART_FeedByte(PORT_UART3, uart3_rx_byte);
MultiUART_FeedByte(PORT_RS485, uart3_rx_byte);
HAL_UART_Receive_IT(&huart3, &uart3_rx_byte, 1);
}
}
@ -149,7 +149,7 @@ void CmdRouter_Task(void)
// ... UART1/UART3处理 ...
for (port_id_t port_id = 0; port_id < PORT_COUNT; port_id++) {
if (port_id == PORT_UART2) continue; // UART2单独处理
if (port_id == PORT_DEBUG) continue; // UART2单独处理
uint8_t byte;
while (MultiUART_ReadByte(port_id, &byte) > 0) {
@ -380,7 +380,7 @@ void Passthrough_Task(void)
if (node->offset < node->length) {
// 发送一个字节
uint8_t byte = node->data[node->offset++];
MultiUART_Send(PORT_UART1, &byte, 1);
MultiUART_Send(PORT_433, &byte, 1);
}
// 检查节点是否发送完成
@ -659,7 +659,7 @@ void CmdRouter_Task_UART3_Enhanced(void)
// 1. 读取UART3接收缓冲区
uint8_t byte;
while (MultiUART_ReadByte(PORT_UART3, &byte) > 0) {
while (MultiUART_ReadByte(PORT_RS485, &byte) > 0) {
// 2. 协议识别
route_result_t route = UART3_Protocol_FeedByte(byte, current_tick);
@ -844,7 +844,7 @@ void Passthrough_Task(void)
// 发送一个字节
uint8_t byte = node->data[node->offset++];
MultiUART_Send(PORT_UART1, &byte, 1);
MultiUART_Send(PORT_433, &byte, 1);
ctx->queue.pending_count--;
ctx->stats.total_bytes_sent++;
@ -902,7 +902,7 @@ uint16_t Passthrough_PushBuffer(const uint8_t *data, uint16_t length)
bool Passthrough_CanSend(void)
{
// 检查UART1 TX是否忙
return (MultiUART_GetTxAvailable(PORT_UART1) > 0) &&
return (MultiUART_GetTxAvailable(PORT_433) > 0) &&
(g_passthrough_ctx.queue.pending_count > 0);
}
```
@ -934,8 +934,8 @@ void CmdRouter_Task(void)
#else
// 原有逻辑所有数据喂给CmdParser
uint8_t byte;
while (MultiUART_ReadByte(PORT_UART3, &byte) > 0) {
CmdParser_SetSourcePort(PORT_UART3);
while (MultiUART_ReadByte(PORT_RS485, &byte) > 0) {
CmdParser_SetSourcePort(PORT_RS485);
CmdParser_FeedByte(byte, current_tick);
}
#endif
@ -955,14 +955,14 @@ static void UART3_SmartRouter_Task(uint32_t current_tick)
uint8_t byte;
// 读取所有待处理的字节
while (MultiUART_ReadByte(PORT_UART3, &byte) > 0) {
while (MultiUART_ReadByte(PORT_RS485, &byte) > 0) {
// 协议识别
route_result_t route = UART3_Protocol_FeedByte(byte, current_tick);
switch (route) {
case ROUTE_CMD:
// 指令路径
CmdParser_SetSourcePort(PORT_UART3);
CmdParser_SetSourcePort(PORT_RS485);
CmdParser_FeedByte(byte, current_tick);
LOG_DEBUG("[UART3] CMD byte: 0x%02X", byte);
break;
@ -997,7 +997,7 @@ static void UART3_SmartRouter_Task(uint32_t current_tick)
#### 3.3.2 透传引擎发送接口
```c
// 复用现有的 MultiUART_Send(PORT_UART1, data, len)
// 复用现有的 MultiUART_Send(PORT_433, data, len)
// 透传引擎将数据写入UART1的发送缓冲区
// 由MultiUART_TxCpltCallback驱动后续发送
```

View File

@ -285,7 +285,7 @@ void CmdRouter_Init(void);
/**
* @brief 向指定端口的解析器喂入数据
* @note 由UART中断回调调用线程安全
* @param port_id: 端口ID (PORT_UART1/PORT_UART2/PORT_UART3)
* @param port_id: 端口ID (PORT_433/PORT_DEBUG/PORT_RS485)
* @param byte: 接收到的字节
* @param current_tick: 系统时间戳
* @retval 无
@ -325,9 +325,9 @@ void CmdRouter_SendResponseFmt(port_id_t port_id, const char *fmt, ...);
```c
/** 端口ID枚举 */
typedef enum {
PORT_UART1 = 0, /**< RF433模块 */
PORT_UART2 = 1, /**< 调试串口 */
PORT_UART3 = 2, /**< RS485模块 */
PORT_433 = 0, /**< RF433模块 */
PORT_DEBUG = 1, /**< 调试串口 */
PORT_RS485 = 2, /**< RS485模块 */
PORT_COUNT
} port_id_t;
@ -396,18 +396,18 @@ void CmdParser_SetResponseCallback(response_callback_t callback);
* @note 静态表根据port_id索引查找对应UART句柄
*/
static UART_HandleTypeDef* const g_port_uart_map[PORT_COUNT] = {
[PORT_UART1] = &huart1, // RF433
[PORT_UART2] = &huart2, // DEBUG
[PORT_UART3] = &huart3, // RS485
[PORT_433] = &huart1, // RF433
[PORT_DEBUG] = &huart2, // DEBUG
[PORT_RS485] = &huart3, // RS485
};
/**
* @brief 端口名称表(用于日志输出)
*/
static const char* const g_port_name_map[PORT_COUNT] = {
[PORT_UART1] = "UART1",
[PORT_UART2] = "UART2",
[PORT_UART3] = "UART3",
[PORT_433] = "UART1",
[PORT_DEBUG] = "UART2",
[PORT_RS485] = "UART3",
};
```
@ -650,9 +650,9 @@ void Configure_UART_Priorities(void)
#include "cmd_parser.h"
typedef enum {
PORT_UART1 = 0,
PORT_UART2 = 1,
PORT_UART3 = 2,
PORT_433 = 0,
PORT_DEBUG = 1,
PORT_RS485 = 2,
PORT_COUNT
} port_id_t;
@ -683,7 +683,7 @@ void MultiUART_SendString(port_id_t port_id, const char *str);
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1) {
MultiUART_FeedByte(PORT_UART1, rf433_uart_rx_tmp, HAL_GetTick());
MultiUART_FeedByte(PORT_433, rf433_uart_rx_tmp, HAL_GetTick());
HAL_UART_Receive_IT(&huart1, &rf433_uart_rx_tmp, 1);
}
// ... 其他端口保持原样 ...

View File

@ -13,7 +13,7 @@ AA TYPE LEN ID [PAYLOAD] SUM
| | | | | +-- 校验和:从 AA 到 PAYLOAD 结束的所有字节累加和 (取低8位)
| | | | +---------- 载荷数据:具体的业务数据内容
| | | +------------------ 设备 ID当前发送设备的唯一标识 (MY_DEVICE_ID)
| | +---------------------- 长度:指明后续 [ID + PAYLOAD] 的总字节数
| | +---------------------- 长度:指明后续 [ID + PAYLOAD + SUM] 的总字节数
| +--------------------------- 数据类型:区分数据来源 (10, 55, 48, AA)
+-------------------------------- 起始符:固定为 0xAA
```
@ -26,13 +26,13 @@ AA TYPE LEN ID [PAYLOAD] SUM
当板载 4 路数字输入 (DI) 电平发生变化时,立即发送此包。
```text
AA 10 02 ID XX SUM
AA 10 03 ID XX SUM
-- -- -- -- -- ---
| | | | | |
| | | | | +-- 校验和
| | | | +------ I/O 状态位 (Bit0:DI1, Bit1:DI2, Bit2:DI3, Bit3:DI4)
| | | +---------- 本机设备 ID
| | +-------------- 长度固定为 0x02 (ID + 1字节状态)
| | +-------------- 长度固定为 0x03 (ID + 1字节状态 + SUM)
| +------------------ 类型标识0x10 (I/O Data)
+---------------------- 固定起始符
```
@ -47,7 +47,7 @@ AA 48 LEN ID [DATA] SUM
| | | | | +-- 校验和
| | | | +--------- RS485 原始数据内容
| | | +--------------- 本机设备 ID
| | +------------------- 长度:(1 + 原始数据长度)
| | +------------------- 长度:(1 + 原始数据长度 + SUM)
| +----------------------- 类型标识0x48 (RS485 Data)
+--------------------------- 固定起始符
```
@ -62,7 +62,7 @@ AA 55 LEN ID [DATA] SUM
| | | | | +-- 校验和
| | | | +--------- 网络原始数据内容
| | | +--------------- 本机设备 ID
| | +------------------- 长度:(1 + 原始数据长度)
| | +------------------- 长度:(1 + 原始数据长度 + SUM)
| +----------------------- 类型标识0x55 (Net Data)
+--------------------------- 固定起始符
```
@ -71,14 +71,14 @@ AA 55 LEN ID [DATA] SUM
系统定时上报当前存活状态,包含当前的 I/O 状态及防丢包序列号。
```text
AA AA 04 ID [IO] [SEQ_H] [SEQ_L] SUM
AA AA 05 ID [IO] [SEQ_H] [SEQ_L] SUM
-- -- -- -- ---- ------- ------- ---
| | | | | | | |
| | | | | | | +-- 校验和
| | | | | +-------+-------- 2字节序列号 (0-65535, 循环自增)
| | | | +----------------------- 当前 4 路 I/O 状态位
| | | +---------------------------- 本机设备 ID
| | +-------------------------------- 长度固定为 0x04 (ID + 3字节Payload)
| | +-------------------------------- 长度固定为 0x05 (ID + 3字节Payload + SUM)
| +------------------------------------ 类型标识0xAA (Heartbeat)
+---------------------------------------- 固定起始符
```