Index: firmware/App/Drivers/ConductivityTeensy.c =================================================================== diff -u -r4643a4b7f7ecaca8e07a809af6b218fac1c74799 -r202978f0c2f12bf5a1e466d6788e3a6fc6243569 --- firmware/App/Drivers/ConductivityTeensy.c (.../ConductivityTeensy.c) (revision 4643a4b7f7ecaca8e07a809af6b218fac1c74799) +++ firmware/App/Drivers/ConductivityTeensy.c (.../ConductivityTeensy.c) (revision 202978f0c2f12bf5a1e466d6788e3a6fc6243569) @@ -8,14 +8,14 @@ * @file ConductivityTeensy.c * * @author (last) Michael Garthwaite -* @date (last) 10-Feb-2026 +* @date (last) 06-Mar-2026 * * @author (original) Arpita Srivastava * @date (original) 03-Nov-2025 * ***************************************************************************/ -#include // Used for calculating the polynomial calibration equation and INF +#include // Used for log() and INF #include // For memcpy #include "sci.h" @@ -52,16 +52,16 @@ #define RX_SIZE_SELECT_SENSOR 1 ///< Expected response bytes length of select sensor cmd. #define RX_SIZE_GET_SINGLE_MEASUREMENT ( sizeof( COND_SENSOR_DATA_T ) ) ///< Expected response bytes length of get single sensor measurement cmd. -#define COND_INIT_TIMEOUT_MS ( 3 * MS_PER_SECOND ) ///< Timeout before which we should transition to idle state. +#define COND_INIT_TIMEOUT_MS ( 5 * MS_PER_SECOND ) ///< Timeout before which we should transition to idle state. #define COND_STATUS_TIMEOUT_MS ( 10 * MS_PER_SECOND ) ///< Timeout before which we should receive acknowledgment from teensy #define COND_DATA_TIMEOUT_MS ( 10 * MS_PER_SECOND ) ///< Timeout before which we should receive data from teensy #define COND_TEMP_OFFSET 25 ///< Temperature offset constant used in RTD calculations. -#define COND_CONVERSION_SM_TO_USCM 100000 ///< Conductivity conversion from siemens per meter to micro siemens per centimeter. +#define COND_CONVERSION_SM_TO_USCM 1000000.0F ///< Conductivity conversion from siemens per meter to micro siemens per centimeter. #define COND_INFINITE_R_VALUE 800000 ///< Resistance value for when the driver detects INF from the sensor. // ********** private data ********** -static COND_COMM_STATE_T condCommState; ///< current Conductivity driver comm state. +static COND_COMM_STATE_T condCommState; ///< Current Conductivity driver comm state. static U32 condInitTime; ///< Tracks duration of init state. static U32 condResponseTime; ///< Tracks duration between cmd sent and its response received. @@ -106,7 +106,7 @@ static COND_MODELS_T currentConductivityModel; ///< Current Conductivity Model selected. static BOOL eepromInit; ///< Boolean to determine if we are loading eeprom data from sensors. -// Command Map +// Teensy Command Map static const COND_CMD_DATA_T teensyCmdMap[] = { // Command Index / Sub state Command Length of expected response data { TEENSY_CMD_INIT_SENSOR , ( U08* )"a" , RX_SIZE_INIT_SENSOR }, @@ -130,7 +130,6 @@ static COND_COMM_STATE_T handleConductivityIdle( void ); static COND_COMM_STATE_T handleConductivityTX( void ); static COND_COMM_STATE_T handleConductivityRX( void ); -static COND_COMM_STATE_T handleFailedState( void ); static void consumeUnexpectedConductivityData( void ); static void setupConductivityDMAForWriteCmd( U32 bytes2Transmit ); @@ -180,7 +179,7 @@ static COND_COMM_STATE_T rxSelectSensor( void ); static COND_PARSE_STATUS parseEEPROMdata( const U08 *buffer, U32 len ); -static COND_PARSE_STATUS parseConductivityMeasurements( const U08 *buffer, U32 len ); +static COND_PARSE_STATUS parseConductivityMeasurements( const U08 *buffer ); static U32 getTeensyCondId( U32 sensorId ); static void calculateConductivity( U32 sensorNum ); @@ -331,10 +330,6 @@ condCommState = handleConductivityRX(); break; - case COND_COMM_STATE_FAILED: - condCommState = handleFailedState(); - break; - default: SET_ALARM_WITH_1_U32_DATA( ALARM_ID_DD_SOFTWARE_FAULT, ( U32 )condCommState ) break; @@ -356,6 +351,7 @@ if ( TRUE == didTimeout( condInitTime, COND_INIT_TIMEOUT_MS ) ) { state = COND_COMM_STATE_IDLE; + consumeUnexpectedConductivityData(); } return state; } @@ -521,50 +517,6 @@ /*********************************************************************//** * @brief - * The handleFailedState function handles failure or errors of the states. - * @details \b Inputs: currentCmd - Current command being executed. - * The error occurred during TX or RX phase of this command. - * @details \b Outputs: none - * @return state - Next state. - *************************************************************************/ -static COND_COMM_STATE_T handleFailedState( void ) -{ - COND_COMM_STATE_T state = COND_COMM_STATE_FAILED; - - switch ( currentCmd ) - { - case TEENSY_CMD_INIT_SENSOR: - break; - case TEENSY_CMD_GET_INIT_STATUS: - break; - case TEENSY_CMD_UPDATE_EEPROM_DATA: - break; - case TEENSY_CMD_GET_EEPROM_DATA: - break; - case TEENSY_CMD_UPDATE_MEASUREMENT_SETTINGS: - break; - case TEENSY_CMD_GET_MEASUREMENT_SETTINGS: - break; - case TEENSY_CMD_START_MEASUREMENT: - break; - case TEENSY_CMD_STOP_MEASUREMENT: - break; - case TEENSY_CMD_GET_ALL_MEASUREMENTS: - break; - case TEENSY_CMD_GET_SINGLE_MEASUREMENT: - break; - case TEENSY_CMD_SELECT_SENSOR: - break; - default: - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_FP_SOFTWARE_FAULT, SW_FAULT_ID_CONDUCTIVITY_SENOR_INVALID_STATE, currentCmd ) - break; - } - - return state; -} - -/*********************************************************************//** - * @brief * The consumeUnexpectedConductivityData function checks to see if a byte is sitting in * the SCI received data register and consumes the byte if found. * @details \b Inputs: SCI FLR register @@ -842,10 +794,6 @@ condWriteCmdRspnsRcvd = FALSE; success = TRUE; } - - // Should not be any data received at this time - //consumeUnexpectedConductivityData(); - return success; } @@ -1432,7 +1380,7 @@ condResponseTime = getMSTimerCount(); // Read the data from the receive buffer - parseStatus = parseConductivityMeasurements( condRxBuffer, COND_RX_BUFFER_LEN ); + parseStatus = parseConductivityMeasurements( condRxBuffer ); // Check if parsing was done successfully if ( COND_PARSE_SUCCESS == parseStatus ) @@ -1595,7 +1543,7 @@ condResponseTime = 0; // Read the data from the receive buffer - parseStatus = parseConductivityMeasurements( condRxBuffer, COND_RX_BUFFER_LEN ); + parseStatus = parseConductivityMeasurements( condRxBuffer ); // Check if parsing was done successfully if ( COND_PARSE_SUCCESS == parseStatus ) @@ -1670,7 +1618,7 @@ condResponseTime = 0; // Read the data from the receive buffer - COND_PARSE_STATUS parseStatus = parseConductivityMeasurements( condRxBuffer, COND_RX_BUFFER_LEN ); + COND_PARSE_STATUS parseStatus = parseConductivityMeasurements( condRxBuffer ); // Check if parsing was done successfully if ( COND_PARSE_SUCCESS == parseStatus ) @@ -1816,7 +1764,7 @@ * len - Length of the input data. * @return COND_PARSE_STATUS to tell if parsing was successful or not. *************************************************************************/ -static COND_PARSE_STATUS parseConductivityMeasurements( const U08 *buffer, U32 len ) +static COND_PARSE_STATUS parseConductivityMeasurements( const U08 *buffer ) { COND_PARSE_STATUS parseStatus = COND_PARSE_NONE; COND_SENSOR_DATA_T tempSensor; @@ -1839,7 +1787,7 @@ else { // Store Raw value in array index for position ( sensorNum - 1 ) - condRawMeasurement[ tempSensor.sensorNum - 1 ] = tempSensor; + memcpy( &condRawMeasurement[ tempSensor.sensorNum - 1 ], &tempSensor, sizeof( COND_SENSOR_DATA_T ) ); // Calculate and store Conductivity from raw values calculateConductivity( tempSensor.sensorNum - 1 ); @@ -1886,6 +1834,10 @@ case D43_COND: sensorNum = TEENSY_SENSOR_5; break; + default: + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DD_SOFTWARE_FAULT, SW_FAULT_ID_INVALID_CONDUCTIVITY_SENSOR_ID, sensorId ) + break; + } return sensorNum; @@ -1897,21 +1849,18 @@ impedance value. * @details \b Inputs: condCalculatedMeasurement * @details \b Outputs: none - * @param sensor ID of conductivity sensor to get conductivity. + * @param sensorId ID of conductivity sensor to get conductivity. * @return The current conductivity of a given conductivity sensor. *************************************************************************/ F32 getTeensyConductivityValue( U32 sensorId ) { U32 sensorNum = getTeensyCondId( sensorId ); F32 result = 0.0F; - if ( D74_COND != sensorId ) + result = condCalculatedMeasurement[ sensorNum ].Conductivity.data; + if ( OVERRIDE_KEY == condCalculatedMeasurement[ sensorNum ].Conductivity.override ) { - result = condCalculatedMeasurement[ sensorNum ].Conductivity.data; - if ( OVERRIDE_KEY == condCalculatedMeasurement[ sensorNum ].Conductivity.override ) - { - result = condCalculatedMeasurement[ sensorNum ].Conductivity.ovData; - } + result = condCalculatedMeasurement[ sensorNum ].Conductivity.ovData; } return result; @@ -1931,13 +1880,10 @@ U32 sensorNum = getTeensyCondId( sensorId ); F32 result = 0.0F; - if ( D74_COND != sensorId ) + result = condCalculatedMeasurement[ sensorNum ].Temperature.data; + if ( OVERRIDE_KEY == condCalculatedMeasurement[ sensorNum ].Temperature.override ) { - result = condCalculatedMeasurement[ sensorNum ].Temperature.data; - if ( OVERRIDE_KEY == condCalculatedMeasurement[ sensorNum ].Temperature.override ) - { - result = condCalculatedMeasurement[ sensorNum ].Temperature.ovData; - } + result = condCalculatedMeasurement[ sensorNum ].Temperature.ovData; } return result; @@ -1957,13 +1903,10 @@ U32 sensorNum = getTeensyCondId( sensorId ); F32 result = 0.0F; - if ( D74_COND != sensorId ) + result = condCalculatedMeasurement[ sensorNum ].Resistance.data; + if ( OVERRIDE_KEY == condCalculatedMeasurement[ sensorNum ].Resistance.override ) { - result = condCalculatedMeasurement[ sensorNum ].Resistance.data; - if ( OVERRIDE_KEY == condCalculatedMeasurement[ sensorNum ].Resistance.override ) - { - result = condCalculatedMeasurement[ sensorNum ].Resistance.ovData; - } + result = condCalculatedMeasurement[ sensorNum ].Resistance.ovData; } return result; @@ -2013,13 +1956,13 @@ *************************************************************************/ static void calculateConductivityAly( U32 sensorNum, BOOL isFPSensor ) { - F64 calculatedConductivity = 0.0; + F32 calculatedConductivity = 0.0; F32 B3 = 0.0; F32 B2 = 0.0; F32 B1 = 0.0; F32 B0 = 0.0; - F64 R = condRawMeasurement[ sensorNum ].impRzMag; - F64 Z = condRawMeasurement[ sensorNum ].rtdRzMag; + F32 R = condRawMeasurement[ sensorNum ].impRzMag; + F32 Z = condRawMeasurement[ sensorNum ].rtdRzMag; // Aly sensors are known to send the driver INF in unfavorable conditions for impedance. e.g: open air // Change it to some high value that can be determined that we are reading INF. @@ -2044,13 +1987,14 @@ B0 = condCoeff[ sensorNum ].beta0DD; } + // B3( 1000 / impRz ) + B2( rtdRz / impRz ) + B1( 100ln(rtdRz) / impRz ) + B0 calculatedConductivity = ( ( B3 * ( 1000.0 / R ) ) + ( B2 * ( Z / R ) ) + ( B1 * ( ( 100 * log( Z ) ) / R ) ) + B0 ); - - condCalculatedMeasurement[ sensorNum ].Conductivity.data = (F32)calculatedConductivity; + calculatedConductivity = calculatedConductivity * COND_CONVERSION_SM_TO_USCM; + condCalculatedMeasurement[ sensorNum ].Conductivity.data = calculatedConductivity; } /*********************************************************************//** @@ -2065,7 +2009,7 @@ *************************************************************************/ static void calculateConductivityUpdatedStandard( U32 sensorNum, BOOL isFPSensor ) { - F64 calculatedConductivity = 0.0; + F32 calculatedConductivity = 0.0; F64 alpha = 0.0; F64 k = 0.0; @@ -2081,10 +2025,10 @@ } calculateResistance( sensorNum, isFPSensor ); calculateTemperature( sensorNum ); - - calculatedConductivity = ( ( k / getTeensyConductivityResistanceValue( sensorNum ) ) * - ( 1 + alpha * ( COND_TEMP_OFFSET - getTeensyConductivityTemperatureValue( sensorNum ) ) ) ); - condCalculatedMeasurement[ sensorNum ].Conductivity.data = (F32)calculatedConductivity; + // ( k / calculatedReistance ) * (1 + alpha*( 25 - calculatedTemperature ) ) + calculatedConductivity = ( ( k / condCalculatedMeasurement[ sensorNum ].Resistance.data ) * + ( 1 + ( alpha * ( COND_TEMP_OFFSET - condCalculatedMeasurement[ sensorNum ].Temperature.data ) ) ) ); + calculatedConductivity = calculatedConductivity * COND_CONVERSION_SM_TO_USCM; } /*********************************************************************//** @@ -2099,10 +2043,10 @@ *************************************************************************/ static void calculateConductivityStandard( U32 sensorNum, BOOL isFPSensor ) { - F64 calculatedConductivity = 0.0; + F32 calculatedConductivity = 0.0; F64 alpha = 0.0; F64 k = 0.0; - F64 R = condRawMeasurement[ sensorNum ].impRzMag; + F32 R = condRawMeasurement[ sensorNum ].impRzMag; // Aly sensors are known to send the driver INF in unfavorable conditions for impedance. e.g: open air // Change it to some high value that can be determined that we are reading INF. @@ -2123,12 +2067,12 @@ k = condCoeff[ sensorNum ].kHighDDS; } calculateTemperature( sensorNum ); - + // ( k / impRz ) * (1 + alpha*( 25 - calculatedTemperature ) ) calculatedConductivity = ( ( k / R ) * - ( 1 + alpha * ( COND_TEMP_OFFSET - getTeensyConductivityTemperatureValue( sensorNum ) ) ) ); + ( 1 + ( alpha * ( COND_TEMP_OFFSET - condCalculatedMeasurement[ sensorNum ].Temperature.data ) ) ) ); calculatedConductivity = calculatedConductivity * COND_CONVERSION_SM_TO_USCM; - condCalculatedMeasurement[ sensorNum ].Conductivity.data = (F32)calculatedConductivity; + condCalculatedMeasurement[ sensorNum ].Conductivity.data = calculatedConductivity; } /*********************************************************************//** @@ -2144,10 +2088,10 @@ static void calculateResistance( U32 sensorNum, BOOL isFPSensor ) { - F64 calculatedResistance = 0.0; + F32 calculatedResistance = 0.0; F64 eta = 0.0; F64 zeta = 0.0; - F64 R = condRawMeasurement[ sensorNum ].impRzMag; + F32 R = condRawMeasurement[ sensorNum ].impRzMag; // Aly sensors are known to send the driver INF in unfavorable conditions. e.g: open air // Change it to some high value that can be determined that we are reading INF. @@ -2168,7 +2112,7 @@ zeta = condCoeff[ sensorNum ].zetaHighDDUS; } calculatedResistance = ( ( eta * R ) + zeta ); - condCalculatedMeasurement[ sensorNum ].Resistance.data = (F32)calculatedResistance; + condCalculatedMeasurement[ sensorNum ].Resistance.data = calculatedResistance; } /*********************************************************************//** @@ -2183,13 +2127,13 @@ static void calculateTemperature( U32 sensorNum ) { - F64 calculatedTemperature = 0.0; + F32 calculatedTemperature = 0.0; F64 beta = condCoeff[ sensorNum ].beta; F64 gamma = condCoeff[ sensorNum ].gamma; - F64 Z = condRawMeasurement[ sensorNum ].rtdRzMag; + F32 Z = condRawMeasurement[ sensorNum ].rtdRzMag; calculatedTemperature = ( ( beta * Z ) + gamma ); - condCalculatedMeasurement[ sensorNum ].Temperature.data = (F32)calculatedTemperature; + condCalculatedMeasurement[ sensorNum ].Temperature.data = calculatedTemperature; } @@ -2333,12 +2277,12 @@ { if ( sizeof( U32 ) == message->hdr.payloadLen ) { - COND_MODELS_T newCondModel; + U32 newCondModel; memcpy( &newCondModel, &message->payload[0], sizeof( U32 ) ); if ( newCondModel < NUM_OF_MODELS ) { - currentConductivityModel = newCondModel; + currentConductivityModel = (COND_MODELS_T)newCondModel; result = TRUE; } }