Index: AD5941_interface09302025/ad5941_library_extension.cpp =================================================================== diff -u -r3c0eedb1a41ef5151d9772f5770918e8e7f1fd25 -rc748054f9dfa9d20920ee43fe7aeb12c2daa1fde --- AD5941_interface09302025/ad5941_library_extension.cpp (.../ad5941_library_extension.cpp) (revision 3c0eedb1a41ef5151d9772f5770918e8e7f1fd25) +++ AD5941_interface09302025/ad5941_library_extension.cpp (.../ad5941_library_extension.cpp) (revision c748054f9dfa9d20920ee43fe7aeb12c2daa1fde) @@ -2,8 +2,8 @@ * @file ad5941_library_extension.cpp * @brief For connecting teensy microcontroller to ad5941 via SPI with interrupt functionality * @author MK, Aly Development - * @date 09/30/2025, last modified - * + * @date 01/09/2026, last modified + * * @details * Teensy 4.0 extension for the AD5941/AD5940 AFE (Analog Front End) library * from Analog Devices Inc. @@ -1166,14 +1166,119 @@ // activeSerial->println("System ready; enter b to perform reading"); } +// MK hotfix version 01/08/2026; checks fifo directly instead of using interrupts (interupts weren't working well on newest boards) +bool AppIMPMeasure(int readingCount) { + uint32_t bufferSize; // Variable to track valid data in buffer + int trigCount = 0; // Counter for completed measurements + unsigned long startTime; // For timeout tracking + const unsigned long TIMEOUT_MS = 5000; // 5 second timeout (adjust as needed) + bool timeoutOccurred = false; + + resetSwitchMatrix(); + AppIMPInit(AppBuff, APPBUFF_SIZE); + AD5940_EnableAdcMaxSaturationIRQ(/*max_code=*/maxValueThresholdADC, /*hysteresis=*/0x0080); + + // Get pointer to impedance configuration + AppIMPCfg_Type *pImpedanceCfg; + AppIMPGetCfg(&pImpedanceCfg); + + // Disable WUPT for on-demand operation + WUPTCfg_Type wupt_cfg; + wupt_cfg.WuptEn = bFALSE; + AD5940_WUPTCfg(&wupt_cfg); + + // Clear any stale FIFO data + AD5940_FIFOCtrlS(FIFOSRC_DFT, bFALSE); + AD5940_Delay10us(10); + AD5940_FIFOCtrlS(FIFOSRC_DFT, bTRUE); + + // Manually trigger the sequencer once + AD5940_SEQMmrTrig(pImpedanceCfg->MeasureSeqInfo.SeqId); + + // Record start time for timeout + startTime = millis(); + + // Wait initial 20ms for measurement to start + delay(20); + + // Poll FIFO count until data is ready or timeout + trigCount = 0; + while (trigCount < 1) { + // Check for timeout + if (millis() - startTime > TIMEOUT_MS) { + timeoutOccurred = true; + break; + } + + // Check FIFO count instead of interrupt + uint32_t fifo_count = AD5940_FIFOGetCnt(); + + if (fifo_count >= pImpedanceCfg->FifoThresh) { + // Data is ready in FIFO + + // Set up buffer for data collection and process the measurement + bufferSize = APPBUFF_SIZE; + AppIMPISR(AppBuff, &bufferSize); + + // Check if we actually got data + if (bufferSize == 0) { + if (verboseMode) { + Serial.println("WARNING: Empty FIFO - measurement did not complete properly"); + } + // Set default invalid values + magnitudeArray[readingCount] = INFINITY; + phaseArray[readingCount] = 0.0f; + storedFrequency = 0.0f; + } else { + // Display the impedance measurement results + // bufferSize now contains the actual number of valid data points + ImpedanceShowResult(AppBuff, bufferSize, readingCount); + + trigCount=1; // exit condition + + // Check if ADC saturated during measurement + if (AD5940_IsAdcMaxSaturatedAndClear()) { + if (verboseMode) { + Serial.println("WARNING: ADC saturation detected, measurement may be invalid"); + } + // Mark this measurement as saturated/invalid + magnitudeArray[readingCount] = INFINITY; + phaseArray[readingCount] = 0; + } + } + + // Increment measurement counter + } + + // Small delay between FIFO checks + delay(1); + } + + // Stop the impedance measurement cleanly + AppIMPCtrl(IMPCTRL_STOPSYNC, 0); + + // Explicitly stopping sequencer + SEQCfg_Type seq_cfg; + seq_cfg.SeqEnable = bFALSE; + AD5940_SEQCfg(&seq_cfg); + AD5940_Delay10us(100); + + // Handle timeout situation + if (timeoutOccurred) { + Serial.println("ERROR: Impedance measurement timeout occurred!"); + } + + return timeoutOccurred; +} + /** * @brief Performs a single impedance measurement and displays the result * * This function conducts a complete impedance measurement cycle using the AD5940. * It initializes the measurement buffer, starts the measurement, waits for * completion, processes the data, and displays the results. */ -bool AppIMPMeasure(int readingCount) +bool AppIMPMeasure_OLD(int readingCount) { uint32_t bufferSize; // Variable to track valid data in buffer int trigCount = 0; // Counter for completed measurements Index: AD5941_interface09302025/ad5941_library_extension.h =================================================================== diff -u -r4be37523c8348f40b96b1df7a4ef376e99c204f6 -rc748054f9dfa9d20920ee43fe7aeb12c2daa1fde --- AD5941_interface09302025/ad5941_library_extension.h (.../ad5941_library_extension.h) (revision 4be37523c8348f40b96b1df7a4ef376e99c204f6) +++ AD5941_interface09302025/ad5941_library_extension.h (.../ad5941_library_extension.h) (revision c748054f9dfa9d20920ee43fe7aeb12c2daa1fde) @@ -1,5 +1,5 @@ -#define VERSION "AD5941_updatedAcqBuild_SATCHECKS" -#define AUTHOR "09/30/2025, MK" +#define VERSION "AD5941_hotFixNoInterruptsExitFixed" +#define AUTHOR "01/09/2026, MK" /** * @file ad5941_library_extension.h