Advertisement
Guest User

old_code

a guest
Mar 2nd, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.70 KB | None | 0 0
  1. uint32_t EE_WriteBuffer(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite)
  2. {
  3.     uint32_t write_status = EE_OK;
  4.     uint8_t NumOfPage = 0, NumOfSingle = 0, count = 0, retry_count = 0;
  5.     uint16_t Addr = 0;
  6.     __IO uint8_t EEDataNum;
  7.    
  8.     Addr = WriteAddr % EE_PAGESIZE;
  9.     count = EE_PAGESIZE - Addr;
  10.     NumOfPage =  NumByteToWrite / EE_PAGESIZE;
  11.     NumOfSingle = NumByteToWrite % EE_PAGESIZE;
  12.     retry_count = EE_NUMBER_RETRY;
  13.  
  14.     /* If WriteAddr is EE_PAGESIZE aligned  */
  15.     if(Addr == 0)
  16.     {
  17.         /* If NumByteToWrite < EE_PAGESIZE */
  18.         if(NumOfPage == 0)
  19.         {
  20.             /* Store the number of data to be written */
  21.             EEDataNum = NumOfSingle;
  22.  
  23.             /* Start writing data */
  24.             write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
  25.             while(write_status && retry_count--)
  26.             {
  27.                write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
  28.             }
  29.  
  30.             /* Wait transfer to be complete */
  31.             EETimeout = EE_LONG_TIMEOUT;
  32.             while (EEDataNum > 0)
  33.             {
  34.                 if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE12);
  35.             }
  36.             write_status = EE_WaitEepromStandbyState() | write_status;
  37.         }
  38.         /* If NumByteToWrite > EE_PAGESIZE */
  39.         else
  40.         {
  41.             while(NumOfPage--)
  42.             {
  43.                 /* Store the number of data to be written */
  44.                 EEDataNum = EE_PAGESIZE;
  45.                 write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
  46.                 while(write_status && retry_count--)
  47.                 {
  48.                   write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
  49.                 }
  50.  
  51.                 /* Wait transfer to be complete */
  52.                 EETimeout = EE_LONG_TIMEOUT;
  53.                 while (EEDataNum > 0)
  54.                 {
  55.                     if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE13);
  56.                 }
  57.                 write_status = EE_WaitEepromStandbyState() | write_status;
  58.                 WriteAddr +=  EE_PAGESIZE;
  59.                 pBuffer += EE_PAGESIZE;
  60.                 //reset retry counter
  61.                 retry_count = EE_NUMBER_RETRY;
  62.             }
  63.  
  64.             if(NumOfSingle!=0)
  65.             {
  66.                 /* Store the number of data to be written */
  67.                 EEDataNum = NumOfSingle;
  68.                 write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
  69.                 while(write_status && retry_count--)
  70.                 {
  71.                   write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
  72.                 }
  73.                 /* Wait transfer to be complete */
  74.                 EETimeout = EE_LONG_TIMEOUT;
  75.                 while (EEDataNum > 0)
  76.                 {
  77.                     if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE14);
  78.                 }
  79.                 write_status = EE_WaitEepromStandbyState() | write_status;
  80.             }
  81.         }
  82.     }
  83.     /* If WriteAddr is not EE_PAGESIZE aligned  */
  84.     else
  85.     {
  86.         /* If NumByteToWrite < EE_PAGESIZE */
  87.         if(NumOfPage== 0)
  88.         {
  89.             /* If the number of data to be written is more than the remaining space
  90.               in the current page: */
  91.             if (NumByteToWrite > count)
  92.             {
  93.                 /* Store the number of data to be written */
  94.                 EEDataNum = count;
  95.                 /* Write the data contained in same page */
  96.                 write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
  97.                 while(write_status && retry_count--)
  98.                 {
  99.                   write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
  100.                 }
  101.                 /* Wait transfer to be complete */
  102.                 EETimeout = EE_LONG_TIMEOUT;
  103.                 while (EEDataNum > 0)
  104.                 {
  105.                     if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE15);
  106.                 }
  107.                 write_status = EE_WaitEepromStandbyState() | write_status;
  108.                 //reset retry counter
  109.                 retry_count = EE_NUMBER_RETRY;
  110.  
  111.                 /* Store the number of data to be written */
  112.                 EEDataNum = (NumByteToWrite - count);
  113.                 /* Write the remaining data in the following page */
  114.                 write_status = EE_WritePage((uint8_t*)(pBuffer + count), (WriteAddr + count), (uint8_t*)(&EEDataNum));
  115.                 while(write_status && retry_count--)
  116.                 {
  117.                   write_status = EE_WritePage((uint8_t*)(pBuffer + count), (WriteAddr + count), (uint8_t*)(&EEDataNum));
  118.                 }
  119.                 /* Wait transfer to be complete */
  120.                 EETimeout = EE_LONG_TIMEOUT;
  121.                 while (EEDataNum > 0)
  122.                 {
  123.                     if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE16);
  124.                 }
  125.                 write_status = EE_WaitEepromStandbyState() | write_status;
  126.             }
  127.             else
  128.             {
  129.                 /* Store the number of data to be written */
  130.                 EEDataNum = NumOfSingle;
  131.                 write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
  132.                 while(write_status && retry_count--)
  133.                 {
  134.                   write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
  135.                 }
  136.                 /* Wait transfer to be complete */
  137.                 EETimeout = EE_LONG_TIMEOUT;
  138.                 while (EEDataNum > 0)
  139.                 {
  140.                     if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE17);
  141.                 }
  142.                 write_status = EE_WaitEepromStandbyState() | write_status;
  143.             }
  144.         }
  145.         /* If NumByteToWrite > EE_PAGESIZE */
  146.         else
  147.         {
  148.             NumByteToWrite -= count;
  149.             NumOfPage =  NumByteToWrite / EE_PAGESIZE;
  150.             NumOfSingle = NumByteToWrite % EE_PAGESIZE;
  151.  
  152.             if(count != 0)
  153.             {
  154.                 /* Store the number of data to be written */
  155.                 EEDataNum = count;
  156.                 write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
  157.                 while(write_status && retry_count--)
  158.                 {
  159.                   write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
  160.                 }
  161.                 /* Wait transfer to be complete */
  162.                 EETimeout = EE_LONG_TIMEOUT;
  163.                 while (EEDataNum > 0)
  164.                 {
  165.                     if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE18);
  166.                 }
  167.                 write_status = EE_WaitEepromStandbyState() | write_status;
  168.                 WriteAddr += count;
  169.                 pBuffer += count;
  170.                 //reset retry counter
  171.                 retry_count = EE_NUMBER_RETRY;
  172.             }
  173.  
  174.             while(NumOfPage--)
  175.             {
  176.                 /* Store the number of data to be written */
  177.                 EEDataNum = EE_PAGESIZE;
  178.                 write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
  179.                 while(write_status && retry_count--)
  180.                 {
  181.                   write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
  182.                 }
  183.                 /* Wait transfer to be complete */
  184.                 EETimeout = EE_LONG_TIMEOUT;
  185.                 while (EEDataNum > 0)
  186.                 {
  187.                     if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE19);
  188.                 }
  189.                 write_status = EE_WaitEepromStandbyState() | write_status;
  190.                 WriteAddr +=  EE_PAGESIZE;
  191.                 pBuffer += EE_PAGESIZE;
  192.                 //reset retry counter
  193.                 retry_count = EE_NUMBER_RETRY;
  194.             }
  195.             if(NumOfSingle != 0)
  196.             {
  197.                 /* Store the number of data to be written */
  198.                 EEDataNum = NumOfSingle;
  199.                 write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
  200.                 while(write_status && retry_count--)
  201.                 {
  202.                   write_status = EE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&EEDataNum));
  203.                 }
  204.                 /* Wait transfer to be complete */
  205.                 EETimeout = EE_LONG_TIMEOUT;
  206.                 while (EEDataNum > 0)
  207.                 {
  208.                     if((EETimeout--) == 0) return EE_TIMEOUT_UserCallback(0xEE1A);
  209.                 }
  210.                 write_status = EE_WaitEepromStandbyState() | write_status;
  211.             }
  212.         }
  213.     }
  214.     return write_status;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement