/**************************************************************************/ /* BEFORE COMPILATION OF THIS MODULE - SEE NVMSWITC.H */ /**************************************************************************/ /***************************************************************************/ /***************************************************************************/ /***************************************************************************/ /******* *******/ /******* U N I V E R S A L *******/ /******* *******/ /******* F U E L Q U A N T I T Y M E A S U R I N G S Y S T E M *******/ /******* *******/ /***************************************************************************/ /***************************************************************************/ /***************************************************************************/ /* */ /* Program Name : Universal Fuel Quantity Measuring System (UFQGS) */ /* Source File Name : nvm.c */ /* CPC : Novram interface processing */ /* Engineer : Linda Jahnke */ /* Criticality Level : Level C */ /* */ /* */ /* */ /* */ /* Description : This CPC provides the read/write and clear */ /* interface to the NOVRAM */ /* */ /* */ /* */ /* Contents of this document are propriety information, property of: */ /* PARKER HANNIFIN CORPORATION */ /* GULL ELECTRONIC SYSTEMS DIVISION */ /* SMITHTOWN, NEW YORK 11787 */ /* Any attempt to copy or use the contents of this document in any manner */ /* whatsoever without express written consent of the owner is prohibited. */ /***************************************************************************/ /***************************************************************************/ /***************************************************************************/ #pragma EJECT /***************************************************************************/ /* Revision History */ /***************************************************************************/ /* */ /* Date Rev Engineer Description */ /* ------ --- -------- ------------------------------------------------ */ /* */ /***************************************************************************/ #pragma EJECT #pragma noam #pragma DEBUG #pragma LARGE #pragma OBJECTEXTEND #pragma SYMBOLS #include "c:\c51\inc\reg51.h" #include "c:\c51\inc\string.h" #include "c:\c51\inc\intrins.h" #include "c:\c51\inc\absacc.h" #include "asicaddr.h" #include "defines.h" #define EXTRN extern #define MAIN_FILE 0 #include "typedefs.h" #include "globals.h" #pragma listinclude #include "nvm.h" #include "fai.h" #include "nvmswitc.h" /************************** module declarations ***************************/ #define DATA_PRESENT 0xaa void nvm_read_eeprom (); // assembly routine that reads the number of // bytes specified starting at a specified location void nvm_write_to_eeprom (); // assembly routine that writes the number of // bytes specified starting at a specified location void nvm_clear_eeprom (); // assembly routine that clears the number of bytes // specified starting a specified location #define WRITE_CONTROL_BYTE 0xa0 // base control word for write to novram #define READ_CONTROL_BYTE 0xa1 // base control word for read from novram #define CLEAR_BYTE 0xff // byte value representing cleared novram byte #define NO_GOOD_REC 0xffff // NOVRAM location setting when novram location is not available #define REC_OUT_OF_RANGE 0xff // RECORD offset out of range #define NVM_NOT_BUSY 0Xff // NOVRAM port initialization value uchar nvm_buffer_size; // variable used by assembly routines - number of bytes to read/write uchar data nvm_port_b_image; // maintained image of port B uchar nvm_next_read_record [MAX_NUM_OF_GROUPS]; // location of next record to read for each group uchar nvm_next_available_record [MAX_NUM_OF_GROUPS]; // location of next record to write to for each group uchar nvm_failures_per_group [MAX_NUM_OF_GROUPS]; // counter of records failed in group void fai_pass_fail_logger(uchar,uchar); #pragma EJECT /************************************************************************* Function nvm_erase_novram This function recieves a group id via parameter to indicate which group to erase - If the group id indicates that all groups need to be erased this function will do so. Since it will take too long to erase large blocks (or even the entire NOVRAM) with one call to the function (16 byte write can take from 3 to 10 ms), the erase will have to be accomplished using multiple passes through the function. With each call a defined (nvmswitc.h) number of bytes will be cleared with each call to NOVRAM. If after the erase of the bytes there are still more locations that have to be cleared, this function returns NVM_ERASE_IN_PROGRESS. This means that more calls need to be made to the function inorder to complete the write. When the final bytes in the group or all of NOVRAM are erased, NVM_ERASE_COMPLETE is returned by the function. This function uses the assembly routine NVM_CLEAR_EEPROM to perform the erase. The erased state is when each byte contains 0FFH. *******************************************************************************/ uchar nvm_erase_novram(uchar group_id) { uchar erase_state; uchar index; static uchar nvm_erase_in_progress; static WORD_BYTE_OVERLAY_T start_location; static uint bytes_left; if (!nvm_erase_in_progress) { nvm_erase_in_progress = TRUE; if (group_id == NVM_ALL_NOVRAM_GROUPS) { start_location.w.word = NVM_GROUP_DEFINITIONS[0].start_location; bytes_left = NVM_NOVRAM_SIZE; /**************************************************************** next pointers within group need to be reset to the first record ; *****************************************************************/ for (index = 0; index < MAX_NUM_OF_GROUPS; index++) { nvm_next_available_record[index] = nvm_next_read_record[index] = 0; } } else { start_location.w.word = NVM_GROUP_DEFINITIONS[group_id].start_location; bytes_left = NVM_GROUP_DEFINITIONS[group_id].num_of_records * NVM_GROUP_DEFINITIONS[group_id].record_size_in_bytes; /**************************************************************** next pointers within group need to be reset to the first record ; *****************************************************************/ nvm_next_available_record[group_id] = nvm_next_read_record[group_id] = 0; } } /********************************************* set up buffer for erase ***********************************************/ nvm_eeprom_write_buffer.write_cntr_byte = _irol_(start_location.b.hbyte,1) | WRITE_CONTROL_BYTE; nvm_eeprom_write_buffer.offset_addr = start_location.b.lbyte; if (bytes_left > MAX_BYTES_TO_ERASE) { /******************************************* not all bytes can be erased at this time ; ********************************************/ nvm_buffer_size = MAX_BYTES_TO_ERASE; erase_state = NVM_ERASE_IN_PROGRESS; /********************************************* set up for next pass ; **********************************************/ start_location.w.word += MAX_BYTES_TO_ERASE; bytes_left -= MAX_BYTES_TO_ERASE; } else { nvm_buffer_size = bytes_left; erase_state = NVM_ERASE_COMPLETE; nvm_erase_in_progress = FALSE; } nvm_clear_eeprom(); return(erase_state); } #pragma EJECT /****************************************************************************** Function : nvm_initialization This function must be called at start up - note that most initializations are performed only at cold start. This function must be called prior to any calls to the other functions within the NVM CPC This function performs the following : 1. Initializes PORT B and the port b image variable 2. searches through the records of each group to establish where the first cleared record exists (if there is one). This information is stored and used by the write routines 3. initializes the next read record location for each group to the start of the group *****************************************************************************/ void nvm_initialization() { NVM_GROUP_DEFINITION_T code *p_definitions; WORD_BYTE_OVERLAY_T location; uint offset; uchar data rec_index; uchar data group_index; uchar cntrl_byte; /********************************* Initialize port **********************************/ nvm_port_b_image = NVM_NOT_BUSY; XBYTE [PORT_B_ADDR] = NVM_NOT_BUSY; if (COLD == g_start_status) { /******************************************************** determine where the first cleared record is for each group *********************************************************/ for (group_index = 0,p_definitions = &NVM_GROUP_DEFINITIONS; group_index < MAX_NUM_OF_GROUPS; group_index++, p_definitions++) { /*********************************************** initialize nvm_next_read_record to be the first record in the group *************************************************/ nvm_next_read_record[group_index] = 0; /***************************************************** search through first element in each records in group to find cleared record *******************************************************/ nvm_buffer_size = 1; location.w.word = p_definitions->start_location; for (offset = 0; offset < p_definitions->num_of_records; offset++,location.w.word += p_definitions->record_size_in_bytes) { /********************************************************** initialize next available clear record to none found **********************************************************/ nvm_next_available_record[group_index] = REC_OUT_OF_RANGE; /******************************************************** adjust record control words for reading *******************************************************/ cntrl_byte = _irol_(location.b.hbyte,1); nvm_eeprom_read_buffer.write_cntr_byte = cntrl_byte | WRITE_CONTROL_BYTE; nvm_eeprom_read_buffer.read_cntr_byte = cntrl_byte | READ_CONTROL_BYTE; nvm_eeprom_read_buffer.offset_addr = location.b.lbyte; /**************************************** read the record ****************************************/ nvm_read_eeprom(); if (!CY) { if ((nvm_failures_per_group[group_index]+1) < p_definitions->max_failures) nvm_failures_per_group[group_index]++; } else if (nvm_eeprom_read_buffer.record[0] == CLEAR_BYTE) { /************************************************* cleared record is found - store its location using the offset **************************************************/ nvm_next_available_record[group_index] = offset; break; /* from record loop */ } else if (nvm_eeprom_read_buffer.record[0] != DATA_PRESENT) nvm_failures_per_group[group_index]++; } //for /*********************************************** determine if the number of records failed in the group exceeds the max amount if yes - erase the group ************************************************/ if (nvm_failures_per_group[group_index] >= p_definitions->max_failures) { while (NVM_ERASE_IN_PROGRESS == nvm_erase_novram(group_index)); nvm_failures_per_group[group_index] = 0; } } // for group index } // cold start } #pragma EJECT /************************************************************************** Function : nvm_write This function should be called whenever a write to a NOVRAM is needed This function recieves the following as parameters : 1. functional group to write to 2. record id to write to (when = NVM_NEXT_WRITE write will be at next available location) 4. pointer to buffer containing data to write to NOVRAM. This function performs the following : 1. will write to the record or next available record as specified 2. If the location to be written to is at the next available record (first blank record) the next avaiable record location gets updated to the next record (or to no good if there is no next location) 3. Calculates the checksum of the data to be written and includes the 2's compliment of the checksum with the data for output to NOVRAM 4. Sets up control and calls the NVM_WRITE_TO_EEPROM assembly routine to write data (including checksum) to NOVRAM 5. Reads back the data (using the NVM_READ_EEPROM assembly routine) and compares the block read with the block written. 6. If write fails, trys another location 7. sends back a bad status only if no valid location found for record 8. sends back the record offset of where record was written *******************************************************************************/ NVM_REC_STATUS_T nvm_write( uchar group_id, char rec_id, uchar xdata *p_buffer){ uchar index; uchar checksum; uchar cntrl_byte; NVM_REC_STATUS_T rec_status; WORD_BYTE_OVERLAY_T location; uchar xdata *p_buffer_in = p_buffer; uchar xdata *p_write_buffer; NVM_GROUP_DEFINITION_T code *p_group_def; uchar xdata *p_next_avail; p_group_def = &NVM_GROUP_DEFINITIONS[group_id]; p_next_avail = &nvm_next_available_record[group_id]; if (rec_id == NVM_NEXT) { location.w.word = (*p_next_avail == REC_OUT_OF_RANGE)?NO_GOOD_REC : *p_next_avail * p_group_def->record_size_in_bytes+p_group_def->start_location; rec_status.rec_offset = *p_next_avail; *p_next_avail = (*p_next_avail+1 < p_group_def->num_of_records)? *p_next_avail+1: REC_OUT_OF_RANGE; } else if ((rec_id < *p_next_avail) && (rec_id < p_group_def->num_of_records)) { /********************************************************* Set up to write to the requested location **********************************************************/ location.w.word = rec_id * p_group_def->record_size_in_bytes + p_group_def->start_location; rec_status.rec_offset = rec_id; } else { /********************************************************************* eroneous rec id recived set up to write to next available location ***********************************************************************/ location.w.word = (*p_next_avail == REC_OUT_OF_RANGE)?NO_GOOD_REC : *p_next_avail * p_group_def->record_size_in_bytes+p_group_def->start_location; rec_status.rec_offset = *p_next_avail; *p_next_avail = (*p_next_avail+1 < p_group_def->num_of_records)? *p_next_avail+1: REC_OUT_OF_RANGE; } rec_status.status = NVM_FAILED_RECORD; while (location.w.word != NO_GOOD_REC) { /***************************************************** setup buffer for output *******************************************************/ p_buffer_in = p_buffer; p_write_buffer = nvm_eeprom_write_buffer.record; nvm_buffer_size = NVM_GROUP_DEFINITIONS[group_id].record_size_in_bytes; cntrl_byte = _irol_(location.b.hbyte,1) | WRITE_CONTROL_BYTE; nvm_eeprom_write_buffer.write_cntr_byte = cntrl_byte; nvm_eeprom_write_buffer.offset_addr = location.b.lbyte; *(p_write_buffer++) = DATA_PRESENT; checksum = 0; /****************************************************************** load data into data portion of eeprom write buffer and calculate the checksum ****************************************************************/ for (index = 0; index < (nvm_buffer_size-2); index++,p_buffer_in++, p_write_buffer++) { checksum += *p_buffer_in; *p_write_buffer = *p_buffer_in; } /*********************************************** store the 2's compliment of the checksum and call nvm_write_to_eeprom to perform the write. ************************************************/ *p_write_buffer = (uchar)(0 - (checksum+DATA_PRESENT)); nvm_write_to_eeprom(); /************************************************ if the carry bit is 0 a failure has been determined by the write driver *************************************************/ if (!CY) { /*** set up to write to next location ***/ rec_status.rec_offset = *p_next_avail; if ((nvm_failures_per_group[group_id]+1) == p_group_def->max_failures) fai_pass_fail_logger(NOVRAM_TEST_ID,FAIL); else nvm_failures_per_group[group_id]++; if (*p_next_avail == REC_OUT_OF_RANGE) { location.w.word = NO_GOOD_REC; rec_status.status = NVM_FAILED_RECORD; } else { *p_next_avail = (*p_next_avail+1 < p_group_def->num_of_records)? *p_next_avail+1: REC_OUT_OF_RANGE; location.w.word += p_group_def->record_size_in_bytes; } } else { /************************************************ read back the record to determine if read is valid *************************************************/ nvm_eeprom_read_buffer.write_cntr_byte = cntrl_byte; nvm_eeprom_read_buffer.offset_addr = location.b.lbyte; nvm_eeprom_read_buffer.read_cntr_byte = cntrl_byte | 1; //read bit high nvm_read_eeprom(); /************************************** compare buffers to determine if write was successful ***************************************/ if (memcmp(nvm_eeprom_read_buffer.record, nvm_eeprom_write_buffer.record,nvm_buffer_size)) { /* compare failed - try to write to next available location */ if ((nvm_failures_per_group[group_id]+1) == p_group_def->max_failures) fai_pass_fail_logger(NOVRAM_TEST_ID,FAIL); else nvm_failures_per_group[group_id]++; rec_status.rec_offset = *p_next_avail; if (*p_next_avail == REC_OUT_OF_RANGE) { location.w.word = NO_GOOD_REC; rec_status.status = NVM_FAILED_RECORD; } else { *p_next_avail = (*p_next_avail+1 < p_group_def->num_of_records)? *p_next_avail+1: REC_OUT_OF_RANGE; location.w.word += p_group_def->record_size_in_bytes; } } else { /* write successful - set up status and exit loop */ rec_status.status = NVM_GOOD_RECORD; break; } } //else } // while return(rec_status); } #pragma EJECT /*************************************************************** Function : nvm_read This function should be used whenever a read from a specific or next location is required This function performs the following : 1. This function will return a bad read status if the record being requested is not within the range of the group specified 2. If within the range, this function will read the record specified. 3. A checksum test will be performed - if ok a pointer to a structure containing a good status as well as the data read will be returned 4. If the checksum does not check out a bad read status will be returned if a direct read is requested - or if when a read next/reset requested, no good location found *********************************************************************/ NVM_OUT_T xdata *nvm_read (uchar group_id, char rec_id) { static NVM_OUT_T return_value; uchar xdata *p_record_read; uchar xdata *p_record_out; uchar checksum; uchar index; WORD_BYTE_OVERLAY_T location; uchar xdata *p_next_avail = &nvm_next_read_record[group_id]; NVM_GROUP_DEFINITION_T code *p_group_def; uchar cntrl_byte; return_value.rec_status.status = NVM_FAILED_RECORD; return_value.rec_status.rec_offset = REC_OUT_OF_RANGE; p_group_def = &NVM_GROUP_DEFINITIONS[group_id]; switch (rec_id) { case NVM_RESET : if (nvm_next_available_record[group_id] == 0) { location.w.word = NO_GOOD_REC; *p_next_avail = REC_OUT_OF_RANGE; } else { /* reset next available read requested */ location.w.word = p_group_def->start_location; return_value.rec_status.rec_offset = 0; *p_next_avail = (p_group_def->num_of_records == 1) ? REC_OUT_OF_RANGE : 1; } break; case NVM_NEXT : /* read from next available location */ location.w.word = ((*p_next_avail == REC_OUT_OF_RANGE) || (*p_next_avail == nvm_next_available_record[group_id]))?NO_GOOD_REC : *p_next_avail * p_group_def->record_size_in_bytes+p_group_def->start_location; return_value.rec_status.rec_offset = *p_next_avail; *p_next_avail = (*p_next_avail+1 >= p_group_def->num_of_records) ? REC_OUT_OF_RANGE : *p_next_avail+1; break; default : if (rec_id < p_group_def->num_of_records) { location.w.word = rec_id * p_group_def->record_size_in_bytes + p_group_def->start_location; return_value.rec_status.rec_offset = rec_id; } else location.w.word = NO_GOOD_REC; } while (location.w.word != NO_GOOD_REC) { /******************************************************** adjust record control words for reading *******************************************************/ cntrl_byte = _irol_(location.b.hbyte,1); nvm_eeprom_read_buffer.write_cntr_byte = cntrl_byte | WRITE_CONTROL_BYTE; nvm_eeprom_read_buffer.read_cntr_byte = cntrl_byte | READ_CONTROL_BYTE; nvm_eeprom_read_buffer.offset_addr = location.b.lbyte; nvm_buffer_size = p_group_def->record_size_in_bytes; /**************************************** read the record ****************************************/ nvm_read_eeprom(); /****************************************** after call to nvm_read_eeprom the carry flag will be set if read was successul *******************************************/ if (CY) { /********************************************** perform a checksum test on data to determine if data is good ***********************************************/ p_record_read = nvm_eeprom_read_buffer.record; p_record_out = return_value.record_data; checksum = 0; for (index = 0; index < (NVM_GROUP_DEFINITIONS[group_id].record_size_in_bytes -2); index++,p_record_out++) { checksum += *p_record_read; *p_record_out = *(++p_record_read); } checksum += *p_record_read++;// add in last byte 2 bytes checksum += *p_record_read; if (!checksum) { /************************************************** the checksum has is zero which means the read is successful ****************************************************/ return_value.rec_status.status = NVM_GOOD_RECORD; break; } else if ((rec_id == NVM_NEXT) || (rec_id == NVM_RESET)) { /*************************************************** checksum failed - if read next requested - get next location ****************************************************/ location.w.word = ((*p_next_avail == REC_OUT_OF_RANGE) || (*p_next_avail == nvm_next_available_record[group_id])) ?NO_GOOD_REC : *p_next_avail * p_group_def->record_size_in_bytes+p_group_def->start_location; return_value.rec_status.rec_offset = *p_next_avail; *p_next_avail = (*p_next_avail+1 >= p_group_def->num_of_records) ? REC_OUT_OF_RANGE : *p_next_avail+1; } else{ return_value.rec_status.rec_offset = REC_OUT_OF_RANGE; return_value.rec_status.status = NVM_FAILED_RECORD; break; } } else if ((rec_id == NVM_NEXT) || (rec_id == NVM_RESET)) { /*************************************************** write failed - read next requested - get next location ****************************************************/ location.w.word = ((*p_next_avail == REC_OUT_OF_RANGE) || (*p_next_avail == nvm_next_available_record[group_id])) ?NO_GOOD_REC : *p_next_avail * p_group_def->record_size_in_bytes+p_group_def->start_location; return_value.rec_status.rec_offset = *p_next_avail; *p_next_avail = (*p_next_avail+1 >= p_group_def->num_of_records) ? REC_OUT_OF_RANGE : *p_next_avail+1; } else { return_value.rec_status.rec_offset = REC_OUT_OF_RANGE; return_value.rec_status.status = NVM_FAILED_RECORD; break; } } return(&return_value); }