From ee76d8782ddc2c05b1ffc6307961edc00159fe7e Mon Sep 17 00:00:00 2001 From: yhf Date: Sat, 18 Jul 2026 23:01:25 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=94=BE=E7=94=B5?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E7=9B=B8=E5=85=B3=E7=9A=84=E8=8B=A5=E5=B9=B2?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 修改apps/chrg/applications/utils/chrg_sink.c: - 调整寄存器数组长度,新增继电器控制相关的初始化配置 - 在放电初始化前先关闭所有功率板继电器 2. 修改apps/chrg/applications/thread/chrg_north.c: - 修复变量类型不匹配问题,将uint8_t改为rt_uint8_t - 新增参数合法性校验逻辑,避免空指针和越界访问 - 新增电压状态标记,仅在电压有效后才将低压视为故障 - 修复无符号整数溢出问题,避免电压差值计算异常 - 优化电流异常时的状态机切换逻辑 - 非工作状态下重置所有状态标记 --- apps/chrg/applications/thread/chrg_north.c | 41 +++++-- apps/chrg/applications/utils/chrg_sink.c | 7 +- apps/chrg/keil_sink_retry_rebuild.log | 130 +++++++++++++++++++++ 3 files changed, 167 insertions(+), 11 deletions(-) create mode 100644 apps/chrg/keil_sink_retry_rebuild.log diff --git a/apps/chrg/applications/thread/chrg_north.c b/apps/chrg/applications/thread/chrg_north.c index cd26970..6e7178f 100644 --- a/apps/chrg/applications/thread/chrg_north.c +++ b/apps/chrg/applications/thread/chrg_north.c @@ -207,11 +207,17 @@ void chrg_source_setting(rt_uint8_t *frame_data, eIDX_SOU_CH idx, void chrg_sink_parse(rt_uint8_t *frame_data, eIDX_SOU_CH idx, struct chrg_switch_t *pSW, struct chrg_north_t *pNOR) { - rt_uint16_t volt_mv = ((frame_data[4] << 8) | frame_data[5])*10; - static uint8_t volt_low[TOTAL_SOU_CHS] = {0}; - static uint8_t no_curr[TOTAL_SOU_CHS] = {0}; - pSW->VZ = ((frame_data[14] << 8) | frame_data[15]); - pSW->VF = ((frame_data[16] << 8) | frame_data[17]); + rt_uint16_t volt_mv = ((frame_data[4] << 8) | frame_data[5]) * 10; + static rt_uint8_t volt_low[TOTAL_SOU_CHS] = {0}; + static rt_uint8_t no_curr[TOTAL_SOU_CHS] = {0}; + static rt_uint8_t volt_set_ok[TOTAL_SOU_CHS] = {0}; + + if ((idx >= TOTAL_SOU_CHS) || (pSW == RT_NULL)) { + return; + } + + pSW->VZ = ((frame_data[14] << 8) | frame_data[15]); + pSW->VF = ((frame_data[16] << 8) | frame_data[17]); #if DEBUG_NORTH LOG_D("Sink[%d] real volt: %d mV", idx, volt_mv); @@ -220,25 +226,42 @@ void chrg_sink_parse(rt_uint8_t *frame_data, eIDX_SOU_CH idx, if(pSW->sink.On_work == 1 && pSW->sink.test.test_mode == 0) { if (volt_mv <= WORK_MIN_VOLT) { // 电压过低,禁止进入工作状态机 // volt_low[idx]++; + /* Treat low voltage as a fault only after voltage was once valid. */ + if (volt_set_ok[idx] == 1) { + if (++volt_low[idx] >= 5) { + volt_set_ok[idx] = 0; + no_curr[idx] = 0; + pSW->sink.work_mode = SINK_WORK_LOW; + } + } // if(volt_low[idx]>=5) pSW->sink.work_on_step = SINK_WORK_LOW; // 复位工作状态机 } else{ // 电压稳定时执行工作状态机(继电器/工作模式切换) volt_low[idx] = 0; + volt_set_ok[idx] = 1; pSW->change_flag = 0; - if((pSW->sink.pro_loadv - volt_mv) <= 2000){ + /* Avoid unsigned underflow when actual voltage is above target. */ + if ((rt_uint32_t)volt_mv + 2000 >= pSW->sink.pro_loadv) { pSW->sink.work_mode = SINK_WORK_RUNING; - if(pSW->Now_current<=200){ - if(no_curr[idx]++>=5){ + /* Voltage is normal but current is still zero: retry from WAIT. */ + if (pSW->Now_current <= 200) { + if (++no_curr[idx] >= 5) { pSW->sink.work_mode = SINK_WORK_WAIT; no_curr[idx] = 0; } } - else no_curr[idx] = 0; + else { + no_curr[idx] = 0; + } + } else { + no_curr[idx] = 0; } } } else{ volt_low[idx] = 0; + no_curr[idx] = 0; + volt_set_ok[idx] = 0; // pSW->sink.work_on_step = SINK_WORK_STOP; // 复位工作状态机 } chrg_sink_work(idx,pSW); diff --git a/apps/chrg/applications/utils/chrg_sink.c b/apps/chrg/applications/utils/chrg_sink.c index 4be4501..2729aea 100644 --- a/apps/chrg/applications/utils/chrg_sink.c +++ b/apps/chrg/applications/utils/chrg_sink.c @@ -448,8 +448,8 @@ static int chrg_sink_work_init(eIDX_SOU_CH idx, struct chrg_switch_t *pSW) { struct chrg_south_t *pSOU = &chrgsouth; struct chrg_north_t *pNOR = &chrgnorth; - rt_uint16_t regs[3] = {REG_CURRENT_OUT + 1, REG_MODE, 0}; - rt_uint16_t vals[3] = {0, pSW->CV_mode, 0}; + rt_uint16_t regs[4] = {REG_CURRENT_OUT + 1, REG_MODE, 0, 0}; + rt_uint16_t vals[4] = {0, pSW->CV_mode, 0, 0}; rt_uint8_t count = 2; if (pSW->sink.test.test_mode_old == 1) { @@ -459,6 +459,9 @@ static int chrg_sink_work_init(eIDX_SOU_CH idx, struct chrg_switch_t *pSW) regs[count] = ModbusRTU_DYNA_CMD_MODE_FLAG; vals[count++] = ModbusRTU_DYNA_CMD_MODE_STOP; } + /* Close both power-board relays before starting sink init. */ + regs[count] = REG_REL_ON; + vals[count++] = ALL_REL_OFF; if (chrg_sou_com_batch_submit(idx, regs, vals, count) != 0) { return -1; } diff --git a/apps/chrg/keil_sink_retry_rebuild.log b/apps/chrg/keil_sink_retry_rebuild.log new file mode 100644 index 0000000..c6f7b18 --- /dev/null +++ b/apps/chrg/keil_sink_retry_rebuild.log @@ -0,0 +1,130 @@ +*** Using Compiler 'V6.16', folder: 'D:\Program Files\Keil_v5\ARM\ARMCLANG\Bin' +Rebuild target 'chrg' +compiling main.c... +compiling chrg_switch.c... +compiling chrg_wdt.c... +compiling chrg_gpio.c... +compiling chrg_tmr.c... +compiling chrg_fal.c... +compiling chrg_tty.c... +compiling chrg_led.c... +compiling chrg_vcom.c... +compiling chrg_rel.c... +compiling chrg_roll_nor.c... +compiling chrg_north.c... +compiling chrg_south.c... +compiling chrg_comm.c... +compiling chrg_roll_sou.c... +compiling chrg_lcd.c... +compiling chrg_thread.c... +compiling chrg_source.c... +compiling chrg_sink.c... +compiling chrg_north_pkg.c... +compiling chrg_eload.c... +compiling chrg_utils.c... +compiling syscall_mem.c... +compiling syscalls.c... +compiling cctype.c... +compiling cstdlib.c... +compiling cstring.c... +compiling ctime.c... +compiling cunistd.c... +compiling cwchar.c... +compiling device.c... +compiling hwtimer.c... +compiling completion_comm.c... +compiling completion_up.c... +compiling condvar.c... +compiling dataqueue.c... +compiling pipe.c... +compiling ringblk_buf.c... +compiling ringbuffer.c... +compiling waitqueue.c... +compiling workqueue.c... +compiling dev_pin.c... +compiling dev_serial.c... +compiling dev_watchdog.c... +compiling stm32f4xx_hal_msp.c... +compiling board.c... +compiling drv_flash_f4.c... +compiling drv_gpio.c... +compiling drv_tim.c... +compiling drv_usart.c... +compiling drv_usbd.c... +compiling drv_wdt.c... +compiling drv_common.c... +compiling fal.c... +compiling fal_flash.c... +compiling fal_partition.c... +compiling fal_rtt.c... +compiling shell.c... +compiling msh_parse.c... +compiling cmd.c... +compiling msh.c... +compiling clock.c... +compiling components.c... +compiling cpu_up.c... +compiling defunct.c... +compiling idle.c... +compiling ipc.c... +compiling irq.c... +compiling kservice.c... +compiling memheap.c... +compiling mempool.c... +compiling object.c... +compiling scheduler_comm.c... +compiling scheduler_up.c... +compiling thread.c... +compiling timer.c... +compiling rt_vsnprintf_tiny.c... +compiling kerrno.c... +compiling kstring.c... +compiling rt_vsscanf.c... +compiling kstdio.c... +compiling atomic_arm.c... +compiling div0.c... +compiling showmem.c... +assembling context_rvds.S... +compiling cpuport.c... +compiling usbdevice.c... +compiling cdc_vcom.c... +compiling usbdevice_core.c... +assembling startup_stm32f405xx.s... +compiling system_stm32f4xx.c... +compiling stm32f4xx_hal_gpio.c... +compiling stm32f4xx_hal_flash.c... +compiling stm32f4xx_hal_usart.c... +compiling stm32f4xx_hal_rng.c... +compiling stm32f4xx_hal_uart.c... +compiling stm32f4xx_hal_wwdg.c... +compiling stm32f4xx_hal_tim.c... +compiling stm32f4xx_hal_cryp_ex.c... +compiling stm32f4xx_hal_pwr.c... +compiling stm32f4xx_hal_rcc.c... +compiling stm32f4xx_hal_hcd.c... +compiling stm32f4xx_ll_usb.c... +compiling stm32f4xx_hal_pccard.c... +compiling stm32f4xx_hal_flash_ex.c... +compiling stm32f4xx_hal_flash_ramfunc.c... +compiling stm32f4xx_hal_tim_ex.c... +compiling stm32f4xx_hal_lptim.c... +compiling stm32f4xx_hal_cec.c... +compiling stm32f4xx_hal_cryp.c... +compiling stm32f4xx_hal.c... +compiling stm32f4xx_hal_iwdg.c... +compiling stm32f4xx_hal_pcd_ex.c... +compiling stm32f4xx_hal_pcd.c... +compiling stm32f4xx_hal_crc.c... +compiling stm32f4xx_hal_dma_ex.c... +compiling stm32f4xx_hal_cortex.c... +compiling stm32f4xx_hal_dma.c... +compiling stm32f4xx_hal_rcc_ex.c... +compiling stm32f4xx_hal_pwr_ex.c... +compiling ulog.c... +compiling console_be.c... +linking... +Program Size: Code=140360 RO-data=15152 RW-data=8544 ZI-data=13712 +FromELF: creating hex file... +After Build - User command #1: fromelf --bin .\build\keil\Obj\chrg.axf -o build/chrg.bin +".\build\keil\Obj\chrg.axf" - 0 Error(s), 0 Warning(s). +Build Time Elapsed: 00:00:29