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

📄 c1667.c

📁 Flash芯片的CFI驱动c语言源代码,对自升级的系统有帮助。
💻 C
📖 第 1 页 / 共 3 页
字号:
/**************** M29KW064E Flash Memory Driver *******************************   Filename:    c1667.c   Description: Library routines for the M29KW064E                64Mb (4Mb x 16) Flash Memory drivers                in different configurations.   Version:     V1.1   Date:        21/03/2003    Author:      Sylvia Goettl, STMicroelectronics, Agrate Brianza (Italy)                Francesco Petruzziello, STMicroelectronics, Arzano (Italy)                   Copyright (c) 2003 STMicroelectronics.   THIS PROGRAM IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER   EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTY   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK   AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE   PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,   REPAIR OR CORRECTION.********************************************************************************   Version History.   Ver.   Date        Comments   1.1    21/03/2003  Initial Release of the software.********************************************************************************   This source file provides library C code for using the M29KW064E flash device.   The following device is supported in the code: M29KW064E   This file can be used to access the devices in 16-bit mode.   The following functions are available in this library:      Flash(BlockErase, ParameterType)            to erase a single block      Flash(CheckCompatibility, ParameterType)    to check the compatibility of the flash       Flash(ChipErase, ParameterType)             to erase the whole chip      Flash(Program, ParameterType)               to program an array of elements      Flash(Read, ParameterType)                  to read from the flash device          Flash(ReadDeviceId, ParameterType)          to get the Device ID from the device         Flash(ReadManufacturerCode, ParameterType)  to get the Manufacture Code from the device      Flash(Reset, ParameterType)                 to reset the flash for normal memory access      Flash(SingleProgram, ParameterType)         to program a single element of the flash      Flash(Write, ParameterType)                 to write a value to the flash device      FlashDataToggle()                           to follow the Data Toggle Flowchart      FlashErrorStr()                             to return the error string of an error      (only available with define VERBOSE)          FlashMWProgram()                            to program an array using the Multiple Word                                                  Program explicitly      FlashTimeOut()                              to return after function timeouts    For further information consult the Data Sheet and the Application Note.   The Application Note gives information about how to modify this code for   a specific application.   The hardware specific functions which may need to be modified by the user are:      FlashWrite() for writing a (double)word to the flash      FlashRead()  for reading a (double)word from the flash   A list of the error conditions can be found at the end of the header file.*******************************************************************************/#include <stdio.h>#include <stdlib.h>#include <string.h>#include "c1667.h" /* Header file with global prototypes */#ifdef TIME_H_EXISTS   #include <time.h>#endif ErrorInfoType eiErrorInfo;/*******************************************************************************Function:     ReturnType Flash( CommandType cmdCommand, ParameterType *fp )Arguments:    cmdCommand is an enum which contains all the available function   commands of the SW driver.              fp is a (union) parameter struct for all flash command parameters.Return Value: The function returns the following conditions:    Flash_AddressInvalid,    Flash_BlockEraseFailed,    Flash_BlockNrInvalid,    Flash_ChipEraseFailed,    Flash_FunctionNotSupported,    Flash_NoInformationAvailable,   Flash_NoOperationToSuspend,   Flash_OperationOngoing,    Flash_OperationTimeOut,    Flash_ProgramFailed,    Flash_ResponseUnclear,    Flash_SpecificError,   Flash_Success,    Flash_VppInvalid,    Flash_WrongTypeDescription:  This function is used to access all functions provided with the   current flash chip.Pseudo Code:   Step 1: Select the right action using the cmdCommand parameter   Step 2: Execute the Flash Function   Step 3: Return the Error Code*******************************************************************************/ReturnType Flash( CommandType cmdCommand, ParameterType *fp ) {    ReturnType  rRetVal;   uCPUBusType  ucDeviceId, ucManufacturerCode;    switch (cmdCommand) {      case BlockErase:           rRetVal = FlashBlockErase( (*fp).BlockErase.ublBlockNr );           break;      case CheckCompatibility:           rRetVal = FlashCheckCompatibility();           break;                 case ChipErase:           rRetVal = FlashChipErase( (*fp).ChipErase.rpResults );           break;      case Program:           rRetVal = FlashProgram( (*fp).Program.udMode,                                   (*fp).Program.udAddrOff,                                   (*fp).Program.udNrOfElementsInArray,                                   (*fp).Program.pArray );                                             break;	        case Read:           (*fp).Read.ucValue = FlashRead( (*fp).Read.udAddrOff );           rRetVal = Flash_Success;           break;            case ReadDeviceId:           rRetVal = FlashReadDeviceId(&ucDeviceId);           (*fp).ReadDeviceId.ucDeviceId = ucDeviceId;           break;      case ReadManufacturerCode:           rRetVal = FlashReadManufacturerCode(&ucManufacturerCode);           (*fp).ReadManufacturerCode.ucManufacturerCode = ucManufacturerCode;           break;            case Reset:           FlashReset();           rRetVal = Flash_Success;           break;                 case SingleProgram:           rRetVal = FlashSingleProgram((*fp).SingleProgram.udAddrOff, (*fp).SingleProgram.ucValue);           break;                 case Write:           FlashWrite( (*fp).Write.udAddrOff, (*fp).Write.ucValue );            rRetVal = Flash_Success;           break;      default:           rRetVal = Flash_FunctionNotSupported;           break;   } /* EndSwitch */   return rRetVal;} /* EndFunction Flash */   /*******************************************************************************Function:     ReturnType FlashBlockErase( uBlockType ublBlockNr )Arguments:    ublBlockNr is the number of the Block to be erased.Return Value: The function returns the following conditions:   Flash_Success                Flash_BlockEraseFailed    Flash_BlockNrInvalid   Flash_VppInvalid Description:  This function can be used to erase the Block specified in ublBlockNr.   The function checks that the block nr is within the valid range before issuing   the erase command. Once the erase has completed the function checks the Status   Register for errors. Any errors are returned, otherwise Flash_Success   is returned.Pseudo Code:   Step 1:  Check that the block number exists   Step 2:  Issue Block Erase Command   Step 3:  Wait until Program/Erase Controller has completed    Step 4:  Return error condition*******************************************************************************/ReturnType FlashBlockErase( uBlockType ublBlockNr) {   ReturnType rRetVal = Flash_Success; /* Holds return value */   uCPUBusType ucStatus; /* Holds the Status Register reads */      /* Step 1: Check that the block number exists */   if ( ublBlockNr >= NUM_BLOCKS ) {      return Flash_BlockNrInvalid;   } /* EndIf */   /* Step 2: Issue Block Erase Command */   BASE_ADDR[0x555] = (uCPUBusType)CMD(0xAA); /* 1st cycle */   BASE_ADDR[0x2AA] = (uCPUBusType)CMD(0x55); /* 2nd cycle */   BASE_ADDR[0x555] = (uCPUBusType)CMD(0x80); /* 3rd cycle */   BASE_ADDR[0x555] = (uCPUBusType)CMD(0xAA); /* 4th cycle */   BASE_ADDR[0x2AA] = (uCPUBusType)CMD(0x55); /* 5th cycle */   BASE_ADDR[BlockOffset[ublBlockNr]] = (uCPUBusType)CMD(0x30); /* 6th cycle */   /* Step 3: Wait until Program/Erase Controller has completed */   if ((rRetVal = FlashDataToggle()) != Flash_Success) {      ucStatus = FlashRead(ANY_ADDR);      FlashReset();      if( ucStatus & CMD(0x10) )         rRetVal = Flash_VppInvalid;      else if( ucStatus & CMD(0x20) )         rRetVal = Flash_BlockEraseFailed;   } /* EndIf */               /* Step 4: Return error condition */   return rRetVal; } /* EndFunction FlashBlockErase */   /******************************************************************************* Function:      ReturnType FlashCheckCompatibility( void ) Arguments:     NoneReturn Values: The function returns the following conditions:     Flash_Success   Flash_WrongType       See the Read Electronic Signature command in the Data Sheet for further    information.  Pseudo Code:    Step 1:  Read the Device Id    Step 2:  Read the Manufacturer Code   Step 3:  Check the results*******************************************************************************/ ReturnType FlashCheckCompatibility( void ) {    ReturnType  rRetVal, rCheck1, rCheck2; /* Holds the results of the Read operations */    uCPUBusType ucDeviceId, ucManufacturerCode; /* Holds the values read */    rRetVal =  Flash_WrongType;   /* Step 1:  Read the Device Id */   rCheck1 =  FlashReadDeviceId( &ucDeviceId );    /* Step 2:  Read the ManufactureCode */   rCheck2 =  FlashReadManufacturerCode( &ucManufacturerCode );    /* Step 3:  Check the results */   if (    (rCheck1 == Flash_Success)          && (rCheck2 == Flash_Success)           && (ucDeviceId == EXPECTED_DEVICE)          && (ucManufacturerCode == MANUFACTURER_ST)  )        rRetVal = Flash_Success;    return rRetVal; } /* EndFunction FlashCheckCompatibility */    /*******************************************************************************Function:     ReturnType FlashChipErase( ReturnType *rpResults )Arguments:    rpResults is a pointer to an array where the results will be    stored. If rpResults == NULL then no results have been stored.   If the ChipErase function return Flash_success, the same will be   written in all rpResults array; otherwise rpResults will be filled   With Flash_BlockEraseFailed.Return Value: The function returns the following conditions:   Flash_Success   Flash_ChipEraseFailedDescription: The function can be used to erase the whole flash chip,   with a single command.  Pseudo Code:   Step 1: Issue Chip Erase Command   Step 2: Wait until Program/Erase Controller has completed    Step 3: Fill rpResults   Step 4: Return error condition*******************************************************************************/ReturnType FlashChipErase( ReturnType *rpResults ) {   ReturnType rRetVal = Flash_Success; /* Holds return value */   ReturnType rBlockRetVal; /* Hold return value for each block */   uCPUBusType ucStatus; /* Holds the Status Register reads */   uword uwCounter;                /* Step 1: Issue Chip Erase Command */   BASE_ADDR[0x555] = (uCPUBusType)CMD(0xAA); /* 1st cycle */   BASE_ADDR[0x2AA] = (uCPUBusType)CMD(0x55); /* 2nd cycle */   BASE_ADDR[0x555] = (uCPUBusType)CMD(0x80); /* 3rd cycle */   BASE_ADDR[0x555] = (uCPUBusType)CMD(0xAA); /* 4th cycle */   BASE_ADDR[0x2AA] = (uCPUBusType)CMD(0x55); /* 5th cycle */   BASE_ADDR[0x555] = (uCPUBusType)CMD(0x10); /* 6th cycle */   /* Step 2: Wait until Program/Erase Controller has completed */   if ((rRetVal = FlashDataToggle()) != Flash_Success) {      ucStatus = FlashRead(ANY_ADDR);      FlashReset();            if( ucStatus & CMD(0x10) )         rRetVal = Flash_VppInvalid;      else if( ucStatus & CMD(0x20) )         rRetVal = Flash_BlockEraseFailed;   } /* EndIf */   /* Step 3: Fill rpResults */   if (rpResults!=NULL) {      if ( rRetVal == Flash_Success)          rBlockRetVal = Flash_Success;      else         rBlockRetVal = Flash_BlockEraseFailed;      for (uwCounter =0; uwCounter<NUM_BLOCKS; ++uwCounter)         rpResults[uwCounter]= rBlockRetVal;   } /* EndIf */               /* Step 4: Return error condition */   return rRetVal; } /* EndFunction FlashChipErase */   /*******************************************************************************Function:     ReturnType FlashDataToggle( void )Arguments:    noneReturn Value: The function returns Flash_Success if the Program/Erase Controller   is successful or Flash_SpecificError if there is a problem. In this case the   field eiErrorInfo.sprRetVal will be filled with FlashSpec_ToggleFailed value.   If the Program/Erase Controller do not finish before time-out expired the   function return Flash_OperationTimeout.Description:  The function is used to monitor the Program/Erase Controller during   erase or program operations. It returns when the Program/Erase Controller has   completed. In the Data Sheets, the Data Toggle Flow Chart shows the operation   of the function.Pseudo Code:   Step 1: Read Status Register   Step 2: Read Status Register again   Step 3: If DQ6 did not toggle between the two reads then return Flash_Success   Step 4: DQ6 Toggling: If DQ5 is zero then operation is not yet complete   Step 5: DQ5 is zero, check DQ6 again

⌨️ 快捷键说明

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