From 1669cd216d81ceef21c4dc0e80962b4d0393691f Mon Sep 17 00:00:00 2001 From: wandering1 <1624155937@qq.com> Date: Sun, 19 Jul 2026 01:18:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=B0=83=E8=AF=95=E6=89=93?= =?UTF-8?q?=E5=8D=B0=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Drivers/BSP/CH395F/ch395f.c | 44 ++++++++------- Drivers/BSP/GD5F2GQ5UE/gd5f2gq5ue.c | 17 ++++++ Drivers/BSP/NET/net_socket.c | 37 +++++++------ Drivers/BSP/TPAFE5160/tpafe5160.c | 15 +++++- Drivers/BSP/dbg_cfg.h | 35 ++++++++++++ Drivers/BSP/dbg_log.h | 84 +++++++++++++++++++++++++++++ MDK-ARM/STM32F407-Demo.uvprojx | 2 +- MDK-ARM/build_log.txt | 49 +---------------- 8 files changed, 196 insertions(+), 87 deletions(-) create mode 100644 Drivers/BSP/dbg_cfg.h create mode 100644 Drivers/BSP/dbg_log.h diff --git a/Drivers/BSP/CH395F/ch395f.c b/Drivers/BSP/CH395F/ch395f.c index 82f70a6..755d5ff 100644 --- a/Drivers/BSP/CH395F/ch395f.c +++ b/Drivers/BSP/CH395F/ch395f.c @@ -17,6 +17,12 @@ #include #include +/* + * 调试输出配置 + */ +#define DBG_TAG "[CH395]" +#include "dbg_log.h" + /* * 私有宏定义区 */ @@ -85,11 +91,11 @@ static void ch395f_spi_dma_transfer(uint16_t len) status = HAL_SPI_TransmitReceive_DMA(&hspi2, s_spi2_dma_tx_buf, s_spi2_dma_rx_buf, len); if (status != HAL_OK) { - printf("[DMA] HAL_SPI_TransmitReceive_DMA FAIL: %d, SPI_State=%d, SPI_Error=0x%lX, DMA_State=%d\r\n", - (int)status, - (int)hspi2.State, - hspi2.ErrorCode, - (int)hspi2.hdmarx->State); + DBG_ERROR("HAL_SPI_TransmitReceive_DMA FAIL: %d, SPI_State=%d, SPI_Error=0x%lX, DMA_State=%d", + (int)status, + (int)hspi2.State, + hspi2.ErrorCode, + (int)hspi2.hdmarx->State); return; } @@ -99,20 +105,20 @@ static void ch395f_spi_dma_transfer(uint16_t len) loop_cnt++; if ((HAL_GetTick() - tickstart) > 1000U) { - printf("[DMA] TIMEOUT after 1s, len=%d, loop=%lu, SPI_State=%d, SPI_Error=0x%lX\r\n", - (int)len, loop_cnt, - (int)hspi2.State, - hspi2.ErrorCode); - printf("[DMA] DMA_RX: State=%d, Dir=%lu, CNT=%lu\r\n", - (int)hspi2.hdmarx->State, - hspi2.hdmarx->Init.Direction, - __HAL_DMA_GET_COUNTER(hspi2.hdmarx)); - printf("[DMA] DMA_TX: State=%d, Dir=%lu, CNT=%lu\r\n", - (int)hspi2.hdmatx->State, - hspi2.hdmatx->Init.Direction, - __HAL_DMA_GET_COUNTER(hspi2.hdmatx)); - printf("[DMA] SPI2_CR1=0x%lX, CR2=0x%lX, SR=0x%lX\r\n", - SPI2->CR1, SPI2->CR2, SPI2->SR); + DBG_ERROR("TIMEOUT after 1s, len=%d, loop=%lu, SPI_State=%d, SPI_Error=0x%lX", + (int)len, loop_cnt, + (int)hspi2.State, + hspi2.ErrorCode); + DBG_ERROR(" DMA_RX: State=%d, Dir=%lu, CNT=%lu", + (int)hspi2.hdmarx->State, + hspi2.hdmarx->Init.Direction, + __HAL_DMA_GET_COUNTER(hspi2.hdmarx)); + DBG_ERROR(" DMA_TX: State=%d, Dir=%lu, CNT=%lu", + (int)hspi2.hdmatx->State, + hspi2.hdmatx->Init.Direction, + __HAL_DMA_GET_COUNTER(hspi2.hdmatx)); + DBG_ERROR(" SPI2_CR1=0x%lX, CR2=0x%lX, SR=0x%lX", + SPI2->CR1, SPI2->CR2, SPI2->SR); /* 强制复位 SPI2 DMA 状态,避免死锁 */ HAL_SPI_Abort(&hspi2); return; diff --git a/Drivers/BSP/GD5F2GQ5UE/gd5f2gq5ue.c b/Drivers/BSP/GD5F2GQ5UE/gd5f2gq5ue.c index 44f6985..37c69be 100644 --- a/Drivers/BSP/GD5F2GQ5UE/gd5f2gq5ue.c +++ b/Drivers/BSP/GD5F2GQ5UE/gd5f2gq5ue.c @@ -13,6 +13,12 @@ #include "gd5f2gq5ue.h" #include +/* + * 调试输出配置 + */ +#define DBG_TAG "[NAND]" +#include "dbg_log.h" + /* 私有宏定义区 */ #define GD5F_SPI_TIMEOUT 100 @@ -295,28 +301,39 @@ int gd5f2gq5ue_init(void) HAL_Delay(10); + DBG_INFO("Resetting NAND..."); ret = gd5f2gq5ue_reset(); if (ret != GD5F_OK) { + DBG_ERROR("Reset failed: %d", ret); return ret; } HAL_Delay(5); + DBG_INFO("Reading NAND ID..."); ret = gd5f2gq5ue_read_id(&mid, &did); if (ret != GD5F_OK) { + DBG_ERROR("Read ID failed: %d", ret); return ret; } + DBG_INFO("NAND ID: MID=0x%02X, DID=0x%02X", mid, did); + if (mid != GD5F_MANUFACTURER_ID || did != GD5F_DEVICE_ID) { + DBG_ERROR("ID mismatch: expected MID=0x%02X DID=0x%02X, got MID=0x%02X DID=0x%02X", + GD5F_MANUFACTURER_ID, GD5F_DEVICE_ID, mid, did); return GD5F_ID_MISMATCH; } + DBG_INFO("Enabling ECC..."); gd5f_set_feature(0xB0, 0x10); gd5f_wait_busy(100); + DBG_INFO("Unlocking block protection..."); gd5f_set_feature(GD5F_REG_PROTECT, 0x00); gd5f_wait_busy(100); + DBG_INFO("NAND init OK"); return GD5F_OK; } diff --git a/Drivers/BSP/NET/net_socket.c b/Drivers/BSP/NET/net_socket.c index 79d203a..b3532eb 100644 --- a/Drivers/BSP/NET/net_socket.c +++ b/Drivers/BSP/NET/net_socket.c @@ -14,13 +14,15 @@ #include #include +/* + * 调试输出配置 + */ +#define DBG_TAG "[NET]" +#include "dbg_log.h" + /* * 私有宏定义区 */ -#define TS_FMT() do { \ - uint32_t _t = HAL_GetTick(); \ - printf("[%lu.%03lu] ", _t / 1000UL, _t % 1000UL); \ -} while(0) /* * 私有变量区 @@ -161,11 +163,10 @@ int net_poll(void) if (int_status & CH395F_GINT_STAT_PHY_CHANGE) { uint8_t phy = ch395f_get_phy_status(); - printf("[NET] PHY_CHANGE: 0x%02X\r\n", phy); + DBG_INFO("PHY_CHANGE: 0x%02X", phy); if (phy == CH395F_PHY_DISCONN) { - /* PHY 断开:所有活动 Socket 可能需要恢复 */ - printf("[NET] PHY disconnected, sockets may need recovery\r\n"); + DBG_ERROR("PHY disconnected, sockets may need recovery"); } } @@ -244,7 +245,7 @@ int net_poll(void) { s_last_dbg = now; uint8_t int_level = (HAL_GPIO_ReadPin(CH395_INT_GPIO_Port, CH395_INT_Pin) == GPIO_PIN_RESET) ? 1 : 0; - printf("[NET] INT#=%d", int_level); + DBG_DEBUG("INT#=%d", int_level); for (i = 0; i < NET_MAX_SOCKETS; i++) { if (g_net_socks[i].in_use) @@ -252,10 +253,9 @@ int net_poll(void) uint8_t st[2]; ch395f_get_socket_status((uint8_t)i, st); uint16_t rl = ch395f_get_recv_len((uint8_t)i); - printf(" s%d:sw%d/tcp%d rl%d", i, st[0], st[1], rl); + DBG_DEBUG(" s%d:sw%d/tcp%d rl%d", i, st[0], st[1], rl); } } - printf("\r\n"); } } @@ -1023,7 +1023,7 @@ static void handle_connect_event(net_sock_t *sock) /* CH395F 自动打开的数据 Socket(多连接模式),首次收到 CONNECT 中断 */ int sock_idx = (int)(sock - g_net_socks); sock->ch395_sock = (uint8_t)sock_idx; - TS_FMT(); printf("[NET] sock%d AUTO-OPENED, CONNECTED\r\n", sock->ch395_sock); + DBG_INFO("sock%d AUTO-OPENED, CONNECTED", sock->ch395_sock); uint8_t remote[6]; ch395f_get_remot_ipp(sock->ch395_sock, remote); memcpy(sock->remote_ip_arr, remote, 4); @@ -1060,7 +1060,7 @@ static void handle_connect_event(net_sock_t *sock) else if (sock->state == NET_SOCK_STATE_TCP_ACCEPT) { /* 数据 Socket 被分配了连接 */ - TS_FMT(); printf("[NET] data sock%d TCP_ACCEPT -> CONNECTED\r\n", sock->ch395_sock); + DBG_INFO("data sock%d TCP_ACCEPT -> CONNECTED", sock->ch395_sock); uint8_t remote[6]; ch395f_get_remot_ipp(sock->ch395_sock, remote); memcpy(sock->remote_ip_arr, remote, 4); @@ -1086,13 +1086,12 @@ static void handle_disconnect_event(net_sock_t *sock) { if (sock->state == NET_SOCK_STATE_LISTENING) { - /* 监听 Socket 收到断开通知(不应发生) */ - printf("[NET] WARNING: listen sock%d got disconnect\r\n", sock->ch395_sock); + DBG_ERROR("WARNING: listen sock%d got disconnect", sock->ch395_sock); } else if (sock->state == NET_SOCK_STATE_ESTABLISHED) { /* CH395F 自动关闭了 Socket,恢复为未使用状态 */ - TS_FMT(); printf("[NET] sock%d DISCONNECTED, back to idle\r\n", sock->ch395_sock); + DBG_INFO("sock%d DISCONNECTED, back to idle", sock->ch395_sock); memset(sock->remote_ip_arr, 0, 4); sock->remote_port = 0; sock->send_ready = 1; @@ -1103,7 +1102,7 @@ static void handle_disconnect_event(net_sock_t *sock) else { /* 未预期的断开 */ - printf("[NET] sock%d disconnect in state %d\r\n", sock->ch395_sock, sock->state); + DBG_ERROR("sock%d disconnect in state %d", sock->ch395_sock, sock->state); fire_event(sock, NET_EVENT_DISCONNECTED); } } @@ -1139,13 +1138,13 @@ static void handle_timeout_event(net_sock_t *sock) if (sock->state == NET_SOCK_STATE_CONNECTING) { /* TCP Client 连接超时 */ - TS_FMT(); printf("[NET] sock%d CONNECT TIMEOUT\r\n", sock->ch395_sock); + DBG_ERROR("sock%d CONNECT TIMEOUT", sock->ch395_sock); sock->state = NET_SOCK_STATE_CREATED; } else if (sock->state == NET_SOCK_STATE_ESTABLISHED) { /* 多连接模式:数据 Socket 超时,恢复为 TCP_ACCEPT 等待新连接 */ - TS_FMT(); printf("[NET] sock%d TIMEOUT, back to ACCEPT\r\n", sock->ch395_sock); + DBG_INFO("sock%d TIMEOUT, back to ACCEPT", sock->ch395_sock); memset(sock->remote_ip_arr, 0, 4); sock->remote_port = 0; sock->send_ready = 1; @@ -1154,7 +1153,7 @@ static void handle_timeout_event(net_sock_t *sock) else if (sock->state == NET_SOCK_STATE_LISTENING) { /* 监听超时(异常情况) */ - printf("[NET] sock%d LISTEN TIMEOUT\r\n", sock->ch395_sock); + DBG_ERROR("sock%d LISTEN TIMEOUT", sock->ch395_sock); } fire_event(sock, NET_EVENT_TIMEOUT); diff --git a/Drivers/BSP/TPAFE5160/tpafe5160.c b/Drivers/BSP/TPAFE5160/tpafe5160.c index 53061a7..0a0a35d 100644 --- a/Drivers/BSP/TPAFE5160/tpafe5160.c +++ b/Drivers/BSP/TPAFE5160/tpafe5160.c @@ -13,6 +13,12 @@ #include "tpafe5160.h" #include "main.h" +/* + * 调试输出配置 + */ +#define DBG_TAG "[ADC]" +#include "dbg_log.h" + /* ======================== 私有宏定义 ======================== */ /* @@ -69,7 +75,13 @@ int tpafe5160_init(void) TPAFE5160_CONVST_LOW(); /* 等待 BUSY 释放,确保上电后无残留转换 */ - return tpafe5160_wait_busy(TPAFE5160_CONV_TIMEOUT_MS); + int ret = tpafe5160_wait_busy(TPAFE5160_CONV_TIMEOUT_MS); + if (ret != TPAFE5160_OK) { + DBG_ERROR("Init wait BUSY timeout"); + } else { + DBG_INFO("Init OK"); + } + return ret; } /* @@ -150,6 +162,7 @@ int tpafe5160_read_all(int16_t *buf) /* 等待转换完成 */ ret = tpafe5160_wait_busy(TPAFE5160_CONV_TIMEOUT_MS); if (ret != TPAFE5160_OK) { + DBG_ERROR("read_all BUSY timeout"); return ret; } diff --git a/Drivers/BSP/dbg_cfg.h b/Drivers/BSP/dbg_cfg.h new file mode 100644 index 0000000..05a31f0 --- /dev/null +++ b/Drivers/BSP/dbg_cfg.h @@ -0,0 +1,35 @@ +/* + * 模块名称:Debug Output Configuration + * 模块功能:全局调试输出配置,定义各调试级别的编译开关 + * 参考 FlashDB fdb_cfg.h 设计模式 + * 使用方法: + * - DBG_ENABLE 总开关,定义后启用 DBG_ERROR / DBG_INFO + * - DBG_DEBUG_ENABLE 子开关,同时定义 DBG_ENABLE 后启用 DBG_DEBUG(含 __func__:__LINE__) + * - 两者都不定义:所有 DBG_* 宏为空操作(release 版本) + * + * 用户可在本文件中直接控制,也可在编译器预定义中覆盖 + */ + +#ifndef __DBG_CFG_H +#define __DBG_CFG_H + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * 总开关:取消注释启用所有调试输出 + */ +/* #define DBG_ENABLE */ + +/* + * 调试级别开关:取消注释额外输出 DBG_DEBUG 级别 + * 仅在 DBG_ENABLE 定义时生效 + */ +/* #define DBG_DEBUG_ENABLE */ + +#ifdef __cplusplus +} +#endif + +#endif /* __DBG_CFG_H */ diff --git a/Drivers/BSP/dbg_log.h b/Drivers/BSP/dbg_log.h new file mode 100644 index 0000000..10b0fbf --- /dev/null +++ b/Drivers/BSP/dbg_log.h @@ -0,0 +1,84 @@ +/* + * 模块名称:Unified Debug Logging + * 模块功能:提供统一的调试输出宏,支持 DEBUG / INFO / ERROR 三级日志控制 + * 参考 FlashDB FDB_DEBUG/FDB_INFO 设计模式 + * 使用方法: + * 1. 在 dbg_cfg.h 中定义 DBG_ENABLE(总开关)和 DBG_DEBUG_ENABLE(DEBUG 级别) + * 2. 每个 .c 文件在包含本文件前定义 DBG_TAG,如: + * #define DBG_TAG "[CH395]" + * #include "dbg_log.h" + * 适用平台:所有 BSP 驱动 + */ + +#ifndef __DBG_LOG_H +#define __DBG_LOG_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include "dbg_cfg.h" + +/* + * 总开关:DBG_ENABLE + * 定义 → 输出 DBG_ERROR / DBG_INFO;若 DBG_DEBUG_ENABLE 也定义则输出 DBG_DEBUG + * 不定义 → 所有输出宏为空操作(release 版本) + * 在 net_config.h 或编译器预定义中控制 + */ +#ifdef DBG_ENABLE + +/* + * 可重定向的打印函数,用户可在编译选项或 net_config.h 中预定义 + * #define DBG_PRINT(...) my_printf(__VA_ARGS__) + */ +#ifndef DBG_PRINT +#define DBG_PRINT(...) printf(__VA_ARGS__) +#endif + +/* + * 模块标签默认值,每个 .c 文件应在包含本文件前定义 DBG_TAG + * #define DBG_TAG "[CH395]" + */ +#ifndef DBG_TAG +#define DBG_TAG "" +#endif + +/* + * DBG_ERROR - 错误输出,受 DBG_ENABLE 控制 + */ +#define DBG_ERROR(fmt, ...) do { \ + DBG_PRINT("[ERR]" DBG_TAG " " fmt "\r\n", ##__VA_ARGS__); \ +} while(0) + +/* + * DBG_INFO - 信息输出,受 DBG_ENABLE 控制 + */ +#define DBG_INFO(fmt, ...) do { \ + DBG_PRINT(DBG_TAG " " fmt "\r\n", ##__VA_ARGS__); \ +} while(0) + +/* + * DBG_DEBUG - 调试输出,需 DBG_ENABLE + DBG_DEBUG_ENABLE 同时定义 + */ +#ifdef DBG_DEBUG_ENABLE +#define DBG_DEBUG(fmt, ...) do { \ + DBG_PRINT(DBG_TAG "(%s:%d) " fmt "\r\n", __func__, __LINE__, ##__VA_ARGS__); \ +} while(0) +#else +#define DBG_DEBUG(fmt, ...) +#endif + +#else /* DBG_ENABLE not defined — all debug output disabled */ + +#define DBG_ERROR(fmt, ...) +#define DBG_INFO(fmt, ...) +#define DBG_DEBUG(fmt, ...) + +#endif /* DBG_ENABLE */ + +#ifdef __cplusplus +} +#endif + +#endif /* __DBG_LOG_H */ diff --git a/MDK-ARM/STM32F407-Demo.uvprojx b/MDK-ARM/STM32F407-Demo.uvprojx index 76db6a9..c18aaa2 100644 --- a/MDK-ARM/STM32F407-Demo.uvprojx +++ b/MDK-ARM/STM32F407-Demo.uvprojx @@ -340,7 +340,7 @@ USE_HAL_DRIVER,STM32F407xx - ../Inc;../Drivers/STM32F4xx_HAL_Driver/Inc;../Drivers/STM32F4xx_HAL_Driver/Inc/Legacy;../Drivers/CMSIS/Device/ST/STM32F4xx/Include;../Drivers/CMSIS/Include;../Drivers/BSP/CH395F;../Drivers/BSP/GD5F2GQ5UE;../Lib/FlashDB/port/fal/inc;../Lib/FlashDB/inc;../Drivers/BSP/TPAFE5160;../Drivers/BSP/SD2506;../Drivers/BSP/RS485;../Drivers/BSP/NET + ../Inc;../Drivers/STM32F4xx_HAL_Driver/Inc;../Drivers/STM32F4xx_HAL_Driver/Inc/Legacy;../Drivers/CMSIS/Device/ST/STM32F4xx/Include;../Drivers/CMSIS/Include;../Drivers/BSP;../Drivers/BSP/CH395F;../Drivers/BSP/GD5F2GQ5UE;../Lib/FlashDB/port/fal/inc;../Lib/FlashDB/inc;../Drivers/BSP/TPAFE5160;../Drivers/BSP/SD2506;../Drivers/BSP/RS485;../Drivers/BSP/NET diff --git a/MDK-ARM/build_log.txt b/MDK-ARM/build_log.txt index d228e1e..8d7825e 100644 --- a/MDK-ARM/build_log.txt +++ b/MDK-ARM/build_log.txt @@ -1,49 +1,4 @@ *** Using Compiler 'V5.06 update 7 (build 960)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin' -Rebuild target 'STM32F407-Demo' -assembling startup_stm32f407xx.s... -compiling gpio.c... -compiling dma.c... -compiling stm32f4xx_hal_i2c_ex.c... -compiling stm32f4xx_hal_flash_ramfunc.c... -compiling stm32f4xx_it.c... -compiling stm32f4xx_hal_pwr_ex.c... -compiling stm32f4xx_hal_gpio.c... -compiling spi.c... -compiling stm32f4xx_hal_flash_ex.c... -compiling i2c.c... -compiling stm32f4xx_hal.c... -compiling stm32f4xx_hal_rcc_ex.c... -compiling stm32f4xx_hal_pwr.c... -compiling stm32f4xx_hal_cortex.c... -compiling stm32f4xx_hal_flash.c... -compiling main.c... -compiling stm32f4xx_hal_exti.c... -compiling usart.c... -compiling stm32f4xx_hal_msp.c... -compiling stm32f4xx_hal_dma.c... -compiling stm32f4xx_hal_rcc.c... -compiling stm32f4xx_hal_dma_ex.c... -compiling stm32f4xx_hal_i2c.c... -compiling fdb.c... -compiling fal.c... -compiling fal_flash.c... -compiling fdb_utils.c... -compiling fal_partition.c... -compiling fdb_tsdb.c... -compiling fdb_kvdb.c... -compiling system_stm32f4xx.c... -compiling stm32f4xx_hal_spi.c... -compiling fal_flash_gd5f2gq5ue.c... -compiling ch395f.c... -compiling stm32f4xx_hal_uart.c... -compiling gd5f2gq5ue.c... -compiling rs485.c... -compiling sd2506.c... -compiling tpafe5160.c... -compiling net_select.c... -compiling net_socket.c... -linking... -Program Size: Code=20964 RO-data=624 RW-data=40 ZI-data=7344 -FromELF: creating hex file... +Build target 'STM32F407-Demo' ".\STM32F407-Demo\STM32F407-Demo.axf" - 0 Error(s), 0 Warning(s). -Build Time Elapsed: 00:00:12 +Build Time Elapsed: 00:00:01