i broke it but good broke it

This commit is contained in:
pszsh 2026-02-03 03:54:27 -08:00
parent 6fc616593b
commit b1427c57c8
1 changed files with 45 additions and 9 deletions

54
main.c
View File

@ -44,6 +44,10 @@ typedef enum {
AppMode CurrentMode = MODE_IDLE; AppMode CurrentMode = MODE_IDLE;
float LFOSCFreq = 32000.0; 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 // Range / RTIA Management
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -168,6 +172,10 @@ void AD5940ImpedanceStructInit(void)
AppIMPCfg_Type *pImpedanceCfg; AppIMPCfg_Type *pImpedanceCfg;
AppIMPGetCfg(&pImpedanceCfg); AppIMPGetCfg(&pImpedanceCfg);
// --- CRITICAL: Force re-initialization of sequences ---
pImpedanceCfg->IMPInited = bFALSE;
// ------------------------------------------------------
pImpedanceCfg->SeqStartAddr = 0; pImpedanceCfg->SeqStartAddr = 0;
pImpedanceCfg->MaxSeqLen = 512; pImpedanceCfg->MaxSeqLen = 512;
pImpedanceCfg->RcalVal = 100.0; pImpedanceCfg->RcalVal = 100.0;
@ -199,6 +207,11 @@ void AD5940AMPStructInit(void)
{ {
AppAMPCfg_Type *pAMPCfg; AppAMPCfg_Type *pAMPCfg;
AppAMPGetCfg(&pAMPCfg); AppAMPGetCfg(&pAMPCfg);
// --- CRITICAL: Force re-initialization of sequences ---
pAMPCfg->AMPInited = bFALSE;
// ------------------------------------------------------
pAMPCfg->WuptClkFreq = LFOSCFreq; pAMPCfg->WuptClkFreq = LFOSCFreq;
pAMPCfg->SeqStartAddr = 0; pAMPCfg->SeqStartAddr = 0;
pAMPCfg->MaxSeqLen = 512; pAMPCfg->MaxSeqLen = 512;
@ -220,6 +233,10 @@ void AD5940RampStructInit(void)
AppRAMPCfg_Type *pRampCfg; AppRAMPCfg_Type *pRampCfg;
AppRAMPGetCfg(&pRampCfg); AppRAMPGetCfg(&pRampCfg);
// --- CRITICAL: Force re-initialization of sequences ---
pRampCfg->RAMPInited = bFALSE;
// ------------------------------------------------------
pRampCfg->SeqStartAddr = 0; pRampCfg->SeqStartAddr = 0;
pRampCfg->MaxSeqLen = 1024; pRampCfg->MaxSeqLen = 1024;
pRampCfg->RcalVal = 100.0; pRampCfg->RcalVal = 100.0;
@ -295,6 +312,7 @@ static int32_t AD5940PlatformCfg(void)
* Call this after a measurement sequence finishes to park the device in a clean state. * Call this after a measurement sequence finishes to park the device in a clean state.
*/ */
void SystemReset(void) { void SystemReset(void) {
sleep_ms(100); // Allow printf buffers to flush
AD5940_SoftReset(); AD5940_SoftReset();
AD5940PlatformCfg(); AD5940PlatformCfg();
} }
@ -318,19 +336,17 @@ void ImpedanceShowResult(uint32_t *pData, uint32_t DataCount)
void AmperometricShowResult(float *pData, uint32_t DataCount) void AmperometricShowResult(float *pData, uint32_t DataCount)
{ {
static int index = 0;
for(int i=0;i<DataCount;i++) for(int i=0;i<DataCount;i++)
{ {
printf("AMP,%d,%.4f\n", index++, pData[i]); printf("AMP,%d,%.4f\n", g_AmpIndex++, pData[i]);
} }
} }
void RampShowResult(float *pData, uint32_t DataCount) void RampShowResult(float *pData, uint32_t DataCount)
{ {
static int index = 0;
for(int i=0;i<DataCount;i++) for(int i=0;i<DataCount;i++)
{ {
printf("RAMP,%d,%.4f\n", index++, pData[i]); printf("RAMP,%d,%.4f\n", g_RampIndex++, pData[i]);
} }
} }
@ -341,6 +357,9 @@ void RampShowResult(float *pData, uint32_t DataCount)
void Routine_CalibrateLFO(void) { void Routine_CalibrateLFO(void) {
printf(">> Calibrating LFOSC...\n"); printf(">> Calibrating LFOSC...\n");
// Reset before calibration to ensure clean state
SystemReset();
LFOSCMeasure_Type cal_cfg; LFOSCMeasure_Type cal_cfg;
cal_cfg.CalDuration = 1000.0; cal_cfg.CalDuration = 1000.0;
cal_cfg.CalSeqAddr = 0; cal_cfg.CalSeqAddr = 0;
@ -352,12 +371,14 @@ void Routine_CalibrateLFO(void) {
printf(">> LFOSC Calibration Failed.\n"); 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) { void Routine_Measure(float freq) {
CurrentMode = MODE_IMPEDANCE; CurrentMode = MODE_IMPEDANCE;
// NO RESET HERE. We assume the device is ready (possibly calibrated).
AppIMPCfg_Type *pCfg; AppIMPCfg_Type *pCfg;
AppIMPGetCfg(&pCfg); AppIMPGetCfg(&pCfg);
@ -380,6 +401,8 @@ void Routine_Measure(float freq) {
void Routine_Sweep(float start, float end, int steps) { void Routine_Sweep(float start, float end, int steps) {
CurrentMode = MODE_IMPEDANCE; CurrentMode = MODE_IMPEDANCE;
// NO RESET HERE. We assume the device is ready (possibly calibrated).
AppIMPCfg_Type *pCfg; AppIMPCfg_Type *pCfg;
AppIMPGetCfg(&pCfg); AppIMPGetCfg(&pCfg);
@ -404,6 +427,9 @@ void Routine_Sweep(float start, float end, int steps) {
void Routine_Amperometric(float bias_mv) { void Routine_Amperometric(float bias_mv) {
CurrentMode = MODE_AMPEROMETRIC; CurrentMode = MODE_AMPEROMETRIC;
g_AmpIndex = 0; // Reset Index
// NO RESET HERE.
printf(">> Starting Amperometry (Bias: %.1f mV, LP Range: %d)...\n", bias_mv, ConfigLptiaVal); 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) { void Routine_LSV(float start_mv, float end_mv, int steps, int duration_ms) {
CurrentMode = MODE_RAMP; 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); 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) { void Routine_CalibrateSystem(void) {
CurrentMode = MODE_IMPEDANCE; 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; AppIMPCfg_Type *pCfg;
AppIMPGetCfg(&pCfg); AppIMPGetCfg(&pCfg);
@ -522,7 +554,10 @@ void Routine_CalibrateSystem(void) {
printf("HSTIA Calibration Failed\n"); 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); else if (CurrentMode == MODE_RAMP) AppRAMPCtrl(APPCTRL_SHUTDOWN, 0);
CurrentMode = MODE_IDLE; CurrentMode = MODE_IDLE;
printf("STOPPED\n"); printf("STOPPED\n");
SystemReset(); // Reset here SystemReset(); // Reset here to clean up
} }
else if (cmd == 'z') { 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_AMPEROMETRIC) AppAMPCtrl(AMPCTRL_SHUTDOWN, 0);
else if (CurrentMode == MODE_RAMP) AppRAMPCtrl(APPCTRL_SHUTDOWN, 0); else if (CurrentMode == MODE_RAMP) AppRAMPCtrl(APPCTRL_SHUTDOWN, 0);
CurrentMode = MODE_IDLE; CurrentMode = MODE_IDLE;
SystemReset(); // Reset here SystemReset(); // Reset here to clean up
} }
} }
} }