diff --git a/Impedance.c b/Impedance.c index a3a66f2..e6d02b4 100644 --- a/Impedance.c +++ b/Impedance.c @@ -81,22 +81,30 @@ int32_t AppIMPCtrl(uint32_t Command, void *pPara) { case IMPCTRL_START: { - WUPTCfg_Type wupt_cfg; - if(AD5940_WakeUp(10) > 10) return AD5940ERR_WAKEUP; - if(AppIMPCfg.IMPInited == bFALSE) return AD5940ERR_APPERROR; - - wupt_cfg.WuptEn = bTRUE; - wupt_cfg.WuptEndSeq = WUPTENDSEQ_A; - wupt_cfg.WuptOrder[0] = SEQID_0; - wupt_cfg.SeqxSleepTime[SEQID_0] = 4; - - // Calculate Wakeup Time based on ODR - // 32kHz clock. For 20Hz: 32000/20 = 1600 ticks. - uint32_t wait_cycles = (uint32_t)(AppIMPCfg.WuptClkFreq/AppIMPCfg.ImpODR); - if(wait_cycles < 20) wait_cycles = 20; // Minimum safety guard - wupt_cfg.SeqxWakeupTime[SEQID_0] = wait_cycles - 4; - - AD5940_WUPTCfg(&wupt_cfg); + // CRITICAL CHANGE: Use Manual Triggering for Sweep to avoid timing hazards + if(AppIMPCfg.SweepCfg.SweepEn) + { + // Trigger first sequence manually + AD5940_SEQMmrTrig(SEQID_0); + } + else + { + // Use WUPT for Continuous Measure (fixed frequency) + WUPTCfg_Type wupt_cfg; + if(AD5940_WakeUp(10) > 10) return AD5940ERR_WAKEUP; + if(AppIMPCfg.IMPInited == bFALSE) return AD5940ERR_APPERROR; + + wupt_cfg.WuptEn = bTRUE; + wupt_cfg.WuptEndSeq = WUPTENDSEQ_A; + wupt_cfg.WuptOrder[0] = SEQID_0; + wupt_cfg.SeqxSleepTime[SEQID_0] = 4; + + uint32_t wait_cycles = (uint32_t)(AppIMPCfg.WuptClkFreq/AppIMPCfg.ImpODR); + if(wait_cycles < 20) wait_cycles = 20; + wupt_cfg.SeqxWakeupTime[SEQID_0] = wait_cycles - 4; + + AD5940_WUPTCfg(&wupt_cfg); + } AppIMPCfg.FifoDataCount = 0; break; @@ -424,6 +432,9 @@ AD5940Err AppIMPCheckFreq(float freq) // Update Wait Times for all 3 measurements for (int i = 0; i < 3; i++) { + // Safety check: Ensure address is valid + if (AppIMPCfg.SeqWaitAddr[i] == 0) continue; + SRAMAddr = AppIMPCfg.MeasureSeqInfo.SeqRamAddr + AppIMPCfg.SeqWaitAddr[i]; // CRITICAL FIX: Double the wait time for High Power Mode (>=80kHz) @@ -633,9 +644,18 @@ int32_t AppIMPISR(void *pBuff, uint32_t *pCount) AD5940_FIFORd((uint32_t *)pBuff, FifoCnt); AD5940_INTCClrFlag(AFEINTSRC_DATAFIFOTHRESH); - // Check if sweep is done - if (AppIMPRegModify(pBuff, &FifoCnt) != AD5940ERR_OK) { - // If sweep is done, we might want to signal main loop + // Update Frequency + if (AppIMPRegModify(pBuff, &FifoCnt) == AD5940ERR_OK) + { + // CRITICAL: Manual Trigger Chain for Sweep + if(AppIMPCfg.SweepCfg.SweepEn) + { + // Only trigger next if we haven't reached the count + if(AppIMPCfg.FifoDataCount < AppIMPCfg.NumOfData) + { + AD5940_SEQMmrTrig(SEQID_0); + } + } } AD5940_SleepKeyCtrlS(SLPKEY_UNLOCK); diff --git a/main.c b/main.c index 0e37c06..2fd5105 100644 --- a/main.c +++ b/main.c @@ -165,12 +165,19 @@ void ImpedanceShowResult(uint32_t *pData, uint32_t DataCount) for(int i=0;i> Calibrating HSTIARTIA_200...\n"); if (AD5940_HSRtiaCal(&hsrtia_cal, &Res) == AD5940ERR_OK) { printf("Calibrated Rtia: Mag = %f Ohm, Phase = %f\n", Res.Magnitude, Res.Phase); - // Optional: Update global config - // pCfg->RtiaVal = Res.Magnitude; + // Update global config with actual calibrated value to fix scaling error + pCfg->RtiaVal = Res.Magnitude; } else { printf("Calibration Failed\n"); } @@ -363,14 +370,6 @@ int main() { AppIMPISR(AppBuff, &temp); if(temp > 0) { ImpedanceShowResult(AppBuff, temp); - - // Check if sweep is done (simple check based on config) - AppIMPCfg_Type *pCfg; - AppIMPGetCfg(&pCfg); - if (pCfg->SweepCfg.SweepEn && pCfg->FifoDataCount >= pCfg->NumOfData) { - printf("DONE\n"); - AD5940_Main_Cleanup(); // Ensure clean stop - } } } }