/************************************************************************** * * Copyright (c) 2024-2025 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 DDInterface.c * * @author (last) Michael Garthwaite * @date (last) 03-Mar-2025 * * @author (original) Michael Garthwaite * @date (original) 03-Mar-2025 * ***************************************************************************/ #include "ROPump.h" #include "DDInterface.h" #include "Messaging.h" #include "MessagePayloads.h" #include "ModeStandby.h" #include "ModeWaterGen.h" #include "ModeWaterPreGen.h" #include "OperationModes.h" #include "PersistentAlarm.h" #include "SystemCommRO.h" #include "TaskGeneral.h" #include "Timers.h" /** * @addtogroup DDInterface * @{ */ // ********** private definitions ********** // ********** private data ********** static DD_OP_MODE_T ddCurrentOpMode; ///< Current DD operation mode. static U32 ddSubMode; ///< Current state (sub-mode) of current DD operation mode. static F32 ddPermeateFlowRate; ///< DD RO Permeate flow rate. // ********** private function prototypes ********** static void setDDPermeateFlowRate( F32 flowRate ); static BOOL handlePreGenCmd( BOOL start, F32 roRate ); static BOOL handleGenWaterCmd( BOOL start, F32 roRate ); /*********************************************************************//** * @brief * The initDDInterface function initializes the DD Interface unit. * @details \b Inputs: none * @details \b Outputs: DD Interface unit initialized. * @return none *************************************************************************/ void initDDInterface( void ) { // Initialize unit variables ddCurrentOpMode = DD_MODE_INIT; ddSubMode = 0; ddPermeateFlowRate = 0.0; } /*********************************************************************//** * @brief * The execDDInterfaceMonitor function executes the DD Interface monitoring * function. Ensures DD is sending fresh data in a timely manner. * @details \b Inputs: none * @details \b Outputs: none * @return none *************************************************************************/ void execDDInterfaceMonitor( void ) { // TODO: DD interface monitor. Ensure DD is sending fresh data in a timely manner. } /*********************************************************************//** * @brief * The getDDOpMode function gets the current latest reported DD operating mode. * @details \b Inputs: ddCurrentOpMode * @details \b Outputs: none * @return Latest reported DD operating mode. *************************************************************************/ DD_OP_MODE_T getDDOpMode( void ) { return ddCurrentOpMode; } /*********************************************************************//** * @brief * The getDDSubMode function gets the latest reported DD operating sub-mode. * @details \b Inputs: tdSubMode * @details \b Outputs: none * @return Latest reported DD operating sub-mode. *************************************************************************/ U32 getDDSubMode( void ) { return ddSubMode; } /*********************************************************************//** * @brief * The setDDPermeateFlowRate function sets the latest DD Permeate flow * rate. * @details \b Inputs: none * @details \b Outputs: ddPermeateFlowRate * @param flowRate. * @return none. *************************************************************************/ void setDDPermeateFlowRate( F32 flowRate ) { ddPermeateFlowRate = flowRate; } /*********************************************************************//** * @brief * The getDDPermeateFlowRate function sets the latest DD Permeate flow * rate. * @details \b Inputs: none * @details \b Outputs: ddPermeateFlowRate * @param Dialyzer Bypass enable. * @return latest DD Permeate Flow Rate. *************************************************************************/ F32 getDDPermeateFlowRate( void ) { return ddPermeateFlowRate; } /*********************************************************************//** * @brief * The handleGenerateWaterRequestMsg function handles a RO permeate * request from DD and updates permeate flow rate. * @details Inputs: none * @details Outputs: message handled * @param message a pointer to the message to handle * @return TRUE if message is sucessfully parsed, FALSE if not. *************************************************************************/ BOOL handleGenerateWaterRequestMsg( MESSAGE_T *message ) { BOOL result = FALSE; if ( message->hdr.payloadLen == sizeof( RO_WATER_REQ_PAYLOAD_T ) ) { RO_WATER_REQ_PAYLOAD_T cmd; memcpy( &cmd, message->payload, sizeof( RO_WATER_REQ_PAYLOAD_T ) ); switch ( cmd.cmdID ) { case RO_PRE_GEN: result = handlePreGenCmd( cmd.start, cmd.roRate ); break; case RO_GEN_WATER: result = handleGenWaterCmd( cmd.start, cmd.roRate ); break; default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_RO_SOFTWARE_FAULT, SW_FAULT_ID_RO_INVALID_RO_CMD, cmd.cmdID ) break; } } return result; } /*********************************************************************//** * @brief * The handlePreGenCmd function handles a RO pre generate water * request from DD and updates permeate flow rate. * @details Inputs: none * @details Outputs: message handled * @param start the TRUE FALSE bool to determine start or stop * @param roRate the flow rate from the dd to set the ro pump * @return TRUE if message is sucessfully parsed, FALSE if not. *************************************************************************/ static BOOL handlePreGenCmd( BOOL start, F32 roRate ) { BOOL result = FALSE; RO_OP_MODE_T roMode = getCurrentOperationMode(); if ( ( RO_MODE_STAN == roMode ) && ( TRUE == start ) ) { setDDPermeateFlowRate( roRate ); setBoostPumpPWMDutyCycle( P12_PUMP, roRate ); result = requestPreGenStart(); } else if ( RO_MODE_PGEN == roMode ) { if ( FALSE == start ) { signalROPumpStop( P12_PUMP ); result = requestPreGenStop(); } else { // Set flow rate and delivery. setDDPermeateFlowRate( roRate ); result = TRUE; } } return result; } /*********************************************************************//** * @brief * The handleGenWaterCmd function handles a generate water * request from DD and updates permeate flow rate. * @details Inputs: none * @details Outputs: message handled * @param start the TRUE FALSE bool to determine start or stop * @param roRate the flow rate from the dd to set the ro pump * @return TRUE if message is sucessfully parsed, FALSE if not. *************************************************************************/ static BOOL handleGenWaterCmd( BOOL start, F32 roRate ) { BOOL result = FALSE; RO_OP_MODE_T roMode = getCurrentOperationMode(); if ( ( RO_MODE_PGEN == roMode ) && ( TRUE == start ) ) { setDDPermeateFlowRate( roRate ); setBoostPumpPWMDutyCycle( P12_PUMP,roRate ); result = requestGenWaterStart(); } else if ( RO_MODE_GENW == roMode ) { if ( FALSE == start ) { signalROPumpStop( P12_PUMP ); result = requestGenWaterStop(); } else { // Set flow rate and delivery. setDDPermeateFlowRate( roRate ); result = TRUE; } } return result; } /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ /**@}*/