Index: NVDataMgmt.c =================================================================== diff -u -rdfc9d962dff2816ce9e98b820bb89ad316b031a9 -r2d3cc187e8103c7dec7e98c324607c737fc7f3fa --- NVDataMgmt.c (.../NVDataMgmt.c) (revision dfc9d962dff2816ce9e98b820bb89ad316b031a9) +++ NVDataMgmt.c (.../NVDataMgmt.c) (revision 2d3cc187e8103c7dec7e98c324607c737fc7f3fa) @@ -18,6 +18,7 @@ // Includes #include // For memcpy +#include // For ceil function #include "system.h" #include "F021.h" @@ -26,6 +27,7 @@ #include "NVDataMgmt.h" #include "RTC.h" #include "SystemCommMessages.h" +#include "TaskGeneral.h" #include "Timers.h" #include "Utilities.h" @@ -86,6 +88,12 @@ #define ERASE_CALIBRATION_KEY 0xD2C3B4A5 ///< 32-bit key required for clearing calibration data. +// ********** Calibration data defines ********** +#define NUM_OF_CONCENTRATE_PUMPS 2U ///< Number of concentrate pumps +#define NUM_OF_BYTES_PER_CAL_PAYLOAD 150U ///< Number of bytes per calibration payload +#define CAL_DATA_SEND_INTERVAL_COUNT ( MS_PER_SECOND / ( 5 * TASK_GENERAL_INTERVAL ) ) ///< Calibration data send time interval in counts + + /// NVDataMgmt self-test states enumeration. typedef enum NVDataMgmt_Self_Test_States { @@ -135,6 +143,14 @@ NUM_OF_NVDATAMGMT_LOC_STATES ///< Total number of location states } NVDATAMGMT_LOCATION_STATE_T; +/// NVDataMgmt calibration states +typedef enum NVDataMgmt_Calibration_State +{ + NVDATAMGMT_CAL_STATE_IDLE = 0, ///< NVDataMgmt calibration state idle + NVDATAMGMT_CAL_STATE_SEND_RECORD, ///< NVDataMgmt calibration state send record + NUM_OF_NVDATAMGMT_CAL_STATES ///< Number of NVDataMgmt calibration state +} NVDATAMGMT_CAL_STATE_T; + #pragma pack(push, 1) /// Memory operations structure. typedef struct @@ -198,18 +214,92 @@ CALIBRATION_DATA_T calData; ///< Calibration data struct U16 crc; ///< Calibration data CRC U08 Padding[ 0x50 - sizeof(CALIBRATION_DATA_T) - sizeof(U16) ]; ///< Padding for reserved cal data space -} CALIBRATION_RECORD_T; +} CALIBRATION_RECORD_T; //TODO remove /// Last disinfection record structure. typedef struct { DISINFECTION_DATE_T date; ///< Disinfection date (char array) U16 crc; ///< Disinfection date CRC } LAST_DISINFECTION_RECORD_T; + +// ********** Calibration data struts ********** + +/// Pressure sensors calibration structure +typedef struct +{ + LINEAR_CAL_PAYLOAD_T pressureSensors[ NUM_OF_CAL_DATA_PRES_SENSORS ]; ///< Pressure sensors to calibrate + // NOTE: The reserved space is for 6 sensors. This portion of the struct should be eventually be + // reduced, so #define was not used for the size of the array + LINEAR_CAL_PAYLOAD_T reservedSpace[ 6 ]; ///< Reserved space for future pressure sensors +} DG_PRES_SENSORS_CAL_RECORD_T; + +/// Flow sensors calibration structure +typedef struct +{ + LINEAR_CAL_PAYLOAD_T flowSensors[ NUM_OF_CAL_DATA_FLOW_SENSORS ]; ///< Flow sensors to calibrate + // NOTE: The reserved space is for 6 sensors. This portion of the struct should be eventually be + // reduced, so #define was not used for the size of the array + LINEAR_CAL_PAYLOAD_T reservedSpace[ 3 ]; ///< Reserved space for future flow sensors +} DG_FLOW_SENSORS_CAL_RECORD_T; + +/// Load cells calibration structure +typedef struct +{ + LINEAR_CAL_PAYLOAD_T loadCells[ NUM_OF_CAL_DATA_LOAD_CELLS ]; ///< Load cells to calibrate +} DG_LOAD_CELLS_CAL_RECORD_T; + +/// Temperature sensors calibration structure +typedef struct +{ + LINEAR_CAL_PAYLOAD_T tempSensors[ NUM_OF_CAL_DATA_TEMP_SENSORS ]; ///< Temperature sensors to calibrate + // NOTE: The reserved space is for 6 sensors. This portion of the struct should be eventually be + // reduced, so #define was not used for the size of the array + LINEAR_CAL_PAYLOAD_T reservedSpace[ 5 ]; ///< Reserved space for future temp sensors +} DG_TEMP_SENSORS_CAL_RECORD_T; + +/// Conductivity sensors calibration structure +typedef struct +{ + LINEAR_CAL_PAYLOAD_T condSensors[ NUM_OF_CAL_DATA_COND_SENSORS ]; ///< Conductivity sensors to calibrate + // NOTE: The reserved space is for 6 sensors. This portion of the struct should be eventually be + // reduced, so #define was not used for the size of the array + LINEAR_CAL_PAYLOAD_T reservedSpace[ 2 ]; ///< Reserved space for future conductivity sensors +} DG_COND_SENSORS_CAL_RECORD_T; + +/// DG pumps calibration structure +typedef struct +{ + // Enum was not used for this array because the DG pumps enum contain DG and RO pumps too + DG_CONCENTRATE_PUMPS_PAYLOAD_T concentratePump[ NUM_OF_CONCENTRATE_PUMPS ]; ///< Concentrate pumps ratios + DG_DRAIN_PUMP_PAYLOAD_T drainPump; ///< Drain pump ratios + DG_RO_PUMP_PAYLOAD_T roPump; ///< RO pump ratios (unknown for now) +} DG_PUMPS_CAL_RECORD_T; + +/// DG calibration records structure +typedef struct +{ + DG_PRES_SENSORS_CAL_RECORD_T presSensorsCalRecord; ///< DG pressure sensors + DG_FLOW_SENSORS_CAL_RECORD_T flowSensorsCalRecord; ///< DG flow sensors + DG_LOAD_CELLS_CAL_RECORD_T loadCellsCalRecord; ///< DG load cells + DG_TEMP_SENSORS_CAL_RECORD_T tempSensorsCalRecord; ///< DG temperature sensors + DG_COND_SENSORS_CAL_RECORD_T condSensorsCalRecord; ///< DG conductivity sensors + //DG_PUMPS_CAL_RECORD_T pumpsCalRecord; ///< DG pumps + U16 crc; ///< CRC for the DG cal record structure +} DG_CALIBRATION_RECORD_T; #pragma pack(pop) +// Calibration variables +static NVDATAMGMT_CAL_STATE_T NVDataMgmtExecCalState = NVDATAMGMT_CAL_STATE_IDLE; ///< NVDataMgmt calibration state +static DG_CALIBRATION_RECORD_T dgCalibrationRecord; ///< DG calibration record +static BOOL isPublishCalRequested = FALSE; ///< Calibration state machine flag +static U32 calPublishMessageCount = 1; ///< Calibration data publish message counter +static U32 calPublishTotalMessages = 1; ///< Calibration data total number of messages to be sent +static U32 calSendDataIntervalCounter = 0; ///< Calibration data send to CAN bust interval counter +static U32 previousCalMessageNum = 0; ///< Calibration previous message number +static U32 dgCalRecordUpdateAddress = 0; ///< DG calibration record update address + // Private variables -//static static MEMORY_OPS_T jobQueue [ QUEUE_MAX_SIZE ]; ///< Job queue buffer static MEMORY_OPS_T currentJob; ///< Current job static LOG_RECORD_T logRecord; ///< Log record variable @@ -256,6 +346,9 @@ static NVDATAMGMT_EXEC_STATE_T handleExecWriteToRAMState ( void ); static NVDATAMGMT_EXEC_STATE_T handleExecReadFromRAMState ( void ); +static NVDATAMGMT_CAL_STATE_T handleExecCalIdleState( void ); +static NVDATAMGMT_CAL_STATE_T handleExecCalSendRecordState( void ); + // Queue functions static void setMemoryOpsStruct ( NVDATAMGMT_OPERATION_STATE_T ops, NVDATAMGMT_LOCATION_STATE_T location, @@ -284,16 +377,34 @@ * queueRearIndex, queueFrontIndex, queueCount * @return none *************************************************************************/ -void initNVDataMgmt ( void ) +void initNVDataMgmt( void ) { NVDataMgmtSelfTestState = NVDATAMGMT_SELF_TEST_STATE_START; NVDataMgmtExecState = NVDATAMGMT_EXEC_STATE_WAIT_FOR_POST; NVDataMgmtSelfTestResult = SELF_TEST_STATUS_IN_PROGRESS; + NVDataMgmtExecCalState = NVDATAMGMT_CAL_STATE_IDLE; queueRearIndex = QUEUE_START_INDEX; queueFrontIndex = QUEUE_START_INDEX; queueCount = 0; + dgCalRecordUpdateAddress = 0; Fapi_initializeFlashBanks( ROUNDED_HCLK_FREQ ); Fapi_setActiveFlashBank( Fapi_FlashBank7 ); + + // TODO FOR TESTING ONLY, REMOVE + dgCalibrationRecord.presSensorsCalRecord.pressureSensors[0].offset = 5.69; + dgCalibrationRecord.presSensorsCalRecord.pressureSensors[0].gain = 15.2; + dgCalibrationRecord.presSensorsCalRecord.pressureSensors[0].calibrationTime = 12345; + dgCalibrationRecord.presSensorsCalRecord.pressureSensors[0].crc = 159; + + dgCalibrationRecord.condSensorsCalRecord.condSensors[CAL_DATA_CD2_COND_SENSOR].gain = 56.89; + dgCalibrationRecord.condSensorsCalRecord.condSensors[CAL_DATA_CD2_COND_SENSOR].offset = 122.3; + dgCalibrationRecord.condSensorsCalRecord.condSensors[CAL_DATA_CD2_COND_SENSOR].calibrationTime = 567890; + dgCalibrationRecord.condSensorsCalRecord.condSensors[CAL_DATA_CD2_COND_SENSOR].crc = 3579; + //dgCalibrationRecord.pumpsCalRecord.roPump.gain3Ratio = 56.78; + //dgCalibrationRecord.pumpsCalRecord.roPump.calibrationTime = 85236; + //dgCalibrationRecord.pumpsCalRecord.roPump.crc = 6789; + dgCalibrationRecord.crc = 23678; + // TODO FOR TESTING ONLY, REMOVE } /*********************************************************************//** @@ -312,6 +423,284 @@ /*********************************************************************//** * @brief + * The execNVDataMgmt runs the NVDataMgmt main tasks. + * @details Inputs: NVDataMgmtExecState + * @details Outputs: NVDataMgmtExecState, alarm if exec state was invalid + * @return none + *************************************************************************/ +void execNVDataMgmt( void ) +{ // TODO - not sure where it should go Dara, but need to look at powerOffIsImminent flag and block + // queuing of any new jobs, maybe even block starting of any new jobs if flag is set + switch ( NVDataMgmtExecState ) + { + case NVDATAMGMT_EXEC_STATE_WAIT_FOR_POST: + + NVDataMgmtExecState = handleExecWaitForPostState(); + break; + + case NVDATAMGMT_EXEC_STATE_IDLE: + + NVDataMgmtExecState = handleExecIdleState(); + break; + + case NVDATAMGMT_EXEC_STATE_WRITE_TO_EEPROM: + + NVDataMgmtExecState = handleExecWriteToEEPROMState(); + break; + + case NVDATAMGMT_EXEC_STATE_READ_FROM_EEPROM: + + NVDataMgmtExecState = handleExecReadFromEEPROMState(); + break; + + case NVDATAMGMT_EXEC_STATE_ERASE_EEPROM: + + NVDataMgmtExecState = handleExecEraseState(); + break; + + case NVDATAMGMT_EXEC_STATE_WRITE_TO_RTC: + + NVDataMgmtExecState = handleExecWriteToRAMState(); + break; + + case NVDATAMGMT_EXEC_STATE_READ_FROM_RTC: + + NVDataMgmtExecState = handleExecReadFromRAMState(); + break; + + default: + +#ifdef _DG_ + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_NVDATAMGMT_EXEC_INVALID_STATE, + NVDataMgmtExecState ); +#else + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_NVDATAMGMT_EXEC_INVALID_STATE, + NVDataMgmtExecState ); +#endif + NVDataMgmtExecState = NVDATAMGMT_EXEC_STATE_IDLE; + break; + } +} + +/*********************************************************************//** + * @brief + * The execNVDataMgmtSelfTest runs the NVDataMgmt POST during the self-test. + * @details Inputs: NVDataMgmtSelfTestState + * @details Outputs: NVDataMgmtSelfTestState, alarm if there was a software fault + * @return NVDataMgmtSelfTestResult the result of self-test + *************************************************************************/ +SELF_TEST_STATUS_T execNVDataMgmtSelfTest ( void ) +{ + switch ( NVDataMgmtSelfTestState ) + { + case NVDATAMGMT_SELF_TEST_STATE_START: + + NVDataMgmtSelfTestState = handleSelfTestStart(); + break; + + case NVDATAMGMT_SELF_TEST_STATE_ENABLE_EEPROM: + + NVDataMgmtSelfTestState = handleSelfTestEnableEEPROM(); + break; + + case NVDATAMGMT_SELF_TEST_STATE_READ_BOOTLOADER_FLAG: + + NVDataMgmtSelfTestState = handleSelfTestReadBootloaderFlag(); + break; + + case NVDATAMGMT_SELF_TEST_STATE_READ_LOG_RECORD: + + NVDataMgmtSelfTestState = handleSelfTestReadLogRecord(); + break; + + case NVDATAMGMT_SELF_TEST_STATE_READ_TREATMENT_TIME: + + NVDataMgmtSelfTestState = handleSelfTestReadHDTreatmentTime(); + break; + + case NVDATAMGMT_SELF_TEST_STATE_READ_WATER_CONSUMPTION: + + NVDataMgmtSelfTestState = handleSelfTestReadDGWaterConsumption(); + break; + + case NVDATAMGMT_SELF_TEST_STATE_READ_MFG_RECORD: + + NVDataMgmtSelfTestState = handleSelfTestReadMfgRecord(); + break; + + case NVDATAMGMT_SELF_TEST_STATE_READ_CAL_RECORD: + + NVDataMgmtSelfTestState = handleSelfTestReadCalibrationRecord(); + break; + + case NVDATAMGMT_SELF_TEST_STATE_READ_SERVICE_RECORD: + + NVDataMgmtSelfTestState = handleSelfTestReadServiceRecord(); + break; + + case NVDATAMGMT_SELF_TEST_STATE_READ_LAST_DISINFECTION_DATE: + + NVDataMgmtSelfTestState = handleSelfTestReadLastDisinfectionDate(); + break; + + case NVDATAMGMT_SELF_TEST_STATE_CHECK_CRC: + + NVDataMgmtSelfTestState = handleSelfTestCheckCRC(); + break; + + case NVDATAMGMT_SELF_TEST_STATE_COMPLETE: + + // Done with POST. Do nothing + break; + + default: +#ifdef _DG_ + SET_ALARM_WITH_2_U32_DATA ( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_NVDATAMGMT_INVALID_SELF_TEST_STATE, + NVDataMgmtSelfTestState ); +#else + SET_ALARM_WITH_2_U32_DATA ( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_NVDATAMGMT_INVALID_SELF_TEST_STATE, + NVDataMgmtSelfTestState ); +#endif + NVDataMgmtSelfTestState = NVDATAMGMT_SELF_TEST_STATE_COMPLETE; + NVDataMgmtSelfTestResult = SELF_TEST_STATUS_FAILED; + break; + } + + return NVDataMgmtSelfTestResult; +} + +/*********************************************************************//** + * @brief + * The execNVDataMgmtCalibration runs the NVDataMgmt calibration related + * tasks. + * @details Inputs: NVDataMgmtExecCalState + * @details Outputs: NVDataMgmtExecCalState, alarm if exec state was invalid + * @return none + *************************************************************************/ +void execNVDataMgmtCalibration( void ) +{ + switch ( NVDataMgmtExecCalState ) + { + case NVDATAMGMT_CAL_STATE_IDLE: + + NVDataMgmtExecCalState = handleExecCalIdleState(); + break; + + case NVDATAMGMT_CAL_STATE_SEND_RECORD: + + NVDataMgmtExecCalState = handleExecCalSendRecordState(); + break; + + default: + +#ifdef _DG_ + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_INVALID_NVDATAMGMT_EXEC_CAL_STATE, + NVDataMgmtExecCalState ); +#else + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_INVALID_NVDATAMGMT_EXEC_CAL_STATE, + NVDataMgmtExecCalState ); +#endif + NVDataMgmtExecCalState = NVDATAMGMT_CAL_STATE_IDLE; + break; + } +} + +/*********************************************************************//** + * @brief + * The getDGCalibrationRecord function sets the calibration state machine + * to read and publish calibration record. + * @details Inputs: isPublishCalRequested, NVDataMgmtExecCalState + * @details Outputs: isPublishCalRequested + * @return TRUE if the request was successfully registered + *************************************************************************/ +BOOL getDGCalibrationRecord( void ) +{ + BOOL status = FALSE; + + // Check if the state machine is in idle state and then set the request + if ( NVDataMgmtExecCalState == NVDATAMGMT_CAL_STATE_IDLE ) + { + isPublishCalRequested = TRUE; + status = TRUE; + } + + return status; +} + +/*********************************************************************//** + * @brief + * The setDGCalibrationRecord function writes the calibration record that + * is received from Dialin into the calibration structure. + * @details Inputs: previousCalMessageNum, dgCalRecordUpdateAddress + * @details Outputs: previousCalMessageNum, dgCalRecordUpdateAddress + * @param currentMessage: current message number that is received from Dialin + * @param totalMessages: total number of messages from Dialin + * @param length: message length in bytes + * @param *addressPtr: address to the beginning of the calibration data from Dialin + * @return TRUE if the request was successfully registered + *************************************************************************/ +BOOL setDGCalibrationRecord( U32 currentMessage, U32 totalMessages, U32 length, U08 *addressPtr ) +{ + BOOL status = TRUE; + + // Check if there is still a message left to be received + if ( currentMessage <= totalMessages ) + { + // Check if the current message is different from the previous message by 1 + if ( ( currentMessage - previousCalMessageNum ) == 1 ) + { + // Define a pointer that points to the DG calibration record + U08* ptr = (U08*)&dgCalibrationRecord; + + // Offset the pointer to length that we should start writing from + ptr += dgCalRecordUpdateAddress; + + memcpy(ptr, addressPtr, length); + + // Now the current message is the previous message + previousCalMessageNum = currentMessage; + // Update the length as it has successfully been written + dgCalRecordUpdateAddress += length; + + // Check if the current message is total messages + // and 0 everything out since we are done writing + if ( currentMessage == totalMessages ) + { + //TODO check CRC + previousCalMessageNum = 0; + dgCalRecordUpdateAddress = 0; + } + } + else + { + status = FALSE; + } + } + + return status; +} + +/*********************************************************************//** + * @brief + * The getDGPressureSensorsCalibrationRecord function copies the DG pressure + * sensors calibration record into a buffer. + * @details Inputs: none + * @details Outputs: none + * @param buffer: address to the buffer to copy the data into it + * @return none + *************************************************************************/ +void getDGPressureSensorsCalibrationRecord( U08* buffer ) +{ + memcpy( buffer, &dgCalibrationRecord.presSensorsCalRecord, sizeof(dgCalibrationRecord.presSensorsCalRecord)); +} + +void getDGFlowSensorsCalibrationRecord( U08* buffer ) +{ + memcpy( buffer, &dgCalibrationRecord.flowSensorsCalRecord, sizeof(dgCalibrationRecord.condSensorsCalRecord)); +} + +/*********************************************************************//** + * @brief * The setMfgData updates the structure that holds the manufacturing data, * calls another function to calculate the CRC for the provided data and * calls another function to erase sector 0 and write the new manufacturing @@ -647,93 +1036,6 @@ /*********************************************************************//** * @brief - * The execNVDataMgmtSelfTest runs the NVDataMgmt POST during the self-test. - * @details Inputs: NVDataMgmtSelfTestState - * @details Outputs: NVDataMgmtSelfTestState, alarm if there was a software fault - * @return NVDataMgmtSelfTestResult the result of self-test - *************************************************************************/ -SELF_TEST_STATUS_T execNVDataMgmtSelfTest ( void ) -{ - switch ( NVDataMgmtSelfTestState ) - { - case NVDATAMGMT_SELF_TEST_STATE_START: - - NVDataMgmtSelfTestState = handleSelfTestStart(); - break; - - case NVDATAMGMT_SELF_TEST_STATE_ENABLE_EEPROM: - - NVDataMgmtSelfTestState = handleSelfTestEnableEEPROM(); - break; - - case NVDATAMGMT_SELF_TEST_STATE_READ_BOOTLOADER_FLAG: - - NVDataMgmtSelfTestState = handleSelfTestReadBootloaderFlag(); - break; - - case NVDATAMGMT_SELF_TEST_STATE_READ_LOG_RECORD: - - NVDataMgmtSelfTestState = handleSelfTestReadLogRecord(); - break; - - case NVDATAMGMT_SELF_TEST_STATE_READ_TREATMENT_TIME: - - NVDataMgmtSelfTestState = handleSelfTestReadHDTreatmentTime(); - break; - - case NVDATAMGMT_SELF_TEST_STATE_READ_WATER_CONSUMPTION: - - NVDataMgmtSelfTestState = handleSelfTestReadDGWaterConsumption(); - break; - - case NVDATAMGMT_SELF_TEST_STATE_READ_MFG_RECORD: - - NVDataMgmtSelfTestState = handleSelfTestReadMfgRecord(); - break; - - case NVDATAMGMT_SELF_TEST_STATE_READ_CAL_RECORD: - - NVDataMgmtSelfTestState = handleSelfTestReadCalibrationRecord(); - break; - - case NVDATAMGMT_SELF_TEST_STATE_READ_SERVICE_RECORD: - - NVDataMgmtSelfTestState = handleSelfTestReadServiceRecord(); - break; - - case NVDATAMGMT_SELF_TEST_STATE_READ_LAST_DISINFECTION_DATE: - - NVDataMgmtSelfTestState = handleSelfTestReadLastDisinfectionDate(); - break; - - case NVDATAMGMT_SELF_TEST_STATE_CHECK_CRC: - - NVDataMgmtSelfTestState = handleSelfTestCheckCRC(); - break; - - case NVDATAMGMT_SELF_TEST_STATE_COMPLETE: - - // Done with POST. Do nothing - break; - - default: -#ifdef _DG_ - SET_ALARM_WITH_2_U32_DATA ( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_NVDATAMGMT_INVALID_SELF_TEST_STATE, - NVDataMgmtSelfTestState ); -#else - SET_ALARM_WITH_2_U32_DATA ( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_NVDATAMGMT_INVALID_SELF_TEST_STATE, - NVDataMgmtSelfTestState ); -#endif - NVDataMgmtSelfTestState = NVDATAMGMT_SELF_TEST_STATE_COMPLETE; - NVDataMgmtSelfTestResult = SELF_TEST_STATUS_FAILED; - break; - } - - return NVDataMgmtSelfTestResult; -} - -/*********************************************************************//** - * @brief * The setBootloaderFlag sets the bootloader flag to RTC RAM. * @details Inputs: none * @details Outputs: none @@ -768,67 +1070,6 @@ /*********************************************************************//** * @brief - * The execNVDataMgmt runs the NVDataMgmt main tasks. - * @details Inputs: NVDataMgmtExecState - * @details Outputs: NVDataMgmtExecState, alarm if exec state was invalid - * @return none - *************************************************************************/ -void execNVDataMgmt( void ) -{ // TODO - not sure where it should go Dara, but need to look at powerOffIsImminent flag and block - // queuing of any new jobs, maybe even block starting of any new jobs if flag is set - switch ( NVDataMgmtExecState ) - { - case NVDATAMGMT_EXEC_STATE_WAIT_FOR_POST: - - NVDataMgmtExecState = handleExecWaitForPostState(); - break; - - case NVDATAMGMT_EXEC_STATE_IDLE: - - NVDataMgmtExecState = handleExecIdleState(); - break; - - case NVDATAMGMT_EXEC_STATE_WRITE_TO_EEPROM: - - NVDataMgmtExecState = handleExecWriteToEEPROMState(); - break; - - case NVDATAMGMT_EXEC_STATE_READ_FROM_EEPROM: - - NVDataMgmtExecState = handleExecReadFromEEPROMState(); - break; - - case NVDATAMGMT_EXEC_STATE_ERASE_EEPROM: - - NVDataMgmtExecState = handleExecEraseState(); - break; - - case NVDATAMGMT_EXEC_STATE_WRITE_TO_RTC: - - NVDataMgmtExecState = handleExecWriteToRAMState(); - break; - - case NVDATAMGMT_EXEC_STATE_READ_FROM_RTC: - - NVDataMgmtExecState = handleExecReadFromRAMState(); - break; - - default: - -#ifdef _DG_ - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_NVDATAMGMT_EXEC_INVALID_STATE, - NVDataMgmtExecState ); -#else - SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_NVDATAMGMT_EXEC_INVALID_STATE, - NVDataMgmtExecState ); -#endif - NVDataMgmtExecState = NVDATAMGMT_EXEC_STATE_IDLE; - break; - } -} - -/*********************************************************************//** - * @brief * The handleSelfTestStart enables the EEPROM bank sectors. * @details Inputs: currentTime * @details Outputs: currentTime @@ -1405,6 +1646,86 @@ /*********************************************************************//** * @brief + * The handleExecCalIdleState handles the idle state of the exec cal state + * machine. + * @details Inputs: isPublishCalRequested, isPublishCalRequested, + * calPublishTotalMessages, calPublishMessageCount, calSendDataIntervalCounter + * @details Outputs: isPublishCalRequested, calPublishTotalMessages, + * calPublishMessageCount, calSendDataIntervalCounter + * @return next state of the state machine + *************************************************************************/ +static NVDATAMGMT_CAL_STATE_T handleExecCalIdleState( void ) +{ + NVDATAMGMT_CAL_STATE_T state = NVDATAMGMT_CAL_STATE_IDLE; + + if ( isPublishCalRequested ) + { + // Set the calibration flag to FALSE + isPublishCalRequested = FALSE; + // Calculate the total number of messages required to be sent using ceilf function. This function rounds up the + // value and its result is converted to U32. + calPublishTotalMessages = (U32)ceilf( (F32)sizeof( DG_CALIBRATION_RECORD_T ) / (F32)NUM_OF_BYTES_PER_CAL_PAYLOAD ); + calPublishMessageCount = 0; + // Time interval in between data to be sent. It is set to the interval count so on the first call + // of the send calibration record function, the first packet of data is sent + calSendDataIntervalCounter = CAL_DATA_SEND_INTERVAL_COUNT; + + state = NVDATAMGMT_CAL_STATE_SEND_RECORD; + } + + return state; +} + +/*********************************************************************//** + * @brief + * The handleExecCalSendRecordState handles the send calibration record + * state of the state machine. + * @details Inputs: dgCalibrationRecord, calPublishTotalMessages, + * calPublishMessageCount, calSendDataIntervalCounter + * @details Outputs: calPublishTotalMessages, calPublishMessageCount, + * calSendDataIntervalCounter + * @return next state of the state machine + *************************************************************************/ +static NVDATAMGMT_CAL_STATE_T handleExecCalSendRecordState( void ) +{ + NVDATAMGMT_CAL_STATE_T state = NVDATAMGMT_CAL_STATE_SEND_RECORD; + + // If the current message number is less than the total, keep sending + if ( calPublishMessageCount < calPublishTotalMessages ) + { + // If it is time to send data + if ( ++calSendDataIntervalCounter >= CAL_DATA_SEND_INTERVAL_COUNT ) + { + // Set to default cal data payload length + U32 length = NUM_OF_BYTES_PER_CAL_PAYLOAD; + U08* startPtr = (U08*)&dgCalibrationRecord; + + // If this is the last calibration data payload, calculate the remainder of the bytes to send + if ( ( calPublishMessageCount + 1 ) == calPublishTotalMessages ) + { + length = sizeof( DG_CALIBRATION_RECORD_T ) - ( calPublishMessageCount * NUM_OF_BYTES_PER_CAL_PAYLOAD ); + } + + // Find the new location of the pointer which is the start of the calibration payload to be sent + startPtr += calPublishMessageCount * NUM_OF_BYTES_PER_CAL_PAYLOAD; + + // Pass the information to be written to CAN bus + sendDGCalibrationRecord( calPublishMessageCount + 1, calPublishTotalMessages, length, startPtr ); + + calPublishMessageCount++; + calSendDataIntervalCounter = 0; + } + } + else + { + state = NVDATAMGMT_CAL_STATE_IDLE; + } + + return state; +} + +/*********************************************************************//** + * @brief * The setMemoryOpsStruct function checks whether it is an EEPROM write, * EEPROM read, or RTC RAM operation. For read or write data logs into EEPROM, * it calls other functions to check for wraps and to find the next address for Index: NVDataMgmt.h =================================================================== diff -u -rd6f53ebf6c481cb909720a2c6910ae89addc6801 -r2d3cc187e8103c7dec7e98c324607c737fc7f3fa --- NVDataMgmt.h (.../NVDataMgmt.h) (revision d6f53ebf6c481cb909720a2c6910ae89addc6801) +++ NVDataMgmt.h (.../NVDataMgmt.h) (revision 2d3cc187e8103c7dec7e98c324607c737fc7f3fa) @@ -55,6 +55,69 @@ NVDATAMGMT_READ_COMPLETE ///< Read status complete } NVDATAMGMT_READ_STATUS_T; +typedef enum dg_pressure_sensors +{ + CAL_DATA_RO_PUMP_INLET_PRES_SENSOR = 0, + CAL_DATA_RO_PUMP_OUTLET_PRES_SENSOR, + CAL_DATA_DRAIN_PUMP_INLET_PRES_SENSOR, + CAL_DATA_DRAIN_PUMP_OUTLET_PRES_SENSOR, + NUM_OF_CAL_DATA_PRES_SENSORS, +} CAL_DATA_DG_PRES_SENSORS_T; + +typedef enum dg_flow_sensors +{ + CAL_DATA_RO_PUMP_FLOW_SENSOR = 0, + NUM_OF_CAL_DATA_FLOW_SENSORS, +} CAL_DATA_DG_FLOW_SENSORS_T; + +typedef enum dg_load_cells +{ + CAL_DATA_LOAD_CELL_A1 = 0, + CAL_DATA_LOAD_CELL_A2, + CAL_DATA_LOAD_CELL_B1, + CAL_DATA_LOAD_CELL_B2, + NUM_OF_CAL_DATA_LOAD_CELLS, +} CAL_DATA_DG_LOAD_CELLS_T; + +typedef enum dg_temperature_sensors +{ + CAL_DATA_FPGA_BOARD_TEMP = 0, + CAL_DATA_LOAD_CELL_A1_B1_TEMP, + CAL_DATA_LOAD_CELL_A2_B2_TEMP, + CAL_DATA_INTERNAL_THDO_RTD_TEMP, + CAL_DATA_INTERNAL_TDI_RTD_TEMP, + CAL_DATA_INTERNAL_COND_SENSOR_TEMP, + CAL_DATA_THERMISTOR_DG_ONBOARD_NTC_TEMP, + CAL_DATA_THERMISTOR_POWER_SUPPLY_1_TEMP, + CAL_DATA_THERMISTOR_POWER_SUPPLY_2_TEMP, + CAL_DATA_OUTLET_REDUNDANT_TEMP, + CAL_DATA_INLET_DIALYSATE_TEMP, + CAL_DATA_INLET_PRIMARY_HEATER_TEMP, + CAL_DATA_OUTLET_PRIMARY_HEATER_TEMP, + CAL_DATA_COND_SENSOR_1_TEMP, + CAL_DATA_COND_SENSOR_2_TEMP, + NUM_OF_CAL_DATA_TEMP_SENSORS, +} CAL_DATA_DG_TEMP_SENSORS_T; + +typedef enum dg_conductivity_sensors +{ + CAL_DATA_CPI_COND_SENSOR = 0, + CAL_DATA_CPO_COND_SENSOR, + CAL_DATA_CD1_COND_SENSOR, + CAL_DATA_CD2_COND_SENSOR, + NUM_OF_CAL_DATA_COND_SENSORS, +} CAL_DATA_DG_COND_SENSORS_T; + +typedef enum dg_pumps +{ + CAL_DATA_CONC_PUMP_1 = 0, + CAL_DATA_CONC_PUMP_2, + CAL_DATA_DRAIN_PUMP, + CAL_DATA_RO_PUMP, + NUM_OF_CAL_DATA_DG_PUMPS +} CAL_DATA_DG_PUMPS_T; + + #pragma pack(push, 1) /// Manufacturing data structure. typedef struct mfg_Data @@ -115,13 +178,76 @@ char disinfectionDate [ MAX_DATE_CHARACTERS ]; ///< Disinfection Date } DISINFECTION_DATE_T; -/// Payload record structure for a linear calibration message. +/// Payload record structure for a linear calibration message. TODO remove typedef struct { F32 gain; F32 offset; } LINEAR_F32_CAL_PAYLOAD_T; +/// Linear calibration structure +typedef struct +{ + F32 gain; + F32 offset; + U32 calibrationTime; + U16 crc; +} LINEAR_CAL_PAYLOAD_T; + +/// DG concentrate pumps one-gain payload +typedef struct +{ + F32 stepSpeed2FlowRatio; + U32 calibrationTime; + U16 crc; +} DG_CONCENTRATE_PUMPS_PAYLOAD_T; + +/// DG drain pump two-gain payload +typedef struct +{ + F32 voltage2SpeedRatio; + F32 stepSpeed2FlowRatio; + U32 calibrationTime; + U16 crc; +} DG_DRAIN_PUMP_PAYLOAD_T; + +// TODO change the name of the RO pumps gains later +typedef struct +{ + F32 gain1Ratio; + F32 gain2Ratio; + F32 gain3Ratio; + U32 calibrationTime; + U16 crc; +} DG_RO_PUMP_PAYLOAD_T; + +/// One constant structure +typedef struct +{ + F32 constant; + U32 calibrationTime; + U16 crc; +} ONE_CONSTANT_PAYLOAD_T ; + +/// Two constants structure +typedef struct +{ + F32 constant1; + F32 constant2; + U32 calibrationTime; + U16 crc; +} TWO_CONSTANTS_PAYLOAD_T; + +/// Three constants structure +typedef struct +{ + F32 constant1; + F32 constant2; + F32 constant3; + U32 calibrationTime; + U16 crc; +} THREE_CONSTANTS_PAYLOAD_T; + #pragma pack(pop) void initNVDataMgmt ( void ); @@ -132,14 +258,24 @@ SELF_TEST_STATUS_T execNVDataMgmtSelfTest ( void ); +void execNVDataMgmtCalibration( void ); + +BOOL getDGCalibrationRecord( void ); + +BOOL setDGCalibrationRecord( U32 currentMessage, U32 totalMessages, U32 length, U08 *addressPtr ); + +void getDGPressureSensorsCalibrationRecord( U08* buffer ); + +void getDGFlowSensorsCalibrationRecord( U08* buffer ); + BOOL setBootloaderFlag ( U32 flag ); U32 getBootloaderFlag ( void ); BOOL setMfgData ( MFG_DATA_T data ); BOOL getMfgData ( MFG_DATA_T* buffer ); -BOOL setCalibrationData ( CALIBRATION_DATA_T data ); -BOOL getCalibrationData ( CALIBRATION_DATA_T* buffer ); +BOOL setCalibrationData ( CALIBRATION_DATA_T data ); //TODO remove +BOOL getCalibrationData ( CALIBRATION_DATA_T* buffer ); //TODO remove BOOL testResetCalibrationData( U32 key ); BOOL setServiceDate ( SERVICE_DATA_T data );