/************************************************************************** * * Copyright (c) 2024-2024 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 PressureSensor.c * * @author (last) Sean Nash * @date (last) 18-Nov-2024 * * @author (original) Sean Nash * @date (original) 18-Nov-2024 * ***************************************************************************/ #include "AlarmMgmtRO.h" #include "FpgaRO.h" #include "Messaging.h" #include "PersistentAlarm.h" #include "PressureSensor.h" #include "TemperatureSensors.h" /** * @addtogroup PressureSensor * @{ */ // ********** private definitions ********** #define PRES_SENSORS_COUNT_ERROR_TIMEOUT_MS ( 2 * MS_PER_SECOND ) ///< Pressure sensors read and error count timeout in milliseconds. #define ONE_BAR_TO_MILLI_BAR 1000 ///< 1 bar to milli-bar conversion. #define COUNTS_TO_MILLI_BAR 100 ///< Counts to milli-bar conversion. #define BAR_TO_MMHG ( 750.062F ) ///< Conversion factor for converting bar to mmHg. #define PRES_SENSORS_READ_ERR_MAX_CNT 0xFF ///< Pressure sensor read and error max count value // ********** private data ********** static OVERRIDE_F32_T currentPressureReadings[ NUM_OF_PRESSURE_SENSORS ]; ///< Current pressure sensor pressure readings (overrideable). static OVERRIDE_F32_T currentPresTempReadings[ NUM_OF_PRESSURE_SENSORS ]; ///< Current pressure sensor temperature readings (overrideable). static OVERRIDE_U32_T lastPressureReadCounter[ NUM_OF_PRESSURE_SENSORS ]; ///< Last pressure sensor read count (Overrideable). static OVERRIDE_U32_T lastPressureErrorCounter[ NUM_OF_PRESSURE_SENSORS ]; ///< Last pressure sensor error count (Overrideable). // ********** private function prototypes ********** //static void checkPressureSensors( void ); /*********************************************************************//** * @brief * The initPressureSensor function initializes the Pressure Sensor unit. * @details \b Inputs: none * @details \b Outputs: Pressure Sensor unit is initialized. * @return none *************************************************************************/ void initPressureSensor( void ) { U32 i; // Initialize override structures for each pressure sensor for ( i = (U32)PRESSURE_SENSOR_FIRST; i < (U32)NUM_OF_PRESSURE_SENSORS; i++ ) { currentPressureReadings[ i ].data = 0.0F; currentPressureReadings[ i ].ovData = 0.0F; currentPressureReadings[ i ].ovInitData = 0.0F; currentPressureReadings[ i ].override = OVERRIDE_RESET; currentPresTempReadings[ i ].data = 0.0F; currentPresTempReadings[ i ].ovData = 0.0F; currentPresTempReadings[ i ].ovInitData = 0.0F; currentPresTempReadings[ i ].override = OVERRIDE_RESET; lastPressureReadCounter[ i ].data = 0; lastPressureReadCounter[ i ].ovData = 0; lastPressureReadCounter[ i ].ovInitData = 0; lastPressureReadCounter[ i ].override = OVERRIDE_RESET; lastPressureErrorCounter[ i ].data = 0; lastPressureErrorCounter[ i ].ovData = 0; lastPressureErrorCounter[ i ].ovInitData = 0; lastPressureErrorCounter[ i ].override = OVERRIDE_RESET; } // Initialize the FPGA persistent alarms // TODO - initialize persistent alarms. } /*********************************************************************//** * @brief * The readPressureSensors function gets the current pressure reading * for a all pressure sensors from the FPGA and also reads the freshness * and error counters to verify that the pressure sensors are being read * by the FPGA without issue. * @note This function should be called periodically to maintain fresh * sensor readings for all pressure sensors. * @details \b Inputs: FPGA * @details \b Outputs: currentPressureReadings[],currentPresTempReadings[], * lastPressureReadCounter[],lastPressureErrorCounter[]. * @return none *************************************************************************/ void readPressureSensors( void ) { // Update and convert raw pressures to mmHg currentPressureReadings[ PRESSURE_SENSOR_WATER_INLET_PRE_REG ].data = convertPressureReading2mmHg( getFPGAPRiRawPressure() ); currentPressureReadings[ PRESSURE_SENSOR_WATER_INLET_POST_REG ].data = convertPressureReading2mmHg( getFPGAPRoRawPressure() ); currentPressureReadings[ PRESSURE_SENSOR_WATER_INLET_PRE_COND ].data = convertPressureReading2mmHg( getFPGAPC2oRawPressure() ); currentPressureReadings[ PRESSURE_SENSOR_PRE_RO_FILTER ].data = convertPressureReading2mmHg( getFPGAPPoRawPressure() ); currentPressureReadings[ PRESSURE_SENSOR_POST_RO_FILTER ].data = convertPressureReading2mmHg( getFPGAPMpRawPressure() ); // Update and convert raw pressure sensor temperatures to deg C currentPresTempReadings[ PRESSURE_SENSOR_WATER_INLET_PRE_REG ].data = convertPressureTempReading2DegC( getFPGAPRiRawTemperature() ); currentPresTempReadings[ PRESSURE_SENSOR_WATER_INLET_POST_REG ].data = convertPressureTempReading2DegC( getFPGAPRoRawTemperature() ); currentPresTempReadings[ PRESSURE_SENSOR_WATER_INLET_PRE_COND ].data = convertPressureTempReading2DegC( getFPGAPC2oRawTemperature() ); currentPresTempReadings[ PRESSURE_SENSOR_PRE_RO_FILTER ].data = convertPressureTempReading2DegC( getFPGAPPoRawTemperature() ); currentPresTempReadings[ PRESSURE_SENSOR_POST_RO_FILTER ].data = convertPressureTempReading2DegC( getFPGAPMpRawTemperature() ); // Update read and error counters for each pressure sensor lastPressureReadCounter[ PRESSURE_SENSOR_WATER_INLET_PRE_REG ].data = (U32)getFPGAPRiReadCount(); lastPressureReadCounter[ PRESSURE_SENSOR_WATER_INLET_POST_REG ].data = (U32)getFPGAPRoReadCount(); lastPressureReadCounter[ PRESSURE_SENSOR_WATER_INLET_PRE_COND ].data = (U32)getFPGAPC2oReadCount(); lastPressureReadCounter[ PRESSURE_SENSOR_PRE_RO_FILTER ].data = (U32)getFPGAPPoReadCount(); lastPressureReadCounter[ PRESSURE_SENSOR_POST_RO_FILTER ].data = (U32)getFPGAPMpReadCount(); lastPressureErrorCounter[ PRESSURE_SENSOR_WATER_INLET_PRE_REG ].data = (U32)getFPGAPRiErrorCount(); lastPressureErrorCounter[ PRESSURE_SENSOR_WATER_INLET_POST_REG ].data = (U32)getFPGAPRoErrorCount(); lastPressureErrorCounter[ PRESSURE_SENSOR_WATER_INLET_PRE_COND ].data = (U32)getFPGAPC2oErrorCount(); lastPressureErrorCounter[ PRESSURE_SENSOR_PRE_RO_FILTER ].data = (U32)getFPGAPPoErrorCount(); lastPressureErrorCounter[ PRESSURE_SENSOR_POST_RO_FILTER ].data = (U32)getFPGAPMpErrorCount(); // // Monitor pressure sensor health // checkPressureSensors(); } /*********************************************************************//** * @brief * The checkPressureSensors function checks the read and error counters for * each pressure sensor. * @details \b Alarm: ALARM_ID_DD_HYD_OUTLET_PRES_TIMEOUT_FAULT if the * hydraulics outlet pressure sensor is not able to be read. * @details \b Alarm: ALARM_ID_DD_BIBAG_PRES_TIMEOUT_FAULT if the * BiBag pressure sensor is not able to be read. * @details \b Alarm: ALARM_ID_DD_SPENT_DIALYSATE_PRES_TIMEOUT_FAULT if the * spent dialysate pressure sensor is not able to be read. * @details \b Alarm: ALARM_ID_DD_FRESH_DIALYSATE_PRES_TIMEOUT_FAULT if the * fresh dialysate pressure sensor is not able to be read. * @details \b Alarm: ALARM_ID_DD_TRANSMEMB_PRES_TIMEOUT_FAULT if the * Transmembrane pressure sensor is not able to be read. * @details \b Inputs: lastPressureReadCounter, lastPressureErrorCounter * @details \b Outputs: none * @return none *************************************************************************/ //static void checkPressureSensors( void ) //{ // checkFPGAPersistentAlarms( FPGA_PERS_ERROR_WATER_INLET_INPUT_PRESSURE, getPressureSensorReadCount( PRESSURE_SENSOR_WATER_INLET_INPUT ) ); // checkFPGAPersistentAlarms( FPGA_PERS_ERROR_WATER_INLET_OUTPUT_PRESSURE, getPressureSensorReadCount( PRESSURE_SENSOR_WATER_INLET_OUTPUT ) ); // checkFPGAPersistentAlarms( FPGA_PERS_ERROR_HYDRAULICS_OUTLET_PRESSURE, getPressureSensorReadCount( PRESSURE_SENSOR_HYDRAULICS_OUTLET ) ); // checkFPGAPersistentAlarms( FPGA_PERS_ERROR_BIBAG_PRESSURE, getPressureSensorReadCount( PRESSURE_SENSOR_BIBAG ) ); // checkFPGAPersistentAlarms( FPGA_PERS_ERROR_SPENT_DIALYSATE_PRESSURE, getPressureSensorReadCount( PRESSURE_SENSOR_SPENT_DIALYSATE ) ); // checkFPGAPersistentAlarms( FPGA_PERS_ERROR_FRESH_DIALYSATE_PRESSURE, getPressureSensorReadCount( PRESSURE_SENSOR_FRESH_DIALYSATE ) ); // checkFPGAPersistentAlarms( FPGA_PERS_ERROR_TRANSMEMBRANE_PRESSURE, getPressureSensorReadCount( PRESSURE_SENSOR_TRANSMEMBRANE ) ); // // checkFPGAPersistentErrorCountAlarm( FPGA_PERS_ERROR_WATER_INLET_INPUT_PRESSURE, getPressureSensorErrorCount( PRESSURE_SENSOR_WATER_INLET_INPUT ) ); // checkFPGAPersistentErrorCountAlarm( FPGA_PERS_ERROR_WATER_INLET_OUTPUT_PRESSURE, getPressureSensorErrorCount( PRESSURE_SENSOR_WATER_INLET_OUTPUT ) ); // checkFPGAPersistentErrorCountAlarm( FPGA_PERS_ERROR_HYDRAULICS_OUTLET_PRESSURE, getPressureSensorErrorCount( PRESSURE_SENSOR_HYDRAULICS_OUTLET ) ); // checkFPGAPersistentErrorCountAlarm( FPGA_PERS_ERROR_BIBAG_PRESSURE, getPressureSensorErrorCount( PRESSURE_SENSOR_BIBAG ) ); // checkFPGAPersistentErrorCountAlarm( FPGA_PERS_ERROR_SPENT_DIALYSATE_PRESSURE, getPressureSensorErrorCount( PRESSURE_SENSOR_SPENT_DIALYSATE ) ); // checkFPGAPersistentErrorCountAlarm( FPGA_PERS_ERROR_FRESH_DIALYSATE_PRESSURE, getPressureSensorErrorCount( PRESSURE_SENSOR_FRESH_DIALYSATE ) ); // checkFPGAPersistentErrorCountAlarm( FPGA_PERS_ERROR_TRANSMEMBRANE_PRESSURE, getPressureSensorErrorCount( PRESSURE_SENSOR_TRANSMEMBRANE ) ); //} /*********************************************************************//** * @brief * The getPressure function gets the current pressure (in mmHg) for a given * pressure sensor. * @details \b Alarm: ALARM_ID_RO_SOFTWARE_FAULT if given pressure sensor is invalid. * @details \b Inputs: currentPressureReadings * @details \b Outputs: none * @param sensor ID of pressure sensor to get pressure reading for. * @return The current pressure (in mmHg) of the given pressure sensor. *************************************************************************/ F32 getPressure( PRESSURE_SENSORS_T sensor ) { F32 result = 0.0F; if ( sensor < NUM_OF_PRESSURE_SENSORS ) { result = currentPressureReadings[ sensor ].data; if ( OVERRIDE_KEY == currentPressureReadings[ sensor ].override ) { result = currentPressureReadings[ sensor ].ovData; } } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RO_SOFTWARE_FAULT, SW_FAULT_ID_PRESSURE_SENSOR_INVALID_SENSOR1, (U32)sensor ) } return result; } /*********************************************************************//** * @brief * The getPressureSensorTemperature function gets the current pressure sensor * temperature (in deg C) for a given pressure sensor. * @details \b Alarm: ALARM_ID_RO_SOFTWARE_FAULT if given pressure sensor is invalid. * @details \b Inputs: currentPresTempReadings * @details \b Outputs: none * @param sensor ID of pressure sensor to get temperature reading for. * @return The current pressure sensor temperature (in deg C) of the given pressure sensor. *************************************************************************/ F32 getPressureSensorTemperature( PRESSURE_SENSORS_T sensor ) { F32 result = 0.0F; if ( sensor < NUM_OF_PRESSURE_SENSORS ) { result = currentPresTempReadings[ sensor ].data; if ( OVERRIDE_KEY == currentPresTempReadings[ sensor ].override ) { result = currentPresTempReadings[ sensor ].ovData; } } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RO_SOFTWARE_FAULT, SW_FAULT_ID_PRESSURE_SENSOR_INVALID_SENSOR2, sensor ) } return result; } /*********************************************************************//** * @brief * The getPressureSensorReadCount function gets the current pressure sensor * read count for a given pressure sensor. * @details \b Alarm: ALARM_ID_RO_SOFTWARE_FAULT if given sensor is invalid. * @details \b Inputs: lastPressureReadCounter * @details \b Outputs: none * @param sensor ID of pressure sensor to get read count for. * @return The current pressure sensor read count of a given pressure sensor. *************************************************************************/ U32 getPressureSensorReadCount( PRESSURE_SENSORS_T sensor ) { U32 result = 0; if ( sensor < NUM_OF_PRESSURE_SENSORS ) { result = lastPressureReadCounter[ sensor ].data; if ( OVERRIDE_KEY == lastPressureReadCounter[ sensor ].override ) { result = lastPressureReadCounter[ sensor ].ovData; } } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RO_SOFTWARE_FAULT, SW_FAULT_ID_PRESSURE_SENSOR_INVALID_SENSOR3, sensor ) } return result; } /*********************************************************************//** * @brief * The getPressureSensorErrorCount function gets the current pressure sensor * error count for a given pressure sensor. * @details \b Alarm: ALARM_ID_RO_SOFTWARE_FAULT if given sensor is invalid. * @details \b Inputs: lastPressureErrorCounter * @details \b Outputs: none * @param sensor ID of pressure sensor to get error count for. * @return The current pressure sensor error count of a given pressure sensor. *************************************************************************/ U32 getPressureSensorErrorCount( PRESSURE_SENSORS_T sensor ) { U32 result = 0; if ( sensor < NUM_OF_PRESSURE_SENSORS ) { result = lastPressureErrorCounter[ sensor ].data; if ( OVERRIDE_KEY == lastPressureErrorCounter[ sensor ].override ) { result = lastPressureErrorCounter[ sensor ].ovData; } } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RO_SOFTWARE_FAULT, SW_FAULT_ID_PRESSURE_SENSOR_INVALID_SENSOR4, sensor ) } return result; } /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ /*********************************************************************//** * @brief * The testPressureSensorReadingsOverride function overrides the value of * the specified pressure sensor with a given value. * @details \b Inputs: none * @details \b Outputs: currentPressureReadings[] * @param message Override message from Dialin which includes an sensor * ID and override value of the pressure sensor. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testPressureSensorReadingsOverride( MESSAGE_T *message ) { BOOL result = f32ArrayOverride( message, ¤tPressureReadings[0], NUM_OF_PRESSURE_SENSORS - 1 ); return result; } /*********************************************************************//** * @brief * The testPressureSensorTemperatureReadingsOverride function overrides the * value of the specified pressure sensor temperature with a given value. * @details \b Inputs: none * @details \b Outputs: currentPresTempReadings[] * @param message Override message from Dialin which includes an sensor * ID and override value of the pressure sensor temperature. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testPressureSensorTemperatureReadingsOverride( MESSAGE_T *message ) { BOOL result = f32ArrayOverride( message, ¤tPresTempReadings[0], NUM_OF_PRESSURE_SENSORS - 1 ); return result; } /*********************************************************************//** * @brief * The testPressureSensorReadCounterOverride function overrides the value * of the specified pressure sensor read counter with a given value. * @details \b Inputs: none * @details \b Outputs: lastPressureReadCounter[] * @param message Override message from Dialin which includes an sensor * ID and override value of the pressure sensor read counter. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testPressureSensorReadCounterOverride( MESSAGE_T *message ) { BOOL result = u32ArrayOverride( message, &lastPressureReadCounter[0], NUM_OF_PRESSURE_SENSORS - 1, 0, PRES_SENSORS_READ_ERR_MAX_CNT ); return result; } /*********************************************************************//** * @brief * The testPressureSensorErrorCounterOverride function overrides the value * of the specified pressure sensor error counter with a given value. * @details \b Inputs: none * @details \b Outputs: lastPressureErrorCounter[] * @param message Override message from Dialin which includes an sensor * ID and override value of the pressure sensor error counter. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testPressureSensorErrorCounterOverride( MESSAGE_T *message ) { BOOL result = u32ArrayOverride( message, &lastPressureErrorCounter[0], NUM_OF_PRESSURE_SENSORS - 1, 0, PRES_SENSORS_READ_ERR_MAX_CNT ); return result; } /**@}*/