/************************************************************************** * * 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) Vinayakam Mani * @date (last) 04-Sep-2024 * * @author (original) Vinayakam Mani * @date (original) 04-Sep-2024 * ***************************************************************************/ #include "AlarmMgmtDD.h" #include "FpgaDD.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 HIGH_PRES_MAX_PSI 145.038F ///< Convert pressure to PSI for 10 bar pressure sensor #define LOW_PRES_MAX_PSI 50.7632F ///< Convert pressure to PSI for 3.5 bar pressure sensor #define PRES_MIN_PSI_ALPHA 0.0F ///< Minimum value for PSI conversion for alpha systems #define PRES_MIN_PSI -14.5038F ///< Minimum value for PSI conversion #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 255 ///< 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). // ********** private function prototypes ********** /*********************************************************************//** * @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 sensor; // Initialize override structures for each pressure sensor for ( sensor = (U32)FIRST_DD_PRESSURE_SENSOR; sensor < (U32)NUM_OF_PRESSURE_SENSORS; sensor++ ) { currentPressureReadings[ sensor ].data = 0.0F; currentPressureReadings[ sensor ].ovData = 0.0F; currentPressureReadings[ sensor ].ovInitData = 0.0F; currentPressureReadings[ sensor ].override = OVERRIDE_RESET; currentPresTempReadings[ sensor ].data = 0.0F; currentPresTempReadings[ sensor ].ovData = 0.0F; currentPresTempReadings[ sensor ].ovInitData = 0.0F; currentPresTempReadings[ sensor ].override = OVERRIDE_RESET; } } /*********************************************************************//** * @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 ) { F32 presMinPSI = PRES_MIN_PSI; // Update and convert raw pressures to mmHg currentPressureReadings[ D9_PRES ].data = convertPressureReading( getFPGAD9PresRawPressure(), presMinPSI, LOW_PRES_MAX_PSI ); currentPressureReadings[ D66_PRES ].data = convertPressureReading( getFPGAD66PresRawPressure(), presMinPSI, LOW_PRES_MAX_PSI ); currentPressureReadings[ D51_PRES ].data = convertPressureReading( getFPGAD51PresRawPressure(), presMinPSI, LOW_PRES_MAX_PSI ); currentPressureReadings[ D18_PRES ].data = convertPressureReading( getFPGAD18PresRawPressure(), presMinPSI, LOW_PRES_MAX_PSI ); currentPressureReadings[ D41_PRES ].data = convertPressureReading( getFPGAD41PresRawPressure(), presMinPSI, LOW_PRES_MAX_PSI ); currentPressureReadings[ M1_PRES ].data = convertPressureReading( getFPGAM1RawPressure(), presMinPSI, HIGH_PRES_MAX_PSI ); currentPressureReadings[ M3_PRES ].data = convertPressureReading( getFPGAM3RawPressure(), presMinPSI, HIGH_PRES_MAX_PSI ); currentPressureReadings[ P8_PRES ].data = convertPressureReading( getFPGAP8RawPressure(), presMinPSI, HIGH_PRES_MAX_PSI ); currentPressureReadings[ P13_PRES ].data = convertPressureReading( getFPGAP13RawPressure(), presMinPSI, HIGH_PRES_MAX_PSI ); currentPressureReadings[ P17_PRES ].data = convertPressureReading( getFPGAP17RawPressure(), presMinPSI, HIGH_PRES_MAX_PSI ); // Update and convert raw pressure sensor temperatures to deg C currentPresTempReadings[ D9_PRES ].data = convertPressureTempReading2DegC( getFPGAD9PresRawTemperature() ); currentPresTempReadings[ D66_PRES ].data = convertPressureTempReading2DegC( getFPGAD66PresRawTemperature() ); currentPresTempReadings[ D51_PRES ].data = convertPressureTempReading2DegC( getFPGAD51PresRawTemperature() ); currentPresTempReadings[ D18_PRES ].data = convertPressureTempReading2DegC( getFPGAD18PresRawTemperature() ); currentPresTempReadings[ D41_PRES ].data = convertPressureTempReading2DegC( getFPGAD41PresRawTemperature() ); currentPresTempReadings[ M1_PRES ].data = convertPressureTempReading2DegC( getFPGAM1RawTemperature() ); currentPresTempReadings[ M3_PRES ].data = convertPressureTempReading2DegC( getFPGAM3RawTemperature() ); currentPresTempReadings[ P8_PRES ].data = convertPressureTempReading2DegC( getFPGAP8RawTemperature() ); currentPresTempReadings[ P13_PRES ].data = convertPressureTempReading2DegC( getFPGAP13RawTemperature() ); currentPresTempReadings[ P17_PRES ].data = convertPressureTempReading2DegC( getFPGAP17RawTemperature() ); } /*********************************************************************//** * @brief * The getPressure function gets the current pressure (in mmHg) for a given * pressure sensor. * @details \b Alarm: ALARM_ID_DD_SOFTWARE_FAULT if given 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_DD_SOFTWARE_FAULT, SW_FAULT_ID_PRESSURE_SENSOR_INVALID_SENSOR1, 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_DD_SOFTWARE_FAULT if given 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_DD_SOFTWARE_FAULT, SW_FAULT_ID_PRESSURE_SENSOR_INVALID_SENSOR2, sensor ) } return result; } /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ /*********************************************************************//** * @brief * The testDDPressureSensorReadingsOverride function overrides the value of the * specified DD 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 DD pressure sensor. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testDDPressureSensorReadingsOverride( MESSAGE_T *message ) { BOOL result = f32ArrayOverride( message, ¤tPressureReadings[0], LAST_DD_PRESSURE_SENSOR ); return result; } /*********************************************************************//** * @brief * The testDDPressureSensorTemperatureReadingsOverride function overrides the value of the * specified DD 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 DD pressure sensor temperature. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testDDPressureSensorTemperatureReadingsOverride( MESSAGE_T *message ) { BOOL result = f32ArrayOverride( message, ¤tPresTempReadings[0], LAST_DD_PRESSURE_SENSOR ); return result; } /*********************************************************************//** * @brief * The testIOFPPressureSensorReadingsOverride function overrides the value of * the specified IOFP 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 IOFP pressure sensor. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testIOFPPressureSensorReadingsOverride( MESSAGE_T *message ) { BOOL result = FALSE; TEST_OVERRIDE_ARRAY_PAYLOAD_T payload; OVERRIDE_TYPE_T ovType = getOverrideArrayPayloadFromMessage( message, &payload ); if ( ( payload.index >= FIRST_IO_PRESSURE_SENSOR ) && ( payload.index <= LAST_FP_PRESSURE_SENSOR ) ) { result = f32ArrayOverride( message, ¤tPressureReadings[0], NUM_OF_PRESSURE_SENSORS - 1 ); } return result; } /*********************************************************************//** * @brief * The testIOFPPressureSensorTemperatureReadingsOverride function overrides the * value of the specified IOFP 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 IOFP pressure sensor temperature. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testIOFPPressureSensorTemperatureReadingsOverride( MESSAGE_T *message ) { BOOL result = FALSE; TEST_OVERRIDE_ARRAY_PAYLOAD_T payload; OVERRIDE_TYPE_T ovType = getOverrideArrayPayloadFromMessage( message, &payload ); if ( ( payload.index >= FIRST_IO_PRESSURE_SENSOR ) && ( payload.index <= LAST_FP_PRESSURE_SENSOR ) ) { result = f32ArrayOverride( message, ¤tPresTempReadings[0], NUM_OF_PRESSURE_SENSORS - 1 ); } return result; } /**@}*/