diff --git a/main.c b/main.c index 16db06f..9497dc9 100644 --- a/main.c +++ b/main.c @@ -44,6 +44,10 @@ typedef enum { AppMode CurrentMode = MODE_IDLE; float LFOSCFreq = 32000.0; +// Global Indices for Data Reporting (Reset on new run) +uint32_t g_AmpIndex = 0; +uint32_t g_RampIndex = 0; + // --------------------------------------------------------------------------- // Range / RTIA Management // --------------------------------------------------------------------------- @@ -168,6 +172,10 @@ void AD5940ImpedanceStructInit(void) AppIMPCfg_Type *pImpedanceCfg; AppIMPGetCfg(&pImpedanceCfg); + // --- CRITICAL: Force re-initialization of sequences --- + pImpedanceCfg->IMPInited = bFALSE; + // ------------------------------------------------------ + pImpedanceCfg->SeqStartAddr = 0; pImpedanceCfg->MaxSeqLen = 512; pImpedanceCfg->RcalVal = 100.0; @@ -199,6 +207,11 @@ void AD5940AMPStructInit(void) { AppAMPCfg_Type *pAMPCfg; AppAMPGetCfg(&pAMPCfg); + + // --- CRITICAL: Force re-initialization of sequences --- + pAMPCfg->AMPInited = bFALSE; + // ------------------------------------------------------ + pAMPCfg->WuptClkFreq = LFOSCFreq; pAMPCfg->SeqStartAddr = 0; pAMPCfg->MaxSeqLen = 512; @@ -219,6 +232,10 @@ void AD5940RampStructInit(void) { AppRAMPCfg_Type *pRampCfg; AppRAMPGetCfg(&pRampCfg); + + // --- CRITICAL: Force re-initialization of sequences --- + pRampCfg->RAMPInited = bFALSE; + // ------------------------------------------------------ pRampCfg->SeqStartAddr = 0; pRampCfg->MaxSeqLen = 1024; @@ -295,6 +312,7 @@ static int32_t AD5940PlatformCfg(void) * Call this after a measurement sequence finishes to park the device in a clean state. */ void SystemReset(void) { + sleep_ms(100); // Allow printf buffers to flush AD5940_SoftReset(); AD5940PlatformCfg(); } @@ -318,19 +336,17 @@ void ImpedanceShowResult(uint32_t *pData, uint32_t DataCount) void AmperometricShowResult(float *pData, uint32_t DataCount) { - static int index = 0; for(int i=0;i> Calibrating LFOSC...\n"); + // Reset before calibration to ensure clean state + SystemReset(); + LFOSCMeasure_Type cal_cfg; cal_cfg.CalDuration = 1000.0; cal_cfg.CalSeqAddr = 0; @@ -352,12 +371,14 @@ void Routine_CalibrateLFO(void) { printf(">> LFOSC Calibration Failed.\n"); } - SystemReset(); // Clean up after calibration + // NOTE: We do NOT reset here. We want to keep the state for the next step. } void Routine_Measure(float freq) { CurrentMode = MODE_IMPEDANCE; + // NO RESET HERE. We assume the device is ready (possibly calibrated). + AppIMPCfg_Type *pCfg; AppIMPGetCfg(&pCfg); @@ -380,6 +401,8 @@ void Routine_Measure(float freq) { void Routine_Sweep(float start, float end, int steps) { CurrentMode = MODE_IMPEDANCE; + // NO RESET HERE. We assume the device is ready (possibly calibrated). + AppIMPCfg_Type *pCfg; AppIMPGetCfg(&pCfg); @@ -404,6 +427,9 @@ void Routine_Sweep(float start, float end, int steps) { void Routine_Amperometric(float bias_mv) { CurrentMode = MODE_AMPEROMETRIC; + g_AmpIndex = 0; // Reset Index + + // NO RESET HERE. printf(">> Starting Amperometry (Bias: %.1f mV, LP Range: %d)...\n", bias_mv, ConfigLptiaVal); @@ -422,6 +448,9 @@ void Routine_Amperometric(float bias_mv) { void Routine_LSV(float start_mv, float end_mv, int steps, int duration_ms) { CurrentMode = MODE_RAMP; + g_RampIndex = 0; // Reset Index + + // NO RESET HERE. printf(">> Starting LSV (%.1f to %.1f mV, %d steps, %d ms, LP Range: %d)...\n", start_mv, end_mv, steps, duration_ms, ConfigLptiaVal); @@ -446,6 +475,9 @@ void Routine_LSV(float start_mv, float end_mv, int steps, int duration_ms) { void Routine_CalibrateSystem(void) { CurrentMode = MODE_IMPEDANCE; + // NO RESET HERE. We want to keep the state from LFO calibration (if any) + // and definitely NOT reset after we are done. + AppIMPCfg_Type *pCfg; AppIMPGetCfg(&pCfg); @@ -522,7 +554,10 @@ void Routine_CalibrateSystem(void) { printf("HSTIA Calibration Failed\n"); } - SystemReset(); // Clean up after calibration + // NOTE: NO RESET HERE. We must preserve the calibration registers. + // Explicitly disable sequencer and interrupts to leave a clean state for the next routine + AD5940_SEQCtrlS(bFALSE); + AD5940_INTCClrFlag(AFEINTSRC_ALLINT); } // --------------------------------------------------------------------------- @@ -599,10 +634,11 @@ void process_command() { else if (CurrentMode == MODE_RAMP) AppRAMPCtrl(APPCTRL_SHUTDOWN, 0); CurrentMode = MODE_IDLE; printf("STOPPED\n"); - SystemReset(); // Reset here + SystemReset(); // Reset here to clean up } else if (cmd == 'z') { - watchdog_reboot(0, 0, 0); + // Explicit Reset Command + SystemReset(); } } @@ -677,7 +713,7 @@ int main() { else if (CurrentMode == MODE_AMPEROMETRIC) AppAMPCtrl(AMPCTRL_SHUTDOWN, 0); else if (CurrentMode == MODE_RAMP) AppRAMPCtrl(APPCTRL_SHUTDOWN, 0); CurrentMode = MODE_IDLE; - SystemReset(); // Reset here + SystemReset(); // Reset here to clean up } } }