Index: firmware/App/Controllers/ConductivitySensors.c =================================================================== diff -u --- firmware/App/Controllers/ConductivitySensors.c (revision 0) +++ firmware/App/Controllers/ConductivitySensors.c (revision 30fd485858736c08d8bc4fe8bcb94cf1b545492e) @@ -0,0 +1,260 @@ +/************************************************************************** +* +* Copyright (c) 2019-2020 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) Quang Nguyen +* @date (last) 13-Jul-2020 +* +* @author (original) Quang Nguyen +* @date (original) 13-Jul-2020 +* +***************************************************************************/ + +#include "ConductivitySensors.h" +#include "SystemCommMessages.h" +#include "FPGA.h" +#include "TaskPriority.h" +#include "TemperatureSensors.h" + +/** + * @addtogroup ConductivitySensors + * @{ + */ + +// ********** private definitions ********** + +#define COND_SENSOR_PROBE_TYPE 1000 +#define COND_SENSOR_TEMPERATURE_COEF 0.02 +#define COND_SENSOR_REPORT_PERIOD (100 / TASK_PRIORITY_INTERVAL) ///< Broadcast conductivity values message every 100 ms. + +// ********** private data ********** + +static U32 readCount[ NUM_OF_CONDUCTIVITY_SENSORS ]; ///< Read count for conductivity readings. +static OVERRIDE_F32_T compensatedConductivityValues[ NUM_OF_CONDUCTIVITY_SENSORS ]; ///< Latest conductivity values. + +static OVERRIDE_U32_T conductivityDataPublishInterval = { COND_SENSOR_REPORT_PERIOD, COND_SENSOR_REPORT_PERIOD, 0, 0 }; +static U32 conductivityDataPublicationTimerCounter = 0; + +// ********** private function prototypes ********** + +static U32 getConductivityDataPublishInterval( void ); + +/************************************************************************* + * @brief + * The initConductivitySensors function initializes the ConductivitySensors module. + * @details + * Inputs : none + * Outputs : ConductivitySensors module initialized + * @return none + *************************************************************************/ +void initConductivitySensors( void ) +{ + U32 i; + + for ( i = 0; i < NUM_OF_CONDUCTIVITY_SENSORS; i++ ) + { + readCount[ i ] = 0; + + compensatedConductivityValues[ i ].data = 0.0; + compensatedConductivityValues[ i ].ovData = 0.0; + compensatedConductivityValues[ i ].ovInitData = 0.0; + compensatedConductivityValues[ i ].override = OVERRIDE_RESET; + +// setFPGAConductivityProbeType( COND_SENSOR_PROBE_TYPE ); + } +} + +/************************************************************************* + * @brief + * The execConductivitySensors function gets conductivity sensors' data from FPGA and advertises them over CAN. + * @details + * Inputs : none + * Outputs : Advertising conductivity sensors' data. + * @return none + *************************************************************************/ +void execConductivitySensors(void) +{ + + U08 const fpgaCPiReadCount = getFPGACPiReadCount(); + if ( readCount[ CONDUCITIVYSENSORS_CPI_SENSOR ] != fpgaCPiReadCount ) { + readCount[ CONDUCITIVYSENSORS_CPI_SENSOR ] = fpgaCPiReadCount; + + F32 const temperatureValue = getTemperatureValue( TEMPSENSORS_INLET_PRIMARY_HEATER_TEMP_SENSOR ); + F32 const compensatedCoef = ( 1 + ( COND_SENSOR_TEMPERATURE_COEF * (temperatureValue - 25) ) ); + F32 const rawConductivity = (F32)( getFPGACPi() ) / 100; + + compensatedConductivityValues[ CONDUCITIVYSENSORS_CPI_SENSOR ].data = rawConductivity * compensatedCoef; + } + + if ( ++conductivityDataPublicationTimerCounter >= getConductivityDataPublishInterval() ) + { + conductivityDataPublicationTimerCounter = 0; + } +} + +/************************************************************************* + * @brief + * The getConductivityValue function gets the compensated conductivity + * value for a given conductivity sensor id. + * @details + * Inputs : compensatedConductivityValues[] + * Outputs : none + * @param sensorId : Id of conductivity sensor to get conductivity value + * @return the compensated conductivity value for the given sensor id. + *************************************************************************/ +F32 getConductivityValue( U32 sensorId ) +{ + F32 result = 0; + + if ( sensorId < NUM_OF_CONDUCTIVITY_SENSORS ) + { + if ( OVERRIDE_KEY == compensatedConductivityValues[ sensorId ].override ) + { + result = compensatedConductivityValues[ sensorId ].ovData; + } + else + { + result = compensatedConductivityValues[ sensorId ].data; + } + } + else + { + activateAlarmNoData( ALARM_ID_SOFTWARE_FAULT ); + } + + return result; +} + +/************************************************************************* + * @brief + * The getConductivityDataPublishInterval function gets the conductivity + * data publication interval. + * @details + * Inputs : getConductivityDataPublishInterval + * Outputs : none + * @return the current conductivity data publication interval (in ms/task interval). + *************************************************************************/ +static U32 getConductivityDataPublishInterval( void ) +{ + U32 result = conductivityDataPublishInterval.data; + + if ( OVERRIDE_KEY == conductivityDataPublishInterval.override ) + { + result = conductivityDataPublishInterval.ovData; + } + + return result; +} + + +/************************************************************************* + * TEST SUPPORT FUNCTIONS + *************************************************************************/ + + +/************************************************************************* + * @brief + * The testSetConductivityOverride function overrides the compensated + * conductivity value of given sensor id. + * @details + * Inputs : none + * Outputs : compensatedConductivityValues[] + * @param sensorId : Id of conductivity sensor to get conductivity value + * @param value : override compensated conductivity value + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testSetConductivityOverride( U32 sensorId, F32 value ) +{ + BOOL result = FALSE; + + if ( sensorId < NUM_OF_CONDUCTIVITY_SENSORS ) + { + if ( isTestingActivated() ) + { + result = TRUE; + compensatedConductivityValues[ sensorId ].ovData = value; + compensatedConductivityValues[ sensorId ].override = OVERRIDE_KEY; + } + } + + return result; +} + +/************************************************************************* + * @brief + * The testResetConductivityOverride function resets the override of the \n + * conductivity sensor value. + * @details + * Inputs : none + * Outputs : compensatedConductivityValues[] + * @param sensorId : Id of the conductivity sensor to override. + * @return TRUE if reset successful, FALSE if not + *************************************************************************/ +BOOL testResetConductivityOverride( U32 sensorId ) +{ + BOOL result = FALSE; + + if ( sensorId < NUM_OF_CONDUCTIVITY_SENSORS ) + { + if ( isTestingActivated() ) + { + result = TRUE; + compensatedConductivityValues[ sensorId ].ovData = compensatedConductivityValues[ sensorId ].ovInitData; + compensatedConductivityValues[ sensorId ].override = OVERRIDE_RESET; + } + } + + return result; +} + +/************************************************************************* + * @brief + * The testSetConductivityDataPublishIntervalOverride function overrides + * the conductivity data publish interval. + * @details + * Inputs : none + * Outputs : conductivityDataPublishInterval + * @param value : override conductivity data publish interval with (in ms) + * @return TRUE if override successful, FALSE if not + *************************************************************************/ +BOOL testSetConductivityDataPublishIntervalOverride( U32 interval_ms ) +{ + BOOL result = FALSE; + + if ( isTestingActivated() ) + { + result = TRUE; + conductivityDataPublishInterval.ovData = interval_ms / TASK_PRIORITY_INTERVAL; + conductivityDataPublishInterval.override = OVERRIDE_KEY; + } + + return result; +} + +/************************************************************************* + * @brief + * The testResetConductivityDataPublishIntervalOverride function resets + * the override of the conductivity data publish interval. + * @details + * Inputs : none + * Outputs : conductivityDataPublishInterval + * @return TRUE if override reset successful, FALSE if not + *************************************************************************/ +BOOL testResetConductivityDataPublishIntervalOverride( void ) +{ + BOOL result = FALSE; + + if ( isTestingActivated() ) + { + result = TRUE; + conductivityDataPublishInterval.ovData = conductivityDataPublishInterval.ovInitData; + conductivityDataPublishInterval.override = OVERRIDE_RESET; + } + + return result; +} Index: firmware/App/Controllers/ConductivitySensors.h =================================================================== diff -u --- firmware/App/Controllers/ConductivitySensors.h (revision 0) +++ firmware/App/Controllers/ConductivitySensors.h (revision 30fd485858736c08d8bc4fe8bcb94cf1b545492e) @@ -0,0 +1,57 @@ +/************************************************************************** +* +* Copyright (c) 2019-2020 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.h +* +* @author (last) Quang Nguyen +* @date (last) 13-Jul-2020 +* +* @author (original) Quang Nguyen +* @date (original) 13-Jul-2020 +* +***************************************************************************/ + +#ifndef __CONDUCTIVITYSENSORS_H__ +#define __CONDUCTIVITYSENSORS_H__ + +#include "DGCommon.h" + +/** + * @defgroup ConductivitySensors ConductivitySensors + * @brief Conductivity Sensors monitor module. + * Monitors and filters conductivity sensor readings. + * + * @addtogroup ConductivitySensors + * @{ + */ + +// ********** public definitions ********** + +/// Sensors name +typedef enum ConductivitySensors +{ + CONDUCITIVYSENSORS_CPI_SENSOR = 0, ///< Inlet water conductivity sensor. + CONDUCITIVYSENSORS_CPO_SENSOR, ///< Outlet water conductivity sensor. + CONDUCITIVYSENSORS_CD1_SENSOR, ///< Acid concentration conductivity sensor. + CONDUCITIVYSENSORS_CD2_SENSOR, ///< Bicarb concentration conductivity sensor. + NUM_OF_CONDUCTIVITY_SENSORS ///< Number of conductivity sensors. +} CONDUCTIVITY_SENSORS_T; + +// ********** public function prototypes ********** + +void initConductivitySensors( void ); +void execConductivitySensors( void ); + +DATA_ARRAY_GET_PROTOTYPE ( F32, getConductivityValue, sensor ); + +BOOL testSetConductivityOverride( U32 sensor, F32 value ); +BOOL testResetConductivityOverride( U32 sensor ); + +BOOL testSetConductivityDataPublishIntervalOverride( U32 interval_ms ); +BOOL testResetConductivityDataPublishIntervalOverride( void ); + +#endif Index: firmware/App/Services/FPGA.c =================================================================== diff -u -ra7bf3ca23ea37a61000379facae628a31b3ecc59 -r30fd485858736c08d8bc4fe8bcb94cf1b545492e --- firmware/App/Services/FPGA.c (.../FPGA.c) (revision a7bf3ca23ea37a61000379facae628a31b3ecc59) +++ firmware/App/Services/FPGA.c (.../FPGA.c) (revision 30fd485858736c08d8bc4fe8bcb94cf1b545492e) @@ -601,21 +601,25 @@ memcpy( &( fpgaWriteCmdBuffer[ FPGA_WRITE_CMD_HDR_LEN ] ), &fpgaActuatorSetPoints, sizeof( FPGA_ACTUATORS_T ) ); crc = crc16( fpgaWriteCmdBuffer, FPGA_WRITE_CMD_HDR_LEN + sizeof( FPGA_ACTUATORS_T ) ); fpgaWriteCmdBuffer[ FPGA_WRITE_CMD_HDR_LEN + sizeof( FPGA_ACTUATORS_T ) ] = GET_MSB_OF_WORD( crc ); - fpgaWriteCmdBuffer[ FPGA_WRITE_CMD_HDR_LEN + sizeof( FPGA_ACTUATORS_T ) + 1 ] = GET_LSB_OF_WORD( crc ); + fpgaWriteCmdBuffer[ FPGA_WRITE_CMD_HDR_LEN + sizeof( FPGA_ACTUATORS_T ) + 1 ] = GET_LSB_OF_WORD( crc ); + // construct bulk read command to read sensor data registers starting at address 8 fpgaReadCmdBuffer[ 0 ] = FPGA_READ_CMD_CODE; fpgaReadCmdBuffer[ 1 ] = GET_LSB_OF_WORD( FPGA_BULK_READ_START_ADDR ); fpgaReadCmdBuffer[ 2 ] = GET_MSB_OF_WORD( FPGA_BULK_READ_START_ADDR ); fpgaReadCmdBuffer[ 3 ] = sizeof(DG_FPGA_SENSORS_T); crc = crc16( fpgaReadCmdBuffer, FPGA_READ_CMD_HDR_LEN ); fpgaReadCmdBuffer[ 4 ] = GET_MSB_OF_WORD( crc ); - fpgaReadCmdBuffer[ 5 ] = GET_LSB_OF_WORD( crc ); + fpgaReadCmdBuffer[ 5 ] = GET_LSB_OF_WORD( crc ); + // prep DMA for sending the bulk write cmd and receiving its response setupDMAForWriteCmd( FPGA_WRITE_CMD_HDR_LEN + sizeof( FPGA_ACTUATORS_T ) + FPGA_CRC_LEN ); - setupDMAForWriteResp( FPGA_WRITE_RSP_HDR_LEN + FPGA_CRC_LEN ); + setupDMAForWriteResp( FPGA_WRITE_RSP_HDR_LEN + FPGA_CRC_LEN ); + // prep DMA for sending the bulk read cmd and receiving its response setupDMAForReadCmd( FPGA_READ_CMD_HDR_LEN + FPGA_CRC_LEN ); - setupDMAForReadResp( FPGA_READ_RSP_HDR_LEN + sizeof( DG_FPGA_SENSORS_T ) + FPGA_CRC_LEN ); + setupDMAForReadResp( FPGA_READ_RSP_HDR_LEN + sizeof( DG_FPGA_SENSORS_T ) + FPGA_CRC_LEN ); + // set fpga comm flags for bulk write cmd and follow-up bulk read command fpgaWriteCommandInProgress = TRUE; fpgaBulkWriteAndReadInProgress = TRUE; @@ -1385,4 +1389,33 @@ U16 getFPGATrimmerColdJunctionTemp ( void ) { return fpgaSensorReadings.fpgaTrimmerHeaterIntJunctionTemp; -} +} + +/************************************************************************* + * @brief + * The getFPGACPiReadCount function gets inlet water conductivity sensor + * read count + * @details + * Inputs : fpgaSensorReadings.fpgaCPiReadCnt + * Outputs : none + * @param none + * @return Last inlet water conductivity sensor read count + *************************************************************************/ +U08 getFPGACPiReadCount ( void ) +{ + return fpgaSensorReadings.fpgaCPiReadCnt; +} + +/************************************************************************* + * @brief + * The getFPGACPi function gets inlet water conductivity value + * @details + * Inputs : fpgaSensorReadings.fpgaCPi + * Outputs : none + * @param none + * @return Last inlet water conductivity value + *************************************************************************/ +U32 getFPGACPi ( void ) +{ + return fpgaSensorReadings.fpgaCPi; +} Index: firmware/App/Services/FPGA.h =================================================================== diff -u -ra7bf3ca23ea37a61000379facae628a31b3ecc59 -r30fd485858736c08d8bc4fe8bcb94cf1b545492e --- firmware/App/Services/FPGA.h (.../FPGA.h) (revision a7bf3ca23ea37a61000379facae628a31b3ecc59) +++ firmware/App/Services/FPGA.h (.../FPGA.h) (revision 30fd485858736c08d8bc4fe8bcb94cf1b545492e) @@ -76,5 +76,8 @@ U16 getFPGAPRimaryColdJunctionTemp ( void ); U16 getFPGATrimmerColdJunctionTemp ( void ); + +U08 getFPGACPiReadCount ( void ); +U32 getFPGACPi ( void ); #endif Index: firmware/App/Tasks/TaskPriority.c =================================================================== diff -u -ra7bf3ca23ea37a61000379facae628a31b3ecc59 -r30fd485858736c08d8bc4fe8bcb94cf1b545492e --- firmware/App/Tasks/TaskPriority.c (.../TaskPriority.c) (revision a7bf3ca23ea37a61000379facae628a31b3ecc59) +++ firmware/App/Tasks/TaskPriority.c (.../TaskPriority.c) (revision 30fd485858736c08d8bc4fe8bcb94cf1b545492e) @@ -27,7 +27,8 @@ #include "TemperatureSensors.h" #include "Valves.h" #include "WatchdogMgmt.h" -#include "TemperatureSensors.h" +#include "TemperatureSensors.h" +#include "ConductivitySensors.h" #include "Heaters.h" /************************************************************************* @@ -56,7 +57,10 @@ #ifndef DISABLE_HEATERS_AND_TEMPS // Temperature sensors read execTemperatureSensors(); -#endif +#endif + + // conductivity sensors read + execConductivitySensors(); // control valves execValves();