flash.c
上传用户:fy98168
上传日期:2015-06-26
资源大小:13771k
文件大小:42k
源码类别:

DVD

开发平台:

C/C++

  1. /****************************************************************************
  2. File Name   : tt_flash.c
  3. Description : Testtool FLASH Commands
  4. Copyright (C) 2004, ST Microelectronics
  5. ****************************************************************************/
  6. /* Includes --------------------------------------------------------------- */
  7. /* Standard Includes */
  8. #include <stdio.h>
  9. #include <stddef.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <debug.h>
  13. #include "stack.h"
  14. #include "stlite.h"
  15. #include "stddefs.h"
  16. #include "stdevice.h"
  17. /* STAPI Includes */
  18. #include "stflash.h"
  19. #include "sttbx.h"
  20. #include "flash.h"
  21. //#include "tt_flash.h"
  22. /* Private Types ------------------------------------------------------ */
  23. typedef struct {
  24.     U8  Bank;           /* Bank number */
  25.     U32 Address;        /* Block start address */
  26.     U32 Length;         /* Length of block */
  27. } BlockInfo_t;
  28. typedef struct {
  29.     U8  Block;          /* Block number */
  30.     U32 Address;        /* flash address */
  31.     U32 Offset;         /* offset into readbuffer */
  32.     U32 Size;           /* number of bytes */
  33. } SectionInfo_t;
  34. /* Private Constants -------------------------------------------------- */
  35. /* Definitions for Flash */
  36. #define STFLASH_BANK_0_BASE     FLASH_BANK_0_BASE_ADDRESS
  37. #define STFLASH_VPP_0_ENABLE    0x45200000
  38. /* if BANK_1 defined it is available */
  39. #if defined(FLASH_BANK_1_BASE_ADDRESS)
  40.     #define STFLASH_BANK_1_BASE     FLASH_BANK_1_BASE_ADDRESS
  41.     #define NUM_BANKS               2
  42. #else
  43.     #define NUM_BANKS               1
  44. #endif
  45. /* if VPP not defined it is not needed */
  46. #if !defined(STFLASH_VPP_0_ENABLE)
  47.     #define STFLASH_VPP_0_ENABLE    (U32)NULL
  48. #endif
  49. #if !defined(STFLASH_VPP_1_ENABLE)
  50.     #define STFLASH_VPP_1_ENABLE    (U32)NULL
  51. #endif
  52. #define DEVICE_TYPE              STFLASH_M29W320DT /*8MB*/
  53. #define MIN_ACCESS_WIDTH         STFLASH_ACCESS_16_BITS
  54. #define MAX_ACCESS_WIDTH         STFLASH_ACCESS_16_BITS
  55. #define NUM_BLOCKS               135
  56. #define STFLASH_MAIN_0_SIZE      STFLASH_MAIN_SIZE
  57. #define STFLASH_MAIN_1_SIZE      STFLASH_MAIN_SIZE
  58. #define STFLASH_MAIN00_SIZE      0x00010000  /* 64Kb */
  59. #define STFLASH_MAIN01_SIZE    0x00008000  /* 32Kb */
  60. #define STFLASH_PARA_0_SIZE     0x00002000  /* 8Kb */
  61. #define STFLASH_BOOT_0_SIZE      0x00004000  /* 16Kb */
  62. #define NUM_SECTIONS             NUM_BLOCKS//80
  63. #define TOTAL_BLOCKS             (NUM_BANKS * NUM_BLOCKS)
  64. #define CR                      13
  65. #define LF                      10
  66. STFLASH_Block_t      BlockData_s[NUM_BLOCKS] =
  67.  {
  68.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 1 */
  69.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 2 */
  70.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 3 */
  71.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 4 */
  72.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 5 */
  73.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 6 */
  74.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 7 */
  75.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 8 */
  76.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 9 */
  77.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 10 */
  78.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 11 */
  79.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 12 */
  80.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 13 */
  81.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 14 */
  82.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 15 */
  83.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 16 */
  84.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 17 */
  85.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 18 */
  86.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 19 */
  87.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 20 */
  88.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 21 */
  89.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 22 */
  90.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 23 */
  91.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 24 */
  92.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 25 */
  93.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 26 */
  94.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 27 */
  95.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 28 */
  96.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 29 */
  97.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 30 */
  98.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 1 */
  99.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 2 */
  100.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 3 */
  101.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 4 */
  102.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 5 */
  103.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 6 */
  104.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 7 */
  105.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 8 */
  106.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 9 */
  107.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 10 */
  108.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 11 */
  109.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 12 */
  110.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 13 */
  111.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 14 */
  112.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 15 */
  113.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 16 */
  114.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 17 */
  115.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 18 */
  116.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 19 */
  117.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 20 */
  118.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 21 */
  119.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 22 */
  120.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 23 */
  121.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 24 */
  122.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 25 */
  123.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 26 */
  124.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 27 */
  125.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 28 */
  126.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 29 */
  127.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 60 */
  128.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 1 */
  129.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 2 */
  130.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 3 */
  131.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 4 */
  132.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 5 */
  133.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 6 */
  134.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 7 */
  135.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 8 */
  136.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 9 */
  137.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 10 */
  138.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 11 */
  139.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 12 */
  140.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 13 */
  141.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 14 */
  142.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 15 */
  143.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 16 */
  144.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 17 */
  145.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 18 */
  146.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 19 */
  147.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 20 */
  148.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 21 */
  149.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 22 */
  150.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 23 */
  151.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 24 */
  152.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 25 */
  153.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 26 */
  154.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 27 */
  155.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 28 */
  156.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 29 */
  157.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 90 */
  158.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 1 */
  159.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 2 */
  160.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 3 */
  161.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 4 */
  162.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 5 */
  163.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 6 */
  164.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 7 */
  165.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 8 */
  166.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 9 */
  167.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 10 */
  168.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 11 */
  169.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 12 */
  170.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 13 */
  171.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 14 */
  172.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 15 */
  173.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 16 */
  174.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 17 */
  175.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 18 */
  176.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 19 */
  177.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 20 */
  178.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 21 */
  179.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 22 */
  180.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 23 */
  181.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 24 */
  182.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 25 */
  183.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 26 */
  184.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 27 */
  185.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 28 */
  186.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 29 */
  187.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 120 */
  188.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 1 */
  189.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 2 */
  190.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 3 */
  191.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 4 */
  192.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 5 */
  193.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 6 */
  194.   {STFLASH_MAIN00_SIZE  , STFLASH_MAIN_BLOCK }, /* 7 */
  195.   {STFLASH_PARA_0_SIZE  , STFLASH_PARAMETER_BLOCK }, /* 8 */
  196.   {STFLASH_PARA_0_SIZE  , STFLASH_PARAMETER_BLOCK }, /* 9 */
  197.   {STFLASH_PARA_0_SIZE  , STFLASH_PARAMETER_BLOCK }, /* 10 */
  198.   {STFLASH_PARA_0_SIZE  , STFLASH_PARAMETER_BLOCK }, /* 11 */
  199.   {STFLASH_PARA_0_SIZE  , STFLASH_PARAMETER_BLOCK }, /* 12 */
  200.   {STFLASH_PARA_0_SIZE  , STFLASH_PARAMETER_BLOCK }, /* 13 */
  201.   {STFLASH_PARA_0_SIZE  , STFLASH_PARAMETER_BLOCK }, /* 14 */
  202.   {STFLASH_PARA_0_SIZE  , STFLASH_PARAMETER_BLOCK }  /* 135 */
  203.  };
  204.  
  205. static U32              FlashSize = 0;          /* Non-zero = file converted */
  206. static char             *FlashData_p = NULL;
  207. static SectionInfo_t    SectionInfo[NUM_SECTIONS];
  208. static BlockInfo_t      BlockInfo[TOTAL_BLOCKS + 1];
  209. static U32              BaseAddress[NUM_BANKS];
  210. static STFLASH_Block_t  GetBlkDat_s[NUM_BLOCKS];
  211. /* Private Macros ----------------------------------------------------- */
  212. /* Global Variables ------------------------------------------------------- */
  213. STFLASH_Handle_t        FLASHHndl[2];
  214. ST_DeviceName_t         FLASHDeviceName[2] = {"Bank0", "Bank1"};
  215. extern ST_Partition_t  *SystemPartition;
  216. /*-------------------------------------------------------------------------
  217.  * Function : FLASH_Close
  218.  *            close FLASH
  219.  * Input    : None
  220.  * Output   :
  221.  * Return   : TRUE if error, FALSE if success
  222.  * ----------------------------------------------------------------------*/
  223. ST_ErrorCode_t FLASH_Close( U8 BankNum )
  224. {
  225.     ST_ErrorCode_t          ErrCode;
  226.     ErrCode = STFLASH_Close(FLASHHndl[BankNum]);
  227.     return ( ErrCode );
  228. } /* end FLASH_Close */
  229. /*-------------------------------------------------------------------------
  230.  * Function : FLASH_GetParams
  231.  *            Get Flash Parameters
  232.  * Input    : None
  233.  * Output   :
  234.  * Return   : TRUE if error, FALSE if success
  235.  * ----------------------------------------------------------------------*/
  236. ST_ErrorCode_t FLASH_GetParams( U8 BankNum, STFLASH_Params_t *FLASHParams_p )
  237. {
  238.     ST_ErrorCode_t      ErrCode;
  239.     /* assign structure for returning data */
  240.     FLASHParams_p->InitParams.Blocks = GetBlkDat_s;
  241.     ErrCode = STFLASH_GetParams(FLASHHndl[BankNum], FLASHParams_p );
  242.     return ( ErrCode );
  243. } /* end FLASH_Close */
  244. /*-------------------------------------------------------------------------
  245.  * Function : FLASH_Init
  246.  *            Initialise FLASH
  247.  * Input    : None
  248.  * Output   :
  249.  * Return   : TRUE if error, FALSE if success
  250.  * ----------------------------------------------------------------------*/
  251. ST_ErrorCode_t FLASH_Init( U8 BankNum )
  252. {
  253.     ST_ErrorCode_t          ErrCode;
  254.     STFLASH_InitParams_t    InitParams;
  255.     InitParams.DeviceType = DEVICE_TYPE;
  256. #if defined (SELECT_DEVICE_STB5118)
  257.     if ( BankNum == BANK0 )
  258.     {
  259.         InitParams.BaseAddress = (U32*)STFLASH_BANK_0_BASE;
  260.         InitParams.VppAddress  = (U32*)NULL;
  261.     }
  262.     else
  263.     {
  264.         #if defined(STFLASH_BANK_1_BASE)
  265.             InitParams.BaseAddress = (U32*)STFLASH_BANK_1_BASE;
  266.             InitParams.VppAddress  = (U32*)NULL;
  267.         #else
  268.             return ST_ERROR_FEATURE_NOT_SUPPORTED;
  269.         #endif
  270.     }
  271. #else
  272.     if ( BankNum == BANK0 )
  273.     {
  274.         InitParams.BaseAddress = (U32*)STFLASH_BANK_0_BASE;
  275.         InitParams.VppAddress  = (U32*)STFLASH_VPP_0_ENABLE;
  276.     }
  277.     else
  278.     {
  279.         #if defined(STFLASH_BANK_1_BASE)
  280.             InitParams.BaseAddress = (U32*)STFLASH_BANK_1_BASE;
  281.             InitParams.VppAddress  = (U32*)STFLASH_VPP_1_ENABLE;
  282.         #else
  283.             return ST_ERROR_FEATURE_NOT_SUPPORTED;
  284.         #endif
  285.     }
  286. #endif
  287.     InitParams.MinAccessWidth  = MIN_ACCESS_WIDTH;
  288.     InitParams.MaxAccessWidth  = MAX_ACCESS_WIDTH;
  289.     InitParams.NumberOfBlocks  = NUM_BLOCKS;
  290.     InitParams.Blocks          = BlockData_s;
  291.     InitParams.DriverPartition = SystemPartition;
  292.     //InitParams.IsSerialFlash   = FALSE;
  293.     ErrCode = STFLASH_Init(FLASHDeviceName[BankNum], &InitParams );
  294.     return ( ErrCode );
  295. } /* end FLASH_Init */
  296. /*-------------------------------------------------------------------------
  297.  * Function : FLASH_Open
  298.  *            Open FLASH
  299.  * Input    : None
  300.  * Output   :
  301.  * Return   : TRUE if error, FALSE if success
  302.  * ----------------------------------------------------------------------*/
  303. ST_ErrorCode_t FLASH_Open( U8 BankNum )
  304. {
  305.     ST_ErrorCode_t          ErrCode;
  306.     STFLASH_OpenParams_t    OpenParams;
  307.     memset((char*)&OpenParams,0,sizeof(STFLASH_OpenParams_t));
  308.     ErrCode = STFLASH_Open(FLASHDeviceName[BankNum], &OpenParams, &FLASHHndl[BankNum]);
  309.     return ( ErrCode );
  310. } /* end FLASH_Open */
  311. /*-------------------------------------------------------------------------
  312.  * Function : FLASH_Term
  313.  *            Terminate FLASH
  314.  * Input    : ForceTerminate
  315.  * Output   :
  316.  * Return   : TRUE if error, FALSE if success
  317.  * ----------------------------------------------------------------------*/
  318. ST_ErrorCode_t FLASH_Term(  BOOL ForceTerminate, U8 BankNum )
  319. {
  320.     ST_ErrorCode_t          ErrCode;
  321.     STFLASH_TermParams_t    TermParams;
  322.     TermParams.ForceTerminate = ForceTerminate;
  323.     ErrCode = STFLASH_Term(FLASHDeviceName[BankNum], &TermParams);
  324.     return ( ErrCode );
  325. } /* end FLASH_Term */
  326. /*-------------------------------------------------------------------------
  327.  * Function : FLASH_SetupBlockInfo
  328.  *            Setup Block Info structure
  329.  * Input    :
  330.  * Output   :
  331.  * Return   : Error Code
  332.  * ----------------------------------------------------------------------*/
  333. ST_ErrorCode_t FLASH_SetupBlockInfo( void )
  334. {
  335.     ST_ErrorCode_t      ErrCode = ST_NO_ERROR;
  336.     STFLASH_Params_t    FLASHParams;
  337.     U8  Bank=0, Block=0;
  338.     U32 BlockStart = 0;
  339.     static U8 Section = 0;
  340.     memset((char*)&FLASHParams,0,sizeof(STFLASH_Params_t));
  341.     if ( Section == 0 )
  342.     {
  343.         /*
  344.         **  Set the address for each section
  345.         */
  346.         for ( Bank = 0; ( Bank < NUM_BANKS ) && ( ErrCode == ST_NO_ERROR ); Bank ++ )
  347.         {
  348.             ErrCode = FLASH_Init( Bank );                /* Init Bank */
  349.             if ( ErrCode == ST_NO_ERROR )
  350.             {
  351.                 ErrCode = FLASH_Open( Bank );
  352.                 if ( ErrCode == ST_NO_ERROR )
  353.                     ErrCode = FLASH_GetParams( Bank, &FLASHParams );
  354.                 if ( ErrCode == ST_NO_ERROR )
  355.                 {
  356.                     BlockStart = BaseAddress[Bank] = (U32) FLASHParams.InitParams.BaseAddress;
  357.                     for ( Block = 0; Block < FLASHParams.InitParams.NumberOfBlocks; Block ++)
  358.                     {
  359.                         BlockInfo[Section].Bank    = Bank;
  360.                         BlockInfo[Section].Address = BlockStart;
  361.                         BlockInfo[Section].Length  = FLASHParams.InitParams.Blocks[Block].Length;
  362.                         #ifdef DEBUG_OUTPUT
  363.                             printf("Block %2d (%d) 0x%08x 0x%08x (%6d) bytesn",
  364.                                          Block, Section,
  365.                                          BlockInfo[Section].Address,
  366.                                          BlockInfo[Section].Length,
  367.                                          BlockInfo[Section].Length );
  368.                         #endif
  369.                         Section ++;
  370.                         BlockStart += FLASHParams.InitParams.Blocks[Block].Length;
  371.                     }
  372.                 }
  373.                 ErrCode = FLASH_Term( TRUE, Bank );
  374.             }
  375.         }
  376.         BlockInfo[Section].Address = BlockStart;
  377.     }
  378.     if ( ErrCode == ST_NO_ERROR )
  379.         printf("Flash setup Ok (%d blocks)n", Section );
  380.     return( ErrCode );
  381. } /* end of FLASH_SetupBlockInfo */
  382. /*-------------------------------------------------------------------------
  383.  * Function : FLASH_Setup
  384.  *            Setup Block Info structure
  385.  * Input    :
  386.  * Output   :
  387.  * Return   : Error Code
  388.  * ----------------------------------------------------------------------*/
  389. ST_ErrorCode_t FLASH_Setup( void )
  390. {
  391.     ST_ErrorCode_t      ErrCode = ST_NO_ERROR;
  392.     U8  Bank;
  393.     /*    **  Set the address for each section*/
  394.         for ( Bank = 0; ( Bank < NUM_BANKS ) && ( ErrCode == ST_NO_ERROR ); Bank ++ )
  395.         {
  396.             ErrCode = FLASH_Init( Bank );                /* Init Bank */
  397.             if ( ErrCode == ST_NO_ERROR )
  398.             {
  399.                 ErrCode = FLASH_Open( Bank );
  400.             }
  401.             if ( ErrCode == ST_NO_ERROR )
  402.             printf("Flash setup Ok.....n" );
  403.         }
  404.     return( ErrCode );
  405. } /* end of Flash_Setup */
  406. /*-------------------------------------------------------------------------
  407.  * Function : FreeFileDataBuffer
  408.  *            as is
  409.  * Input    :
  410.  * Output   :
  411.  * Return   : None
  412.  * ----------------------------------------------------------------------*/
  413. static void FreeFileDataBuffer(void)
  414. {
  415.     /* deallocate File data buffer */
  416.     if ( FlashData_p != NULL )
  417.         memory_deallocate( SystemPartition, FlashData_p );
  418. } /* end of FreeFileDataBuffer() */
  419. /*-------------------------------------------------------------------------
  420.  * Function : GetBlockNumber
  421.  *            Get block number from address
  422.  * Input    :
  423.  * Output   :
  424.  * Return   : Block number of address
  425.  * ----------------------------------------------------------------------*/
  426. static U8 GetBlockNumber( U32 Address )
  427. {
  428.     U8  Block;
  429.     /* find the block this address is in */
  430.     for ( Block = 0; Block < TOTAL_BLOCKS; Block++ )
  431.     {
  432.         if (( Address >= BlockInfo[Block].Address ) &&
  433.             ( Address < (BlockInfo[Block].Address+BlockInfo[Block].Length)))
  434.         {
  435.             break;
  436.         }
  437.     }
  438.     return Block;
  439. } /* end GetBlockNumber */
  440. /*-------------------------------------------------------------------------
  441.  * Function : ConvertMemory
  442.  *            Convert Raw Hex file into memory buffer suitable to write to
  443.  *            flash copying created data over data read from filem, the
  444.  *            created data will be smaller as each byte takes three bytes
  445.  *            in the Hex file.
  446.  * Input    :
  447.  * Output   :
  448.  * Return   : size of data in buffer
  449.  * ----------------------------------------------------------------------*/
  450. static U32 ConvertMemory( long unsigned int MemSize )
  451. {
  452.     char                *OldPtr, *NewPtr;
  453.     int                 Section;
  454.     U32                 InOffset;
  455.     U32                 OutOffset;
  456.     U32                 BlockOffset;
  457.     U32                 Value=0;
  458.     U8                  Count=0 , Block=0;
  459.     for ( Section = 0; Section < NUM_SECTIONS; Section ++ )
  460.     {
  461.         SectionInfo[Section].Block = 0xff;
  462.         SectionInfo[Section].Address = 0;
  463.         SectionInfo[Section].Offset = 0;
  464.         SectionInfo[Section].Size = 0;
  465.     }
  466.     /* Convert File */
  467.     Section = 0;
  468.     OldPtr = FlashData_p;
  469.     NewPtr = (char*) NULL;
  470.     for ( InOffset = 0, OutOffset = 0; InOffset < MemSize; )
  471.     {
  472.         /* Line format =
  473.         ** Address  Data  (both in hex)
  474.         ** 12345678 11 22 33 44 55 etc. CR/LF
  475.         */
  476.         Value = (U32) strtol( OldPtr, &NewPtr, 16 );
  477.         if ( NewPtr != OldPtr ) /* if conversion possible */
  478.         {
  479.             if ( Value > 255 )  /* which means it is an address */
  480.             {
  481.                 if (( Block = GetBlockNumber( Value )) == TOTAL_BLOCKS )/*changed temp*/
  482.                 {
  483.                     printf("Invalid address 0x%xn", Value );
  484.                     break;
  485.                 }
  486.                 else
  487.                 {
  488.                     /*
  489.                     ** check if the address is non-contiguous
  490.                     ** or if it is a new block
  491.                     */
  492.                     if ((( SectionInfo[Section].Address + SectionInfo[Section].Size ) != Value ) ||
  493.                         ( SectionInfo[Section].Block != Block ))
  494.                     {
  495.                         Section ++;
  496.                     }
  497.                     /* Start New Block */
  498.                     if ( SectionInfo[Section].Block == 0xff )
  499.                     {
  500.                         SectionInfo[Section].Address = Value;
  501.                         SectionInfo[Section].Block = Block;
  502.                         SectionInfo[Section].Offset = OutOffset;
  503.                         SectionInfo[Section].Size = 0;
  504.                     }
  505.                 }
  506.             }
  507.             else
  508.             {
  509.                 BlockOffset = OutOffset;
  510.                 for ( Count = 0; Count < Section; Count++ )
  511.                     BlockOffset -= SectionInfo[Count].Size;
  512.                 if (( BlockOffset+1 ) > BlockInfo[SectionInfo[Section].Block].Length )
  513.                 {
  514.                     Section++;
  515.                 }
  516.                 /* Start New Block */
  517.                 if ( SectionInfo[Section].Block == 0xff )
  518.                 {
  519.                     SectionInfo[Section].Address = BlockInfo[Block].Address + BlockOffset;
  520.                     SectionInfo[Section].Block = GetBlockNumber(SectionInfo[Section].Address);
  521.                     SectionInfo[Section].Offset = OutOffset;
  522.                     SectionInfo[Section].Size = 0;
  523.                 }
  524.                 SectionInfo[Section].Size ++;
  525.                 FlashData_p[OutOffset++] = (char) Value;
  526.              }
  527.             InOffset += (U32)( NewPtr - OldPtr );
  528.             OldPtr = NewPtr;
  529.             if ( Section > NUM_SECTIONS )
  530.             {
  531.                 printf("Program Limitationn");
  532.                 exit(0);
  533.             }
  534.        }
  535.         else
  536.             break;
  537.     } /* end of for loop */
  538.     printf("n");
  539.     /* display the first few bytes of each section */
  540.     for ( Section = 0; Section < NUM_SECTIONS; Section ++ )
  541.     {
  542.         if ( SectionInfo[Section].Block != 0xFF )
  543.         {
  544.             printf("Section %2d, Block %3d, Addr 0x%08x, Offset 0x%08x, Size %6dn",
  545.                          Section,
  546.                          SectionInfo[Section].Block,
  547.                          SectionInfo[Section].Address,
  548.                          SectionInfo[Section].Offset,
  549.                          SectionInfo[Section].Size );
  550.             #ifdef DEBUG_INFO
  551.                 for ( InOffset = 0;
  552.                       ( InOffset < 32 ) && ( InOffset < SectionInfo[Section].Size );
  553.                       InOffset ++ )
  554.                 {
  555.                     printf(" %02x", FlashData_p[ SectionInfo[Section].Offset + InOffset ] );
  556.                 }
  557.                 printf("n");
  558.             #endif
  559.         }
  560.     }
  561.     return ( OutOffset );
  562. } /* end ConvertMemory */
  563. /*-------------------------------------------------------------------------
  564.  * Function : ReadFile
  565.  *            Read HEX file into memory
  566.  * Input    :
  567.  * Output   :
  568.  * Return   : File size
  569.  * ----------------------------------------------------------------------*/
  570. U32 FLASH_ReadFile( char *Filename )
  571. {
  572.     long int    HexFile_p=0;            /* datafile descriptor          */
  573.     long int    HexFileSize=0;          /* size in bytes of the file    */
  574.     /* Open and Read file into memory */
  575.     HexFile_p = debugopen(Filename, "rb");
  576.     printf("HexFile_p = 0x%8xn",HexFile_p);
  577.     if(HexFile_p < 0)
  578.     {
  579.         printf("Error opening file '%s'n", Filename );
  580.         return (0);
  581.     }
  582.     else
  583.     {
  584.         HexFileSize = debugfilesize(HexFile_p);
  585.         printf("HexFileSize = 0x%8xn",HexFileSize);
  586.         /* allocate File data buffer */
  587.         FreeFileDataBuffer();
  588.         FlashData_p = (char*) memory_allocate( SystemPartition, (U32) HexFileSize );
  589.         if ( FlashData_p != NULL )
  590.         {
  591.             printf("Loading '%s' into memory, wait .. ", Filename );
  592.             debugread(HexFile_p, FlashData_p, (size_t) HexFileSize);
  593.             printf("%d bytesn", HexFileSize );
  594.         }
  595.         else
  596.         {
  597.             printf("Not enough memory for HEX file (%d bytes)n", HexFileSize);
  598.             HexFileSize = 0;
  599.         }
  600.         debugclose(HexFile_p);
  601.     }
  602.     if ( HexFileSize > 0 )
  603.     {
  604.         /* convert buffer to binary and resize memory */
  605.         printf("Converting file in memory, wait .. ");
  606.         FlashSize = ConvertMemory( HexFileSize );
  607.         if ( FlashSize > 0 )
  608.         {
  609.             printf("%d bytesn", FlashSize );
  610.             FlashData_p = (char*) memory_reallocate( SystemPartition, FlashData_p, FlashSize );
  611.         }
  612.         else
  613.             printf("Invalid filen");
  614.     }
  615.     return( FlashSize );
  616. } /* end of FLASH_ReadFile */
  617. /*-------------------------------------------------------------------------
  618.  * Function : EraseFlash
  619.  *            Function to Read Hex file and erase areas required
  620.  * Input    : EraseAll
  621.  * Output   :
  622.  * Return   : EraseFailed = TRUE else FALSE
  623.  * ----------------------------------------------------------------------*/
  624. BOOL EraseFlash( BOOL EraseAll )
  625. {
  626.     ST_ErrorCode_t ErrCode = ST_NO_ERROR;
  627.     U8   Section, Bank;
  628.     U32  EraseOffset;
  629.     BOOL EraseFailed = TRUE;
  630.     /* check data size */
  631.     if (( FlashSize != 0 ) || EraseAll )
  632.     {
  633.         EraseFailed = FALSE;
  634.         printf("tt+------------+------------+n");
  635.         if ( EraseAll )
  636.         {
  637.             for ( Section = 0;( Section < TOTAL_BLOCKS ) && ( ErrCode == ST_NO_ERROR ); Section ++)
  638.             {
  639.                 Bank = BlockInfo[Section].Bank;
  640.                 EraseOffset = BlockInfo[Section].Address - BaseAddress[Bank];
  641.                 printf("%d block %2dt| 0x%08x | 0x%08x | ",
  642.                              Bank, Section,
  643.                              BlockInfo[Section].Address,
  644.                              BlockInfo[Section].Address + BlockInfo[Section].Length - 1 );
  645.                 ErrCode = STFLASH_Erase( FLASHHndl[Bank],
  646.                                          EraseOffset,
  647.                                          BlockInfo[Section].Length );
  648.                 if ( ErrCode != ST_NO_ERROR )
  649.                 {
  650.                     printf("Erase FAILEDn");
  651.                     EraseFailed = TRUE;
  652.                 }
  653.                 else
  654.                     printf("ERASEDn");
  655.             }
  656.         }
  657.         else
  658.         {
  659.             /* Find out which blocks are to be Erased */
  660.             for ( Section = 0; ( Section < NUM_SECTIONS ) && ( EraseFailed == FALSE ); Section ++ )
  661.             {
  662.                 if ( SectionInfo[Section].Block != 0xFF )
  663.                 {
  664.                     /*
  665.                     ** Erase = erase whole block, valid parameters are block base address & block size
  666.                     */
  667.                     Bank = BlockInfo[ SectionInfo[Section].Block ].Bank;
  668.                     EraseOffset = BlockInfo[ SectionInfo[Section].Block ].Address - BaseAddress[Bank];
  669.                     printf("block %2dt| 0x%08x | 0x%08x | ",
  670.                                  SectionInfo[Section].Block,
  671.                                  BlockInfo[ SectionInfo[Section].Block ].Address,
  672.                                  BlockInfo[ SectionInfo[Section].Block ].Address +
  673.                                  BlockInfo[ SectionInfo[Section].Block ].Length - 1 );
  674.                     ErrCode = STFLASH_Erase( FLASHHndl[Bank],
  675.                                              EraseOffset,
  676.                                              BlockInfo[ SectionInfo[Section].Block ].Length );
  677.                     if ( ErrCode != ST_NO_ERROR )
  678.                     {
  679.                         printf("ErrCode = 0x%8xn",ErrCode);
  680.                         printf("Erase FAILEDn");
  681.                         printf("FLASHHndl[Bank] = 0x%8Xn",FLASHHndl[Bank]);
  682.                         EraseFailed = TRUE;
  683.                     }
  684.                     else
  685.                         printf("ERASEDn");
  686.                 }
  687.             }
  688.         }
  689.         printf("tt+------------+------------+n");
  690.     }
  691.     if ( EraseFailed == TRUE )
  692.     {
  693.         FreeFileDataBuffer();
  694.         printf("!!!! ERASE Failed !!!!n");
  695.         return TRUE;
  696.     }
  697.     return EraseFailed;
  698. } /* end ProgramFlash */
  699. /*-------------------------------------------------------------------------
  700.  * Function : ProgramFlash
  701.  *            Function to Read Hex file and program flash with contents
  702.  * Input    :
  703.  * Output   :
  704.  * Return   : None
  705.  * ----------------------------------------------------------------------*/
  706. BOOL ProgramFlash( void )
  707. {
  708.     ST_ErrorCode_t      ErrCode;
  709.     int                 Section, Bank;
  710.     U32                 NumberOfBytes;
  711.     BOOL                ProgramFailed;
  712. FlashSize=0x2000;
  713.     /* check data size */
  714.     if ( FlashSize == 0 )
  715.         ProgramFailed = TRUE;
  716.     else
  717.     {
  718.         ProgramFailed = FALSE;
  719.         printf("tt+------------+------------+n");
  720.     }
  721.     /* Find out which blocks are to be written to */
  722.     /* Write the data to the Flash */
  723.     /*printf("Printing the contents of BlockInfo n"));
  724.     for ( Section = 0; ( Section < NUM_SECTIONS ) ; Section ++ )
  725.     {
  726.         printf("Section=%dt", Section ));
  727.         printf("SectionInfo[Section]=%dn", SectionInfo[Section] ));
  728.         printf("SectionInfo[Section].Block=%dt", SectionInfo[Section].Block ));
  729.         printf("BlockInfo[ SectionInfo[Section].Block ].Bank=%dn",BlockInfo[ SectionInfo[Section].Block ].Bank));
  730.     }*/
  731.     for ( Section = 0; ( Section < NUM_SECTIONS ) && ( ProgramFailed == FALSE ); Section ++ )
  732.     {
  733.         if ( SectionInfo[Section].Block != 0xFF )
  734.         {
  735.             U32  WriteOffset;
  736.             /*
  737.             ** Write = write bytes from Offset
  738.             */
  739.             Bank = BlockInfo[ SectionInfo[Section].Block ].Bank;
  740.             printf("Section=%dt", Section );
  741.             printf("SectionInfo[Section]=%dn", SectionInfo[Section] );
  742.             printf("SectionInfo[Section].Block=%dt", SectionInfo[Section].Block );
  743.             printf("BlockInfo[ SectionInfo[Section].Block ].Bank=%dn",BlockInfo[ SectionInfo[Section].Block ].Bank);
  744.             WriteOffset = SectionInfo[Section].Address - BaseAddress[Bank];
  745.             /*SectionInfo[Section].Address += 0x4; Tara*/
  746.             printf("block %2dt| 0x%08x | 0x%08x | ",
  747.                          SectionInfo[Section].Block,
  748.                          SectionInfo[Section].Address,
  749.                          SectionInfo[Section].Address + SectionInfo[Section].Size - 1 );
  750.             ErrCode = STFLASH_Write( FLASHHndl[Bank],
  751.                                      WriteOffset,
  752.                                      (U8*)&FlashData_p[SectionInfo[Section].Offset],
  753.                                      SectionInfo[Section].Size,
  754.                                      &NumberOfBytes );
  755.             if ( ErrCode != ST_NO_ERROR )
  756.             {
  757.                 printf("Program FAILEDn");
  758.                 ProgramFailed = TRUE;
  759.             }
  760.             else
  761.                 printf("PROGRAMMEDn");
  762.         }
  763.     }
  764.     if ( ProgramFailed == TRUE )
  765.     {
  766.         FreeFileDataBuffer();
  767.         printf("!!!! PROGRAM Failed !!!!n");
  768.         return TRUE;
  769.     }
  770.     else
  771.     {
  772.         printf("tt+------------+------------+n");
  773.         return FALSE;
  774.     }
  775. } /* end ProgramFlash */
  776. /*-------------------------------------------------------------------------
  777.  * Function : VerifyFlash
  778.  *            Function to Read Hex file and verify flash with contents
  779.  * Input    :
  780.  * Output   :
  781.  * Return   : None
  782.  * ----------------------------------------------------------------------*/
  783. BOOL VerifyFlash( void )
  784. {
  785.     ST_ErrorCode_t      ErrCode = ST_NO_ERROR;
  786.     int                 Section, Bank;
  787.     U32                 Length;
  788.     U32                 ReadOffset;
  789.     U32                 NumberOfBytes;
  790.     U8                  *Buffer_p;
  791.     BOOL                VerifyFailed;
  792.     /* check data size */
  793.     if ( FlashSize == 0 )
  794.         VerifyFailed = TRUE;
  795.     else
  796.     {
  797.         VerifyFailed = FALSE;
  798.         printf("tt+------------+------------+n");
  799.     }
  800.     /* Find out which blocks are to be verified */
  801.     /* Read the data and compare */
  802.     for ( Section = 0; ( Section < NUM_SECTIONS ) && ( VerifyFailed == FALSE ); Section ++ )
  803.     {
  804.         if ( SectionInfo[Section].Block != 0xFF )
  805.         {
  806.             Bank = BlockInfo[ SectionInfo[Section].Block ].Bank;
  807.             Length = BlockInfo[ SectionInfo[Section].Block ].Length;
  808.             ReadOffset = SectionInfo[Section].Address - BaseAddress[Bank];
  809.             /* allocate temp buffer to put flash data in */
  810.             Buffer_p = memory_allocate( SystemPartition, Length );
  811.             if ( Buffer_p != NULL )
  812.             {
  813.                 printf("block %2dt| 0x%08x | 0x%08x | ",
  814.                          SectionInfo[Section].Block,
  815.                          SectionInfo[Section].Address,
  816.                          SectionInfo[Section].Address + SectionInfo[Section].Size - 1 );
  817.                 ErrCode = STFLASH_Read( FLASHHndl[Bank],
  818.                                         ReadOffset,
  819.                                         Buffer_p,
  820.                                         SectionInfo[Section].Size,
  821.                                         &NumberOfBytes );
  822.                 if(ErrCode != ST_NO_ERROR)
  823.                 {
  824.                 }
  825.                 if ( memcmp( Buffer_p,(U8*)&FlashData_p[SectionInfo[Section].Offset],
  826.                              SectionInfo[Section].Size ) != 0 )
  827.                 {
  828.                     printf("Verify FAILEDn");
  829.                     VerifyFailed = TRUE;
  830.                 }
  831.                 else
  832.                     printf("VERIFIEDn");
  833.                 /* deallocate temp buffer */
  834.                 memory_deallocate( SystemPartition, Buffer_p );
  835.             }
  836.         }
  837.     }
  838.     FreeFileDataBuffer();
  839.     if ( VerifyFailed == TRUE )
  840.     {
  841.         printf("!!!! VERIFY Failed !!!!n");
  842.         return TRUE;
  843.     }
  844.     else
  845.     {
  846.         printf("tt+------------+------------+n");
  847.         return FALSE;
  848.     }
  849. } /* end VerifyFlash */
  850. //---------------------------------------------------------------------------------------
  851. //
  852. //上面接口用于烧录应用程序
  853. //
  854. //下面接口用于应用程序调用
  855. //
  856. //---------------------------------------------------------------------------------------
  857. ST_ErrorCode_t KB_FLASHSetupBlockInfo( void )
  858. {
  859.     ST_ErrorCode_t      ErrCode = ST_NO_ERROR;
  860.     STFLASH_Params_t    FLASHParams;
  861.     U8  Bank=0, Block=0;
  862.     U32 BlockStart = 0;
  863.     static U8 Section = 0;
  864.     memset((char*)&FLASHParams,0,sizeof(STFLASH_Params_t));
  865.     if ( Section == 0 )
  866.     {
  867.         /*
  868.         **  Set the address for each section
  869.         */
  870.         for ( Bank = 0; ( Bank < NUM_BANKS ) && ( ErrCode == ST_NO_ERROR ); Bank ++ )
  871.         {
  872.             ErrCode = FLASH_Init( Bank );                /* Init Bank */
  873.             if ( ErrCode == ST_NO_ERROR )
  874.             {
  875.                 ErrCode = FLASH_Open( Bank );
  876.                 if ( ErrCode == ST_NO_ERROR )
  877.                     ErrCode = FLASH_GetParams( Bank, &FLASHParams );
  878.                 if ( ErrCode == ST_NO_ERROR )
  879.                 {
  880.                     BlockStart = BaseAddress[Bank] = (U32) FLASHParams.InitParams.BaseAddress;
  881.                     for ( Block = 0; Block < FLASHParams.InitParams.NumberOfBlocks; Block ++)
  882.                     {
  883.                         BlockInfo[Section].Bank    = Bank;
  884.                         BlockInfo[Section].Address = BlockStart;
  885.                         BlockInfo[Section].Length  = FLASHParams.InitParams.Blocks[Block].Length;
  886.                         #ifdef DEBUG_OUTPUT
  887.                             printf("Block %2d (%d) 0x%08x 0x%08x (%6d) bytesn",
  888.                                          Block, Section,
  889.                                          BlockInfo[Section].Address,
  890.                                          BlockInfo[Section].Length,
  891.                                          BlockInfo[Section].Length );
  892.                         #endif
  893.                         Section ++;
  894.                         BlockStart += FLASHParams.InitParams.Blocks[Block].Length;
  895.                     }
  896.                 }
  897.                 ErrCode = FLASH_Term( TRUE, Bank );
  898.             }
  899.         }
  900.         BlockInfo[Section].Address = BlockStart;
  901.     }
  902.     if ( ErrCode == ST_NO_ERROR )
  903.         printf("Flash setup Ok (%d blocks)n", Section );
  904.     return( ErrCode );
  905. }
  906. void test_FlashInit(void )
  907. {
  908. #if 1
  909. KB_FLASHSetupBlockInfo();
  910. FLASH_Setup();
  911. #else //or OK
  912.      ST_ErrorCode_t          ErrCode;
  913.     if ( ErrCode == ST_NO_ERROR )
  914.         ErrCode  = FLASH_SetupBlockInfo();
  915.     if ( ErrCode == ST_NO_ERROR )
  916.         {
  917.             ErrCode = FLASH_Init(BANK0);
  918.             if ( ErrCode == ST_NO_ERROR )
  919.             ErrCode = FLASH_Open(BANK0);
  920.         }
  921.     #ifdef STFLASH_BANK_1_BASE
  922.         if ( ErrCode == ST_NO_ERROR )
  923.         {
  924.             ErrCode = FLASH_Init(BANK1);
  925.             if ( ErrCode == ST_NO_ERROR )
  926.                 ErrCode = FLASH_Open(BANK1);
  927.         }
  928.     #endif
  929. #endif
  930. }
  931. #if 1
  932. RetValue KB_FLASHInit(void)
  933. {
  934.      ST_ErrorCode_t ErrCode;
  935. KB_FLASHSetupBlockInfo();
  936. FLASH_Setup();
  937. return RETOK;
  938. }
  939. RetValue KB_FlashErase(UINT32 secID)
  940. {
  941. ST_ErrorCode_t ErrCode = ST_NO_ERROR;
  942.      int Bank;
  943. U32 EraseOffset;
  944.        Bank = BlockInfo[secID].Bank;
  945.        EraseOffset = BlockInfo[secID].Address - BaseAddress[Bank];
  946.      //printf("n[Flash]:Erase address[0x%x] len[0x%x].",BlockInfo[secID].Address,BlockInfo[secID].Length);
  947.        ErrCode = STFLASH_Erase( FLASHHndl[Bank],EraseOffset,BlockInfo[secID].Length );
  948.       if ( ErrCode != ST_NO_ERROR )
  949.       {
  950.           printf("n[Flash]:Erase failed[0x%x].",ErrCode);
  951. return RETFIAL1;
  952.       }
  953.       else
  954.       {
  955.             //printf("n[Flash]:Erase success.");
  956. return RETOK;
  957.       }
  958. }
  959. RetValue KB_FlashWrite(UINT32 secID,UINT32 Offset,UINT8 *Buffer,UINT32 NumberToWrite)
  960. {
  961. ST_ErrorCode_t ErrCode = ST_NO_ERROR;
  962.      int Bank;
  963. U32 numWrited,WriteOffset;
  964.    if ( SectionInfo[secID].Block != 0xFF )
  965.        {
  966. KB_FlashErase(secID);
  967.            Bank = BlockInfo[ SectionInfo[secID].Block ].Bank;
  968.              WriteOffset = BlockInfo[secID].Address - BaseAddress[Bank];
  969.              //printf("n[Flash]:Write address[0x%x] WriteOffset[0x%x] NumberToWrite[0x%x].",BlockInfo[secID].Address,WriteOffset,NumberToWrite);
  970. //printf("nSectionInfo address[0x%x] base address[0x%x] WriteOffset[0x%x]",SectionInfo[secID].Address,BaseAddress[Bank],WriteOffset);
  971.              ErrCode = STFLASH_Write( FLASHHndl[Bank],WriteOffset,(U8*)Buffer,NumberToWrite,&numWrited );
  972.              if ( ErrCode != ST_NO_ERROR )
  973.              {
  974.                  printf("n[Flash]:Write failed[0x%x].",ErrCode);
  975. return RETFIAL1;
  976.              }
  977.              else
  978.              {
  979.                  //printf("n[Flash]:Write success.");
  980. return RETOK;
  981.              }
  982.      }
  983. else
  984. {
  985. printf("n[Flash]:Write section block error[0x%x].",SectionInfo[secID].Block);
  986. return RETFIAL1;
  987. }
  988. }
  989. RetValue KB_FlashRead( UINT32 secID,UINT32 Offset,UINT8 *Buffer,UINT32 NumberToRead)
  990. {
  991. ST_ErrorCode_t ErrCode = ST_NO_ERROR;
  992.      int Bank;
  993. U32 numReaded,ReadOffset;
  994.    if ( SectionInfo[secID].Block != 0xFF )
  995.        {
  996.            Bank = BlockInfo[ SectionInfo[secID].Block ].Bank;
  997.              ReadOffset = BlockInfo[secID].Address - BaseAddress[Bank];
  998.              //printf("n[Flash]:Read address[0x%x] ReadOffset[0x%x] NumberToRead[0x%x].",BlockInfo[secID].Address,ReadOffset,NumberToRead);
  999. //printf("nSectionInfo address[0x%x] base address[0x%x] WriteOffset[0x%x]",SectionInfo[secID].Address,BaseAddress[Bank],WriteOffset);
  1000.              ErrCode = STFLASH_Read( FLASHHndl[Bank],ReadOffset,(U8*)Buffer,NumberToRead,&numReaded );
  1001.              if ( ErrCode != ST_NO_ERROR )
  1002.              {
  1003.                  printf("n[Flash]:Read failed[0x%x].",ErrCode);
  1004. return RETFIAL1;
  1005.              }
  1006.              else
  1007.              {
  1008.                  //printf("n[Flash]:Read success.");
  1009. return RETOK;
  1010.              }
  1011.      }
  1012. else
  1013. {
  1014. printf("n[Flash]:Read section block error[0x%x].",SectionInfo[secID].Block);
  1015. return RETFIAL1;
  1016. }
  1017. }
  1018. UINT8 *KB_FlashGetSectorAddr(UINT32 secID)
  1019. {
  1020. UINT8 *addr;
  1021. addr=(UINT8 *)BlockInfo[secID].Address;
  1022. return addr;
  1023. }
  1024. #endif
  1025. /* EOF --------------------------------------------------------------------- */