/************************************************************************** * * 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 * @date (last) 22-Aug-2024 * * @author (original) Sean * @date (original) 22-Aug-2024 * ***************************************************************************/ #include "FpgaTD.h" #include "PressureSensor.h" /** * @addtogroup PressureSensor * @{ */ // ********** private definitions ********** #define KPA_TO_MMHG ( 7.50062F ) ///< Conversion factor for converting kPa to mmHg. #define PRESSURE_FS ( 80.0F ) ///< Pressure sensor full scale percentage. #define PRESSURE_ZERO ( 0.0F ) ///< Pressure sensor zero value. #define PRESSURE_FS_RATIO ( 0.9F ) ///< Pressure sensor full scale ratio. #define PRESSURE_ZERO_RATIO ( 0.1F ) ///< Pressure sensor zero ratio. #define PRESSURE_DIVIDER ( 8388608.0F ) ///< Pressure sensor divider. /// Scaler value for converting pressure readings to kPa. #define PRESSURE_SCALER ( ( PRESSURE_FS - PRESSURE_ZERO ) / \ ( PRESSURE_FS_RATIO - PRESSURE_ZERO_RATIO ) ) #define PRESSURE_TEMP_DIVIDER ( 65536.0F ) ///< Pressure sensor temperature divider. #define PRESSURE_TEMP_OFFSET ( 25.0F ) ///< Pressure sensor temperature offset. // ********** 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 ********** 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 ) { currentPressureReadings[ PRESSURE_SENSOR_ARTERIAL ].data = 0; currentPressureReadings[ PRESSURE_SENSOR_ARTERIAL ].ovData = 0; currentPressureReadings[ PRESSURE_SENSOR_ARTERIAL ].ovInitData = 0; currentPressureReadings[ PRESSURE_SENSOR_ARTERIAL ].override = OVERRIDE_RESET; currentPressureReadings[ PRESSURE_SENSOR_VENOUS ].data = 0; currentPressureReadings[ PRESSURE_SENSOR_VENOUS ].ovData = 0; currentPressureReadings[ PRESSURE_SENSOR_VENOUS ].ovInitData = 0; currentPressureReadings[ PRESSURE_SENSOR_VENOUS ].override = OVERRIDE_RESET; } /*********************************************************************//** * @brief * The readPressureSensors function gets the current pressure reading * for a all pressure sensors from the FPGA. * @note This function should be called periodically to maintain fresh * sensor readings for all pressure sensors. * @details \b Inputs: FPGA * @details \b Outputs: none * @return none *************************************************************************/ void readPressureSensors( void ) { F32 pressure_kPa; // Convert raw pressures to kPa and then to mmHg pressure_kPa = ( ( (F32)getPBAPressure() / PRESSURE_DIVIDER ) - PRESSURE_ZERO_RATIO ) * PRESSURE_SCALER; currentPressureReadings[ PRESSURE_SENSOR_ARTERIAL ].data = pressure_kPa * KPA_TO_MMHG; pressure_kPa = ( ( (F32)getPBOPressure() / PRESSURE_DIVIDER ) - PRESSURE_ZERO_RATIO ) * PRESSURE_SCALER; currentPressureReadings[ PRESSURE_SENSOR_VENOUS ].data = pressure_kPa * KPA_TO_MMHG; // Convert raw pressure sensor temperatures to deg C currentPresTempReadings[ PRESSURE_SENSOR_ARTERIAL ].data = ( (F32)getPBATemperature() / PRESSURE_TEMP_DIVIDER ) + PRESSURE_TEMP_OFFSET; currentPresTempReadings[ PRESSURE_SENSOR_VENOUS ].data = ( (F32)getPBOTemperature() / PRESSURE_TEMP_DIVIDER ) + PRESSURE_TEMP_OFFSET; // Monitor pressure sensor health checkPressureSensors(); } // TODO a function that gets and checks read and error counters for both pressure sensors static void checkPressureSensors( void ) { //U08 getPBAReadCounter( void ); //U08 getPBAErrorCounter( void ); } /*********************************************************************//** * @brief * The getPressure function gets the current pressure (in mmHg) for a given * pressure sensor. * @details \b Alarm: ALARM_ID_TD_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_TD_SOFTWARE_FAULT, SW_FAULT_ID_PRESSURE_SENSOR_INVALID_SENSOR1, sensor ) } return result; } /*********************************************************************//** * @brief * The getPressure function gets the current pressure sensor temperature (in deg C) * for a given pressure sensor. * @details \b Alarm: ALARM_ID_TD_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_TD_SOFTWARE_FAULT, SW_FAULT_ID_PRESSURE_SENSOR_INVALID_SENSOR2, sensor ) } return result; } /**@}*/