Index: AD5941_interface09302025/AD5941_interface09302025.ino =================================================================== diff -u -r4ed225961d7e2ebf9a71c19b1a3bb5788c349c12 -r27e8dd0f97a999fe4d0b60dd5f9d083455f93509 --- AD5941_interface09302025/AD5941_interface09302025.ino (.../AD5941_interface09302025.ino) (revision 4ed225961d7e2ebf9a71c19b1a3bb5788c349c12) +++ AD5941_interface09302025/AD5941_interface09302025.ino (.../AD5941_interface09302025.ino) (revision 27e8dd0f97a999fe4d0b60dd5f9d083455f93509) @@ -64,19 +64,9 @@ digitalWrite(EN_B, HIGH); digitalWrite(EN_C, HIGH); - detectSerial(); - if (activeSerial) { - // activeSerial->println("Serial initialized successfully!"); - } - // // Initialize serial communication at 115200 baud - // beginSerial(activeSerial, 115200); - - - // // Wait for serial connection to be established - // while (!activeSerial); + Serial7.begin(115200); - //initializeAllUnits(); initializeSelectedUnits(); selectFirstWorkingUnit(); // activeSerial->print("*"); @@ -89,7 +79,7 @@ void loop() { // Check if serial data is available and process commandsd - while (activeSerial->available()) + while (Serial7.available()) { // Process incoming serial command readCommand(); Index: AD5941_interface09302025/ad5941_library_extension.cpp =================================================================== diff -u -r3c0eedb1a41ef5151d9772f5770918e8e7f1fd25 -r27e8dd0f97a999fe4d0b60dd5f9d083455f93509 --- AD5941_interface09302025/ad5941_library_extension.cpp (.../ad5941_library_extension.cpp) (revision 3c0eedb1a41ef5151d9772f5770918e8e7f1fd25) +++ AD5941_interface09302025/ad5941_library_extension.cpp (.../ad5941_library_extension.cpp) (revision 27e8dd0f97a999fe4d0b60dd5f9d083455f93509) @@ -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. @@ -34,7 +34,7 @@ #include // Stores the instance of currently connected / detected Serial port -Stream *activeSerial = nullptr; +Stream *activeSerial = &Serial7; Stream *serialPorts[] = { @@ -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 @@ -1336,7 +1441,7 @@ { // Read a line from the serial port until newline character - String command = activeSerial->readStringUntil('\n'); + String command = Serial7.readStringUntil('\0'); // Handle configuration commands (format: cfg,parameter,value) if (command.startsWith("cfg,")) @@ -1402,6 +1507,27 @@ return; } + // Get EEPROM data for given sensor + if (command.startsWith("u,")) + { + int firstComma = command.indexOf(','); + if (firstComma > 0) + { + int unit = command.substring(firstComma + 1).toInt(); + + if ((isSensorValid (unit)) && (isSensorInitialized (unit))) + { + // Select the sensor + selectUnit(unit); + activeSerial->write((uint8_t)1); + } + else + { + activeSerial->write((uint8_t)0); + } + } + } + // Process commands based on the first character switch (command.charAt(0)) { @@ -1764,7 +1890,6 @@ // sendAllSensorData(); } break; - default: // Handle unknown commands { activeSerial->println("Unknown command. Type ? for help."); @@ -2778,80 +2903,7 @@ // ************************************ Diality ********************************************** -void detectSerial() -{ - unsigned long startTime = 0; - // First check Serial7 explicitly - - beginSerial(&Serial7, 115200); - startTime = millis(); - - while ((millis() - startTime) < 3000) - { // Wait up to 3 seconds - if (Serial7.available()) - { - activeSerial = &Serial7; - // activeSerial->println("Using Serial7 (pins 28,29)"); - return; // Done, since Serial7 is preferred - } - } - delay(50); - - for (int i = 0; i < 8; i++) - { - beginSerial(serialPorts[i], 115200); - delay(50); // Give time for init - // For USB Serial, wait for connection - if (serialPorts[i] == &Serial) - { - startTime = millis(); - while (!Serial && millis() - startTime < 2000) - { - // Wait up to 2s for USB - } - activeSerial = serialPorts[i]; - break; - } - } -} - -void beginSerial(Stream * s, unsigned long baud) -{ - if (s == &Serial) - { - Serial.begin(baud); - } - else if (s == &Serial1) - { - Serial1.begin(baud); - } - else if (s == &Serial2) - { - Serial2.begin(baud); - } - else if (s == &Serial3) - { - Serial3.begin(baud); - } - else if (s == &Serial4) - { - Serial4.begin(baud); - } - else if (s == &Serial5) - { - Serial5.begin(baud); - } - else if (s == &Serial6) - { - Serial6.begin(baud); - } - else if (s == &Serial7) - { - Serial7.begin(baud); - } -} - -// Function to measure all successfully initialized units + // Function to measure all successfully initialized units bool getAllMesaurements(void) { int successCount = 0; @@ -2995,6 +3047,8 @@ singleSensorPacket.rtdDataPoints = sensorPacket[sensorIdx].rtdDataPoints; // Impedance Data Points. Hard coded to 1 singleSensorPacket.rtdRzMag = sensorPacket[sensorIdx].rtdRzMag; // Value of rtd_resistance singleSensorPacket.rtdRzPhase = sensorPacket[sensorIdx].rtdRzPhase; // RTD Rz Phase. Hard coded to 0.0 + + activeSerial->write((uint8_t *)&singleSensorPacket, sizeof(singleSensorPacket)); } } } @@ -3850,21 +3904,20 @@ { while (sendSensorData) { - String command = activeSerial->readStringUntil('\n'); - if (command.length() > 0) + if (activeSerial->available()) { - activeSerial->write((uint8_t)1); - break; // Exit if command received - } - else - { - if (true == getAllMesaurements()) + String command = activeSerial->readStringUntil('\0'); + if (command.length() > 0) { - // Send all the sensor data to DD - activeSerial->write((uint8_t *)&singleSensorPacket, sizeof(singleSensorPacket)); + activeSerial->write((uint8_t)1); + break; // Exit if command received } - delay(250); } + + else + { + getAllMesaurements(); + } } } Index: AD5941_interface09302025/ad5941_library_extension.h =================================================================== diff -u -r4be37523c8348f40b96b1df7a4ef376e99c204f6 -r27e8dd0f97a999fe4d0b60dd5f9d083455f93509 --- AD5941_interface09302025/ad5941_library_extension.h (.../ad5941_library_extension.h) (revision 4be37523c8348f40b96b1df7a4ef376e99c204f6) +++ AD5941_interface09302025/ad5941_library_extension.h (.../ad5941_library_extension.h) (revision 27e8dd0f97a999fe4d0b60dd5f9d083455f93509) @@ -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