Index: NVDataMgmt.c =================================================================== diff -u -r2d3cc187e8103c7dec7e98c324607c737fc7f3fa -ra63f7d2ae7e000824aae3c17bddf9c391517d71f --- NVDataMgmt.c (.../NVDataMgmt.c) (revision 2d3cc187e8103c7dec7e98c324607c737fc7f3fa) +++ NVDataMgmt.c (.../NVDataMgmt.c) (revision a63f7d2ae7e000824aae3c17bddf9c391517d71f) @@ -38,60 +38,61 @@ // Private defines -#define QUEUE_MAX_SIZE 20U ///< Max queue size -#define QUEUE_START_INDEX 0U ///< Queue start index -#define MIN_JOBS_NEEDED_FOR_DATA_LOG 3U ///< Min queue required for data log (3) -#define MIN_JOBS_NEEDED_FOR_SECTOR_0 4U ///< Min queue count needed to write all (4) records back in sector 0 +#define QUEUE_MAX_SIZE 20U ///< Max queue size. +#define QUEUE_START_INDEX 0U ///< Queue start index. +#define MIN_JOBS_NEEDED_FOR_DATA_LOG 3U ///< Min queue required for data log (3). +#define MIN_JOBS_NEEDED_FOR_SECTOR_0 4U ///< Min queue count needed to write all (4) records back in sector 0. // The clock frequency comes from HCLK_FREQ and it has to be rounded up to the // nearest number -#define ROUNDED_HCLK_FREQ FLOAT_TO_INT_WITH_ROUND(HCLK_FREQ) ///< Rounded HCLK for EERPOM clock -#define BANK7_SECTOR_0_31_ENABLE_BIT_MASK 0x0000000F ///< Bank7 sector 0 t0 31 enable mask -#define BANK7_SECTOR_32_63_ENABLE_BIT_MASK 0x00000000 ///< Bank7 sector 32 to 63 enable mask +#define ROUNDED_HCLK_FREQ FLOAT_TO_INT_WITH_ROUND(HCLK_FREQ) ///< Rounded HCLK for EERPOM clock. +#define BANK7_SECTOR_0_31_ENABLE_BIT_MASK 0x0000000F ///< Bank7 sector 0 t0 31 enable mask. +#define BANK7_SECTOR_32_63_ENABLE_BIT_MASK 0x00000000 ///< Bank7 sector 32 to 63 enable mask. -#define BANK7_SECTOR0_START_ADDRESS 0xF0200000 ///< Bank7 sector 0 start address -#define BANK7_SECTOR0_END_ADDRESS 0xF0203FFF ///< Bank7 sector 0 end address +#define BANK7_SECTOR0_START_ADDRESS 0xF0200000 ///< Bank7 sector 0 start address. +#define BANK7_SECTOR0_END_ADDRESS 0xF0203FFF ///< Bank7 sector 0 end address. -#define BANK7_SECTOR1_START_ADDRESS 0xF0204000 ///< Bank7 sector 1 start address -#define BANK7_SECTOR1_END_ADDRESS 0xF0207FFF ///< Bank7 sector 1 end address +#define BANK7_SECTOR1_START_ADDRESS 0xF0204000 ///< Bank7 sector 1 start address. +#define BANK7_SECTOR1_END_ADDRESS 0xF0207FFF ///< Bank7 sector 1 end address. -#define BANK7_SECTOR2_START_ADDRESS 0xF0208000 ///< Bank7 sector 2 start address -#define BANK7_SECTOR2_END_ADDRESS 0xF020BFFF ///< Bank7 sector 2 end address +#define BANK7_SECTOR2_START_ADDRESS 0xF0208000 ///< Bank7 sector 2 start address. +#define BANK7_SECTOR2_END_ADDRESS 0xF020BFFF ///< Bank7 sector 2 end address. -#define BANK7_SECTOR3_START_ADDRESS 0xF020C000 ///< Bank7 sector 3 start address -#define BANK7_SECTOR3_END_ADDRESS 0xF020FFFF ///< Bank7 sector 3 end address +#define BANK7_SECTOR3_START_ADDRESS 0xF020C000 ///< Bank7 sector 3 start address. +#define BANK7_SECTOR3_END_ADDRESS 0xF020FFFF ///< Bank7 sector 3 end address. -#define MAX_EEPROM_WRITE_BUFFER_BYTES 16U ///< Max allowed bytes for an EEPROM write (16 bytes) +#define MAX_EEPROM_WRITE_BUFFER_BYTES 16U ///< Max allowed bytes for an EEPROM write (16 bytes). -#define MAX_JOB_DATA_SIZE_BYTES 32U ///< Max bytes per job (32 bytes) -#define LOG_DATA_START_INDEX 0U ///< Log data start index -#define MAX_NUM_OF_SECTORS_FOR_LOG_DATA 3U ///< Max number of sector (3 sectors) +#define MAX_JOB_DATA_SIZE_BYTES 32U ///< Max bytes per job (32 bytes). +#define LOG_DATA_START_INDEX 0U ///< Log data start index. +#define MAX_NUM_OF_SECTORS_FOR_LOG_DATA 3U ///< Max number of sector (3 sectors). #define MAX_LOG_DATA_PER_SECTOR (((BANK7_SECTOR1_END_ADDRESS + 1) - \ - BANK7_SECTOR1_START_ADDRESS) / MAX_JOB_DATA_SIZE_BYTES) ///< Max log data per sector (512 for now) -#define MAX_NUM_OF_EVENTS_IN_SECTOR3 (MAX_NUM_OF_SECTORS_FOR_LOG_DATA * MAX_LOG_DATA_PER_SECTOR) ///< Max number of accumulated logs in sector 3 (1536) -#define MAX_NUM_OF_EVENTS_IN_SECTOR2 ((MAX_NUM_OF_SECTORS_FOR_LOG_DATA - 1) * MAX_LOG_DATA_PER_SECTOR) ///< Max number of accumulated logs in sector 2 (1024) -#define MAX_NUM_OF_EVENTS_IN_SECTOR1 ((MAX_NUM_OF_SECTORS_FOR_LOG_DATA - 2) * MAX_LOG_DATA_PER_SECTOR) ///< Max number of accumulated logs in sector 1 (512) + BANK7_SECTOR1_START_ADDRESS) / MAX_JOB_DATA_SIZE_BYTES) ///< Max log data per sector (512 for now). +#define MAX_NUM_OF_EVENTS_IN_SECTOR3 (MAX_NUM_OF_SECTORS_FOR_LOG_DATA * MAX_LOG_DATA_PER_SECTOR) ///< Max number of accumulated logs in sector 3 (1536). +#define MAX_NUM_OF_EVENTS_IN_SECTOR2 ((MAX_NUM_OF_SECTORS_FOR_LOG_DATA - 1) * MAX_LOG_DATA_PER_SECTOR) ///< Max number of accumulated logs in sector 2 (1024). +#define MAX_NUM_OF_EVENTS_IN_SECTOR1 ((MAX_NUM_OF_SECTORS_FOR_LOG_DATA - 2) * MAX_LOG_DATA_PER_SECTOR) ///< Max number of accumulated logs in sector 1 (512). // Data addresses and length in RTC RAM -#define BOOTLOADER_FLAG_ADDRESS 0x00000000 ///< Bootloader start address in RTC RAM (0) -#define BOOTLOADER_FLAG_LENGTH_BYTES 4U ///< Bootloader number of bytes (4 bytes) -#define LOG_RECORD_START_ADDRESS 0x00000010 // 16 ///< Log record start address in RTC RAM (16) -#define HD_TREATMENT_TIME_ADDRESS 0X00000030 // 48 ///< HD treatment time start address in RTC RAM (48) -#define DG_CONSUMED_WATER_ADDRESS 0X00000040 // 64 ///< DG water consumption start address in RTC RAM (64) -#define SERVICE_DATE_START_ADDRESS 0X00000050 // 80 ///< Service date record start address in RTC RAM (80) -#define LAST_DISINFECTION_DATE_ADDRESS 0x00000060 // 96 ///< Last disinfection date start address in RTC RAM (96) +#define BOOTLOADER_FLAG_ADDRESS 0x00000000 ///< Bootloader start address in RTC RAM (0). +#define BOOTLOADER_FLAG_LENGTH_BYTES 4U ///< Bootloader number of bytes (4 bytes). +#define LOG_RECORD_START_ADDRESS 0x00000010 // 16 ///< Log record start address in RTC RAM (16). +#define HD_TREATMENT_TIME_ADDRESS 0X00000030 // 48 ///< HD treatment time start address in RTC RAM (48). +#define DG_CONSUMED_WATER_ADDRESS 0X00000040 // 64 ///< DG water consumption start address in RTC RAM (64). +#define SERVICE_DATE_START_ADDRESS 0X00000050 // 80 ///< Service date record start address in RTC RAM (80). +#define LAST_DISINFECTION_DATE_ADDRESS 0x00000060 // 96 ///< Last disinfection date start address in RTC RAM (96). // Data addresses in EEPROM -#define CALIBRATION_RECORD_START_ADDRESS ( BANK7_SECTOR0_START_ADDRESS + sizeof(MFG_RECORD_T) ) ///< Calibration record start address in EEPROM +#define CALIBRATION_RECORD_START_ADDRESS ( BANK7_SECTOR0_START_ADDRESS + sizeof(MFG_RECORD_T) ) ///< Calibration record start address in EEPROM. -#define COMMAND_TIME_OUT 500U // time in ms ///< Timeout for an EEPROM or RTC command in ms +#define COMMAND_TIME_OUT 500U // time in ms ///< Timeout for an EEPROM or RTC command in ms. #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 +#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. +#define CAL_DATA_RECEIVE_TIMEOUT_MS ( MS_PER_SECOND / 2 ) ///< Calibration data receive packets time out. /// NVDataMgmt self-test states enumeration. @@ -151,6 +152,14 @@ NUM_OF_NVDATAMGMT_CAL_STATES ///< Number of NVDataMgmt calibration state } NVDATAMGMT_CAL_STATE_T; +typedef enum NVDataMgmt_Calibration_Status +{ + NVDATAMGMT_CAL_STATUS_NONE = 0, + NVDATAMGMT_CAL_STATUS_WRITE, + NVDATAMGMT_CAL_STATUS_READ, + NUM_OF_NVDATAMGMT_STATUS_STATES +}NVDATAMGMT_CAL_STATUS_T; + #pragma pack(push, 1) /// Memory operations structure. typedef struct @@ -290,14 +299,16 @@ #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 +static NVDATAMGMT_CAL_STATE_T NVDataMgmtExecCalState = NVDATAMGMT_CAL_STATE_IDLE; ///< NVDataMgmt calibration state. +static NVDATAMGMT_CAL_STATUS_T NVDataMgmtCalStatus = NVDATAMGMT_CAL_STATUS_NONE; ///< NVDataMgmt calibration status. +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. +static U32 calDataReceiveTime = 0; ///< Time stamp the calibration/service was received. // Private variables static MEMORY_OPS_T jobQueue [ QUEUE_MAX_SIZE ]; ///< Job queue buffer @@ -383,10 +394,12 @@ NVDataMgmtExecState = NVDATAMGMT_EXEC_STATE_WAIT_FOR_POST; NVDataMgmtSelfTestResult = SELF_TEST_STATUS_IN_PROGRESS; NVDataMgmtExecCalState = NVDATAMGMT_CAL_STATE_IDLE; + NVDataMgmtCalStatus = NVDATAMGMT_CAL_STATUS_NONE; queueRearIndex = QUEUE_START_INDEX; queueFrontIndex = QUEUE_START_INDEX; queueCount = 0; dgCalRecordUpdateAddress = 0; + Fapi_initializeFlashBanks( ROUNDED_HCLK_FREQ ); Fapi_setActiveFlashBank( Fapi_FlashBank7 ); @@ -631,12 +644,16 @@ * @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 + * @details Inputs: previousCalMessageNum, dgCalRecordUpdateAddress, + * calDataReceiveTime + * @details Outputs: previousCalMessageNum, dgCalRecordUpdateAddress, + * calDataReceiveTime + * @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 + * @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 ) @@ -657,22 +674,47 @@ 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; + previousCalMessageNum = 0; dgCalRecordUpdateAddress = 0; + + // Get the CRC of the structure without the last 16 bits which is the CRC itself + U16 calcCRC = crc16 ( (U08*)&dgCalibrationRecord, sizeof(DG_CALIBRATION_RECORD_T) - sizeof(U16) ); + U16 recordCRC = dgCalibrationRecord.crc; + + if ( calcCRC != recordCRC ) + { + // CRC failed, request a read to read the data back from NV memory + NVDataMgmtCalStatus = NVDATAMGMT_CAL_STATUS_READ; + status = FALSE; + } + else + { + // CRC passed, request a write to write the data to NV memory + NVDataMgmtCalStatus = NVDATAMGMT_CAL_STATUS_WRITE; + } } + else + { + // Update the length as it has successfully been written + dgCalRecordUpdateAddress += length; + + // Now the current message is the previous message + previousCalMessageNum = currentMessage; + + // Data received, set the timer for the next time + calDataReceiveTime = getMSTimerCount(); + } } - else + // TODO do we have to cancel if one the receives timed out? + // If the next data packet was not received in time, cancel the receiving procedure and + // request a read of the data from NV memory + else if ( didTimeout( calDataReceiveTime, CAL_DATA_RECEIVE_TIMEOUT_MS ) ) { + NVDataMgmtCalStatus = NVDATAMGMT_CAL_STATUS_READ; status = FALSE; } } @@ -1478,8 +1520,16 @@ { NVDATAMGMT_EXEC_STATE_T state = NVDATAMGMT_EXEC_STATE_IDLE; - if ( !isQueueEmpty() ) + // If cal status is not none, process it + if ( NVDataMgmtCalStatus != NVDATAMGMT_CAL_STATUS_NONE ) { + + } + + // Check if the queue is not empty and the calibration/service status is none + // Processing calibration and service status have priority + if ( !isQueueEmpty() && NVDataMgmtCalStatus == NVDATAMGMT_CAL_STATUS_NONE ) + { dequeue(); NVDATAMGMT_OPERATION_STATE_T ops = currentJob.memoryOperation; NVDATAMGMT_LOCATION_STATE_T location = currentJob.memoryLocation; @@ -1516,7 +1566,6 @@ writeToRAM ( startAddress, currentJob.buffer, length ); state = NVDATAMGMT_EXEC_STATE_WRITE_TO_RTC; } - } else if ( ops == NVDATAMGMT_READ && location == NVDATAMGMT_RTC ) {