/************************************************************************** * * Copyright (c) 2022-2023 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 AirPump.c * * @author (last) Sean Nash * @date (last) 28-Mar-2023 * * @author (original) Michael Garthwaite * @date (original) 21-Nov-2022 * ***************************************************************************/ #include "reg_het.h" #include "gio.h" #include "AirPump.h" #include "AlarmMgmt.h" #include "ModeTreatmentParams.h" #include "OperationModes.h" #include "PersistentAlarm.h" #include "SystemCommMessages.h" #include "TaskGeneral.h" #include "TaskPriority.h" #include "Timers.h" /** * @addtogroup AirPump * @{ */ // ********** private definitions ********** #define AIR_PUMP_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) #define DATA_PUBLISH_COUNTER_START_COUNT 13 #define AIR_PUMP_GPIO_PIN 0x04 #define AIR_PUMP_OPERATION_TIMEOUT ( 10 * MS_PER_SECOND ) typedef enum AirPumpMotorStates { AIR_PUMP_MOTOR_OFF = 0, ///< Air Pump Off AIR_PUMP_MOTOR_ON, ///< Air Pump On NUM_OF_AIR_PUMP_MOTOR_STATES, } AIR_PUMP_MOTOR_STATE_T; // ********** private data ********** static AIR_PUMP_STATE_T currentAirPumpState; static U32 airPumpDataPublicationTimerCounter; static OVERRIDE_U32_T airPumpDataPublishInterval = { AIR_PUMP_DATA_PUB_INTERVAL, AIR_PUMP_DATA_PUB_INTERVAL, 0, 0 }; static AIR_PUMP_MOTOR_STATE_T currentAirPumpMotorState; // ********** private function prototypes ********** static AIR_PUMP_STATE_T handleAirPumpStartState( void ); static AIR_PUMP_STATE_T handleAirPumpOffState( void ); static AIR_PUMP_STATE_T handleAirPumpOnState ( void ); static void publishAirPumpData( void ); static void execAirPumpMonitor( void ); static void setAirPumpMotor( AIR_PUMP_MOTOR_STATE_T state ); static AIR_PUMP_MOTOR_STATE_T getAirPumpMotorState( void ); /*********************************************************************//** * @brief * The initAirPump function initializes the valve driver. * @details Inputs: none * @details Outputs: currentAirPumpState, currentAirPumpMotorState * @return none *************************************************************************/ void initAirPump(void) { airPumpDataPublicationTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; currentAirPumpState = AIR_PUMP_STATE_INIT; currentAirPumpMotorState = AIR_PUMP_MOTOR_OFF; initPersistentAlarm( ALARM_ID_HD_AIR_PUMP_TIMEOUT, 0, AIR_PUMP_OPERATION_TIMEOUT ); } /*********************************************************************//** * @brief * The setAirPumpMotor function sets the air pump Pin. * @details Inputs: AIR_PUMP_GPIO_PIN, hetPORT1 * @details Outputs: none * @param state HIGH/LOW for the air pump pin. * @return none. *************************************************************************/ static void setAirPumpMotor( AIR_PUMP_MOTOR_STATE_T state ) { if ( state < NUM_OF_AIR_PUMP_MOTOR_STATES ) { gioSetBit( hetPORT1, AIR_PUMP_GPIO_PIN, (U32)state ); currentAirPumpMotorState = state; } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_HD_AIR_PUMP_INVALID_MOTOR_STATE_SELECTED, (U32)state ) } } /*********************************************************************//** * @brief * The getAirPumpMotorState function returns the current air pump motor state. * @details Inputs: currentAirPumpMotorState * @details Outputs: currentAirPumpMotorState * @return current state of the air pump state machine. *************************************************************************/ static AIR_PUMP_MOTOR_STATE_T getAirPumpMotorState( void ) { return currentAirPumpMotorState; } /*********************************************************************//** * @brief * The setAirPumpState function sets the current air pump state machine state. * @details Inputs: currentAirPumpState * @details Outputs: currentAirPumpState * @return none *************************************************************************/ void setAirPumpState( AIR_PUMP_STATE_T state ) { if ( state < NUM_OF_AIR_PUMP_STATES ) { currentAirPumpState = state; } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_HD_AIR_PUMP_INVALID_STATE, (U32)state ) } } /*********************************************************************//** * @brief * The getAirPumpState function returns the current air pump state machine. * @details Inputs: currentAirPumpState * @details Outputs: currentAirPumpState * @return current state of the air pump state machine. *************************************************************************/ AIR_PUMP_STATE_T getAirPumpState( void ) { return currentAirPumpState; } /*********************************************************************//** * @brief * The execAirPumpController function executes the air pump state machine. * @details Inputs: currentAirPumpState * @details Outputs: currentAirPumpState * @return none *************************************************************************/ void execAirPumpController( void ) { switch( currentAirPumpState ) { case AIR_PUMP_STATE_INIT: currentAirPumpState = handleAirPumpStartState(); break; case AIR_PUMP_STATE_OFF: currentAirPumpState = handleAirPumpOffState(); break; case AIR_PUMP_STATE_ON: currentAirPumpState = handleAirPumpOnState(); break; default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_HD_AIR_PUMP_INVALID_STATE, (U32)currentAirPumpState ) break; } execAirPumpMonitor(); publishAirPumpData(); } /*********************************************************************//** * @brief * The execAirPumpMonitor function executes the air pump monitor. * @details Inputs: HD operation mode * @details Outputs: none * @return none *************************************************************************/ void execAirPumpMonitor( void ) { if ( MODE_TREA == getCurrentOperationMode() ) { BOOL isAirPumpOn = ( AIR_PUMP_MOTOR_OFF == getAirPumpMotorState() ? FALSE : TRUE ); if ( TRUE == isPersistentAlarmTriggered( ALARM_ID_HD_AIR_PUMP_TIMEOUT, isAirPumpOn ) ) { SET_ALARM_WITH_1_U32_DATA( ALARM_ID_HD_AIR_PUMP_TIMEOUT, isAirPumpOn ); setAirPumpState( AIR_PUMP_STATE_OFF ); } } } /*********************************************************************//** * @brief * The handleAirPumpStartState function starts the air pump state machine. * @details Inputs: none * @details Outputs: none * @return next state of the air pump state machine *************************************************************************/ static AIR_PUMP_STATE_T handleAirPumpStartState( void ) { AIR_PUMP_STATE_T state = AIR_PUMP_STATE_OFF; return state; } /*********************************************************************//** * @brief * The handleAirPumpOffState function stops the air pump * @details Inputs: none * @details Outputs: none * @return next state of the air pump state machine *************************************************************************/ static AIR_PUMP_STATE_T handleAirPumpOffState( void ) { AIR_PUMP_STATE_T state = AIR_PUMP_STATE_OFF; setAirPumpMotor( AIR_PUMP_MOTOR_OFF ); return state; } /*********************************************************************//** * @brief * The handleAirPumpOnState function starts the air pump * @details Inputs: none * @details Outputs: none * @return next state of the air pump state machine *************************************************************************/ static AIR_PUMP_STATE_T handleAirPumpOnState( void ) { AIR_PUMP_STATE_T state = AIR_PUMP_STATE_ON; setAirPumpMotor( AIR_PUMP_MOTOR_ON ); return state; } /*********************************************************************//** * @brief * The publishAirPumpData function constructs and sends the air pump data * broadcast message. * @details Inputs: currentAirPumpState * @details Outputs: data * @return none *************************************************************************/ static void publishAirPumpData( void ) { if (++airPumpDataPublicationTimerCounter >= getU32OverrideValue( &airPumpDataPublishInterval ) ) { AIR_PUMP_PAYLOAD_T data; data.airPumpStateStatus = getAirPumpState(); broadcastData( MSG_ID_HD_AIR_PUMP_DATA, COMM_BUFFER_OUT_CAN_HD_BROADCAST, (U08*)&data, sizeof( AIR_PUMP_PAYLOAD_T ) ); airPumpDataPublicationTimerCounter = 0; } } /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ /*********************************************************************//** * @brief * The testSetAirPumpDataPublishIntervalOverride function overrides the * air pump data publish interval. * @details Inputs: none * @details Outputs: airPumpDataPublishInterval * @param value override air trap data publish interval with (in ms) * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testSetAirPumpDataPublishIntervalOverride( U32 value ) { BOOL result = FALSE; if ( TRUE == isTestingActivated() ) { U32 intvl = value / TASK_GENERAL_INTERVAL; result = TRUE; airPumpDataPublishInterval.ovData = intvl; airPumpDataPublishInterval.override = OVERRIDE_KEY; } return result; } /*********************************************************************//** * @brief * The testResetAirPumpDataPublishIntervalOverride function resets the override * of the air pump data publish interval. * @details Inputs: none * @details Outputs: airPumpDataPublishInterval * @return TRUE if override reset successful, FALSE if not *************************************************************************/ BOOL testResetAirPumpDataPublishIntervalOverride( void ) { BOOL result = FALSE; if ( TRUE == isTestingActivated() ) { result = TRUE; airPumpDataPublishInterval.override = OVERRIDE_RESET; airPumpDataPublishInterval.ovData = airPumpDataPublishInterval.ovInitData; } return result; } /*********************************************************************//** * @brief * The testSetAirPump function sets the Air Pump state. * @details Inputs: none * @details Outputs: currentAirPumpState * @param state, the air pump state to be set to. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testSetAirPump( U32 state ) { BOOL result = FALSE; if ( TRUE == isTestingActivated() ) { setAirPumpState( ( AIR_PUMP_STATE_T ) state ); result = TRUE; } return result; } /**@}*/