⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c2635.c

📁 Flash Memory 依據標準CFI CMD做Erase/Program過程的參考碼
💻 C
📖 第 1 页 / 共 5 页
字号:
      return Flash_AddressInvalid; /* udAddrOff must be 32 Word/64Bytes aligned */
   if(udNrOfElementsInArray<1 || udNrOfElementsInArray > ShAddr(0x20)) 
      return Flash_AddressInvalid; /* Elements should smaller than the buffer: 32Word/64Bytes */
   
   /* Step 1: Check if the data to be programmed are within the Flash memory space   */
   udLastOff = udAddrOff + udNrOfElementsInArray - 1; 
   if( udLastOff >= FLASH_SIZE ) 
      return Flash_AddressInvalid; 

   /* Step 2: Determine first and last block to program */
   for (ublFirstBlock=0; ublFirstBlock < NUM_BLOCKS-1;ublFirstBlock++)
      if (udAddrOff < BlockOffset[ublFirstBlock+1]) 
         break;
   for (ublLastBlock=ublFirstBlock; ublLastBlock < NUM_BLOCKS-1;ublLastBlock++)
      if (udLastOff < BlockOffset[ublLastBlock+1]) 
         break;

   /* Step 3: Check protection status for the blocks to be programmed */
   for (ublCurBlock = ublFirstBlock; ublCurBlock <= ublLastBlock; ublCurBlock++) {
      if ( (rProtStatus = FlashCheckBlockProtection(ublCurBlock)) != Flash_BlockUnprotected ) {
         rRetVal = Flash_BlockProtected;
         if (ublCurBlock == ublFirstBlock) {
            eiErrorInfo.udGeneralInfo[0] = udAddrOff;
            return rRetVal; 
         } else {
            eiErrorInfo.udGeneralInfo[0] = BlockOffset[ublCurBlock];
            udLastOff = BlockOffset[ublCurBlock]-1;
         } /* EndIf ublCurBlock == ublFirstBlock */
      } /* EndIf rProtStatus */
   } /* Next ublCurBlock */
 
   ucpArrayPointer = (uCPUBusType *)pArray; 

   /* Step 4a: Issue Write to Program Buffer command */ 
       FlashWrite( ConvAddr(0x00555), (uCPUBusType)CMD(0x00AA) ); /* 1st cycle */
       FlashWrite( ConvAddr(0x002AA), (uCPUBusType)CMD(0x0055) ); /* 2nd cycle */
       FlashWrite( udAddrOff, (uCPUBusType)CMD(0x0025) ); /* 3nd cycle */
       FlashWrite( udAddrOff, udNrOfElementsInArray-1); /* 4th cycle */
   /* Step 4b: Write Content to buffer */
   while( udAddrOff <= udLastOff ) {
       FlashWrite( udAddrOff, (uCPUBusType)CMD(*ucpArrayPointer) ); /* 5th cycle */
   
   if (udMode == 0) /* Decision between direct and single value programming */
         ucpArrayPointer++;
      udAddrOff++;
   } /* EndWhile */

   return rRetVal;
} /* EndFunction FlashWriteProgramBuffer */ 




/******************************************************************************* 
Function:     ReturnType FlashBufferProgramConfirm( udword udAddrOff ) 
Arguments:  udAddrOff is the address offset exactly same as the one issued 
			by the Write to Buffer Command 
Return Value: The function returns the following conditions:   
               Flash_Success       
               Flash_ProgramFailed 
Note:         
Description: This function confirms the Write program Buffer Command(done by function FlashWriteProgramBuffer)
		and start the buffer program
		
Pseudo Code: 
   Step 1: Issue Buffer Program Confirm command
   Step 2: Wait until the Program/Erase Controller has completed
********************************************************************************/ 
ReturnType FlashBufferProgramConfirm( udword udAddrOff ) {
	
   ReturnType  rRetVal = Flash_Success; /* Return Value: Initially optimistic */ 

   /* Step 1:  Issue Buffer Program Confirm command */
   FlashWrite( udAddrOff, (uCPUBusType)CMD(0x0029) ); /* 1st cycle */

   /* Step 2: Wait until Program/Erase Controller has completed */
   if( FlashDataToggle() != Flash_Success) {
   /* Step 3: Return to Read Mode */
         FlashWrite( ANY_ADDR, (uCPUBusType)CMD(0x00F0) ); /* Use single instruction cycle method */
         rRetVal=Flash_ProgramFailed;
         eiErrorInfo.udGeneralInfo[0] = udAddrOff;
         return rRetVal; /* exit while cycle */
      } /* EndIf */

    return rRetVal;
    
}/* EndFunction FlashBufferProgramConfirm */ 




/******************************************************************************* 
Function:     ReturnType FlashBufferProgramAbort( void ) 
Arguments:  none
Return Value: The function returns the following conditions:   
               Flash_Success       
Note:         
Description: This function Abort the Write program Buffer Command(done by function FlashWriteProgramBuffer)
		and Reset to read array mode 
		
Pseudo Code: 
   Step 1: Issue Buffer Program Abort command
********************************************************************************/ 
ReturnType FlashBufferProgramAbort( void ){
	
	 /* Step 1: Issue Buffer Program Abort command */
       FlashWrite( ConvAddr(0x00555), (uCPUBusType)CMD(0x00AA) ); /* 1st cycle */
       FlashWrite( ConvAddr(0x002AA), (uCPUBusType)CMD(0x0055) ); /* 2nd cycle */
       FlashWrite( ConvAddr(0x00555), (uCPUBusType)CMD(0x00F0) ); /* 3rd cycle */
	 return Flash_Success;
} /* EndFunction  FlashBufferProgramAbort */




/******************************************************************************* 
Function:     ReturnType FlashBufferProgram( udword udMode,udword udAddrOff, udword udNrOfElementsInArray, void *pArray  ) 
Arguments:  udMode changes between programming modes
            udAddrOff must be 16Word/32Byte aligned, is the address offset into the flash to be programmed
            udNrOfElementsInArray holds the number of elements (uCPUBusType) in the array. 
            pArray is a void pointer to the array with the contents to be programmed.
Return Value: The function returns the following conditions:   
               Flash_Success       
               Flash_ProgramFailed 
Note:         
Description: This function use buffer program method to speed up the program process
		it will call FlashWriteProgramBuffer and FlashBufferProgramConfirm
		
Pseudo Code: 
   Step 1: Check whether the data to be programmed are within the Flash memory 
   Step 2: Determine first and last block to program
   Step 3: Check protection status for the blocks to be programmed
   Step 4: Call FlashWriteProgramBuffer 
   Step 5: Call FlashBufferProgramConfirm to start buffer program
   Step 6: Judge conditions
   		 if Step 5 return Flash_ProgramFailed,return;
             if Step 5 return Flash_Success and there is data remains, repeat step 4 and step 5
             if Step 5 return Flash_Success and no data remains, go to next step
   Step 7: Return to Read Mode 
********************************************************************************/ 
ReturnType FlashBufferProgram( udword udMode,udword udAddrOff, udword udNrOfElementsInArray, void *pArray ) {

   ReturnType  rRetVal = Flash_Success; /* Return Value: Initially optimistic */ 
   ReturnType  rProtStatus; /* Protection Status of a block */
   uCPUBusType *ucpArrayPointer; /* Use an uCPUBusType to access the array */ 
   udword      udLastOff; /* Holds the last offset to be programmed */ 
   uBlockType  ublFirstBlock; /* The block where start to program */
   uBlockType  ublLastBlock; /* The last block to be programmed */
   uBlockType  ublCurBlock; /* Current block */
   udword remains; /* remain numbers to be written in the programming array */
   udword curLength; /* current length need to write to the buffer */

   if (udMode > 1) 
      return Flash_FunctionNotSupported;
   if (udAddrOff & ConvAddr(0x1F) != 0x0) 
      return Flash_AddressInvalid; /* udAddrOff must be 32 Word/64 Bytes aligned */

   /* Step 1: Check if the data to be programmed are within the Flash memory Space   */
   udLastOff = udAddrOff + udNrOfElementsInArray - 1; 
   if( udLastOff >= FLASH_SIZE ) 
      return Flash_AddressInvalid; 

   /* Step 2: Determine first and last block to program */
   for (ublFirstBlock=0; ublFirstBlock < NUM_BLOCKS-1;ublFirstBlock++)
      if (udAddrOff < BlockOffset[ublFirstBlock+1]) 
         break;
   for (ublLastBlock=ublFirstBlock; ublLastBlock < NUM_BLOCKS-1;ublLastBlock++)
      if (udLastOff < BlockOffset[ublLastBlock+1]) 
         break;

   /* Step 3: Check protection status for the blocks to be programmed */
   for (ublCurBlock = ublFirstBlock; ublCurBlock <= ublLastBlock; ublCurBlock++) {
      if ( (rProtStatus = FlashCheckBlockProtection(ublCurBlock)) != Flash_BlockUnprotected ) {
         rRetVal = Flash_BlockProtected;
         if (ublCurBlock == ublFirstBlock) {
            eiErrorInfo.udGeneralInfo[0] = udAddrOff;
            return rRetVal; 
         } else {
            eiErrorInfo.udGeneralInfo[0] = BlockOffset[ublCurBlock];
            udLastOff = BlockOffset[ublCurBlock]-1;
         } /* EndIf ublCurBlock == ublFirstBlock */
      } /* EndIf rProtStatus */
   } /* Next ublCurBlock */
   
   ucpArrayPointer = (uCPUBusType *)pArray;
   remains = udNrOfElementsInArray;
   
   while( remains > 0 ){
   /* Program Buffer size: 32Words/64Bytes */
   curLength = (remains > ShAddr(0x20)) ? ShAddr(0x20) : remains;
   
   /* Step 4: Call FlashWriteProgramBuffer */
   rRetVal = FlashWriteProgramBuffer(udMode,udAddrOff,curLength,ucpArrayPointer);
   if( rRetVal != Flash_Success ){
   	 FlashBufferProgramAbort(); /* there is some error,so abort the operation */
   	 return rRetVal;
   }
   
   /* Step 5: Call FlashBufferProgramConfirm to start buffer program */
   rRetVal = FlashBufferProgramConfirm(udAddrOff);
   
   udAddrOff += curLength;
   if (udMode == 0) /* Decision between direct and single value programming */
   	ucpArrayPointer += curLength;
   remains -= curLength;
   
   /* Step 6: Judge conditions */
   if( rRetVal != Flash_Success ) return rRetVal;
   else if( (rRetVal == Flash_Success) && remains>0 )
   	continue;
   else if( (rRetVal == Flash_Success) && remains==0 )
      break;
   }/* End while */
   
   /* Step 7: Return to Read Mode */
   FlashWrite( ANY_ADDR, CMD(0x00F0) );
   
   return rRetVal;
}/* EndFunction FlashBufferProgram */ 


/******************************************************************************* 
Function:     ReturnType FlashUnlockBypass( void )
Arguments:    None
Return Value: The function returns the following conditions: 
              Flash_Success, successful operation 
Description:  This function is used in conjunction with the FlashUnlockBypassProgram 
              function to program faster the memory. After the FlashUnlockBypass command,
              the flash enters into Unlock Bypass mode. To return the flash in the Read 
              Array mode, the FlashUnlockBypassReset() function can be used.

  Pseudo Code: 
   Step 1: Issue the unlock Bypass Command
*******************************************************************************/ 
ReturnType FlashUnlockBypass( void ) { 

   /* Step 1: Issue the Unlock Bypass Command */
   FlashWrite( ConvAddr(0x00555), (uCPUBusType)CMD(0x00AA) ); /* 1st cycle */
   FlashWrite( ConvAddr(0x002AA), (uCPUBusType)CMD(0x0055) ); /* 2nd cycle */
   FlashWrite( ConvAddr(0x00555), (uCPUBusType)CMD(0x0020) ); /* 3nd cycle */
   return Flash_Success;
} /* EndFunction FlashUnlockBypass */


/*******************************************************************************
Function:     ReturnType  FlashUnlockBypassProgram (udword udAddrOff, udword NumWords, 
                          void *pArray)

Arguments:    udAddrOff is the word offset into the flash to be programmed. 
   NumWords holds the number of words in the array.
   pArray is a pointer to the array to be programmed.
             
Return Value: The function returns the following conditions: 
   Flash_Success
   Flash_ProgramFailed
   Flash_AddressInvalid

   When all addresses are successfully programmed the function returns Flash_Success.
   The function returns Flash_ProgramFail if a programming failure occurs:
   udFirstAddrOffProgramFailed will be filled with the first address on which
   the program operation has failed and the functions does not continue to program
   on the remaining addresses.
   If part of the address range to be programmed falls within a protected block,
   the function returns  nothing is programmed and the function no error return. 
   If the address range to be programmed exceeds the address range of the Flash
   Device the function returns Flash_AddressInvalid and nothing is programmed.

Description:  This function is used to program an array into the flash. It does
   not erase the flash first and may fail if the block(s) are not erased first.
   This function can be used only when the device is in unlock bypass mode. The memory 
   offers accellerated program operations through the Vpp pin. When the system asserts 
   Vpp on Vpp pin the memory enters the unlock bypass mode.

Pseudo Code:
   Step 1: Check the offset range is valid
   Step 2: Check that the block(s) to be programmed are not protected
   Step 3: Send the Unlock Bypass command
   Step 4: While there is more to be programmed
   Step 5: Program the next word
   Step 6: Follow Data Toggle Flow Chart until Program/Erase Controller has 
           completed
   Step 7: Return to Read Mode (if an error occurred)
   Step 8: Send the Unlock Bypass Reset command
*******************************************************************************/
ReturnType FlashUnlockBypassProgram (udword udAddrOff, udword NumWords, void *pArray){
   udword  udLastOff;
   uCPUBusType *ucpArrayPointer;  
   ReturnType rRetVal = Flash_Success; /* Return Value: Initially optimistic */ 

   udLastOff = udAddrOff + NumWords- 1;

   /* Step 1: Check that the offset and range are valid */   
   if( udLastOff >= FLASH_SIZE )
      return Flash_AddressInvalid;


   /* Step 4: While there is more to be programmed */
   ucpArrayPointer = (uCPUBusType *)pArray;
   while( udAddrOff <= udLastOff ){
      /* Step 5: Unlock Bypass Program the next word */
      FlashWrite( ANY_ADDR, (uCPUBusType)CMD(0x00A0) ); /* 1st cycle */
      FlashWrite( udAddrOff++,*ucpArrayPointer++ ); /* Program value */                              

      /* Step 6: Follow Data Toggle Flow Chart until Program/Erase Controller
                 has completed */

      /* See Data Toggle Flow Chart of the Data Sheet */

      if( FlashDataToggle() != Fl

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -