Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- uint32_t EE_WriteBuffer(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite)
- {
- uint32_t write_status = EE_OK;
- uint8_t NumOfPage = 0, NumOfSingle = 0, count = 0, retry_count = 0;
- uint16_t Addr = 0;
- __IO uint8_t EEDataNum;
- Addr = WriteAddr % EE_PAGESIZE;
- count = EE_PAGESIZE - Addr;
- NumOfPage = NumByteToWrite / EE_PAGESIZE;
- NumOfSingle = NumByteToWrite % EE_PAGESIZE;
- retry_count = EE_NUMBER_RETRY;
- /* If WriteAddr is EE_PAGESIZE aligned */
- if(Addr == 0)
- {
- /* If NumByteToWrite < EE_PAGESIZE */
- if(NumOfPage == 0)
- {
- /* Store the number of data to be written */
- EEDataNum = NumOfSingle;
- /* Start writing data */
- write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
- while(write_status && retry_count--)
- {
- write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
- }
- /* Wait transfer to be complete */
- EETimeout = EE_LONG_TIMEOUT;
- while (EEDataNum > 0)
- {
- if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE12);
- }
- write_status = EE_WaitEepromStandbyState() | write_status;
- }
- /* If NumByteToWrite > EE_PAGESIZE */
- else
- {
- while(NumOfPage--)
- {
- /* Store the number of data to be written */
- EEDataNum = EE_PAGESIZE;
- write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
- while(write_status && retry_count--)
- {
- write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
- }
- /* Wait transfer to be complete */
- EETimeout = EE_LONG_TIMEOUT;
- while (EEDataNum > 0)
- {
- if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE13);
- }
- write_status = EE_WaitEepromStandbyState() | write_status;
- WriteAddr += EE_PAGESIZE;
- pBuffer += EE_PAGESIZE;
- //reset retry counter
- retry_count = EE_NUMBER_RETRY;
- }
- if(NumOfSingle!=0)
- {
- /* Store the number of data to be written */
- EEDataNum = NumOfSingle;
- write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
- while(write_status && retry_count--)
- {
- write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
- }
- /* Wait transfer to be complete */
- EETimeout = EE_LONG_TIMEOUT;
- while (EEDataNum > 0)
- {
- if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE14);
- }
- write_status = EE_WaitEepromStandbyState() | write_status;
- }
- }
- }
- /* If WriteAddr is not EE_PAGESIZE aligned */
- else
- {
- /* If NumByteToWrite < EE_PAGESIZE */
- if(NumOfPage== 0)
- {
- /* If the number of data to be written is more than the remaining space
- in the current page: */
- if (NumByteToWrite > count)
- {
- /* Store the number of data to be written */
- EEDataNum = count;
- /* Write the data contained in same page */
- write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
- while(write_status && retry_count--)
- {
- write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
- }
- /* Wait transfer to be complete */
- EETimeout = EE_LONG_TIMEOUT;
- while (EEDataNum > 0)
- {
- if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE15);
- }
- write_status = EE_WaitEepromStandbyState() | write_status;
- //reset retry counter
- retry_count = EE_NUMBER_RETRY;
- /* Store the number of data to be written */
- EEDataNum = (NumByteToWrite - count);
- /* Write the remaining data in the following page */
- write_status = EE_WritePage((uint8_t*)(pBuffer + count), (WriteAddr + count), (uint8_t*)(&EEDataNum));
- while(write_status && retry_count--)
- {
- write_status = EE_WritePage((uint8_t*)(pBuffer + count), (WriteAddr + count), (uint8_t*)(&EEDataNum));
- }
- /* Wait transfer to be complete */
- EETimeout = EE_LONG_TIMEOUT;
- while (EEDataNum > 0)
- {
- if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE16);
- }
- write_status = EE_WaitEepromStandbyState() | write_status;
- }
- else
- {
- /* Store the number of data to be written */
- EEDataNum = NumOfSingle;
- write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
- while(write_status && retry_count--)
- {
- write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
- }
- /* Wait transfer to be complete */
- EETimeout = EE_LONG_TIMEOUT;
- while (EEDataNum > 0)
- {
- if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE17);
- }
- write_status = EE_WaitEepromStandbyState() | write_status;
- }
- }
- /* If NumByteToWrite > EE_PAGESIZE */
- else
- {
- NumByteToWrite -= count;
- NumOfPage = NumByteToWrite / EE_PAGESIZE;
- NumOfSingle = NumByteToWrite % EE_PAGESIZE;
- if(count != 0)
- {
- /* Store the number of data to be written */
- EEDataNum = count;
- write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
- while(write_status && retry_count--)
- {
- write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
- }
- /* Wait transfer to be complete */
- EETimeout = EE_LONG_TIMEOUT;
- while (EEDataNum > 0)
- {
- if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE18);
- }
- write_status = EE_WaitEepromStandbyState() | write_status;
- WriteAddr += count;
- pBuffer += count;
- //reset retry counter
- retry_count = EE_NUMBER_RETRY;
- }
- while(NumOfPage--)
- {
- /* Store the number of data to be written */
- EEDataNum = EE_PAGESIZE;
- write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
- while(write_status && retry_count--)
- {
- write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
- }
- /* Wait transfer to be complete */
- EETimeout = EE_LONG_TIMEOUT;
- while (EEDataNum > 0)
- {
- if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE19);
- }
- write_status = EE_WaitEepromStandbyState() | write_status;
- WriteAddr += EE_PAGESIZE;
- pBuffer += EE_PAGESIZE;
- //reset retry counter
- retry_count = EE_NUMBER_RETRY;
- }
- if(NumOfSingle != 0)
- {
- /* Store the number of data to be written */
- EEDataNum = NumOfSingle;
- write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
- while(write_status && retry_count--)
- {
- write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
- }
- /* Wait transfer to be complete */
- EETimeout = EE_LONG_TIMEOUT;
- while (EEDataNum > 0)
- {
- if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE1A);
- }
- write_status = EE_WaitEepromStandbyState() | write_status;
- }
- }
- }
- return write_status;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement