flash.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:42k
- /****************************************************************************
- File Name : tt_flash.c
- Description : Testtool FLASH Commands
- Copyright (C) 2004, ST Microelectronics
- ****************************************************************************/
- /* Includes --------------------------------------------------------------- */
- /* Standard Includes */
- #include <stdio.h>
- #include <stddef.h>
- #include <stdlib.h>
- #include <string.h>
- #include <debug.h>
- #include "stack.h"
- #include "stlite.h"
- #include "stddefs.h"
- #include "stdevice.h"
- /* STAPI Includes */
- #include "stflash.h"
- #include "sttbx.h"
- #include "flash.h"
- //#include "tt_flash.h"
- /* Private Types ------------------------------------------------------ */
- typedef struct {
- U8 Bank; /* Bank number */
- U32 Address; /* Block start address */
- U32 Length; /* Length of block */
- } BlockInfo_t;
- typedef struct {
- U8 Block; /* Block number */
- U32 Address; /* flash address */
- U32 Offset; /* offset into readbuffer */
- U32 Size; /* number of bytes */
- } SectionInfo_t;
- /* Private Constants -------------------------------------------------- */
- /* Definitions for Flash */
- #define STFLASH_BANK_0_BASE FLASH_BANK_0_BASE_ADDRESS
- #define STFLASH_VPP_0_ENABLE 0x45200000
- /* if BANK_1 defined it is available */
- #if defined(FLASH_BANK_1_BASE_ADDRESS)
- #define STFLASH_BANK_1_BASE FLASH_BANK_1_BASE_ADDRESS
- #define NUM_BANKS 2
- #else
- #define NUM_BANKS 1
- #endif
- /* if VPP not defined it is not needed */
- #if !defined(STFLASH_VPP_0_ENABLE)
- #define STFLASH_VPP_0_ENABLE (U32)NULL
- #endif
- #if !defined(STFLASH_VPP_1_ENABLE)
- #define STFLASH_VPP_1_ENABLE (U32)NULL
- #endif
- #define DEVICE_TYPE STFLASH_M29W320DT /*8MB*/
- #define MIN_ACCESS_WIDTH STFLASH_ACCESS_16_BITS
- #define MAX_ACCESS_WIDTH STFLASH_ACCESS_16_BITS
- #define NUM_BLOCKS 135
- #define STFLASH_MAIN_0_SIZE STFLASH_MAIN_SIZE
- #define STFLASH_MAIN_1_SIZE STFLASH_MAIN_SIZE
- #define STFLASH_MAIN00_SIZE 0x00010000 /* 64Kb */
- #define STFLASH_MAIN01_SIZE 0x00008000 /* 32Kb */
- #define STFLASH_PARA_0_SIZE 0x00002000 /* 8Kb */
- #define STFLASH_BOOT_0_SIZE 0x00004000 /* 16Kb */
- #define NUM_SECTIONS NUM_BLOCKS//80
- #define TOTAL_BLOCKS (NUM_BANKS * NUM_BLOCKS)
- #define CR 13
- #define LF 10
- STFLASH_Block_t BlockData_s[NUM_BLOCKS] =
- {
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 1 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 2 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 3 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 4 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 5 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 6 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 7 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 8 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 9 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 10 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 11 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 12 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 13 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 14 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 15 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 16 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 17 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 18 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 19 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 20 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 21 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 22 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 23 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 24 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 25 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 26 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 27 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 28 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 29 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 30 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 1 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 2 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 3 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 4 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 5 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 6 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 7 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 8 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 9 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 10 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 11 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 12 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 13 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 14 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 15 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 16 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 17 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 18 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 19 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 20 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 21 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 22 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 23 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 24 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 25 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 26 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 27 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 28 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 29 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 60 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 1 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 2 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 3 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 4 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 5 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 6 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 7 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 8 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 9 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 10 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 11 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 12 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 13 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 14 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 15 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 16 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 17 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 18 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 19 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 20 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 21 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 22 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 23 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 24 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 25 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 26 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 27 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 28 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 29 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 90 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 1 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 2 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 3 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 4 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 5 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 6 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 7 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 8 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 9 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 10 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 11 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 12 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 13 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 14 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 15 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 16 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 17 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 18 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 19 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 20 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 21 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 22 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 23 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 24 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 25 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 26 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 27 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 28 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 29 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 120 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 1 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 2 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 3 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 4 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 5 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 6 */
- {STFLASH_MAIN00_SIZE , STFLASH_MAIN_BLOCK }, /* 7 */
- {STFLASH_PARA_0_SIZE , STFLASH_PARAMETER_BLOCK }, /* 8 */
- {STFLASH_PARA_0_SIZE , STFLASH_PARAMETER_BLOCK }, /* 9 */
- {STFLASH_PARA_0_SIZE , STFLASH_PARAMETER_BLOCK }, /* 10 */
- {STFLASH_PARA_0_SIZE , STFLASH_PARAMETER_BLOCK }, /* 11 */
- {STFLASH_PARA_0_SIZE , STFLASH_PARAMETER_BLOCK }, /* 12 */
- {STFLASH_PARA_0_SIZE , STFLASH_PARAMETER_BLOCK }, /* 13 */
- {STFLASH_PARA_0_SIZE , STFLASH_PARAMETER_BLOCK }, /* 14 */
- {STFLASH_PARA_0_SIZE , STFLASH_PARAMETER_BLOCK } /* 135 */
- };
-
- static U32 FlashSize = 0; /* Non-zero = file converted */
- static char *FlashData_p = NULL;
- static SectionInfo_t SectionInfo[NUM_SECTIONS];
- static BlockInfo_t BlockInfo[TOTAL_BLOCKS + 1];
- static U32 BaseAddress[NUM_BANKS];
- static STFLASH_Block_t GetBlkDat_s[NUM_BLOCKS];
- /* Private Macros ----------------------------------------------------- */
- /* Global Variables ------------------------------------------------------- */
- STFLASH_Handle_t FLASHHndl[2];
- ST_DeviceName_t FLASHDeviceName[2] = {"Bank0", "Bank1"};
- extern ST_Partition_t *SystemPartition;
- /*-------------------------------------------------------------------------
- * Function : FLASH_Close
- * close FLASH
- * Input : None
- * Output :
- * Return : TRUE if error, FALSE if success
- * ----------------------------------------------------------------------*/
- ST_ErrorCode_t FLASH_Close( U8 BankNum )
- {
- ST_ErrorCode_t ErrCode;
- ErrCode = STFLASH_Close(FLASHHndl[BankNum]);
- return ( ErrCode );
- } /* end FLASH_Close */
- /*-------------------------------------------------------------------------
- * Function : FLASH_GetParams
- * Get Flash Parameters
- * Input : None
- * Output :
- * Return : TRUE if error, FALSE if success
- * ----------------------------------------------------------------------*/
- ST_ErrorCode_t FLASH_GetParams( U8 BankNum, STFLASH_Params_t *FLASHParams_p )
- {
- ST_ErrorCode_t ErrCode;
- /* assign structure for returning data */
- FLASHParams_p->InitParams.Blocks = GetBlkDat_s;
- ErrCode = STFLASH_GetParams(FLASHHndl[BankNum], FLASHParams_p );
- return ( ErrCode );
- } /* end FLASH_Close */
- /*-------------------------------------------------------------------------
- * Function : FLASH_Init
- * Initialise FLASH
- * Input : None
- * Output :
- * Return : TRUE if error, FALSE if success
- * ----------------------------------------------------------------------*/
- ST_ErrorCode_t FLASH_Init( U8 BankNum )
- {
- ST_ErrorCode_t ErrCode;
- STFLASH_InitParams_t InitParams;
- InitParams.DeviceType = DEVICE_TYPE;
- #if defined (SELECT_DEVICE_STB5118)
- if ( BankNum == BANK0 )
- {
- InitParams.BaseAddress = (U32*)STFLASH_BANK_0_BASE;
- InitParams.VppAddress = (U32*)NULL;
- }
- else
- {
- #if defined(STFLASH_BANK_1_BASE)
- InitParams.BaseAddress = (U32*)STFLASH_BANK_1_BASE;
- InitParams.VppAddress = (U32*)NULL;
- #else
- return ST_ERROR_FEATURE_NOT_SUPPORTED;
- #endif
- }
- #else
- if ( BankNum == BANK0 )
- {
- InitParams.BaseAddress = (U32*)STFLASH_BANK_0_BASE;
- InitParams.VppAddress = (U32*)STFLASH_VPP_0_ENABLE;
- }
- else
- {
- #if defined(STFLASH_BANK_1_BASE)
- InitParams.BaseAddress = (U32*)STFLASH_BANK_1_BASE;
- InitParams.VppAddress = (U32*)STFLASH_VPP_1_ENABLE;
- #else
- return ST_ERROR_FEATURE_NOT_SUPPORTED;
- #endif
- }
- #endif
- InitParams.MinAccessWidth = MIN_ACCESS_WIDTH;
- InitParams.MaxAccessWidth = MAX_ACCESS_WIDTH;
- InitParams.NumberOfBlocks = NUM_BLOCKS;
- InitParams.Blocks = BlockData_s;
- InitParams.DriverPartition = SystemPartition;
- //InitParams.IsSerialFlash = FALSE;
- ErrCode = STFLASH_Init(FLASHDeviceName[BankNum], &InitParams );
- return ( ErrCode );
- } /* end FLASH_Init */
- /*-------------------------------------------------------------------------
- * Function : FLASH_Open
- * Open FLASH
- * Input : None
- * Output :
- * Return : TRUE if error, FALSE if success
- * ----------------------------------------------------------------------*/
- ST_ErrorCode_t FLASH_Open( U8 BankNum )
- {
- ST_ErrorCode_t ErrCode;
- STFLASH_OpenParams_t OpenParams;
- memset((char*)&OpenParams,0,sizeof(STFLASH_OpenParams_t));
- ErrCode = STFLASH_Open(FLASHDeviceName[BankNum], &OpenParams, &FLASHHndl[BankNum]);
- return ( ErrCode );
- } /* end FLASH_Open */
- /*-------------------------------------------------------------------------
- * Function : FLASH_Term
- * Terminate FLASH
- * Input : ForceTerminate
- * Output :
- * Return : TRUE if error, FALSE if success
- * ----------------------------------------------------------------------*/
- ST_ErrorCode_t FLASH_Term( BOOL ForceTerminate, U8 BankNum )
- {
- ST_ErrorCode_t ErrCode;
- STFLASH_TermParams_t TermParams;
- TermParams.ForceTerminate = ForceTerminate;
- ErrCode = STFLASH_Term(FLASHDeviceName[BankNum], &TermParams);
- return ( ErrCode );
- } /* end FLASH_Term */
- /*-------------------------------------------------------------------------
- * Function : FLASH_SetupBlockInfo
- * Setup Block Info structure
- * Input :
- * Output :
- * Return : Error Code
- * ----------------------------------------------------------------------*/
- ST_ErrorCode_t FLASH_SetupBlockInfo( void )
- {
- ST_ErrorCode_t ErrCode = ST_NO_ERROR;
- STFLASH_Params_t FLASHParams;
- U8 Bank=0, Block=0;
- U32 BlockStart = 0;
- static U8 Section = 0;
- memset((char*)&FLASHParams,0,sizeof(STFLASH_Params_t));
- if ( Section == 0 )
- {
- /*
- ** Set the address for each section
- */
- for ( Bank = 0; ( Bank < NUM_BANKS ) && ( ErrCode == ST_NO_ERROR ); Bank ++ )
- {
- ErrCode = FLASH_Init( Bank ); /* Init Bank */
- if ( ErrCode == ST_NO_ERROR )
- {
- ErrCode = FLASH_Open( Bank );
- if ( ErrCode == ST_NO_ERROR )
- ErrCode = FLASH_GetParams( Bank, &FLASHParams );
- if ( ErrCode == ST_NO_ERROR )
- {
- BlockStart = BaseAddress[Bank] = (U32) FLASHParams.InitParams.BaseAddress;
- for ( Block = 0; Block < FLASHParams.InitParams.NumberOfBlocks; Block ++)
- {
- BlockInfo[Section].Bank = Bank;
- BlockInfo[Section].Address = BlockStart;
- BlockInfo[Section].Length = FLASHParams.InitParams.Blocks[Block].Length;
- #ifdef DEBUG_OUTPUT
- printf("Block %2d (%d) 0x%08x 0x%08x (%6d) bytesn",
- Block, Section,
- BlockInfo[Section].Address,
- BlockInfo[Section].Length,
- BlockInfo[Section].Length );
- #endif
- Section ++;
- BlockStart += FLASHParams.InitParams.Blocks[Block].Length;
- }
- }
- ErrCode = FLASH_Term( TRUE, Bank );
- }
- }
- BlockInfo[Section].Address = BlockStart;
- }
- if ( ErrCode == ST_NO_ERROR )
- printf("Flash setup Ok (%d blocks)n", Section );
- return( ErrCode );
- } /* end of FLASH_SetupBlockInfo */
- /*-------------------------------------------------------------------------
- * Function : FLASH_Setup
- * Setup Block Info structure
- * Input :
- * Output :
- * Return : Error Code
- * ----------------------------------------------------------------------*/
- ST_ErrorCode_t FLASH_Setup( void )
- {
- ST_ErrorCode_t ErrCode = ST_NO_ERROR;
- U8 Bank;
- /* ** Set the address for each section*/
- for ( Bank = 0; ( Bank < NUM_BANKS ) && ( ErrCode == ST_NO_ERROR ); Bank ++ )
- {
- ErrCode = FLASH_Init( Bank ); /* Init Bank */
- if ( ErrCode == ST_NO_ERROR )
- {
- ErrCode = FLASH_Open( Bank );
- }
- if ( ErrCode == ST_NO_ERROR )
- printf("Flash setup Ok.....n" );
- }
- return( ErrCode );
- } /* end of Flash_Setup */
- /*-------------------------------------------------------------------------
- * Function : FreeFileDataBuffer
- * as is
- * Input :
- * Output :
- * Return : None
- * ----------------------------------------------------------------------*/
- static void FreeFileDataBuffer(void)
- {
- /* deallocate File data buffer */
- if ( FlashData_p != NULL )
- memory_deallocate( SystemPartition, FlashData_p );
- } /* end of FreeFileDataBuffer() */
- /*-------------------------------------------------------------------------
- * Function : GetBlockNumber
- * Get block number from address
- * Input :
- * Output :
- * Return : Block number of address
- * ----------------------------------------------------------------------*/
- static U8 GetBlockNumber( U32 Address )
- {
- U8 Block;
- /* find the block this address is in */
- for ( Block = 0; Block < TOTAL_BLOCKS; Block++ )
- {
- if (( Address >= BlockInfo[Block].Address ) &&
- ( Address < (BlockInfo[Block].Address+BlockInfo[Block].Length)))
- {
- break;
- }
- }
- return Block;
- } /* end GetBlockNumber */
- /*-------------------------------------------------------------------------
- * Function : ConvertMemory
- * Convert Raw Hex file into memory buffer suitable to write to
- * flash copying created data over data read from filem, the
- * created data will be smaller as each byte takes three bytes
- * in the Hex file.
- * Input :
- * Output :
- * Return : size of data in buffer
- * ----------------------------------------------------------------------*/
- static U32 ConvertMemory( long unsigned int MemSize )
- {
- char *OldPtr, *NewPtr;
- int Section;
- U32 InOffset;
- U32 OutOffset;
- U32 BlockOffset;
- U32 Value=0;
- U8 Count=0 , Block=0;
- for ( Section = 0; Section < NUM_SECTIONS; Section ++ )
- {
- SectionInfo[Section].Block = 0xff;
- SectionInfo[Section].Address = 0;
- SectionInfo[Section].Offset = 0;
- SectionInfo[Section].Size = 0;
- }
- /* Convert File */
- Section = 0;
- OldPtr = FlashData_p;
- NewPtr = (char*) NULL;
- for ( InOffset = 0, OutOffset = 0; InOffset < MemSize; )
- {
- /* Line format =
- ** Address Data (both in hex)
- ** 12345678 11 22 33 44 55 etc. CR/LF
- */
- Value = (U32) strtol( OldPtr, &NewPtr, 16 );
- if ( NewPtr != OldPtr ) /* if conversion possible */
- {
- if ( Value > 255 ) /* which means it is an address */
- {
- if (( Block = GetBlockNumber( Value )) == TOTAL_BLOCKS )/*changed temp*/
- {
- printf("Invalid address 0x%xn", Value );
- break;
- }
- else
- {
- /*
- ** check if the address is non-contiguous
- ** or if it is a new block
- */
- if ((( SectionInfo[Section].Address + SectionInfo[Section].Size ) != Value ) ||
- ( SectionInfo[Section].Block != Block ))
- {
- Section ++;
- }
- /* Start New Block */
- if ( SectionInfo[Section].Block == 0xff )
- {
- SectionInfo[Section].Address = Value;
- SectionInfo[Section].Block = Block;
- SectionInfo[Section].Offset = OutOffset;
- SectionInfo[Section].Size = 0;
- }
- }
- }
- else
- {
- BlockOffset = OutOffset;
- for ( Count = 0; Count < Section; Count++ )
- BlockOffset -= SectionInfo[Count].Size;
- if (( BlockOffset+1 ) > BlockInfo[SectionInfo[Section].Block].Length )
- {
- Section++;
- }
- /* Start New Block */
- if ( SectionInfo[Section].Block == 0xff )
- {
- SectionInfo[Section].Address = BlockInfo[Block].Address + BlockOffset;
- SectionInfo[Section].Block = GetBlockNumber(SectionInfo[Section].Address);
- SectionInfo[Section].Offset = OutOffset;
- SectionInfo[Section].Size = 0;
- }
- SectionInfo[Section].Size ++;
- FlashData_p[OutOffset++] = (char) Value;
- }
- InOffset += (U32)( NewPtr - OldPtr );
- OldPtr = NewPtr;
- if ( Section > NUM_SECTIONS )
- {
- printf("Program Limitationn");
- exit(0);
- }
- }
- else
- break;
- } /* end of for loop */
- printf("n");
- /* display the first few bytes of each section */
- for ( Section = 0; Section < NUM_SECTIONS; Section ++ )
- {
- if ( SectionInfo[Section].Block != 0xFF )
- {
- printf("Section %2d, Block %3d, Addr 0x%08x, Offset 0x%08x, Size %6dn",
- Section,
- SectionInfo[Section].Block,
- SectionInfo[Section].Address,
- SectionInfo[Section].Offset,
- SectionInfo[Section].Size );
- #ifdef DEBUG_INFO
- for ( InOffset = 0;
- ( InOffset < 32 ) && ( InOffset < SectionInfo[Section].Size );
- InOffset ++ )
- {
- printf(" %02x", FlashData_p[ SectionInfo[Section].Offset + InOffset ] );
- }
- printf("n");
- #endif
- }
- }
- return ( OutOffset );
- } /* end ConvertMemory */
- /*-------------------------------------------------------------------------
- * Function : ReadFile
- * Read HEX file into memory
- * Input :
- * Output :
- * Return : File size
- * ----------------------------------------------------------------------*/
- U32 FLASH_ReadFile( char *Filename )
- {
- long int HexFile_p=0; /* datafile descriptor */
- long int HexFileSize=0; /* size in bytes of the file */
- /* Open and Read file into memory */
- HexFile_p = debugopen(Filename, "rb");
- printf("HexFile_p = 0x%8xn",HexFile_p);
- if(HexFile_p < 0)
- {
- printf("Error opening file '%s'n", Filename );
- return (0);
- }
- else
- {
- HexFileSize = debugfilesize(HexFile_p);
- printf("HexFileSize = 0x%8xn",HexFileSize);
- /* allocate File data buffer */
- FreeFileDataBuffer();
- FlashData_p = (char*) memory_allocate( SystemPartition, (U32) HexFileSize );
- if ( FlashData_p != NULL )
- {
- printf("Loading '%s' into memory, wait .. ", Filename );
- debugread(HexFile_p, FlashData_p, (size_t) HexFileSize);
- printf("%d bytesn", HexFileSize );
- }
- else
- {
- printf("Not enough memory for HEX file (%d bytes)n", HexFileSize);
- HexFileSize = 0;
- }
- debugclose(HexFile_p);
- }
- if ( HexFileSize > 0 )
- {
- /* convert buffer to binary and resize memory */
- printf("Converting file in memory, wait .. ");
- FlashSize = ConvertMemory( HexFileSize );
- if ( FlashSize > 0 )
- {
- printf("%d bytesn", FlashSize );
- FlashData_p = (char*) memory_reallocate( SystemPartition, FlashData_p, FlashSize );
- }
- else
- printf("Invalid filen");
- }
- return( FlashSize );
- } /* end of FLASH_ReadFile */
- /*-------------------------------------------------------------------------
- * Function : EraseFlash
- * Function to Read Hex file and erase areas required
- * Input : EraseAll
- * Output :
- * Return : EraseFailed = TRUE else FALSE
- * ----------------------------------------------------------------------*/
- BOOL EraseFlash( BOOL EraseAll )
- {
- ST_ErrorCode_t ErrCode = ST_NO_ERROR;
- U8 Section, Bank;
- U32 EraseOffset;
- BOOL EraseFailed = TRUE;
- /* check data size */
- if (( FlashSize != 0 ) || EraseAll )
- {
- EraseFailed = FALSE;
- printf("tt+------------+------------+n");
- if ( EraseAll )
- {
- for ( Section = 0;( Section < TOTAL_BLOCKS ) && ( ErrCode == ST_NO_ERROR ); Section ++)
- {
- Bank = BlockInfo[Section].Bank;
- EraseOffset = BlockInfo[Section].Address - BaseAddress[Bank];
- printf("%d block %2dt| 0x%08x | 0x%08x | ",
- Bank, Section,
- BlockInfo[Section].Address,
- BlockInfo[Section].Address + BlockInfo[Section].Length - 1 );
- ErrCode = STFLASH_Erase( FLASHHndl[Bank],
- EraseOffset,
- BlockInfo[Section].Length );
- if ( ErrCode != ST_NO_ERROR )
- {
- printf("Erase FAILEDn");
- EraseFailed = TRUE;
- }
- else
- printf("ERASEDn");
- }
- }
- else
- {
- /* Find out which blocks are to be Erased */
- for ( Section = 0; ( Section < NUM_SECTIONS ) && ( EraseFailed == FALSE ); Section ++ )
- {
- if ( SectionInfo[Section].Block != 0xFF )
- {
- /*
- ** Erase = erase whole block, valid parameters are block base address & block size
- */
- Bank = BlockInfo[ SectionInfo[Section].Block ].Bank;
- EraseOffset = BlockInfo[ SectionInfo[Section].Block ].Address - BaseAddress[Bank];
- printf("block %2dt| 0x%08x | 0x%08x | ",
- SectionInfo[Section].Block,
- BlockInfo[ SectionInfo[Section].Block ].Address,
- BlockInfo[ SectionInfo[Section].Block ].Address +
- BlockInfo[ SectionInfo[Section].Block ].Length - 1 );
- ErrCode = STFLASH_Erase( FLASHHndl[Bank],
- EraseOffset,
- BlockInfo[ SectionInfo[Section].Block ].Length );
- if ( ErrCode != ST_NO_ERROR )
- {
- printf("ErrCode = 0x%8xn",ErrCode);
- printf("Erase FAILEDn");
- printf("FLASHHndl[Bank] = 0x%8Xn",FLASHHndl[Bank]);
- EraseFailed = TRUE;
- }
- else
- printf("ERASEDn");
- }
- }
- }
- printf("tt+------------+------------+n");
- }
- if ( EraseFailed == TRUE )
- {
- FreeFileDataBuffer();
- printf("!!!! ERASE Failed !!!!n");
- return TRUE;
- }
- return EraseFailed;
- } /* end ProgramFlash */
- /*-------------------------------------------------------------------------
- * Function : ProgramFlash
- * Function to Read Hex file and program flash with contents
- * Input :
- * Output :
- * Return : None
- * ----------------------------------------------------------------------*/
- BOOL ProgramFlash( void )
- {
- ST_ErrorCode_t ErrCode;
- int Section, Bank;
- U32 NumberOfBytes;
- BOOL ProgramFailed;
- FlashSize=0x2000;
- /* check data size */
- if ( FlashSize == 0 )
- ProgramFailed = TRUE;
- else
- {
- ProgramFailed = FALSE;
- printf("tt+------------+------------+n");
- }
- /* Find out which blocks are to be written to */
- /* Write the data to the Flash */
- /*printf("Printing the contents of BlockInfo n"));
- for ( Section = 0; ( Section < NUM_SECTIONS ) ; Section ++ )
- {
- printf("Section=%dt", Section ));
- printf("SectionInfo[Section]=%dn", SectionInfo[Section] ));
- printf("SectionInfo[Section].Block=%dt", SectionInfo[Section].Block ));
- printf("BlockInfo[ SectionInfo[Section].Block ].Bank=%dn",BlockInfo[ SectionInfo[Section].Block ].Bank));
- }*/
- for ( Section = 0; ( Section < NUM_SECTIONS ) && ( ProgramFailed == FALSE ); Section ++ )
- {
- if ( SectionInfo[Section].Block != 0xFF )
- {
- U32 WriteOffset;
- /*
- ** Write = write bytes from Offset
- */
- Bank = BlockInfo[ SectionInfo[Section].Block ].Bank;
- printf("Section=%dt", Section );
- printf("SectionInfo[Section]=%dn", SectionInfo[Section] );
- printf("SectionInfo[Section].Block=%dt", SectionInfo[Section].Block );
- printf("BlockInfo[ SectionInfo[Section].Block ].Bank=%dn",BlockInfo[ SectionInfo[Section].Block ].Bank);
- WriteOffset = SectionInfo[Section].Address - BaseAddress[Bank];
- /*SectionInfo[Section].Address += 0x4; Tara*/
- printf("block %2dt| 0x%08x | 0x%08x | ",
- SectionInfo[Section].Block,
- SectionInfo[Section].Address,
- SectionInfo[Section].Address + SectionInfo[Section].Size - 1 );
- ErrCode = STFLASH_Write( FLASHHndl[Bank],
- WriteOffset,
- (U8*)&FlashData_p[SectionInfo[Section].Offset],
- SectionInfo[Section].Size,
- &NumberOfBytes );
- if ( ErrCode != ST_NO_ERROR )
- {
- printf("Program FAILEDn");
- ProgramFailed = TRUE;
- }
- else
- printf("PROGRAMMEDn");
- }
- }
- if ( ProgramFailed == TRUE )
- {
- FreeFileDataBuffer();
- printf("!!!! PROGRAM Failed !!!!n");
- return TRUE;
- }
- else
- {
- printf("tt+------------+------------+n");
- return FALSE;
- }
- } /* end ProgramFlash */
- /*-------------------------------------------------------------------------
- * Function : VerifyFlash
- * Function to Read Hex file and verify flash with contents
- * Input :
- * Output :
- * Return : None
- * ----------------------------------------------------------------------*/
- BOOL VerifyFlash( void )
- {
- ST_ErrorCode_t ErrCode = ST_NO_ERROR;
- int Section, Bank;
- U32 Length;
- U32 ReadOffset;
- U32 NumberOfBytes;
- U8 *Buffer_p;
- BOOL VerifyFailed;
- /* check data size */
- if ( FlashSize == 0 )
- VerifyFailed = TRUE;
- else
- {
- VerifyFailed = FALSE;
- printf("tt+------------+------------+n");
- }
- /* Find out which blocks are to be verified */
- /* Read the data and compare */
- for ( Section = 0; ( Section < NUM_SECTIONS ) && ( VerifyFailed == FALSE ); Section ++ )
- {
- if ( SectionInfo[Section].Block != 0xFF )
- {
- Bank = BlockInfo[ SectionInfo[Section].Block ].Bank;
- Length = BlockInfo[ SectionInfo[Section].Block ].Length;
- ReadOffset = SectionInfo[Section].Address - BaseAddress[Bank];
- /* allocate temp buffer to put flash data in */
- Buffer_p = memory_allocate( SystemPartition, Length );
- if ( Buffer_p != NULL )
- {
- printf("block %2dt| 0x%08x | 0x%08x | ",
- SectionInfo[Section].Block,
- SectionInfo[Section].Address,
- SectionInfo[Section].Address + SectionInfo[Section].Size - 1 );
- ErrCode = STFLASH_Read( FLASHHndl[Bank],
- ReadOffset,
- Buffer_p,
- SectionInfo[Section].Size,
- &NumberOfBytes );
- if(ErrCode != ST_NO_ERROR)
- {
- }
- if ( memcmp( Buffer_p,(U8*)&FlashData_p[SectionInfo[Section].Offset],
- SectionInfo[Section].Size ) != 0 )
- {
- printf("Verify FAILEDn");
- VerifyFailed = TRUE;
- }
- else
- printf("VERIFIEDn");
- /* deallocate temp buffer */
- memory_deallocate( SystemPartition, Buffer_p );
- }
- }
- }
- FreeFileDataBuffer();
- if ( VerifyFailed == TRUE )
- {
- printf("!!!! VERIFY Failed !!!!n");
- return TRUE;
- }
- else
- {
- printf("tt+------------+------------+n");
- return FALSE;
- }
- } /* end VerifyFlash */
- //---------------------------------------------------------------------------------------
- //
- //上面接口用于烧录应用程序
- //
- //下面接口用于应用程序调用
- //
- //---------------------------------------------------------------------------------------
- ST_ErrorCode_t KB_FLASHSetupBlockInfo( void )
- {
- ST_ErrorCode_t ErrCode = ST_NO_ERROR;
- STFLASH_Params_t FLASHParams;
- U8 Bank=0, Block=0;
- U32 BlockStart = 0;
- static U8 Section = 0;
- memset((char*)&FLASHParams,0,sizeof(STFLASH_Params_t));
- if ( Section == 0 )
- {
- /*
- ** Set the address for each section
- */
- for ( Bank = 0; ( Bank < NUM_BANKS ) && ( ErrCode == ST_NO_ERROR ); Bank ++ )
- {
- ErrCode = FLASH_Init( Bank ); /* Init Bank */
- if ( ErrCode == ST_NO_ERROR )
- {
- ErrCode = FLASH_Open( Bank );
- if ( ErrCode == ST_NO_ERROR )
- ErrCode = FLASH_GetParams( Bank, &FLASHParams );
- if ( ErrCode == ST_NO_ERROR )
- {
- BlockStart = BaseAddress[Bank] = (U32) FLASHParams.InitParams.BaseAddress;
- for ( Block = 0; Block < FLASHParams.InitParams.NumberOfBlocks; Block ++)
- {
- BlockInfo[Section].Bank = Bank;
- BlockInfo[Section].Address = BlockStart;
- BlockInfo[Section].Length = FLASHParams.InitParams.Blocks[Block].Length;
- #ifdef DEBUG_OUTPUT
- printf("Block %2d (%d) 0x%08x 0x%08x (%6d) bytesn",
- Block, Section,
- BlockInfo[Section].Address,
- BlockInfo[Section].Length,
- BlockInfo[Section].Length );
- #endif
- Section ++;
- BlockStart += FLASHParams.InitParams.Blocks[Block].Length;
- }
- }
- ErrCode = FLASH_Term( TRUE, Bank );
- }
- }
- BlockInfo[Section].Address = BlockStart;
- }
- if ( ErrCode == ST_NO_ERROR )
- printf("Flash setup Ok (%d blocks)n", Section );
- return( ErrCode );
- }
- void test_FlashInit(void )
- {
- #if 1
- KB_FLASHSetupBlockInfo();
- FLASH_Setup();
- #else //or OK
- ST_ErrorCode_t ErrCode;
- if ( ErrCode == ST_NO_ERROR )
- ErrCode = FLASH_SetupBlockInfo();
- if ( ErrCode == ST_NO_ERROR )
- {
- ErrCode = FLASH_Init(BANK0);
- if ( ErrCode == ST_NO_ERROR )
- ErrCode = FLASH_Open(BANK0);
- }
- #ifdef STFLASH_BANK_1_BASE
- if ( ErrCode == ST_NO_ERROR )
- {
- ErrCode = FLASH_Init(BANK1);
- if ( ErrCode == ST_NO_ERROR )
- ErrCode = FLASH_Open(BANK1);
- }
- #endif
- #endif
- }
- #if 1
- RetValue KB_FLASHInit(void)
- {
- ST_ErrorCode_t ErrCode;
- KB_FLASHSetupBlockInfo();
- FLASH_Setup();
- return RETOK;
- }
- RetValue KB_FlashErase(UINT32 secID)
- {
- ST_ErrorCode_t ErrCode = ST_NO_ERROR;
- int Bank;
- U32 EraseOffset;
-
- Bank = BlockInfo[secID].Bank;
- EraseOffset = BlockInfo[secID].Address - BaseAddress[Bank];
- //printf("n[Flash]:Erase address[0x%x] len[0x%x].",BlockInfo[secID].Address,BlockInfo[secID].Length);
- ErrCode = STFLASH_Erase( FLASHHndl[Bank],EraseOffset,BlockInfo[secID].Length );
- if ( ErrCode != ST_NO_ERROR )
- {
- printf("n[Flash]:Erase failed[0x%x].",ErrCode);
- return RETFIAL1;
- }
- else
- {
- //printf("n[Flash]:Erase success.");
- return RETOK;
- }
- }
- RetValue KB_FlashWrite(UINT32 secID,UINT32 Offset,UINT8 *Buffer,UINT32 NumberToWrite)
- {
- ST_ErrorCode_t ErrCode = ST_NO_ERROR;
- int Bank;
- U32 numWrited,WriteOffset;
- if ( SectionInfo[secID].Block != 0xFF )
- {
- KB_FlashErase(secID);
-
- Bank = BlockInfo[ SectionInfo[secID].Block ].Bank;
- WriteOffset = BlockInfo[secID].Address - BaseAddress[Bank];
- //printf("n[Flash]:Write address[0x%x] WriteOffset[0x%x] NumberToWrite[0x%x].",BlockInfo[secID].Address,WriteOffset,NumberToWrite);
- //printf("nSectionInfo address[0x%x] base address[0x%x] WriteOffset[0x%x]",SectionInfo[secID].Address,BaseAddress[Bank],WriteOffset);
- ErrCode = STFLASH_Write( FLASHHndl[Bank],WriteOffset,(U8*)Buffer,NumberToWrite,&numWrited );
- if ( ErrCode != ST_NO_ERROR )
- {
- printf("n[Flash]:Write failed[0x%x].",ErrCode);
- return RETFIAL1;
- }
- else
- {
- //printf("n[Flash]:Write success.");
- return RETOK;
- }
- }
- else
- {
- printf("n[Flash]:Write section block error[0x%x].",SectionInfo[secID].Block);
- return RETFIAL1;
- }
- }
- RetValue KB_FlashRead( UINT32 secID,UINT32 Offset,UINT8 *Buffer,UINT32 NumberToRead)
- {
- ST_ErrorCode_t ErrCode = ST_NO_ERROR;
- int Bank;
- U32 numReaded,ReadOffset;
-
- if ( SectionInfo[secID].Block != 0xFF )
- {
- Bank = BlockInfo[ SectionInfo[secID].Block ].Bank;
- ReadOffset = BlockInfo[secID].Address - BaseAddress[Bank];
- //printf("n[Flash]:Read address[0x%x] ReadOffset[0x%x] NumberToRead[0x%x].",BlockInfo[secID].Address,ReadOffset,NumberToRead);
- //printf("nSectionInfo address[0x%x] base address[0x%x] WriteOffset[0x%x]",SectionInfo[secID].Address,BaseAddress[Bank],WriteOffset);
- ErrCode = STFLASH_Read( FLASHHndl[Bank],ReadOffset,(U8*)Buffer,NumberToRead,&numReaded );
- if ( ErrCode != ST_NO_ERROR )
- {
- printf("n[Flash]:Read failed[0x%x].",ErrCode);
- return RETFIAL1;
- }
- else
- {
- //printf("n[Flash]:Read success.");
- return RETOK;
- }
- }
- else
- {
- printf("n[Flash]:Read section block error[0x%x].",SectionInfo[secID].Block);
- return RETFIAL1;
- }
- }
- UINT8 *KB_FlashGetSectorAddr(UINT32 secID)
- {
- UINT8 *addr;
- addr=(UINT8 *)BlockInfo[secID].Address;
- return addr;
- }
- #endif
- /* EOF --------------------------------------------------------------------- */