diff --git a/Inc/board_config.h b/Inc/board_config.h index dda76b5..415a9cf 100644 --- a/Inc/board_config.h +++ b/Inc/board_config.h @@ -1,6 +1,14 @@ #ifndef BOARD_CONFIG_H #define BOARD_CONFIG_H +#ifndef DISABLE +#define DISABLE 0 +#endif + +#ifndef ENABLE +#define ENABLE 1 +#endif + #define GD32E23XF4 0x10 #define GD32E23XF6 0x20 #define GD32E23XF8 0x40 @@ -11,30 +19,25 @@ /* >>>>>>>>>>>>>>>>>>>>[IIC TYPE DEFINE]<<<<<<<<<<<<<<<<<<<< */ -// #define SOFTWARE_IIC // IIC Type : Software IIC -#undef SOFTWARE_IIC // IIC Type : Hardware IIC +#define SOFTWARE_IIC DISABLE // DISABLE: Hardware IIC; ENABLE: Software IIC /* >>>>>>>>>>>>>>>>>>>>[DEBUG MODE]<<<<<<<<<<<<<<<<<<<< */ -// #define DEBUG_MODE // Operating Mode : Debug Mode -#undef DEBUG_MODE // Operating Mode : Release Mode +#define DEBUG_MODE DISABLE // DISABLE: Release Mode; ENABLE: Debug Mode /* >>>>>>>>>>>>>>>>>>>>[COMMAND DEBUG]<<<<<<<<<<<<<<<<<<<< */ -// #define COM_DEBUG // Enable Command Debug Information -#undef COM_DEBUG // Disable Command Debug Information +#define COM_DEBUG DISABLE // DISABLE: Command debug off; ENABLE: Command debug on /* >>>>>>>>>>>>>>>>>>>>[DEBUG ASSERTIONS DEFINE]<<<<<<<<<<<<<<<<<<<< */ -// #define DEBUG_VERBOSE // Debug Assertions Status : Debug Verbose Information -#undef DEBUG_VERBOSE // Debug Assertions Status : No Debug Verbose Information +#define DEBUG_VERBOSE DISABLE // DISABLE: Verbose debug off; ENABLE: Verbose debug on /* >>>>>>>>>>>>>>>>>>>>[SEGGER RTT DETECTION]<<<<<<<<<<<<<<<<<<<< */ -#define SEGGER_RTT_DETECTION // SEGGER RTT Detection : Enable -// #undef SEGGER_RTT_DETECTION // SEGGER RTT Detection : Disable +#define SEGGER_RTT_DETECTION ENABLE // DISABLE: SEGGER RTT off; ENABLE: SEGGER RTT on -#ifdef SEGGER_RTT_DETECTION +#if SEGGER_RTT_DETECTION == ENABLE #include "SEGGER_RTT.h" #define RTT_printf(ch, ...) SEGGER_RTT_printf(ch, __VA_ARGS__) #define RTT_WriteString(ch, s) SEGGER_RTT_WriteString(ch, s) diff --git a/Inc/i2c.h b/Inc/i2c.h index ab42e62..c3319d4 100644 --- a/Inc/i2c.h +++ b/Inc/i2c.h @@ -86,7 +86,9 @@ i2c_result_t i2c_bus_reset(void); \param[out] none \retval none */ +#if DEBUG_VERBOSE == ENABLE void i2c_scan(void); +#endif /*! \brief write 16-bit data to I2C device @@ -139,7 +141,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8 */ i2c_result_t i2c_read_raw(uint8_t slave_addr, uint8_t *data, uint8_t length); -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE /*! \brief get status string for debugging \param[in] status: i2c_result_t value diff --git a/README.md b/README.md index 0b61f0a..1470ef5 100644 --- a/README.md +++ b/README.md @@ -110,17 +110,17 @@ ## 板级配置(`Inc/board_config.h`) -所有功能开关和引脚定义集中在 `Inc/board_config.h`。以下为完整的宏开关说明和推荐使用方式。 +所有功能开关和引脚定义集中在 `Inc/board_config.h`。功能开关均为单行数值宏:将右侧的 `ENABLE` 或 `DISABLE` 改为另一值即可;其中 `ENABLE` 为 `1`,`DISABLE` 为 `0`。以下为完整的宏开关说明和推荐使用方式。 ### 功能开关速查表 | 宏 | 作用 | 默认值 | Release 建议 | |---|------|:---:|:---:| -| `SOFTWARE_IIC` | I2C 实现方式 | ❌ 硬件 | 硬件 | -| `DEBUG_MODE` | printf 串口输出 | ❌ 关闭 | ❌ | -| `COM_DEBUG` | 命令帧调试打印 | ❌ 关闭 | ❌ | -| `DEBUG_VERBOSE` | 详细调试信息 | ❌ 关闭 | ❌ | -| `SEGGER_RTT_DETECTION` | SEGGER RTT 支持 | ✅ 开启 | ❌ | +| `SOFTWARE_IIC` | I2C 实现方式 | `DISABLE`(硬件) | `DISABLE` | +| `DEBUG_MODE` | printf 串口输出 | `DISABLE` | `DISABLE` | +| `COM_DEBUG` | 命令帧调试打印 | `DISABLE` | `DISABLE` | +| `DEBUG_VERBOSE` | 详细调试信息 | `DISABLE` | `DISABLE` | +| `SEGGER_RTT_DETECTION` | SEGGER RTT 支持 | `ENABLE` | `DISABLE` | --- @@ -128,12 +128,10 @@ 选择 I2C 使用硬件外设还是软件 GPIO 模拟。 -```c -// 使用软件 I2C(GPIO 模拟) -#define SOFTWARE_IIC +> 当前工程尚未实现 `SOFTWARE_IIC` 的条件编译驱动选择;该宏已迁移为数值配置,但改值不会在此版本切换 I2C 实现。 -// 使用硬件 I2C(外设 I2C0) —— 默认 -#undef SOFTWARE_IIC +```c +#define SOFTWARE_IIC DISABLE // DISABLE: 硬件 I2C(默认);ENABLE: 软件 I2C(GPIO 模拟) ``` | 选项 | 优点 | 缺点 | @@ -150,11 +148,7 @@ 开启后 USART0 输出 printf 调试信息。**Release 固件必须关闭。** ```c -// 开启调试输出 -#define DEBUG_MODE - -// 关闭 —— 默认 -#undef DEBUG_MODE +#define DEBUG_MODE DISABLE // DISABLE: 关闭(默认);ENABLE: 开启调试输出 ``` **影响范围:** @@ -169,11 +163,7 @@ 开启后串口命令解析过程打印每帧的详细内容(地址、长度、数据、校验)。**仅调试通信协议时开启。** ```c -// 开启命令帧调试 -#define COM_DEBUG - -// 关闭 —— 默认 -#undef COM_DEBUG +#define COM_DEBUG DISABLE // DISABLE: 关闭(默认);ENABLE: 开启命令帧调试 ``` **依赖:** 需要先开启 `DEBUG_MODE`,否则输出无法外发。 @@ -192,11 +182,7 @@ 在 `DEBUG_MODE` 基础上输出更底层的信息,如 I2C 总线扫描结果、MCU 型号识别等。 ```c -// 开启详细调试 -#define DEBUG_VERBOSE - -// 关闭 —— 默认 -#undef DEBUG_VERBOSE +#define DEBUG_VERBOSE DISABLE // DISABLE: 关闭(默认);ENABLE: 开启详细调试 ``` **额外输出:** @@ -213,11 +199,7 @@ RTT(Real-Time Transfer)是 SEGGER 的调试通道技术,通过 SWD 接口传输数据,不占用串口引脚,速度远超 UART。 ```c -// 启用 RTT —— 默认 -#define SEGGER_RTT_DETECTION - -// 禁用 -#undef SEGGER_RTT_DETECTION +#define SEGGER_RTT_DETECTION ENABLE // DISABLE: 禁用 RTT;ENABLE: 启用 RTT(默认) ``` **启用时:** @@ -226,8 +208,9 @@ RTT(Real-Time Transfer)是 SEGGER 的调试通道技术,通过 SWD 接口 - 可用 J-Link RTT Viewer 或 VSCode + cortex-debug 查看实时日志 **禁用时:** -- RTT 宏展开为空操作,零开销 -- SEGGER_RTT 模块不参与编译 +- 应用层 RTT 头文件引用与调用代码均在预处理阶段排除 +- RTT 宏展开为空操作;应用目标不生成 RTT 调用代码 +- 为保持现有 CMake SDK 加载方式,`SDK/SEGGER_RTT/` 仍会参与构建;静态库中未被引用的对象不会被链接器提取 > **Release 建议关闭** — RTT 依赖调试器连接,量产固件中无意义且占用 ROM。 diff --git a/Src/command.c b/Src/command.c index 1e7c72c..ab423cf 100644 --- a/Src/command.c +++ b/Src/command.c @@ -101,7 +101,7 @@ void system_software_reset(void) } /* Debug output control */ -#ifdef COM_DEBUG +#if COM_DEBUG == ENABLE #include #define COMMAND_DEBUG(fmt, ...) printf("[COMMAND] " fmt "\n", ##__VA_ARGS__) #else @@ -565,7 +565,7 @@ void command_process(void) { // 到帧尾,进行各项校验 bool verification_status = true; - #ifdef DEBUG_VERBOSE + #if DEBUG_VERBOSE == ENABLE if (cmd_buf[0] != PROTOCOL_PACKAGE_HEADER) { send_response(RESP_TYPE_HEADER_ERR, s_report_status_err, sizeof(s_report_status_err)); verification_status = false; diff --git a/Src/gd32e23x_it.c b/Src/gd32e23x_it.c index 6c67dbb..b7b49cc 100644 --- a/Src/gd32e23x_it.c +++ b/Src/gd32e23x_it.c @@ -108,7 +108,7 @@ void USART0_IRQHandler(void) { g_usart_config.irq_handler(); // 通过函数指针调用对应的处理函数 } // 作为调试口(第二串口)时也接收数据,便于模拟上位机命令 -#ifdef DEBUG_MODE +#if DEBUG_MODE == ENABLE else { usart0_irq_handler(); } diff --git a/Src/i2c.c b/Src/i2c.c index 66c9dcd..4dadd06 100644 --- a/Src/i2c.c +++ b/Src/i2c.c @@ -79,7 +79,7 @@ i2c_result_t i2c_bus_reset(void) { i2c_disable(I2C0); i2c_deinit(I2C0); -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("I2C bus reset\r\n"); #endif @@ -145,6 +145,7 @@ i2c_result_t i2c_bus_reset(void) { * 如果在某个地址上发现了设备,则会打印出该设备的地址。 * 最后会打印出找到的设备总数。 */ +#if DEBUG_VERBOSE == ENABLE void i2c_scan(void) { uint32_t timeout; uint8_t address; @@ -249,6 +250,7 @@ void i2c_scan(void) { while (usart_flag_get(UART_PHY, USART_FLAG_TC) == RESET) {} } } +#endif /** * @brief 内部辅助函数:等待指定的I2C标志位被设置,带有超时。 @@ -260,7 +262,7 @@ static i2c_result_t _i2c_wait_flag_timeout(uint32_t flag) uint16_t timeout = 0; while(!i2c_flag_get(I2C0, flag)){ if(timeout++ > I2C_TIME_OUT){ -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE const char* fname = "UNKNOWN"; switch (flag) { case I2C_FLAG_SBSEND: fname = "SBSEND"; break; @@ -359,7 +361,7 @@ i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data } else { i2c_flag_clear(I2C0, I2C_FLAG_AERR); timeout =0; -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("IIC write failed for Error Slave Address. \n"); #endif return I2C_RESULT_NACK; @@ -438,7 +440,7 @@ i2c_result_t i2c_write_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t data retry_count ++; if (retry_count >= I2C_MAX_RETRY) { -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("IIC write failed after %d retries\n", I2C_MAX_RETRY); #endif return I2C_RESULT_ERROR; @@ -612,7 +614,7 @@ i2c_result_t i2c_read_16bits(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data retry_count++; if (retry_count >= I2C_MAX_RETRY) { -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("IIC read failed after %d retries\n", I2C_MAX_RETRY); #endif return I2C_RESULT_ERROR; @@ -652,7 +654,7 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint /* parameter validation */ if (data == NULL || slave_addr > 0x7F || length == 0) { -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("I2C read invalid param: slave=0x%02X, len=%u, data=%p\r\n", slave_addr, length, data); #endif return I2C_RESULT_INVALID_PARAM; @@ -712,7 +714,7 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint i2c_flag_clear(I2C0, I2C_FLAG_AERR); i2c_stop_on_bus(I2C0); timeout = 0; -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("I2C write failed for Error Slave Address. \n"); #endif return I2C_RESULT_NACK; @@ -787,7 +789,7 @@ i2c_result_t i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint retry_count++; if (retry_count >= I2C_MAX_RETRY) { -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("IIC write failed after %d retries\n", I2C_MAX_RETRY); #endif return I2C_RESULT_ERROR; @@ -832,7 +834,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8 /* enable acknowledge */ i2c_ack_config(I2C0, I2C_ACK_ENABLE); -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("I2C read start: slave=0x%02X reg=0x%02X len=%u\r\n", slave_addr, reg_addr, length); #endif @@ -884,7 +886,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8 } /* debug: show address ack and error flags */ -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("I2C CLEAR_ADDRESS: write_phase=%d ADDSEND=%d AERR=%d\r\n", (int)write_phase, (int)i2c_flag_get(I2C0, I2C_FLAG_ADDSEND), @@ -902,19 +904,19 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8 - length > 2: keep ACK enabled and clear ADDR; we'll handle N-2/BTF sequence later */ if (length == 1) { i2c_ack_config(I2C0, I2C_ACK_DISABLE); -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("I2C READ phase: length=1, disabling ACK before clearing ADDSEND\r\n"); #endif } else if (length == 2) { i2c_ackpos_config(I2C0, I2C_ACKPOS_NEXT); i2c_ack_config(I2C0, I2C_ACK_DISABLE); -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("I2C READ phase: length=2, set ACKPOS_NEXT and disabling ACK before clearing ADDSEND\r\n"); #endif } else { /* length > 2: keep ACK enabled so slave will clock out data */ i2c_ack_config(I2C0, I2C_ACK_ENABLE); -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("I2C READ phase: length=%u (>2), keeping ACK enabled\r\n", length); #endif } @@ -947,7 +949,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8 break; } -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("I2C RESTART: after BTC, delay 5ms and issuing repeated START\r\n"); #endif /* small delay to allow slave device to prepare multi-byte data before repeated start */ @@ -970,13 +972,13 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8 write_phase = false; state = I2C_STATE_CLEAR_ADDRESS; timeout = 0; -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("I2C addressing sent for read (slave=0x%02X)\r\n", slave_addr); #endif break; case I2C_STATE_RECEIVE_DATA: -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("I2C RECEIVE_DATA: expecting %u bytes\r\n", length); #endif if (length == 1) { @@ -1058,7 +1060,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8 case I2C_STATE_ERROR: /* I2C bus error, try to reset the bus and retry */ -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("I2C_STATE_ERROR: resetting bus and retrying (retry_count=%u)\r\n", retry_count); #endif i2c_bus_reset(); @@ -1069,7 +1071,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8 retry_count++; if (retry_count >= I2C_MAX_RETRY) { -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE /* Print diagnostic flags to help root-cause analysis */ printf("I2C read final failure after %d retries, last_state=%d\r\n", I2C_MAX_RETRY, state); printf(" FLAGS: AERR=%d BERR=%d LOSTARB=%d I2CBSY=%d\r\n", @@ -1078,7 +1080,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8 (int)i2c_flag_get(I2C0, I2C_FLAG_LOSTARB), (int)i2c_flag_get(I2C0, I2C_FLAG_I2CBSY)); #endif -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("I2C read failed after %d retries\n", I2C_MAX_RETRY); #endif return I2C_RESULT_ERROR; @@ -1099,7 +1101,7 @@ i2c_result_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint8 } } /* timeout path: provide debug info */ -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE printf("I2C read timeout (end state). slave=0x%02X, len=%u\r\n", slave_addr, length); printf(" FLAGS: AERR=%d BERR=%d LOSTARB=%d I2CBSY=%d\r\n", (int)i2c_flag_get(I2C0, I2C_FLAG_AERR), @@ -1267,7 +1269,7 @@ i2c_result_t i2c_read_raw(uint8_t slave_addr, uint8_t *data, uint8_t length) return I2C_RESULT_TIMEOUT; } -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE /*! \brief get status string for debugging \param[in] status: i2c_status_t value diff --git a/Src/main.c b/Src/main.c index 8710b70..9bc9432 100644 --- a/Src/main.c +++ b/Src/main.c @@ -62,15 +62,15 @@ int main(void) i2c_config(); -#ifdef DEBUG_MODE +#if DEBUG_MODE == ENABLE printf("Hello World!\r\n"); #endif -#ifdef SEGGER_RTT_DETECTION +#if SEGGER_RTT_DETECTION == ENABLE RTT_printf(0, "Hello World!\r\n"); #endif -#ifdef DEBUG_VERBOSE +#if DEBUG_VERBOSE == ENABLE i2c_scan(); i2c_bus_reset();