/************************************************************************** * * Copyright (c) 2020-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 BoostPump.c * * @author (last) Sean Nash * @date (last) 12-Nov-2024 * * @author (original) Sean Nash * @date (original) 12-Nov-2024 * ***************************************************************************/ #include "BoostPump.h" #include "FpgaRO.h" #include "Messaging.h" #include "MessageSupport.h" #include "PersistentAlarm.h" #include "Timers.h" #include "Utilities.h" /** * @addtogroup BoostPump * @{ */ // ********** private definitions ********** #define BOOST_PUMP_COUNTS_2_RPM_NUMERATOR 1500000.0F ///< Numerator in counts to RPM conversion. #define BOOST_PUMP_COUNTS_FOR_STOPPED_PUMP 0xFFFF ///< FPGA will report FFFF counts when pump is stopped. /// Payload record structure for boost pump set PWM request typedef struct { U32 pumpID; ///< Pump ID U32 pwm; ///< Pump PWM magnitude (0..500). } BOOST_PUMP_CMD_PAYLOAD_T; // ********** private data ********** static U16 boostPumpCmdDutyCycle[ NUM_OF_BOOST_PUMPS ]; ///< Commanded duty cycles for boost pumps. static OVERRIDE_U32_T boostPumpReadDutyCycle[ NUM_OF_BOOST_PUMPS ]; ///< Boost pump duty cycles read back from FPGA. static OVERRIDE_F32_T boostPumpMeasRPM[ NUM_OF_BOOST_PUMPS ]; ///< Boost pump measured speeds (RPM). // ********** private function prototypes ********** /*********************************************************************//** * @brief * The initBoostPump function initializes the Boost Pump driver unit. * @details \b Inputs: none * @details \b Outputs: Boost pump unit initialized. * @return none *************************************************************************/ void initBoostPump( void ) { U32 pump; // initialize duty cycles and measured speeds for ( pump = 0; pump < NUM_OF_BOOST_PUMPS; pump++ ) { boostPumpCmdDutyCycle[ pump ] = 0; boostPumpReadDutyCycle[ pump ].data = 0; boostPumpReadDutyCycle[ pump ].ovData = 0; boostPumpReadDutyCycle[ pump ].ovInitData = 0; boostPumpReadDutyCycle[ pump ].override = OVERRIDE_RESET; boostPumpMeasRPM[ pump ].data = 0.0F; boostPumpMeasRPM[ pump ].ovData = 0.0F; boostPumpMeasRPM[ pump ].ovInitData = 0.0F; boostPumpMeasRPM[ pump ].override = OVERRIDE_RESET; } // set RO pump to stop setROPumpPWMPct( 0 ); // TODO set boost pump to stop } /*********************************************************************//** * @brief * The readBoostPumps function reads the PWM and tach counts for each boost pump. * @details \b Inputs: none * @details \b Outputs: boostPumpReadDutyCycle[], boostPumpMeasRPM[] * @return none *************************************************************************/ void readBoostPumps( void ) { U32 tach; // get latest RO pump duty cycle read back from FPGA boostPumpReadDutyCycle[ RO_PUMP ].data = getROPumpPWMPct(); // get latest RO pump tachometer count from FPGA and convert to RPM tach = (U32)getROPumpTachCount(); if ( ( tach != 0 ) && ( tach < BOOST_PUMP_COUNTS_FOR_STOPPED_PUMP ) ) { boostPumpMeasRPM[ RO_PUMP ].data = BOOST_PUMP_COUNTS_2_RPM_NUMERATOR / (F32)tach; } else { boostPumpMeasRPM[ RO_PUMP ].data = 0.0F; } // TODO - get latest for booster pump PWM and tach } /*********************************************************************//** * @brief * The setBoostPumpPWMDutyCycle function sets the commanded PWM magnitude (0..500) * for the given boost pump. * @details \b Alarm: ALARM_ID_RO_SOFTWARE_FAULT if invalid boost pump ID given. * @details \b Inputs: none * @details \b Outputs: boostPumpCmdDutyCycle[] * @param pumpID ID of boost pump to set commanded PWM magnitude for. * @param pwm Value (0..500) indicating magnitude of PWM duty cycle to set * @return TRUE if duty cycle set successfully, FALSE if not *************************************************************************/ BOOL setBoostPumpPWMDutyCycle( RO_BOOST_PUMP_T pumpID, U16 pwm ) { BOOL result = FALSE; if ( pumpID < NUM_OF_BOOST_PUMPS ) { // enable/disable pump per given PWM if ( 0 == pwm ) { setROPumpEnable( FALSE ); } else { setROPumpEnable( TRUE ); } // constrain given pwm magnitude to valid range pwm = MIN( pwm, MAX_BOOST_PUMP_PWM_DUTY_CYCLE ); // set commanded duty cycle for given boost pump to given pwm magnitude boostPumpCmdDutyCycle[ pumpID ] = pwm; if ( RO_PUMP == pumpID ) { setROPumpPWMPct( pwm ); } else { // TODO set boost pump PWM } result = TRUE; } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RO_SOFTWARE_FAULT, SW_FAULT_ID_INVALID_BOOST_PUMP_ID1, (U32)pumpID ) } return result; } /*********************************************************************//** * @brief * The getBoostPumpPWMDutyCycle function gets the commanded PWM magnitude (0..500) * for the given boost pump. * @details \b Alarm: ALARM_ID_RO_SOFTWARE_FAULT if invalid boost pump ID given. * @details \b Inputs: boostPumpCmdDutyCycle[] * @details \b Outputs: none * @param pumpID ID of boost pump to get commanded PWM magnitude for. * @return Value (0..500) indicating magnitude of PWM duty cycle *************************************************************************/ U16 getBoostPumpPWMDutyCycle( RO_BOOST_PUMP_T pumpID ) { U16 result = 0; if ( pumpID < NUM_OF_BOOST_PUMPS ) { result = boostPumpCmdDutyCycle[ pumpID ]; } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RO_SOFTWARE_FAULT, SW_FAULT_ID_INVALID_BOOST_PUMP_ID2, (U32)pumpID ) } return result; } /*********************************************************************//** * @brief * The getBoostPumpReadPWMDutyCycle function gets the PWM magnitude (0..500) * read back from FPGA for the given boost pump. * @details \b Alarm: ALARM_ID_RO_SOFTWARE_FAULT if invalid boost pump ID given. * @details \b Inputs: boostPumpReadDutyCycle[] * @details \b Outputs: none * @param pumpID ID of boost pump to get read back PWM magnitude for. * @return Value (0..500) indicating magnitude of PWM duty cycle *************************************************************************/ U16 getBoostPumpReadPWMDutyCycle( RO_BOOST_PUMP_T pumpID ) { U16 result = 0; if ( pumpID < NUM_OF_BOOST_PUMPS ) { result = (U16)getU32OverrideValue( &boostPumpReadDutyCycle[ pumpID ] ); } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RO_SOFTWARE_FAULT, SW_FAULT_ID_INVALID_BOOST_PUMP_ID3, (U32)pumpID ) } return result; } /*********************************************************************//** * @brief * The getBoostPumpRPM function gets the measured speed (RPM) for the given * boost pump. * @details \b Alarm: ALARM_ID_RO_SOFTWARE_FAULT if invalid boost pump ID given. * @details \b Inputs: boostPumpMeasRPM[] * @details \b Outputs: none * @param pumpID ID of boost pump to get measured speed for. * @return measured pump speed (RPM) for the given pump *************************************************************************/ F32 getBoostPumpRPM( RO_BOOST_PUMP_T pumpID ) { F32 result = 0.0F; if ( pumpID < NUM_OF_BOOST_PUMPS ) { result = (U16)getF32OverrideValue( &boostPumpMeasRPM[ pumpID ] ); } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RO_SOFTWARE_FAULT, SW_FAULT_ID_INVALID_BOOST_PUMP_ID4, (U32)pumpID ) } return result; } /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ /*********************************************************************//** * @brief * The testSetBoostPumpPWM function sets a given boost pump to a given PWM * value. * @details \b Inputs: none * @details \b Outputs: boostPumpCmdDutyCycle[] * @param message Override message from Dialin which includes an ID of * the pump to override and the PWM to override the pump to. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testSetBoostPumpPWM( MESSAGE_T *message ) { BOOL result = FALSE; if ( TRUE == isTestingActivated() ) { if ( sizeof( BOOST_PUMP_CMD_PAYLOAD_T ) == message->hdr.payloadLen ) { BOOST_PUMP_CMD_PAYLOAD_T payload; memcpy( &payload, &message->payload[0], sizeof( BOOST_PUMP_CMD_PAYLOAD_T ) ); result = setBoostPumpPWMDutyCycle( (RO_BOOST_PUMP_T)payload.pumpID, (U16)payload.pwm ); } } return result; } /*********************************************************************//** * @brief * The testBoostPumpPWMOverride function overrides the measured PWM of the * given boost pump. * @details \b Inputs: none * @details \b Outputs: boostPumpReadDutyCycle[] * @param message Override message from Dialin which includes an ID of * the boost pump to override PWM for and the PWM to override the pump to. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testBoostPumpPWMOverride( MESSAGE_T *message ) { BOOL result = u32ArrayOverride( message, &boostPumpReadDutyCycle[0], NUM_OF_BOOST_PUMPS - 1, 0, MAX_BOOST_PUMP_PWM_DUTY_CYCLE ); return result; } /*********************************************************************//** * @brief * The testBoostPumpRPMOverride function overrides the measured speed (RPM) * of the given boost pump. * @details \b Inputs: none * @details \b Outputs: boostPumpMeasRPM[] * @param message Override message from Dialin which includes an ID of * the boost pump to override speed for and the speed to override the pump to. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testBoostPumpRPMOverride( MESSAGE_T *message ) { BOOL result = f32ArrayOverride( message, &boostPumpMeasRPM[0], NUM_OF_BOOST_PUMPS - 1 ); return result; } /**@}*/