浏览代码

1.完成ms1031功耗优化:空管下关闭高速晶振与进rtc时为空管又临时开启高速晶振方案。
2.完成ms1031采集顺序调整,目前先采集温度再采集飞行时间,才能避免中断无法进入的bug。
3.降低OFFSET电压增加tw2充电时间与设置stop端口为直流充电来减少静水下的飞行时间差。
4.增加温度异常采集与显示。

HBR 1 天之前
父节点
当前提交
93b739e560
共有 6 个文件被更改,包括 318 次插入268 次删除
  1. 3 2
      Drivers/Board/board.h
  2. 6 4
      Users/lcd/lcd_ghg102.c
  3. 3 3
      Users/main.c
  4. 11 4
      Users/rtc_isr.c
  5. 69 45
      Users/tdc_tof/tdc_ms1030.c
  6. 226 210
      Users/tdc_tof/tof_isr.c

+ 3 - 2
Drivers/Board/board.h

@@ -23,7 +23,7 @@
 
 #define BOOTLOADER_ENABLE // 从0x0000启动运行时,屏蔽该宏
 
- //#define APP_DEBUG_PRINT // 调试打印开关
+#define APP_DEBUG_PRINT // 调试打印开关
 
 // #define DEBUG_LOW_POWER
 //#define DOG_ENABLE // WXL2026-5-12
@@ -407,7 +407,8 @@ typedef void (*McuFlash_WordPP_fpt)(uint32_t addr, uint32_t data);
     extern uint8_t G_STATIC_OT_SETTING_FLAG;        // 20260429 正在设置静态OT,不进行飞行时间差滤波
     extern void set_rtc_interrupt_time(float time); // add by yuewei 20260519,设置RTC:多少秒钟进一次中断,time:0.5s, 1.0s, 1.5s, 2.0s...
     extern float get_rtc_interrupt_time(void);      // add by yuewei 20260519,从 RTC->CR0 读取 PRDX 值,返回中断间隔秒数
-
+    extern void MS1031_DisableHighSpeedOsc(void);     //add by hbr20260824,关闭ms1031高速晶振
+    extern void MS1031_EnableHighSpeedOsc(void);
     // add by wuxiaolin 20260313 start
     // param.h 参数和记录读写相关接口
     extern uint8_t get_param_firmware_ver(void); // 获取当前版本号

+ 6 - 4
Users/lcd/lcd_ghg102.c

@@ -328,7 +328,9 @@ void lcd_dev_id_part2(void)
 //modify by HBR 20260608
 void lcd_temp(void)
 {
-     if (WaterTemp > 100.0f || WaterTemp < 0.0f)
+    extern uint8_t temp_error_flag;
+    
+     if ((WaterTemp > 100.0f || WaterTemp < 0.0f)||temp_error_flag)
     {
         lcd_temp_err();                                 //add by HBR 20260603,err7 display
     }
@@ -591,10 +593,10 @@ void lcd_show_page(void)
 
 void lcd_blank_disp(void)
 {
-//     if (TdcItf.blank_flag)   //暂时修改,主动显示空管
-//    {
+     if (TdcItf.blank_flag)   //暂时修改,主动显示空管
+    {
         DispMem_Status |= LCD_STATUS_CIRCLE;
-//    }
+    }
 
 }
 

+ 3 - 3
Users/main.c

@@ -370,11 +370,11 @@ static void tdc_business_thread(void *parameter)
             // rt_thread_mdelay(15);
             rtc_run_t *rr;
             get_rtc_run(&rr);
-#ifdef DEBUG_TDC
+//#ifdef DEBUG_TDC
             LOG("Diff=%.6f, AvgDiff=%.6f, T=%.6f, vol=%.6f\n", SerPlot.f.Data[0], SerPlot.f.Data[3], WaterPrmt[0], FlowVol * 0.000001f * 3600);
             // LOG("diff=%.6f, U=%.6f, D=%.6f\n", SerPlot.f.Data[0], SerPlot.f.Data[1], SerPlot.f.Data[2]);
             rt_thread_mdelay(4);
-#endif
+//#endif
 #endif
             G_TDC_DEEP_SLEEP = 1;
             // rt_thread_mdelay(10);
@@ -613,7 +613,7 @@ int32_t main(void)
 
     rtc_thread_create();      // 创建RTC线程
     tdc_thread_create();      // 创建TDC线程
-    nb_thread_create();       // 创建NB线程
+//    nb_thread_create();       // 创建NB线程
     lcd_thread_create();      // 创建LCD线程
     Key_press_timer_create(); // 创建按键时长检测定时器
 

+ 11 - 4
Users/rtc_isr.c

@@ -1,6 +1,7 @@
 
 #include "board.h"
 #include "tdc_tof.h"
+#include "alarm.h"
 
 static rtc_run_t *pRtcRun;
 void rtc_init(rtc_run_t *rtc_run)
@@ -215,12 +216,18 @@ void rtc_interrupt_deal_tdc(void)
             {
                 tof_flag |= TOF_FLAG_TDC_FAULT;
             }
-
-            TdcIntStep=0x04;
-            tdc_start_temp(TDC_CFG_DO_CKENA|TDC_CFG_DO_WREG | TDC_CFG_DO_CKDIS);
-//	        tdc_start_cal(TDC_CFG_DO_CKENA|TDC_CFG_DO_WREG | TDC_CFG_DO_CKDIS);
+            
+         if (TdcItf.blank_flag)   
+        {
+          MS1031_EnableHighSpeedOsc();
+        }
+            
+            
+            TdcIntStep=0x01;
+            rt_sem_release(&tdc_isr_sem);
 //            tdc_start_tof(TDC_CFG_DO_CKENA | TDC_CFG_DO_CKDIS | TDC_CFG_DO_WREG);
 			
             //LOG("---------------------------------2\n");
         }
+        
 }

+ 69 - 45
Users/tdc_tof/tdc_ms1030.c

@@ -4,7 +4,7 @@
 #define ANZ_FIRE 		12u//fire发送脉冲数
 #define DIV_FIRE 		3u	//内部时钟产生脉冲的分频
 #define DIV_CLKHS 	    0u	//高速时钟分频
-#define START_CLKHS     2u	//高速启动后测量前晶振启动间隔时间,120us
+#define START_CLKHS     2u	//高速启动后测量前晶振启动间隔时间,120us;0关闭,1持续开启,2延迟120us后开启
 #define NEG_START  	    0u	//start通道边缘
 #define NEG_STOP		0u	//stop通道边缘
 #define HITIN			9u	//stop预期脉冲数
@@ -29,7 +29,7 @@
 #define EN_INT_ALU      1  //alu中断触发位
 #define RFEDGE          0  //stop通道边沿敏感性
 #define UP_DELVAL1      200  //当EN_FIST_WAVE=1时,为第一波屏蔽窗口 BIT12~27  暂未设置
-#define OFFSET          30	//当en_hc=1时, 62~-64mV   暂未设置
+#define OFFSET          0	//当en_hc=1时, 62~-64mV   暂未设置
 #define DFLT_WREG1   (( EN_INT_TO<<31)  | \
                       ( EN_INT_HIT<<30) | \
                       ( EN_INT_ALU<<29) | \
@@ -74,14 +74,14 @@
 #define EN_ANALOG			1       //模拟测量控制开关
 #define HZ60				0       //顺流测量和逆流测量的时间单位
 #define TCYCLE				1       //温度测量循环时间
-#define ANZ_FAKE			1       //温度测量热身次数,0:两次热身,1:7次热身
+#define ANZ_FAKE			0       //温度测量热身次数,0:两次热身,1:7次热身
 #define SEL_ECLK_TMP		1       //选择温度测量内部参考时钟
-#define TW2					4       //120us充电时间,与1030不同
+#define TW2					5       //180us充电时间,与1030不同
 #define EN_PEAK				0		//峰值检波使能关闭,1030不同
 #define EN_DISCHARGE		0		//检波放电使能关闭,1030不同
 #define CYCLE_TOF			0		//顺流逆流自动检测模式下,顺流逆流测量间隔时间,初步确定为3=20ms 1030不同
 #define EN_ERR_VAL			1       //溢出时alu将全f写到结果寄存器
-#define EN_DC				0		//stop端口充电模式 这里设置为直流  1030不同
+#define EN_DC				1		//stop端口充电模式 这里设置为直流  1030不同
 #define FIRE0_DEF			1       //fire通道默认状态,fire通道默认电平
 #define DOUBLE_RES			0       //双精度模式
 #define QUAD_RES			1       //4精度模式
@@ -116,9 +116,9 @@
 #define SEL_SRC_CLK		(0<<7)  //选择高速时钟输入源
 #define EN_HC			(0<<6)  //比较器调节步长
 #define CURR32K			(0<<5)  //32K晶振功耗设置
-#define DIS_Vref		(0<<4)  //基准电压输出控制
+#define DIS_Vref		(1<<4)  //基准电压输出控制
 #define ADJ_LDO_ROUGH	(0<<3)  //内部LDO输出粗调
-#define ADJ_LDO_FINE	(4<<0)	//内部LDO输出细调
+#define ADJ_LDO_FINE	(0<<0)	//内部LDO输出细调
 #define DFLT_WREG6 (DOWN_DELVAL1|SEL_SRC_CLK|EN_HC|CURR32K|DIS_Vref|ADJ_LDO_ROUGH|ADJ_LDO_FINE)
             
 
@@ -152,20 +152,6 @@ void tdc_tof_init(void)
     tdc_spi_wr_code(0x50);
     us_delay(1000);     // 1000us > specification(500us)
 
-
-//    tdc_spi_wr_d32(0x80, 0x1818088c);  // 修正:SEL_TIMO_MB = 8(合法)
-//    tdc_spi_wr_d32(0x81, 0x00000000);
-//    tdc_spi_wr_d32(0x82, 0x00000000);
-//    tdc_spi_wr_d32(0x83, 0x00000000);
-//    tdc_spi_wr_d32(0x84, 0x46280540);
-//    tdc_spi_wr_d32(0x85, 0x00000000);
-//    tdc_spi_wr_d32(0x86, 0x0000001c); 
-// 修正后的配置
-    
-//    tdc_spi_wr_code(0x70);
-    
-//    uint16_t aa=0;
-//    aa=tdc_spi_rd_d16(0xd0);
      tdc_spi_wr_d32(0x80, DFLT_WREG0);
      uint32_t read_a0=tdc_spi_rd_d32(0xA0);
      read_a0=read_a0&0xFFu;
@@ -232,15 +218,13 @@ void tdc_start_tof(uint32_t cfg)
 
     if (cfg & TDC_CFG_DO_WREG){
 
-
-        // write_reg3
         
-        u32tmp = DFLT_WREG3 |
-//                  ( 0u<<13) |       // measure FW pulse width, 0:on; 1:off
-                  (TdcFwOfst<<6);   // FW wave_offs, 0:0mv; 1~63:2~126mV; 64~127:-128~-2mV;
-//        if (tof_state&TOF_ST_VOID){
-//            u32tmp |= (1u<<13);     // disable FW pulse width to save power
-//        }
+     u32tmp = (DFLT_WREG3 & ~(1u << 13)) | (TdcFwOfst << 6);
+                    // measure FW pulse width, 0:on; 1:off
+                   // FW wave_offs, 0:0mv; 1~63:2~126mV; 64~127:-128~-2mV;
+        if (tof_state&TOF_ST_VOID){
+            u32tmp |= (1u<<13);     // disable FW pulse width to save power
+        }
         tdc_spi_wr_d32(0x83, u32tmp);
     }
 
@@ -293,13 +277,16 @@ void tdc_read_tof(uint32_t cfg)
 
     if ((TdcStatus&0x03F0)!=0x0090u){
         tof_flag |= TOF_FLAG_BLANK_PIPE;
-//        tdc_spi_wr_code(0x70);
+        tdc_spi_wr_code(0x70);
+        tdc_spi_wr_d32(0x80, DFLT_WREG0 & ~(0x07 << 14));   //一旦为空管马上关闭高速晶振,降低功耗
     } 
     else 
      {
        
             TdcUPwFw = tdc_spi_rd_d16(0xd3);    // First wave pulse width
             TdcUPwHw = tdc_spi_rd_d16(0xd4);    // 1st Hit wave pulse width
+            TdcDPwFw = tdc_spi_rd_d16(0xd3);   
+            TdcDPwHw = tdc_spi_rd_d16(0xd4);  
 
             // TOF_UP_HITs
             TdcUToF[0] = tdc_spi_rd_d32(0xb8)*(1.0f/8);  // average of all
@@ -328,9 +315,9 @@ void tdc_start_cal(uint32_t cfg)
     // 8*T_32k = 244.140625us
     if (cfg & TDC_CFG_DO_WREG){
         // write_reg0
-        tdc_spi_wr_d32(0x80, DFLT_WREG0 |
-                              ( 2u<< 4)     // SEL_TIMO_MB, 0:64us; 1:128us; 2:256us; ~ 6:4096us;
-                      );
+//        tdc_spi_wr_d32(0x80, DFLT_WREG0 |
+//                              ( 2u<< 4)     // SEL_TIMO_MB, 0:64us; 1:128us; 2:256us; ~ 6:4096us;
+//                      );
 
         // write_reg3
         tdc_spi_wr_d32(0x83, DFLT_WREG3 |
@@ -366,7 +353,7 @@ void tdc_read_cal(uint32_t cfg)
         TdcScale += (244140.625f/TdcCal - TdcScale)*(1.0f/2);
 				//LOG("----------TdcScale=%.6f\n",TdcScale);
     } else {
-        tdc_spi_wr_code(0x70);
+//        tdc_spi_wr_code(0x70);
     }
 
     if (cfg & TDC_CFG_DO_CKDIS){
@@ -387,14 +374,14 @@ void tdc_start_temp(uint32_t cfg)
     if (cfg & TDC_CFG_DO_WREG){
        
               
-        tdc_spi_wr_d32(0x80, DFLT_WREG0 |
-                              ( 3u<< 6)     // SEL_TIMO_MB, 0:64us; 1:128us; 2:256us; 3:512us; ~ 6:4096us;
-                      );
+//        tdc_spi_wr_d32(0x80, DFLT_WREG0 |
+//                              ( 3u<< 6)     // SEL_TIMO_MB, 0:64us; 1:128us; 2:256us; 3:512us; ~ 6:4096us;
+//                      );
 
-        // write_reg1
-        tdc_spi_wr_d32(0x81, DFLT_WREG1 |
-                              ( 0u<< 12)   // DELVAL1=0us, format:16.5, time unit:Tref;
-                      );
+//        // write_reg1
+//        tdc_spi_wr_d32(0x81, DFLT_WREG1 |
+//                              ( 0u<< 12)   // DELVAL1=0us, format:16.5, time unit:Tref;
+//                      );
 
         // write_reg3
         tdc_spi_wr_d32(0x83, DFLT_WREG3 |
@@ -411,7 +398,7 @@ void tdc_start_temp(uint32_t cfg)
     }
 //rt_exit_critical();
 }
-
+uint8_t temp_error_flag=0;
 void tdc_read_temp(uint32_t cfg)
 {
 
@@ -421,16 +408,17 @@ void tdc_read_temp(uint32_t cfg)
     }
 
     TdcStatus = tdc_spi_rd_d16(0xd0);   // status
-
+    
     TdcPT[0] = tdc_spi_rd_d32(0xd1);  // REF 5K +/- 5
     TdcPT[1] = tdc_spi_rd_d32(0xd2);  // U7
 
-    if(TdcPT[0]==0xffffffff || TdcPT[1]==0xffffffff || TdcPT[0] ==0x00000000 || TdcPT[0]==0x00000000)
+    if(TdcPT[0]==0xffffffff || TdcPT[1]==0xffffffff || TdcPT[0] ==0x00000000 || TdcPT[1]==0x00000000)
     {
-        TdcResist=0;
+        temp_error_flag=1;
     }
     else
     {
+     temp_error_flag=0;   
      TdcResist = 2500.0f*TdcPT[1]/TdcPT[0];  // ohmX10
      Ohm2Dgr2USSpd(TdcResist, WaterPrmt);        // [0]:Temperature; [1]:ultrasonic speed @Temp
     }    
@@ -442,3 +430,39 @@ void tdc_read_temp(uint32_t cfg)
 
 }
 
+void MS1031_DisableHighSpeedOsc(void)
+{
+   PeriClk_MutEnable(PeriClk_Spi1);
+   DMA_ReqClkOn();
+    
+   tdc_spi_wr_d32(0x80, DFLT_WREG0 & ~(0x07 << 14));   //一旦为空管马上关闭高速晶振,降低功耗
+    
+   DMA_ReqClkOff();
+   PeriClk_MutDisable(PeriClk_Spi1);
+
+}
+
+void MS1031_EnableHighSpeedOsc(void)
+{
+  PeriClk_MutEnable(PeriClk_Spi1);
+  DMA_ReqClkOn();
+    
+    GpioClrPins(GpioPortD, GpioBitMsk2);   
+    us_delay(10);      
+    GpioSetPins(GpioPortD, GpioBitMsk2);   
+    us_delay(10);       
+        
+    tdc_spi_wr_d32(0x80,DFLT_WREG0);
+    tdc_spi_wr_d32(0x81,DFLT_WREG1);
+    tdc_spi_wr_d32(0x82,DFLT_WREG2);
+    tdc_spi_wr_d32(0x83,DFLT_WREG3);
+    tdc_spi_wr_d32(0x84,DFLT_WREG4);
+    tdc_spi_wr_d32(0x85,DFLT_WREG5);
+    tdc_spi_wr_d32(0x86,DFLT_WREG6);
+    tdc_spi_wr_d32(0x87,0x20000000);//!!!!!!!!do not delete!!!!!!!!!
+
+    
+ DMA_ReqClkOff();
+ PeriClk_MutDisable(PeriClk_Spi1);
+    
+}

+ 226 - 210
Users/tdc_tof/tof_isr.c

@@ -1,6 +1,7 @@
 
 #include "board.h"
 #include "tdc_tof.h"
+#include "alarm.h"
 
 #define TOF_PWFW_DIFF ((uint16_t)(0x1620u * 25 / 100))
 #define TOF_PWFW_LOW ((uint16_t)(0x1620u * 40 / 100))
@@ -228,47 +229,15 @@ void DiffToF2Flow(float dtof)
     pTdcItf->flow_vol(vol, TmpFloat);
 }
 
-void tof_isr_convert_tof_to_flow(void)
+void tdoa_filter_convert(void)
 {
+    
     int16_t Tmp16;
     float TmpFloat;
     uint32_t Tu32;
     uint32_t ch = get_mux_ch();
-
-    switch (TdcIntStep)
-    {
-
-    case (0x01u):      
-
-        // 根据RTC_CntInt设置校准、采集温度,读取飞行时间
-//        if ((RTC_CntInt & RTC_CNTMSK_CAL) == 0x0u)
-//        {
-//            // 间隔8秒进行一次校准
-//            tdc_read_tof(TDC_CFG_DO_CKENA);
-//            tdc_start_cal(TDC_CFG_DO_CKENA|TDC_CFG_DO_WREG | TDC_CFG_DO_CKDIS);
-//            if (tof_flag & TOF_FLAG_UNALGND_CHOP)
-//            {
-//                TdcIntStep = 0x03u;
-//            }
-//            else
-//            {
-//                TdcIntStep = 0x02u;
-//            }
-//        }
-//          if ((RTC_CntInt & RTC_CNTMSK_TEMP) == 0x4u)
-//        {
-//            // 间隔4秒采集一次温度
-//            tdc_read_tof(TDC_CFG_DO_CKENA);
-//            tdc_start_temp(TDC_CFG_DO_CKENA|TDC_CFG_DO_WREG | TDC_CFG_DO_CKDIS);
-//            TdcIntStep = 0x04u;
-//        }
-//        else
-//        {
-            // 读取飞行时间
-//            tdc_read_tof(TDC_CFG_DO_CKENA | TDC_CFG_DO_CKDIS);
-//            TdcIntStep = 0xFFu;
-//        }
-
+//        LOG("a\n");
+//        LOG("%x\n",TdcStatus);
         //            LOG("ch-%d-1-U:%.6f-D:%.6f\n\n", ch, TdcScale*TdcUToF[0],TdcScale*TdcDToF[0]);
         //            if(Tu32 == TDC_CFG_FIRE_UP)
         //            {
@@ -278,189 +247,236 @@ void tof_isr_convert_tof_to_flow(void)
         //            {
         //                LOG("ch-%d-1-D, %.6f\n\n", ch, TdcScale*TdcDToF[0]);
         //            }
-
         // 清反向计量标识
-//        pTdcItf->bw_flow_flag = 0;
-//        // 空管
-//        if (tof_flag & TOF_FLAG_BLANK_PIPE)
-//        {
-//            // 设置对应德通道为空管
-//            pTdcItf->set_blank(ch);
-
-//            // DiffTof from normal channel
-//            // 正常的通道进行计量
-//            if (pTdcItf->ch_bit_map ^ TdcItf.blank_flag)
-//            {
-//                DiffToF2Flow(DiffToF);
-//                SERPLOT_PRINT_4FLOAT_1PREAMBLE(DiffToF, UToFAvg, DToFAvg, CumFlow);
-//            }
-//            break;
-//        }
-//        else
-//        {
-//            // 指定通道清除空管
-//            pTdcItf->clr_blank(ch);
-//        }
-
-//        // 计算脉冲宽度
-//        Tmp16 = TdcUPwFw - TdcDPwFw;
-//        // 判断脉冲宽度有效性
-//        if (Tmp16 < (0 - TOF_PWFW_DIFF) || Tmp16 > TOF_PWFW_DIFF)
-//        {
-//            // 无效脉冲宽度
-//            tof_flag |= TOF_FLAG_UNPAIR_WAVE;
-//            CntErrEvent++;
-//        }
-//        else
-//        {
-//            // 有效脉冲宽度
-//            tof_flag &= ~TOF_FLAG_UNPAIR_WAVE;
-//            // 记录UP和DOWM平均飞行时间
-//            UToFAvg = TdcScale * TdcUToF[0];
-//            DToFAvg = TdcScale * TdcDToF[0];
-//        }
-
-//        // 判断offset有效性
-//        if (TdcFwOfst < TOF_OFST_LOW || TdcFwOfst > TOF_OFST_HIGH)
-//        {
-//            // out of range
-//            TdcFwOfst = (TOF_OFST_LOW + TOF_OFST_HIGH) / 2; // default offset
-//        }
-//        else if (tof_flag & TOF_FLAG_UNPAIR_WAVE)
-//        {
-//            // first wave for up and down is different
-//            TdcFwOfst += (TdcFwOfst <= (TOF_OFST_LOW + 3)) ? 2 : -2;
-//        }
-//        else if (TdcUPwFw < TOF_PWFW_LOW && TdcDPwFw < TOF_PWFW_LOW && TdcFwOfst > TOF_OFST_LOW)
-//        {
-//            TdcFwOfst -= 1;
-//        }
-//        else if (TdcUPwFw > TOF_PWFW_HIGH && TdcDPwFw > TOF_PWFW_HIGH && TdcFwOfst < TOF_OFST_HIGH)
-//        {
-//            TdcFwOfst += 1;
-//        }
-
-//        // 本次UP和DOWN飞行时间求和
-//        SumToF = UToFAvg + DToFAvg; // alomost fixed regardless of water speed
-//        // 本次飞行时间和与上次飞行时间和德差值在50内认为有效
-//        if (fabsf(SumToF - LastSumToF) < 50.0f)
-//        {
-//            // 计算飞行时间
-//            DiffToF = UToFAvg - DToFAvg - pTdcItf->path_skew[ch];
-//            // add by yuewei 20260325 低通平滑滤波
-//            // DiffToF = DiffToF * DIFF_TOFF_FILTER_ALPHA + (1 - DIFF_TOFF_FILTER_ALPHA) * DiffToF_LAST;
-
-//            // DiffToF = tof_filter(DiffToF);//20260429
-//            DiffToF = tof_filter_input_poll(ch, DiffToF);
-//            // LOG("ch%d, U=%.6f, D=%.6f\n", ch, UToFAvg, DToFAvg);
-//            // LOG("int_cnt=%d, step=%d, ch=%d, DiffToF=%.6f\n\n", int_cnt, get_tdc_int_step(), ch, DiffToF);
-//        }
-//        // 本次飞行时间和赋值给上次飞行时间和
-//        LastSumToF = SumToF;
-
-//        if (tof_state & (TOF_ST_METER | TOF_ST_CALIB))
-//        {
-//            DiffToFAvg += (DiffToF - DiffToFAvg) * (1.0f / 6);
-//            DiffToFAvg2 += (DiffToF - DiffToFAvg2) * (1.0f / 2);
-//            DiffToFAvg128 += (DiffToF - DiffToFAvg128) * (1.0f / 128);
-//            if (tof_flag & TOF_FLAG_VALID_FLOW)
-//            {
-//                if (fabsf(DiffToFAvg) < 0.35f)
-//                {
-//                    tof_flag &= ~TOF_FLAG_VALID_FLOW;
-//                    if (tof_state & TOF_ST_CALIB && CalibTime > CALIB_TIME_LTH)
-//                    {
-//                        tof_flag |= TOF_FLAG_CALIB_DONE;
-//                    }
-//                }
-//            }
-//            else
-//            {
-
-//                // modified by yuewei 20260519,加入n值,始动流量
-//                // if (fabsf(DiffToFAvg) > 0.45f || fabsf(DiffToFAvg2) > 1.0f)
-//                if (fabsf(DiffToFAvg * 100.00f) > Paramx.n || fabsf(DiffToFAvg2) > 1.0f)
-//                {
-//                    tof_flag |= TOF_FLAG_VALID_FLOW;
-//                    if (tof_state & TOF_ST_CALIB)
-//                    {
-//                        tof_flag |= TOF_FLAG_CALIB_FLOW;
-//                        CumFlow = 0;
-//                        CalibTime = 0;
-//                        CalibSpdLvl = 0;
-//                        CalibTemp = 0;
-//                    }
-//                }
-//            }
-
-//            // 是有效德流量
-//            if (tof_flag & TOF_FLAG_VALID_FLOW)
-//            {
-//                TmpFloat = DiffToF * UsonicSpd * UsonicSpd * 0.00001f;
-
-//                // 一次校准模式
-//                if (tof_state & TOF_ST_CALIB)
-//                {
-//                    CumFlow += TmpFloat * pTdcItf->pipe_coe;
-//                    pTdcItf->check_flow(CumFlow, TmpFloat, WaterTemp);
-//                    if (tof_flag & TOF_FLAG_CALIB_FLOW)
-//                    {
-//                        if (DiffToFAvg2 > 0.5f)
-//                        {
-//                            CalibSpdLvl += TmpFloat;
-//                            CalibTemp += WaterTemp;
-//                            CalibTime++;
-//                        }
-//                        else
-//                        {
-//                            tof_flag &= ~TOF_FLAG_CALIB_FLOW;
-//                            if (CalibTime > CALIB_TIME_LTH)
-//                            {
-//                                CalibSpdLvl /= CalibTime;
-//                                CalibTemp /= CalibTime;
-//                                tof_flag |= TOF_FLAG_CALIB_FALL;
-//                            }
-//                        }
-//                    }
-//                }
-//                else
-//                {
-//                    // 正常计量模式
-//                    DiffToF2Flow(DiffToF);
-//                    // DiffToF2Flow(DiffToFAvg);
-//                }
-//            }
-//            else
-//            {
-//                FlowVol = 0; // 始动流量切除时,瞬时流量清零yw2026-6-25
-//            }
-//            // 串口打印,功能未开启
-//            // SERPLOT_PRINT_4FLOAT_1PREAMBLE(DiffToF, UToFAvg, DToFAvg, DiffToFAvg);
-//            SERPLOT_PRINT_4FLOAT_1PREAMBLE(DiffToF, DiffToF * UsonicSpd * UsonicSpd * 0.00001f, DToFAvg, DiffToFAvg);
-//        }
-//        break;
-
-//    case (0x02u):
-//        tdc_read_cal(TDC_CFG_DO_CKENA);
-//        tdc_start_cal(TDC_CFG_DO_CKDIS);
-//        TdcIntStep = 0x03u;
-//        break;
-
-//    case (0x03u):
-//        tdc_read_cal(TDC_CFG_DO_CKENA | TDC_CFG_DO_CKDIS);
-//        TdcIntStep = 0xFFu;
-//        break;
-
-    case (0x04u):
+        pTdcItf->bw_flow_flag = 0;
+        // 空管
+        if (tof_flag & TOF_FLAG_BLANK_PIPE)
+        {
+            // 设置对应德通道为空管
+            pTdcItf->set_blank(ch);
+            // DiffTof from normal channel
+            // 正常的通道进行计量
+            if (pTdcItf->ch_bit_map ^ TdcItf.blank_flag)
+            {
+                DiffToF2Flow(DiffToF);
+                SERPLOT_PRINT_4FLOAT_1PREAMBLE(DiffToF, UToFAvg, DToFAvg, CumFlow);
+            }
+           return;
+        }
+        else
+        {
+            // 指定通道清除空管
+            pTdcItf->clr_blank(ch);
+        }
+
+        // 计算脉冲宽度
+        Tmp16 = TdcUPwFw - TdcDPwFw;
+        // 判断脉冲宽度有效性
+        if (Tmp16 < (0 - TOF_PWFW_DIFF) || Tmp16 > TOF_PWFW_DIFF)
+        {
+            // 无效脉冲宽度
+            tof_flag |= TOF_FLAG_UNPAIR_WAVE;
+            CntErrEvent++;
+        }
+        else
+        {
+            // 有效脉冲宽度
+            tof_flag &= ~TOF_FLAG_UNPAIR_WAVE;
+            // 记录UP和DOWM平均飞行时间
+            UToFAvg = TdcScale * TdcUToF[0];
+            DToFAvg = TdcScale * TdcDToF[0];
+        }
+
+        // 判断offset有效性
+        if (TdcFwOfst < TOF_OFST_LOW || TdcFwOfst > TOF_OFST_HIGH)
+        {
+            // out of range
+            TdcFwOfst = (TOF_OFST_LOW + TOF_OFST_HIGH) / 2; // default offset
+        }
+        else if (tof_flag & TOF_FLAG_UNPAIR_WAVE)
+        {
+            // first wave for up and down is different
+            TdcFwOfst += (TdcFwOfst <= (TOF_OFST_LOW + 3)) ? 2 : -2;
+        }
+        else if (TdcUPwFw < TOF_PWFW_LOW && TdcDPwFw < TOF_PWFW_LOW && TdcFwOfst > TOF_OFST_LOW)
+        {
+            TdcFwOfst -= 1;
+        }
+        else if (TdcUPwFw > TOF_PWFW_HIGH && TdcDPwFw > TOF_PWFW_HIGH && TdcFwOfst < TOF_OFST_HIGH)
+        {
+            TdcFwOfst += 1;
+        }
+
+        // 本次UP和DOWN飞行时间求和
+        SumToF = UToFAvg + DToFAvg; // alomost fixed regardless of water speed
+        // 本次飞行时间和与上次飞行时间和德差值在50内认为有效
+        if (fabsf(SumToF - LastSumToF) < 50.0f)
+        {
+            // 计算飞行时间
+            DiffToF = UToFAvg - DToFAvg - pTdcItf->path_skew[ch];
+            // add by yuewei 20260325 低通平滑滤波
+            // DiffToF = DiffToF * DIFF_TOFF_FILTER_ALPHA + (1 - DIFF_TOFF_FILTER_ALPHA) * DiffToF_LAST;
+
+            // DiffToF = tof_filter(DiffToF);//20260429
+            DiffToF = tof_filter_input_poll(ch, DiffToF);
+            // LOG("ch%d, U=%.6f, D=%.6f\n", ch, UToFAvg, DToFAvg);
+            // LOG("int_cnt=%d, step=%d, ch=%d, DiffToF=%.6f\n\n", int_cnt, get_tdc_int_step(), ch, DiffToF);
+        }
+        // 本次飞行时间和赋值给上次飞行时间和
+        LastSumToF = SumToF;
+
+        if (tof_state & (TOF_ST_METER | TOF_ST_CALIB))
+        {
+            DiffToFAvg += (DiffToF - DiffToFAvg) * (1.0f / 6);
+            DiffToFAvg2 += (DiffToF - DiffToFAvg2) * (1.0f / 2);
+            DiffToFAvg128 += (DiffToF - DiffToFAvg128) * (1.0f / 128);
+            if (tof_flag & TOF_FLAG_VALID_FLOW)
+            {
+                if (fabsf(DiffToFAvg) < 0.35f)
+                {
+                    tof_flag &= ~TOF_FLAG_VALID_FLOW;
+                    if (tof_state & TOF_ST_CALIB && CalibTime > CALIB_TIME_LTH)
+                    {
+                        tof_flag |= TOF_FLAG_CALIB_DONE;
+                    }
+                }
+            }
+            else
+            {
+
+                // modified by yuewei 20260519,加入n值,始动流量
+                // if (fabsf(DiffToFAvg) > 0.45f || fabsf(DiffToFAvg2) > 1.0f)
+                if (fabsf(DiffToFAvg * 100.00f) > Paramx.n || fabsf(DiffToFAvg2) > 1.0f)
+                {
+                    tof_flag |= TOF_FLAG_VALID_FLOW;
+                    if (tof_state & TOF_ST_CALIB)
+                    {
+                        tof_flag |= TOF_FLAG_CALIB_FLOW;
+                        CumFlow = 0;
+                        CalibTime = 0;
+                        CalibSpdLvl = 0;
+                        CalibTemp = 0;
+                    }
+                }
+            }
+
+            // 是有效的流量
+            if (tof_flag & TOF_FLAG_VALID_FLOW)
+            {
+                TmpFloat = DiffToF * UsonicSpd * UsonicSpd * 0.00001f;
+
+                // 一次校准模式
+                if (tof_state & TOF_ST_CALIB)
+                {
+                    CumFlow += TmpFloat * pTdcItf->pipe_coe;
+                    pTdcItf->check_flow(CumFlow, TmpFloat, WaterTemp);
+                    if (tof_flag & TOF_FLAG_CALIB_FLOW)
+                    {
+                        if (DiffToFAvg2 > 0.5f)
+                        {
+                            CalibSpdLvl += TmpFloat;
+                            CalibTemp += WaterTemp;
+                            CalibTime++;
+                        }
+                        else
+                        {
+                            tof_flag &= ~TOF_FLAG_CALIB_FLOW;
+                            if (CalibTime > CALIB_TIME_LTH)
+                            {
+                                CalibSpdLvl /= CalibTime;
+                                CalibTemp /= CalibTime;
+                                tof_flag |= TOF_FLAG_CALIB_FALL;
+                            }
+                        }
+                    }
+                }
+                else
+                {
+                    // 正常计量模式
+                    DiffToF2Flow(DiffToF);
+                    // DiffToF2Flow(DiffToFAvg);
+                }
+            }
+            else
+            {
+                FlowVol = 0; // 始动流量切除时,瞬时流量清零yw2026-6-25
+            }
+            // 串口打印,功能未开启
+            // SERPLOT_PRINT_4FLOAT_1PREAMBLE(DiffToF, UToFAvg, DToFAvg, DiffToFAvg);
+            SERPLOT_PRINT_4FLOAT_1PREAMBLE(DiffToF, DiffToF * UsonicSpd * UsonicSpd * 0.00001f, DToFAvg, DiffToFAvg);
+        }
+        
+}
+
+
+
+void tof_isr_convert_tof_to_flow(void)
+{
+
+    switch (TdcIntStep)
+    {
+    case (0x01u):      
+        // 根据RTC_CntInt设置校准、采集温度,读取飞行时间
+        if ((RTC_CntInt & RTC_CNTMSK_CAL) == 0x0u)
+        {
+            // 间隔8秒进行一次校准
+            tdc_start_cal(TDC_CFG_DO_CKENA|TDC_CFG_DO_WREG | TDC_CFG_DO_CKDIS);
+            if (tof_flag & TOF_FLAG_UNALGND_CHOP)
+            {
+                TdcIntStep = 0x03u;
+            }
+            else
+            {
+                TdcIntStep = 0x02u;
+            }
+        }
+        else if ((RTC_CntInt & RTC_CNTMSK_TEMP) == 0x4u)
+        {
+//            // 间隔4秒采集一次温度
+            tdc_start_temp(TDC_CFG_DO_CKENA|TDC_CFG_DO_WREG | TDC_CFG_DO_CKDIS);
+            TdcIntStep = 0x04u;
+        }
+        else
+        {
+            // 读取飞行时间
+            tdc_start_tof(TDC_CFG_DO_CKENA | TDC_CFG_DO_CKDIS | TDC_CFG_DO_WREG);
+            TdcIntStep = 0x05u;
+        }
+        break;
         
+    case (0x02u):
+        tdc_read_cal(TDC_CFG_DO_CKENA);
+        tdc_start_cal(TDC_CFG_DO_CKDIS);
+        TdcIntStep = 0x03u;
+        break;
+
+    case (0x03u):
+        tdc_read_cal(TDC_CFG_DO_CKENA | TDC_CFG_DO_CKDIS);
+        tdc_start_tof(TDC_CFG_DO_CKENA | TDC_CFG_DO_CKDIS | TDC_CFG_DO_WREG);
+        TdcIntStep = 0x05u;
+        break;
+
+    case (0x04u):     
         tdc_read_temp(TDC_CFG_DO_CKENA | TDC_CFG_DO_CKDIS);
+        tdc_start_tof(TDC_CFG_DO_CKENA | TDC_CFG_DO_CKDIS | TDC_CFG_DO_WREG);
+        TdcIntStep = 0x05u;
+        break;
+    
+    case (0x05u): 
+        tdc_read_tof(TDC_CFG_DO_CKENA | TDC_CFG_DO_CKDIS);
+        tdoa_filter_convert();
         TdcIntStep = 0xFFu;
         break;
-
+    
     case (0x80u):
         TdcIntStep = 0xFFu;
+         break;
+    
+    default:  
+         break;
     }
 }
+
 uint8_t temp_flag=0;
 void PORTD_F_IRQHandler(void)
 {