/************************************************************************** * * 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 FlowSensor.c * * @author (last) Sean Nash * @date (last) 21-Nov-2024 * * @author (original) Sean Nash * @date (original) 21-Nov-2024 * ***************************************************************************/ #include "AlarmMgmtRO.h" #include "FlowSensor.h" #include "FpgaRO.h" #include "Messaging.h" #include "PersistentAlarm.h" /** * @addtogroup FlowSensor * @{ */ // ********** private definitions ********** #define FLOW_TEMPERATURE_DIVIDER 10.0F ///< Divider for converting flow readings to mL/min flow rates. // ********** private data ********** static OVERRIDE_S32_T currentFlowReadings[ NUM_OF_FLOW_SENSORS ]; ///< Current flow sensor pressure readings (overrideable). static OVERRIDE_F32_T currentFlowTempReadings[ NUM_OF_FLOW_SENSORS ]; ///< Current flow sensor temperature readings (overrideable). // ********** private function prototypes ********** /*********************************************************************//** * @brief * The initFlowSensor function initializes the Flow Sensor unit. * @details \b Inputs: none * @details \b Outputs: Flow Sensor unit is initialized. * @return none *************************************************************************/ void initFlowSensor( void ) { FLOW_SENSORS_T sensor; // Initialize override structures for each pressure sensor for ( sensor = FLOW_SENSOR_FIRST; sensor < NUM_OF_FLOW_SENSORS; sensor++ ) { currentFlowReadings[ sensor ].data = 0; currentFlowReadings[ sensor ].ovData = 0; currentFlowReadings[ sensor ].ovInitData = 0; currentFlowReadings[ sensor ].override = OVERRIDE_RESET; currentFlowTempReadings[ sensor ].data = 0.0F; currentFlowTempReadings[ sensor ].ovData = 0.0F; currentFlowTempReadings[ sensor ].ovInitData = 0.0F; currentFlowTempReadings[ sensor ].override = OVERRIDE_RESET; } } /*********************************************************************//** * @brief * The readFlowSensors function gets the current flow rate reading * for a all flow sensors from the FPGA. * @note This function should be called periodically to maintain fresh * sensor readings for all flow sensors. * @details \b Inputs: FPGA * @details \b Outputs: currentFlowReadings[], currentFlowTempReadings[] * @return none *************************************************************************/ void readFlowSensors( void ) { // Update raw flow rates to mL/min currentFlowReadings[ P16_FLOW ].data = ( (S16)getFPGAFlowP16() ); currentFlowReadings[ P7_FLOW ].data = ( (S16)getFPGAFlowP7() ); // Update and convert raw flow sensor temperatures to deg C currentFlowTempReadings[ P16_FLOW ].data = (F32)( (S16)getFPGAFlowP16Temp() ) / FLOW_TEMPERATURE_DIVIDER; currentFlowTempReadings[ P7_FLOW ].data = (F32)( (S16)getFPGAFlowP7Temp() ) / FLOW_TEMPERATURE_DIVIDER; } /*********************************************************************//** * @brief * The getFlowRate function gets the current flow rate (in mL/min) for a given * flow sensor. * @details \b Alarm: ALARM_ID_RO_SOFTWARE_FAULT if given flow sensor is invalid. * @details \b Inputs: currentFlowReadings[] * @details \b Outputs: none * @param sensor ID of flow sensor to get flow rate for. * @return The current flow rate (in mL/min) of the given flow sensor. *************************************************************************/ S32 getFlowRate( FLOW_SENSORS_T sensor ) { S32 result = 0; if ( sensor < NUM_OF_FLOW_SENSORS ) { result = currentFlowReadings[ sensor ].data; if ( OVERRIDE_KEY == currentFlowReadings[ sensor ].override ) { result = currentFlowReadings[ sensor ].ovData; } } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_FP_SOFTWARE_FAULT, SW_FAULT_ID_FLOW_SENSOR_INVALID_SENSOR1, (U32)sensor ) } return result; } /*********************************************************************//** * @brief * The getFlowTemperature function gets the current flow sensor * temperature (in deg C) for a given flow sensor. * @details \b Alarm: ALARM_ID_RO_SOFTWARE_FAULT if given flow sensor is invalid. * @details \b Inputs: currentFlowTempReadings * @details \b Outputs: none * @param sensor ID of flow sensor to get flow rate for. * @return The current flow sensor temperature (in deg C) of the given flow sensor. *************************************************************************/ F32 getFlowTemperature( FLOW_SENSORS_T sensor ) { F32 result = 0.0F; if ( sensor < NUM_OF_FLOW_SENSORS ) { result = currentFlowTempReadings[ sensor ].data; if ( OVERRIDE_KEY == currentFlowTempReadings[ sensor ].override ) { result = currentFlowTempReadings[ sensor ].ovData; } } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_FP_SOFTWARE_FAULT, SW_FAULT_ID_FLOW_SENSOR_INVALID_SENSOR2, (U32)sensor ) } return result; } /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ /*********************************************************************//** * @brief * The testFlowSensorReadingsOverride function overrides the value of * the specified flow sensor with a given value. * @details \b Inputs: none * @details \b Outputs: currentFlowReadings[] * @param message Override message from Dialin which includes a sensor * ID and override value of the flow sensor. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testFlowSensorReadingsOverride( MESSAGE_T *message ) { BOOL result = s32ArrayOverride( message, ¤tFlowReadings[0], NUM_OF_FLOW_SENSORS - 1 ); return result; } /*********************************************************************//** * @brief * The testFlowSensorTemperatureReadingsOverride function overrides the * value of the specified flow sensor temperature with a given value. * @details \b Inputs: none * @details \b Outputs: currentFlowTempReadings[] * @param message Override message from Dialin which includes a sensor * ID and override value of the flow sensor temperature. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testFlowSensorTemperatureReadingsOverride( MESSAGE_T *message ) { BOOL result = f32ArrayOverride( message, ¤tFlowTempReadings[0], NUM_OF_FLOW_SENSORS - 1 ); return result; } /**@}*/