Index: firmware/App/Modes/OperationModes.c =================================================================== diff -u -ra31707ccbf01a1b40f8500bc491dc9c616e7a163 -rce247681f11b5e164e00992fb73ba5c595d349aa --- firmware/App/Modes/OperationModes.c (.../OperationModes.c) (revision a31707ccbf01a1b40f8500bc491dc9c616e7a163) +++ firmware/App/Modes/OperationModes.c (.../OperationModes.c) (revision ce247681f11b5e164e00992fb73ba5c595d349aa) @@ -1,17 +1,17 @@ /************************************************************************** * -* Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. +* Copyright (c) 2019-2022 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 OperationModes.c +* @file OperationModes.c * -* @author (last) Sean Nash -* @date (last) 24-Sep-2020 +* @author (last) Darren Cox +* @date (last) 25-Aug-2022 * -* @author (original) Dara Navaei -* @date (original) 05-Nov-2019 +* @author (original) Dara Navaei +* @date (original) 05-Nov-2019 * ***************************************************************************/ @@ -26,9 +26,12 @@ #include "ModePreTreat.h" #include "ModeTreatment.h" #include "ModePostTreat.h" +#include "NVDataMgmt.h" #include "OperationModes.h" +#include "RTC.h" #include "SystemCommMessages.h" #include "TaskGeneral.h" +#include "Timers.h" /** * @addtogroup HDOperationModes @@ -37,18 +40,19 @@ // ********** private data ********** -/// HD operation mode broadcast interval (in task interval/sec). -#define BROADCAST_HD_OP_MODE_INTERVAL ( 250 / TASK_GENERAL_INTERVAL ) +#define BROADCAST_HD_OP_MODE_INTERVAL ( 250 / TASK_GENERAL_INTERVAL ) ///< HD operation mode broadcast interval (in task interval/sec). +#define DATA_PUBLISH_COUNTER_START_COUNT 11 ///< Data publish counter start count. // ********** private data ********** static volatile BOOL modeRequest[ NUM_OF_MODES - 1 ]; ///< Pending operation mode change requests. static HD_OP_MODE_T lastMode = MODE_INIT; ///< Last operation mode prior to current mode. static HD_OP_MODE_T currentMode = MODE_INIT; ///< Current operation mode. static U32 currentSubMode = 0; ///< The currently active state of the active mode. -static U32 broadcastModeIntervalCtr = 11; ///< Interval counter used to determine when to broadcast operation mode. Initialize to 11 to stagger broadcast. +static U32 broadcastModeIntervalCtr; ///< Interval counter used to determine when to broadcast operation mode. Initialize to 11 to stagger broadcast. /// Interval (in task intervals) at which to publish operation mode data to CAN bus. static OVERRIDE_U32_T opModePublishInterval = { BROADCAST_HD_OP_MODE_INTERVAL, BROADCAST_HD_OP_MODE_INTERVAL, BROADCAST_HD_OP_MODE_INTERVAL, 0 }; +static U32 priorSubMode = 0; ///< The prior submode state. /// This matrix determines legal transitions from one mode to another static const HD_OP_MODE_T MODE_TRANSITION_TABLE[ NUM_OF_MODES - 1 ][ NUM_OF_MODES - 1 ] = { @@ -86,8 +90,11 @@ } // Start in init mode - currentMode = MODE_INIT; - currentSubMode = 0; + currentMode = MODE_INIT; + currentSubMode = 0; + priorSubMode = 0; + broadcastModeIntervalCtr = DATA_PUBLISH_COUNTER_START_COUNT; + transitionToNewOperationMode( MODE_INIT ); // Call initializers for the individual modes @@ -112,6 +119,8 @@ { HD_OP_MODE_T newMode; + priorSubMode = currentSubMode; + // Any new mode requests? newMode = arbitrateModeRequest(); // Will return current mode if no pending requests newMode = MODE_TRANSITION_TABLE[ currentMode ][ newMode ]; @@ -130,6 +139,16 @@ lastMode = currentMode; transitionToNewOperationMode( newMode ); currentMode = newMode; + + if ( MODE_TREA == lastMode ) + { + // If the current mode is treatment but transitioning to another mode has been requested (including transitioning to fault mode) + // it means the treatment is done. Get the elapsed time since the beginning of the treatment and convert it to hours to be written + U32 txElapsedTimeMS = calcTimeSince( getTreatmentStartTimeStamp() ); + F32 txElapsedTimeHrs = (F32)txElapsedTimeMS / ( MIN_PER_HOUR * SEC_PER_MIN * MS_PER_SECOND ); + // Write the treatment hours and set the service to be false so the treatment hours is not reset + setTxTimeHours( txElapsedTimeHrs, FALSE ); + } } // Mode specific processing to be done continuously @@ -174,6 +193,13 @@ break; } // End switch + // Send sub-mode change event when appropriate + if ( priorSubMode != currentSubMode ) + { + SEND_EVENT_WITH_2_U32_DATA( HD_EVENT_SUB_MODE_CHANGE, priorSubMode, currentSubMode ) + } + priorSubMode = currentSubMode; + // Broadcast current operation mode on interval broadcastOperationMode(); } @@ -323,33 +349,37 @@ switch ( newMode ) { case MODE_FAUL: - transitionToFaultMode(); + currentSubMode = transitionToFaultMode(); break; case MODE_SERV: - transitionToServiceMode(); + currentSubMode = transitionToServiceMode(); break; case MODE_INIT: - transitionToInitAndPOSTMode(); + currentSubMode = transitionToInitAndPOSTMode(); break; case MODE_STAN: - transitionToStandbyMode(); + currentSubMode = transitionToStandbyMode(); break; case MODE_TPAR: - transitionToTreatParamsMode(); + currentSubMode = transitionToTreatParamsMode(); break; case MODE_PRET: - transitionToPreTreatmentMode(); + currentSubMode = transitionToPreTreatmentMode(); break; case MODE_TREA: - transitionToTreatmentMode(); + currentSubMode = transitionToTreatmentMode(); break; case MODE_POST: - transitionToPostTreatmentMode(); + currentSubMode = transitionToPostTreatmentMode(); break; default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_OP_MODES_INVALID_MODE_TO_TRANSITION_TO, newMode ) break; } + + SEND_EVENT_WITH_2_U32_DATA( HD_EVENT_SUB_MODE_CHANGE, priorSubMode, currentSubMode ) + + priorSubMode = currentSubMode; } /*********************************************************************//** @@ -364,8 +394,12 @@ { if ( ++broadcastModeIntervalCtr >= getU32OverrideValue( &opModePublishInterval ) ) { + OP_MODE_PAYLOAD_T data; + broadcastModeIntervalCtr = 0; - broadcastHDOperationMode( (U32)currentMode, currentSubMode ); + data.opMode = (U32)currentMode; + data.subMode = currentSubMode; + broadcastData( MSG_ID_HD_OP_MODE, COMM_BUFFER_OUT_CAN_HD_BROADCAST, (U08*)&data, sizeof( OP_MODE_PAYLOAD_T ) ); } }