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

📄 c1667.c

📁 Flash芯片的CFI驱动c语言源代码,对自升级的系统有帮助。
💻 C
📖 第 1 页 / 共 3 页
字号:
/*******************************************************************************Function:    uCPUBusType FlashRead( udword udAddrOff )Arguments:   udAddrOff is the offset into the flash to read from.Return value:     The uCPUBusType content at the address offset.Description: This function is used to read a uCPUBusType from the flash.   On many microprocessor systems a macro can be used instead, increasing the   speed of the flash routines. For example:#define FlashRead( udAddrOff ) ( BASE_ADDR[udAddrOff] )   A function is used here instead to allow the user to expand it if necessary.Pseudo Code:   Step 1: Return the value at double-word offset udAddrOff*******************************************************************************/uCPUBusType FlashRead( udword udAddrOff ) {   /* Step 1 Return the value at double-word offset udAddrOff */   return BASE_ADDR[udAddrOff];} /* EndFunction FlashRead */   /*******************************************************************************Function:      ReturnType FlashReadDeviceId( uCPUBusType *ucpDeviceId )Arguments:     - ucpDeviceId = <return value> The function returns the Device Code.   The device code for the part is:      M29KW064E   0x88AF   In case a common response of more flash chips is not identical the real read   value will be given (Flash_ResponseUnclear).Return Value:  The function returns the following conditions:   Flash_Success   Flash_ResponseUnclearDescription:   This function can be used to read the device code of the flash.Pseudo Code:   Step 1:  Send the Auto Select instruction   Step 2:  Read the DeviceId   Step 3:  Return the device to Read Array mode   Step 4:  Check flash response (more flashes could give different results)*******************************************************************************/ReturnType FlashReadDeviceId( uCPUBusType *ucpDeviceId ) {   /* Step 1: Send the Auto Select instruction */   FlashWrite(0x555,(uCPUBusType)CMD(0xAA));   FlashWrite(0x2AA,(uCPUBusType)CMD(0x55));   FlashWrite(0x555,(uCPUBusType)CMD(0x90));   /* Step 2: Read the DeviceId */    *ucpDeviceId = FlashRead(1);      /* Step 3: Return to Read Array Mode */   FlashReset();   /* Step 4: Check flash response (more flashes could give different results) */   return FlashResponseIntegrityCheck( ucpDeviceId );    } /* EndFunction FlashReadDeviceId */   /*******************************************************************************Function:      ReturnType FlashReadManufacturerCode( uCPUBusType *ucpManufacturerCode )Arguments:     - ucpManufacturerCode = <return value> The function returns    the manufacture code (for ST = 0x0020).    In case a common response of more flash chips is not identical the real read   value will be given (Flash_ResponseUnclear)Return Value:  The function returns the following conditions:   Flash_Success   Flash_ResponseUnclearDescription:   This function can be used to read the manufacture code of the flash.Pseudo Code:   Step 1:  Send the Auto Select instruction   Step 2:  Read the DeviceId   Step 3:  Return the device to Read Array mode   Step 4:  Check flash response (more flashes could give different results)*******************************************************************************/ReturnType FlashReadManufacturerCode( uCPUBusType *ucpManufacturerCode ) {   /* Step 1: Send the Auto Select instruction  */   FlashWrite(0x555,(uCPUBusType)CMD(0xAA));   FlashWrite(0x2AA,(uCPUBusType)CMD(0x55));   FlashWrite(0x555,(uCPUBusType)CMD(0x90));   /* Step 2: Read the Manufacturer Code */   *ucpManufacturerCode = FlashRead(0);      /* Step 3: Return to Read Array Mode */   FlashReset();   /* Step 4: Check flash response (more flashes could give different results) */   return FlashResponseIntegrityCheck( ucpManufacturerCode );    } /* EndFunction FlashReadManufacturerCode */   /*******************************************************************************Function:      void FlashReset( void )Arguments:     noneReturn Value:  noneDescription:   This function places the flash in the Read Array mode described   in the Data Sheet. In this mode the flash can be read as normal memory.   All of the other functions leave the flash in the Read Array mode so this is   not strictly necessary. It is provided for completeness and in case of   problems.Pseudo Code:   Step 1: write command sequence (see Instructions Table of the Data Sheet)*******************************************************************************/ReturnType FlashReset( void ) {   /* Step 1: write command sequence */   FlashWrite( 0x555, (uCPUBusType)CMD(0xAA) );   FlashWrite( 0x2AA, (uCPUBusType)CMD(0x55) );   FlashWrite( ANY_ADDR, (uCPUBusType)CMD(0xF0) );   return Flash_Success;} /* EndFunction FlashReset */   /*******************************************************************************Function:      ReturnType FlashResponseIntegrityCheck(uCPUBusType *ucpFlashResponse)Arguments:     - ucpFlashResponse <parameter> + <return value>    The function returns a unique value in case one flash or an   array of flashes return all the same value (Consistent Response = Flash_Success).   In case an array of flashes returns different values the function returns the   received response without any changes (Inconsistent Response = Flash_ResponseUnclear).Return Value:  The function returns the following conditions:   Flash_Success   Flash_ResponseUnclear               Description:   This function is used to create one response in multi flash   environments, instead of giving multiple answers of the single flash   devices.    For example: Using a 32bit CPU and two 16bit Flash devices, the device Id   would be directly read: 00170017h, because each device gives an answer   within the range of the databus. In order to give a simple response   like 00000017h in all possible configurations, this subroutine is used.    In case the two devices give different results for the device Id, the   answer would then be: 00150017h. This allows debugging and helps to   discover multiple flash configuration problems.       Pseudo Code:    Step 1:  Extract the first single flash response    Step 2:  Compare all next possible flash responses with the first one   Step 3a: Return all flash responses in case of different values   Step 3b: Return only the first single flash response in case of matching values    *******************************************************************************/ReturnType FlashResponseIntegrityCheck(uCPUBusType *ucpFlashResponse) {#ifdef USE_32BIT_CPU_ACCESSING_1_16BIT_FLASH   return Flash_Success;#else   ubyte a;   union {      uCPUBusType ucFlashResponse;      ubyte       ubBytes[sizeof(uCPUBusType)];   } FullResponse;   union {      uCPUBusType ucSingleResponse;      ubyte       ubBytes[FLASH_BIT_DEPTH/8];   } SingleResponse;   SingleResponse.ucSingleResponse = 0;   FullResponse.ucFlashResponse    = *ucpFlashResponse;   /* Step 1: Extract the first single flash response */   memcpy(SingleResponse.ubBytes, FullResponse.ubBytes, FLASH_BIT_DEPTH/8);   /* Step 2: Compare all next possible flash responses with the first one */   for (a = 0; a < sizeof(uCPUBusType); a += FLASH_BIT_DEPTH/8) {      if (memcmp (&FullResponse.ubBytes[a], SingleResponse.ubBytes, FLASH_BIT_DEPTH/8) != 0)         /* Step 3a: Return all flash responses in case of different values */         return Flash_ResponseUnclear;   } /* Next a */                                                                       /* Step 3b: Return only the first single flash response in case of matching values */   *ucpFlashResponse = SingleResponse.ucSingleResponse;   return Flash_Success;#endif /* defined USE_32BIT_CPU_ACCESSING_1_16BIT_FLASH */} /* EndFunction FlashResponseIntegrityCheck */   /*******************************************************************************Function:     ReturnType FlashSingleProgram( udword udAddrOff, uCPUBusType ucVal )Arguments:    udAddrOff is the offset in the flash to write to.   ucVal is the value to be written.Return Value:   Flash_Success   Flash_AddressInvalid      Flash_BlockProtected   Flash_OperationTimeOut    Flash_ProgramFailed    Flash_VppInvalidDescription: This function is used to write a single element to the flash.Pseudo Code:   Step 1:  Check whether the data to be programmed is within the Flash memory space    Step 2:  Send Word Program Command Sequence   Step 3:  Wait until Program/Erase Controller has completed   Step 4:  Return to Read mode (if an error occurred)   Step 5:  Return the error condition*******************************************************************************/ReturnType FlashSingleProgram(udword udAddrOff, uCPUBusType ucValue ) {   ReturnType rRetVal = Flash_Success; /* Return Value: Initially optimistic */   ReturnType rRetVal2;   /*Step 1:Check whether the data to be programmed are within the Flash memory space */   if( udAddrOff >= FLASH_SIZE )      return Flash_AddressInvalid;   /* Step 2: Send Word Program Command Sequence  */   FlashWrite( 0x555, CMD(0xAA) );    FlashWrite( 0x2AA, CMD(0x55) );    FlashWrite( 0x555, CMD(0xA0) );    FlashWrite( udAddrOff, ucValue );    /* Step 3: Wait until Program/Erase Controller has completed */   rRetVal2 = FlashDataToggle();   if( rRetVal2 !=  Flash_Success ) {      /* Step 4: Return to Read mode (if an error occurred) */      FlashWrite( ANY_ADDR, (uCPUBusType)CMD(0x00F0) ); /* Use single instruction cycle method */      FlashReset();      rRetVal=Flash_ProgramFailed;   } /* EndIf */   /* Step 5: Return the error condition */   return rRetVal;} /* EndFunction FlashSingleProgram */    /*******************************************************************************Function:     ReturnType FlashTimeOut(udword udSeconds)Arguments:    fSeconds holds the number of seconds before giving a TimeOutReturn Value: The function returns the following conditions:     Flash_OperationTimeOut      Flash_OperationOngoing Example:   FlashTimeOut(0)  // Initializes the Timer           While(1) {              ...              If (FlashTimeOut(5) == Flash_OperationTimeOut) break;              // The loop is executed for 5 Seconds before leaving it           } EndWhile*******************************************************************************/#ifdef TIME_H_EXISTS/*-----------------------------------------------------------------------------Description:   This function realizes a timeout for flash polling actions or   other operations which would otherwise never return.   The Routine uses the function clock() inside ANSI C library "time.h". -----------------------------------------------------------------------------*/ReturnType FlashTimeOut(udword udSeconds){   static clock_t clkReset,clkCount;   if (udSeconds == 0) { /* Set Timeout to 0 */      clkReset=clock();   } /* EndIf */   clkCount = clock() - clkReset;   if (clkCount<(CLOCKS_PER_SEC*(clock_t)udSeconds))      return Flash_OperationOngoing;   else      return Flash_OperationTimeOut;} /* EndFunction FlashTimeOut */#else /*-----------------------------------------------------------------------------Description:   This function realizes a timeout for flash polling actions or   other operations which would otherwise never return.   The Routine uses COUNT_FOR_A_SECOND which describes the performance of    the current Hardware. If I count in a loop to COUNT_FOR_A_SECOND   I would reach 1 Second. Needs to be adopted to the current Hardware. -----------------------------------------------------------------------------*/ReturnType FlashTimeOut(udword udSeconds) {   static udword udCounter;   if (udSeconds == 0) { /* Set Timeout to 0 */      udCounter = 0;   } /* EndIf */   if (udCounter == (udSeconds * COUNT_FOR_A_SECOND)) {      return Flash_OperationTimeOut;   } else {      udCounter++;      return Flash_OperationOngoing;   } /* EndIf */} /* EndFunction FlashTimeOut */#endif /* TIME_H_EXISTS */   /*******************************************************************************Function:     void FlashWrite( udword udAddrOff, uCPUBusType ucVal )Arguments:    udAddrOff is double-word offset in the flash to write to.   ucVal is the value to be writtenReturn Value: NoneDescription: This function is used to write a uCPUBusType to the flash.*******************************************************************************/void FlashWrite( udword udAddrOff, uCPUBusType ucVal ) {   /* Write ucVal to the double-word offset in flash */   BASE_ADDR[udAddrOff] = ucVal;} /* EndFunction FlashWrite *//*******************************************************************************  End of c1667.c*******************************************************************************/ 

⌨️ 快捷键说明

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