/******************************************************************************* * Function Name: CFG_EEPROM_StartWrite ******************************************************************************** * * Summary: * Starts a write of a row (16 bytes) of data to the EEPROM. * This function does not block. The function returns once the SPC has begun * writing the data. This function must be used in combination with * CFG_EEPROM_Query(). CFG_EEPROM_Query() must be called * until it returns a status other than CYRET_STARTED. That indicates that the * write has completed. Until CFG_EEPROM_Query() detects that * the write is complete, the SPC is marked as locked to prevent another * SPC operation from being performed. For a reliable write procedure to occur * you should call CFG_EEPROM_UpdateTemperature() API if the temperature * of the silicon has changed for more than 10C since component was started. * * Parameters: * rowData: The address of the data to write to the EEPROM. * rowNumber: The row number to write. * * Return: * CYRET_STARTED, if the SPC command to write was successfully started. * CYRET_BAD_PARAM, if the parameter rowNumber is out of range. * CYRET_LOCKED, if the SPC is being used. * CYRET_UNKNOWN, if there was an SPC error. * * Side effects: * After calling this API, the device should not be powered down, reset or switched * to low power modes until EEPROM operation is complete. * Ignoring this recommendation may lead to data corruption or silicon * unexpected behavior. * *******************************************************************************/ cystatus CFG_EEPROM_StartWrite(const uint8 * rowData, uint8 rowNumber) \ { cystatus status; CySpcStart(); if(rowNumber < (uint8) CY_EEPROM_NUMBER_ROWS) { /* See if we can get SPC. */ if(CySpcLock() == CYRET_SUCCESS) { /* Plan for failure */ status = CYRET_UNKNOWN; /* Command to load a row of data */ if(CySpcLoadRow(CY_SPC_FIRST_EE_ARRAYID, rowData, CYDEV_EEPROM_ROW_SIZE) == CYRET_STARTED) { while(CY_SPC_BUSY) { /* Wait until SPC becomes idle */ } /* SPC is idle now */ if(CY_SPC_STATUS_SUCCESS == CY_SPC_READ_STATUS) { status = CYRET_SUCCESS; } /* Command to erase and program the row. */ if(status == CYRET_SUCCESS) { if(CySpcWriteRow(CY_SPC_FIRST_EE_ARRAYID, (uint16)rowNumber, dieTemperature[0u], dieTemperature[1u]) == CYRET_STARTED) { status = CYRET_STARTED; } else { status = CYRET_UNKNOWN; } } else { status = CYRET_UNKNOWN; } } } else { status = CYRET_LOCKED; } } else { status = CYRET_BAD_PARAM; } return(status); }
void `$INSTANCE_NAME`_WriteByte(uint8 value, uint16 address) { uint8 row, offset; cystatus status; if(address < `$INSTANCE_NAME`_EEPROM_SIZE) { // calculate the row, and offset within the row row = address / `$INSTANCE_NAME`_EEPROM_ROW_SIZE; offset = address - ((uint16)row*(uint16)`$INSTANCE_NAME`_EEPROM_ROW_SIZE); // Turn on the SPC CySpcStart(); // lock the spc to prevent some other process from using it CySpcLock(); // loads a byte of data into the temporary SPC write buffer. Write the byte into the proper offset // in the temporary SPC buffer. The offset is an artifact of the "row" oriented write operations of // our EEPROM and flash. The load multibyte SPC function loads the temporary "row" with the data we intend // to write. later, this temporary row is written into the proper "row" with another function call. // to place the byte where we want it, we have to calculate the destination row, and offset within that // row. if(CySpcLoadMultiByte(CY_SPC_FIRST_EE_ARRAYID, offset, &value, `$INSTANCE_NAME`_SPC_BYTE_WRITE_SIZE) == CYRET_STARTED) { while(CY_SPC_BUSY) { /* Wait until SPC becomes idle */ } // this is where we tell the SPC to write the write the temporary row into the EEPROM. if(CySpcWriteRow(CY_SPC_FIRST_EE_ARRAYID, row, dieTemperature[0], dieTemperature[1]) == CYRET_STARTED) { while(CY_SPC_BUSY) { /* Wait until SPC becomes idle */ } } } /* Unlock the SPC so someone else can use it. */ CySpcUnlock(); // turn off the SPC CySpcStop(); } }
/******************************************************************************* * Function Name: CyWriteRowFull ******************************************************************************** * Summary: * Sends a command to the SPC to load and program a row of data in flash. * rowData array is expected to contain Flash and ECC data if needed. * * Parameters: * arrayId: FLASH or EEPROM array id. * rowData: pointer to a row of data to write. * rowNumber: Zero based number of the row. * rowSize: Size of the row. * * Return: * CYRET_SUCCESS if successful. * CYRET_LOCKED if the SPC is already in use. * CYRET_CANCELED if command not accepted * CYRET_UNKNOWN if there was an SPC error. * *******************************************************************************/ cystatus CyWriteRowFull(uint8 arrayId, uint16 rowNumber, const uint8* rowData, uint16 rowSize) \ { cystatus status; if(CySpcLock() == CYRET_SUCCESS) { /* Load row data into SPC internal latch */ status = CySpcLoadRow(arrayId, rowData, rowSize); if(CYRET_STARTED == status) { while(CY_SPC_BUSY) { /* Wait for SPC to finish and get SPC status */ CyDelayUs(1u); } /* Hide SPC status */ if(CY_SPC_STATUS_SUCCESS == CY_SPC_READ_STATUS) { status = CYRET_SUCCESS; } else { status = CYRET_UNKNOWN; } if(CYRET_SUCCESS == status) { /* Erase and program flash with the data from SPC interval latch */ status = CySpcWriteRow(arrayId, rowNumber, dieTemperature[0u], dieTemperature[1u]); if(CYRET_STARTED == status) { while(CY_SPC_BUSY) { /* Wait for SPC to finish and get SPC status */ CyDelayUs(1u); } /* Hide SPC status */ if(CY_SPC_STATUS_SUCCESS == CY_SPC_READ_STATUS) { status = CYRET_SUCCESS; } else { status = CYRET_UNKNOWN; } } } } CySpcUnlock(); } else { status = CYRET_LOCKED; } return(status); }
/******************************************************************************* * Function Name: CFG_EEPROM_ByteWritePos ******************************************************************************** * * Summary: * Writes a byte of data to the EEPROM. This is a blocking call. It will not * return until the write operation succeeds or fails. * * Parameters: * dataByte: The byte of data to write to the EEPROM. * rowNumber: The EEPROM row number to program. * byteNumber: The byte number within the row to program. * * Return: * CYRET_SUCCESS, if the operation was successful. * CYRET_BAD_PARAM, if the parameter rowNumber or byteNumber is out of range. * CYRET_LOCKED, if the SPC is being used. * CYRET_UNKNOWN, if there was an SPC error. * *******************************************************************************/ cystatus CFG_EEPROM_ByteWritePos(uint8 dataByte, uint8 rowNumber, uint8 byteNumber) \ { cystatus status; /* Start SPC */ CySpcStart(); if((rowNumber < (uint8) CY_EEPROM_NUMBER_ROWS) && (byteNumber < (uint8) SIZEOF_EEPROM_ROW)) { /* See if we can get SPC. */ if(CySpcLock() == CYRET_SUCCESS) { /* Plan for failure */ status = CYRET_UNKNOWN; /* Command to load byte of data */ if(CySpcLoadMultiByte(CY_SPC_FIRST_EE_ARRAYID, (uint16)byteNumber, &dataByte,\ CFG_EEPROM_SPC_BYTE_WRITE_SIZE) == CYRET_STARTED) { while(CY_SPC_BUSY) { /* Wait until SPC becomes idle */ } /* SPC is idle now */ if(CY_SPC_STATUS_SUCCESS == CY_SPC_READ_STATUS) { status = CYRET_SUCCESS; } /* Command to erase and program the row. */ if(status == CYRET_SUCCESS) { if(CySpcWriteRow(CY_SPC_FIRST_EE_ARRAYID, (uint16)rowNumber, dieTemperature[0u], dieTemperature[1u]) == CYRET_STARTED) { /* Plan for failure */ status = CYRET_UNKNOWN; while(CY_SPC_BUSY) { /* Wait until SPC becomes idle */ } /* SPC is idle now */ if(CY_SPC_STATUS_SUCCESS == CY_SPC_READ_STATUS) { status = CYRET_SUCCESS; } } else { status = CYRET_UNKNOWN; } } else { status = CYRET_UNKNOWN; } } /* Unlock SPC so that someone else can use it. */ CySpcUnlock(); } else { status = CYRET_LOCKED; } } else { status = CYRET_BAD_PARAM; } return(status); }
/******************************************************************************* * Function Name: CFG_EEPROM_WriteByte ******************************************************************************** * * Summary: * Writes a byte of data to the EEPROM. This function blocks until * the function is complete. For a reliable write procedure to occur you should * call CFG_EEPROM_UpdateTemperature() function if the temperature of the * silicon has been changed for more than 10C since the component was started. * * Parameters: * dataByte: The byte of data to write to the EEPROM * address: The address of data to be written. The maximum address is dependent * on the EEPROM size. * * Return: * CYRET_SUCCESS, if the operation was successful. * CYRET_BAD_PARAM, if the parameter sectorNumber is out of range. * CYRET_LOCKED, if the SPC is being used. * CYRET_UNKNOWN, if there was an SPC error. * *******************************************************************************/ cystatus CFG_EEPROM_WriteByte(uint8 dataByte, uint16 address) { cystatus status; uint16 rowNumber; uint16 byteNumber; CySpcStart(); if (address < CY_EEPROM_SIZE) { rowNumber = address/(uint16)CY_EEPROM_SIZEOF_ROW; byteNumber = address - (rowNumber * ((uint16)CY_EEPROM_SIZEOF_ROW)); if(CYRET_SUCCESS == CySpcLock()) { status = CySpcLoadMultiByte(CY_SPC_FIRST_EE_ARRAYID, byteNumber, &dataByte, \ CFG_EEPROM_SPC_BYTE_WRITE_SIZE); if (CYRET_STARTED == status) { /* Plan for failure */ status = CYRET_UNKNOWN; while(CY_SPC_BUSY) { /* Wait until SPC becomes idle */ } if(CY_SPC_STATUS_SUCCESS == CY_SPC_READ_STATUS) { status = CYRET_SUCCESS; } /* Command to erase and program the row. */ if(CYRET_SUCCESS == status) { if(CySpcWriteRow(CY_SPC_FIRST_EE_ARRAYID, (uint16)rowNumber, dieTemperature[0u], dieTemperature[1u]) == CYRET_STARTED) { /* Plan for failure */ status = CYRET_UNKNOWN; while(CY_SPC_BUSY) { /* Wait until SPC becomes idle */ } /* SPC is idle now */ if(CY_SPC_STATUS_SUCCESS == CY_SPC_READ_STATUS) { status = CYRET_SUCCESS; } } else { status = CYRET_UNKNOWN; } } else { status = CYRET_UNKNOWN; } } else { if (CYRET_BAD_PARAM != status) { status = CYRET_UNKNOWN; } } CySpcUnlock(); } else { status = CYRET_LOCKED; } } else { status = CYRET_BAD_PARAM; } return (status); }
/******************************************************************************* * Function Name: CyWriteRowFull ******************************************************************************** * Summary: * Sends a command to the SPC to load and program a row of data in the Flash. * rowData array is expected to contain Flash and ECC data if needed. * * Parameters: * arrayId: FLASH or EEPROM array id. * rowData: Pointer to a row of data to write. * rowNumber: Zero based number of the row. * rowSize: Size of the row. * * Return: * CYRET_SUCCESS if successful. * CYRET_LOCKED if the SPC is already in use. * CYRET_CANCELED if command not accepted * CYRET_UNKNOWN if there was an SPC error. * *******************************************************************************/ cystatus CyWriteRowFull(uint8 arrayId, uint16 rowNumber, const uint8* rowData, uint16 rowSize) \ { cystatus status = CYRET_SUCCESS; if((arrayId <= CY_SPC_LAST_FLASH_ARRAYID) && (arrayId > (CY_FLASH_NUMBER_ARRAYS + CY_SPC_FIRST_FLASH_ARRAYID))) { status = CYRET_BAD_PARAM; } if(arrayId > CY_SPC_LAST_EE_ARRAYID) { status = CYRET_BAD_PARAM; } if((arrayId >= CY_SPC_FIRST_EE_ARRAYID) && (arrayId > (CY_FLASH_EEPROM_NUMBER_ARRAYS + CY_SPC_FIRST_EE_ARRAYID))) { status = CYRET_BAD_PARAM; } if(arrayId <= CY_SPC_LAST_FLASH_ARRAYID) { /* Flash */ if(rowNumber > (CY_FLASH_NUMBER_ROWS/CY_FLASH_NUMBER_ARRAYS)) { status = CYRET_BAD_PARAM; } } else { /* EEPROM */ if(rowNumber > (CY_EEPROM_NUMBER_ROWS/CY_FLASH_EEPROM_NUMBER_ARRAYS)) { status = CYRET_BAD_PARAM; } if(CY_EEPROM_SIZEOF_ROW != rowSize) { status = CYRET_BAD_PARAM; } } if(rowData == NULL) { status = CYRET_BAD_PARAM; } if(status == CYRET_SUCCESS) { if(CySpcLock() == CYRET_SUCCESS) { /* Load row data into SPC internal latch */ status = CySpcLoadRowFull(arrayId, rowNumber, rowData, rowSize); if(CYRET_STARTED == status) { while(CY_SPC_BUSY) { /* Wait for SPC to finish and get SPC status */ CyDelayUs(1u); } /* Hide SPC status */ if(CY_SPC_STATUS_SUCCESS == CY_SPC_READ_STATUS) { status = CYRET_SUCCESS; } else { status = CYRET_UNKNOWN; } if(CYRET_SUCCESS == status) { /* Erase and program flash with data from SPC interval latch */ status = CySpcWriteRow(arrayId, rowNumber, dieTemperature[0u], dieTemperature[1u]); if(CYRET_STARTED == status) { while(CY_SPC_BUSY) { /* Wait for SPC to finish and get SPC status */ CyDelayUs(1u); } /* Hide SPC status */ if(CY_SPC_STATUS_SUCCESS == CY_SPC_READ_STATUS) { status = CYRET_SUCCESS; } else { status = CYRET_UNKNOWN; } } } } CySpcUnlock(); } /* if(CySpcLock() == CYRET_SUCCESS) */ else { status = CYRET_LOCKED; } } return(status); }