Index: firmware/App/Controllers/ConductivitySensors.c =================================================================== diff -u -r674e18b10904e97f4b2624b826abc21f3220dd32 -rc7ed4f90b600fe684097654f150e4972646ffec9 --- firmware/App/Controllers/ConductivitySensors.c (.../ConductivitySensors.c) (revision 674e18b10904e97f4b2624b826abc21f3220dd32) +++ firmware/App/Controllers/ConductivitySensors.c (.../ConductivitySensors.c) (revision c7ed4f90b600fe684097654f150e4972646ffec9) @@ -1,14 +1,14 @@ /************************************************************************** * -* Copyright (c) 2019-2022 Diality Inc. - All Rights Reserved. +* Copyright (c) 2020-2022 Diality Inc. - All Rights Reserved. * * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * @file ConductivitySensors.c * * @author (last) Dara Navaei -* @date (last) 06-Nov-2021 +* @date (last) 02-Mar-2022 * * @author (original) Quang Nguyen * @date (original) 13-Jul-2020 @@ -18,8 +18,9 @@ #include "ConductivitySensors.h" #include "FPGA.h" -#include "PersistentAlarm.h" +#include "NVDataMgmt.h" #include "MessageSupport.h" +#include "PersistentAlarm.h" #include "SystemCommMessages.h" #include "TaskGeneral.h" #include "TaskPriority.h" @@ -64,6 +65,7 @@ #define EMSTAT_PICO_GOOD_STATUS 0x10 ///< Measurement good status. #define EMSTAT_PICO_TIMING_NOT_MET_STATUS 0x11 ///< Measurement takes too long status. #define EMSTAT_PICO_FIFO_EMPTY_MASK 0x8000 ///< Emstat Pico buffer empty indication bit. +#define DATA_PUBLISH_COUNTER_START_COUNT 40 ///< Data publish counter start count. #pragma pack(push,1) /// Emstat pico measurement data package structure @@ -92,14 +94,15 @@ static U08 readCount[ NUM_OF_CONDUCTIVITY_SENSORS ]; ///< Read count for conductivity readings. static U32 internalErrorCount[ NUM_OF_CONDUCTIVITY_SENSORS ]; ///< Internal error count for conductivity readings. static OVERRIDE_F32_T compensatedConductivityValues[ NUM_OF_CONDUCTIVITY_SENSORS ]; ///< Latest compensated conductivity values. +static F32 rawConductivityValues[ NUM_OF_CONDUCTIVITY_SENSORS ]; ///< Latest raw conductivity values. static F32 roRejectionRatio; ///< Latest RO rejection ratio. static OVERRIDE_U32_T conductivityDataPublishInterval = { COND_SENSOR_REPORT_PERIOD, COND_SENSOR_REPORT_PERIOD, 0, 0 }; ///< Conductivity sensors publish time interval override. -static U32 conductivityDataPublicationTimerCounter = 0; ///< Conductivity sensors data publish timer counter. +static U32 conductivityDataPublicationTimerCounter; ///< Conductivity sensors data publish timer counter. static BOOL packageStarted = FALSE; ///< Flag to indicate the start of a package measurement data. -static U08 packageIndex = 0U; ///< Current package measurement data bytes index. +static U08 packageIndex; ///< Current package measurement data bytes index. static U08 package[ 50 ]; ///< Storage of package bytes until ready to process. static DG_COND_SENSORS_CAL_RECORD_T condSensorsCalRecord; ///< Conductivity sensors' calibration record. @@ -112,7 +115,6 @@ static U32 prefixStrToSIFactor( U08 prefix ); static void processMeasurementDataPackage( U32 sensorId ); static void processCD1CD2SensorRead( U16 fpgaReadCount, U08 fpgaErrorCount ); -static BOOL processCalibrationData( void ); static F32 getCalibrationAppliedConductivityValue( U32 sensorId, F32 compensatedValue ); /*********************************************************************//** @@ -125,7 +127,9 @@ void initConductivitySensors( void ) { U32 i; - roRejectionRatio = 0.0; + roRejectionRatio = 0.0; + packageIndex = 0U; + conductivityDataPublicationTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; for ( i = 0; i < NUM_OF_CONDUCTIVITY_SENSORS; i++ ) { @@ -136,6 +140,8 @@ compensatedConductivityValues[ i ].ovData = 0.0; compensatedConductivityValues[ i ].ovInitData = 0.0; compensatedConductivityValues[ i ].override = OVERRIDE_RESET; + + rawConductivityValues[ i ] = 0.0; } setFPGACPiProbeType( COND_CPI_SENSOR_PROBE_TYPE ); @@ -162,8 +168,8 @@ // Check if a new calibration is available if ( TRUE == isNewCalibrationRecordAvailable() ) { - // Get the new calibration data and check its validity - processCalibrationData(); + getNVRecord2Driver( GET_CAL_CONDUCTIVITY_SENSORS, (U08*)&condSensorsCalRecord, sizeof( condSensorsCalRecord ), + NUM_OF_CAL_DATA_COND_SENSORS, ALARM_ID_DG_COND_SENSORS_INVALID_CAL_RECORD ); } processCPiCPoSensorRead( CONDUCTIVITYSENSORS_CPI_SENSOR, getFPGACPi(), getFPGACPiReadCount(), getFPGACPiErrorCount(), getFPGACPiFault() ); @@ -177,12 +183,18 @@ calcRORejectionRatio(); conductivityDataPublicationTimerCounter = 0; - data.roRejectionRatio = roRejectionRatio; + data.roRejectionRatio = roRejectionRatio; + data.cpi = getConductivityValue( CONDUCTIVITYSENSORS_CPI_SENSOR ); data.cpo = getConductivityValue( CONDUCTIVITYSENSORS_CPO_SENSOR ); data.cd1 = getConductivityValue( CONDUCTIVITYSENSORS_CD1_SENSOR ); data.cd2 = getConductivityValue( CONDUCTIVITYSENSORS_CD2_SENSOR ); + data.cpiRaw = rawConductivityValues[ CONDUCTIVITYSENSORS_CPI_SENSOR ]; + data.cpoRaw = rawConductivityValues[ CONDUCTIVITYSENSORS_CPO_SENSOR ]; + data.cd1Raw = rawConductivityValues[ CONDUCTIVITYSENSORS_CD1_SENSOR ]; + data.cd2Raw = rawConductivityValues[ CONDUCTIVITYSENSORS_CD2_SENSOR ]; + broadcastData( MSG_ID_DG_CONDUCTIVITY_DATA, COMM_BUFFER_OUT_CAN_DG_BROADCAST, (U08*)&data, sizeof( CONDUCTIVITY_DATA_T ) ); } } @@ -199,7 +211,8 @@ { SELF_TEST_STATUS_T result = SELF_TEST_STATUS_IN_PROGRESS; - BOOL calStatus = processCalibrationData(); + BOOL calStatus = getNVRecord2Driver( GET_CAL_CONDUCTIVITY_SENSORS, (U08*)&condSensorsCalRecord, sizeof( condSensorsCalRecord ), + NUM_OF_CAL_DATA_COND_SENSORS, ALARM_ID_DG_COND_SENSORS_INVALID_CAL_RECORD ); if ( TRUE == calStatus ) { @@ -303,7 +316,7 @@ * @brief * The calcCompensatedConductivity function calculates the compensated * conductivity based on given temperature and conductivity taken at - * reference temperature of 25 Celcius. + * reference temperature of 25 degree Celsius. * @details Inputs: conductivity, temperature * @details Outputs: none * @param conductivity conductivity value @@ -366,6 +379,7 @@ readCount[ sensorId ] = fpgaReadCount; internalErrorCount[ sensorId ] = 0; compensatedConductivityValues[ sensorId ].data = getCalibrationAppliedConductivityValue( sensorId, compensatedCond ); + rawConductivityValues[ sensorId ] = conductivity; // store raw conductivity data from CPi and CPo } else { @@ -444,10 +458,13 @@ internalErrorCount[ sensorId ] = 0; compensatedConductivityValues[ sensorId ].data = getCalibrationAppliedConductivityValue( sensorId, compensatedCond ); + rawConductivityValues[ sensorId ] = conductivity; // store raw conductivity data from CD1 and CD2 + } else { compensatedConductivityValues[ sensorId ].data = 0.0; + rawConductivityValues[ sensorId ] = 0.0; } if ( EMSTAT_PICO_TIMING_NOT_MET_STATUS == status ) @@ -528,47 +545,6 @@ /*********************************************************************//** * @brief - * The processCalibrationData function gets the calibration data and makes - * sure it is valid by checking the calibration date. The calibration date - * should not be 0. - * @details Inputs: none - * @details Outputs: condSensorsCalRecord - * @return TRUE if the calibration record is valid, otherwise FALSE - *************************************************************************/ -static BOOL processCalibrationData( void ) -{ - BOOL status = TRUE; - U32 sensor; - - // Get the calibration record from NVDataMgmt - DG_COND_SENSORS_CAL_RECORD_T calData = getDGConducitivitySensorsCalibrationRecord(); - - for ( sensor = 0; sensor < NUM_OF_CAL_DATA_COND_SENSORS; sensor++ ) - { -#ifndef SKIP_CAL_CHECK - // Check if the calibration data that was received from NVDataMgmt is legitimate - // The calibration date item should not be zero. If the calibration date is 0, - // then the data is not stored in the NV memory or it was corrupted. - if ( 0 == calData.condSensors[ sensor ].calibrationTime ) - { - SET_ALARM_WITH_1_U32_DATA( ALARM_ID_DG_COND_SENSORS_INVALID_CAL_RECORD, (U32)sensor ); - status = FALSE; - } -#endif - - // The calibration data was valid, update the local copy - condSensorsCalRecord.condSensors[ sensor ].fourthOrderCoeff = calData.condSensors[ sensor ].fourthOrderCoeff; - condSensorsCalRecord.condSensors[ sensor ].thirdOrderCoeff = calData.condSensors[ sensor ].thirdOrderCoeff; - condSensorsCalRecord.condSensors[ sensor ].secondOrderCoeff = calData.condSensors[ sensor ].secondOrderCoeff; - condSensorsCalRecord.condSensors[ sensor ].gain = calData.condSensors[ sensor ].gain; - condSensorsCalRecord.condSensors[ sensor ].offset = calData.condSensors[ sensor ].offset; - } - - return status; -} - -/*********************************************************************//** - * @brief * The getCalibrationAppliedConductivityValue function gets the temperature * compensated conductivity value and applies calibration to it. * @details Inputs: condSensorsCalRecord