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

📄 erase_16x4_ghs.c

📁 ARM入门的好帮手.包含了从简单到相对较复杂的程序.
💻 C
字号:
//*-----------------------------------------------------------------------------//*      ATMEL Microcontroller Software Support  -  ROUSSET  -//*-----------------------------------------------------------------------------//* The software is delivered "AS IS" without warranty or condition of any//* kind, either express, implied or statutory. This includes without//* limitation any warranty or condition with respect to merchantability or//* fitness for any particular purpose, or against the infringements of//* intellectual property rights of others.//*-----------------------------------------------------------------------------//* File Name               : erase_16x4_ghs.c//* Object                  : FLASH eraser for ://*                             AT49BV1604/AT49BV1604T//*                             AT49BV1614/AT49BV1604T//*//* Translator              : Green Hills Multi2K Software Development Toolkit V3.01//*//* 1.1 10/01/02 PFi        : Creation//*-----------------------------------------------------------------------------/* Include Standard c Libraries to allow stand alone compiling and operation */#include <stdio.h>#include <stdlib.h>#define TRUE    1#define FALSE   0/* Target Identification */#define FLASH_TYPE           "Flash 16x4"/* Trials Number to erase a sector */#define NB_TRIAL_ERASE      10typedef volatile unsigned short flash_word ;#define MIN_NO_OF_ARGS      2/* Timeout loop count */#define TIME_OUT            0x7FFFFFFF/* Loop count for delay after sequence */#define DELAY               100/* Wait for flash ready by poliing RDY/BUSY pin selection *///#define   POLL_RDY_NBUSY/* PIO connected to RDY/BUSY pin of the flash *//* used only if POLL_RDY_NBUSY is defined */#define RDY_PIO_BUSY        (1<<25)/* Define Flash Codes */#define FLASH_SEQ_ADD1      (0x5555)#define FLASH_SEQ_ADD2      (0x2AAA)#define FLASH_CODE1         ((flash_word)(0xAA))#define FLASH_CODE2         ((flash_word)(0x55))#define ID_IN_CODE          ((flash_word)(0x90))#define ID_OUT_CODE         ((flash_word)(0xF0))#define WRITE_CODE          ((flash_word)(0xA0))#define ERASE_CODE          ((flash_word)(0x80))#define CHIP_ERASE_CODE     ((flash_word)(0x10))#define ERASE_SECTOR_CODE   ((flash_word)(0x30))/* Defines organization structure */typedef struct OrgDef{    unsigned int    sector_number ;    unsigned int    sector_size ;} OrgDef ;/* Defines supported flash organizations */const OrgDef OrgAT49BV16x4[] ={    /* 8 x 8kbytes sectors */    {        8,        8*1024    },    /* 2 x 32 kbytes sectors */    {        2,        32*1024,    },    /* 30 x 64 kbytes sectors */    {        30,        64*1024,    }};const OrgDef OrgAT49BV16x4T[] ={    /* 30 x 64 kbytes sectors */    {        30,        64*1024,    },    /* 2 x 32 kbytes sectors */    {        2,        32*1024,    },    /* 8 x 8kbytes sectors */    {        8,        8*1024    }};/* Defines Flash device definition structure */typedef struct FlashDef{    unsigned int    flash_size;    char            *flash_name;    unsigned int    flash_manuf_id;    unsigned int    flash_id;    unsigned int    flash_mask;    const OrgDef    *flash_org;    unsigned int    flash_block_nb;}FlashDef;/* Define supported flash Table */const FlashDef FlashTable[] ={    {        2*1024*1024,        "AT49BV16x4",        0x001F,//      0x161F,        0x00C0,//      0x16C0,        0x001FFFFF,        OrgAT49BV16x4,        sizeof(OrgAT49BV16x4)/sizeof(OrgDef)    },    {        2*1024*1024,        "AT49BV16x4T",        0x001F,//      0x161F,        0x00C2,//      0x16C2,        0x001FFFFF,        OrgAT49BV16x4T,        sizeof(OrgAT49BV16x4T)/sizeof(OrgDef)    }};/* Defines number of flash supported */#define NB_FLASH_SUPPORTED  sizeof(FlashTable)/sizeof(FlashDef)void wait ( int count ){    int     i ;    for ( i = count ; i > 0 ; i -- ) ;}//*--------------------------------------------------------------------------------------//* Function Name       : display_help//* Object              : Display help when an error occures//* Input Parameters    : none//* Output Parameters   : none//* Functions called    : none//*--------------------------------------------------------------------------------------void display_help ( void )//* Begin{    //* Display Error    printf ( "\n" ) ;    printf ( "Erro in arguments, correct syntax is :\n" ) ;    //* Dispaly Syntax    printf ( "\tload erase_16x4.axf <address>\n" ) ;    printf ( "\twhere:\n" ) ;    //* Display arguments    printf ( "\t\t<address>  - Base address of the flash\n" ) ;    printf ( "\n" ) ;}//* End//*--------------------------------------------------------------------------------------//* Function Name       : flash_identify//* Object              : Read the flash manufacturer code and Flash ID code//* Input Parameters    : flash_word *load_addr = Flash bass address//* Output Parameters   : Pointer to the Flash identified//* Functions called    : none//*--------------------------------------------------------------------------------------const FlashDef *flash_identify ( flash_word *load_addr )//* Begin{    flash_word      manuf_code ;    flash_word      device_code ;    const FlashDef  *flash_pt ;    flash_word      *base_addr ;    int             exit = FALSE ;    //* Initialize Flash Table pointer    flash_pt = FlashTable ;    //* Look for the  device in the known flash table    while ( exit == FALSE )    {        //* Initialize Flash Base Address        base_addr = (flash_word *) ((int)load_addr & ~(flash_pt->flash_mask)) ;        //* Display Flash Identification Header        printf ( "Trying to identify Flash at base address (0x%x)\n", (int)base_addr) ;        //* Display Flash Tested        printf ( "Trying %s\n", flash_pt->flash_name ) ;        //* Enter Software Product Identification Mode        *(base_addr + FLASH_SEQ_ADD1) = FLASH_CODE1;        *(base_addr + FLASH_SEQ_ADD2) = FLASH_CODE2;        *(base_addr + FLASH_SEQ_ADD1) = ID_IN_CODE;        //* Read Manufacturer and device code from the device        manuf_code  = *base_addr ;        device_code = *(base_addr + 1) ;        //* Exit Software Product Identification Mode        *(base_addr + FLASH_SEQ_ADD1) = FLASH_CODE1;        *(base_addr + FLASH_SEQ_ADD2) = FLASH_CODE2;        *(base_addr + FLASH_SEQ_ADD1) = ID_OUT_CODE;                //* If both manufacturer and device codes corresponds        if (( flash_pt->flash_id == device_code ) &&            ( flash_pt->flash_manuf_id == manuf_code ))        {            //* Exit the search loop            exit = TRUE ;        }        //* Else        else        {            //* Next Flash, If end of table            if ( ++flash_pt >= FlashTable + NB_FLASH_SUPPORTED )            {                //* Return 0, Display Error and Exit loop                flash_pt = (const FlashDef *)0 ;                printf ( "Error - Unknown device: manufacturer %02x / device %02x \n",                        manuf_code, device_code );                exit = TRUE ;            }            //* Endif        }        //* Endif    }    //* EndWhile    //* Return pointer to Flash found    return ( flash_pt ) ;}//*--------------------------------------------------------------------------------------//* Function Name       : init_flash_ready//* Object              : Setup the PIO line connected to the RDY/BUSY line of the flash//* Input Parameters    : none//* Output Parameters   : none//* Functions called    : none//*--------------------------------------------------------------------------------------void init_flash_ready ( void ){#ifdef POLL_RDY_NBUSY    *(( volatile unsigned int *) 0xFFFF0000 ) = RDY_PIO_BUSY ;    *(( volatile unsigned int *) 0xFFFF0014 ) = RDY_PIO_BUSY ;#endif}//*--------------------------------------------------------------------------------------//* Function Name       : wait_flash_ready//* Object              : check if data is written by software polling//* Input Parameters    : Data and address of the data.//* Output Parameters   : TRUE or FALSE//* Functions called    : none//*--------------------------------------------------------------------------------------int wait_flash_ready ( flash_word *address ){//* Begin    int i = 0 ;#ifdef POLL_RDY_NBUSY    //* While RDY/BUSY is not set    while (((*(( volatile unsigned int *) 0xFFFF0024 ) & RDY_PIO_BUSY ) == 0 ) &&           ( i++ < TIME_OUT )) ;#else    //* While two consecutive read don't give same value or timeout    while (( *address != *address ) && ( i++ < TIME_OUT )) ;#endif    //* If timeout    if ( i < TIME_OUT )    {        //* Return True        return ( TRUE ) ;    }    //* Else    else    {        //* Return False        return ( FALSE ) ;    }    //* Endif}//* End//*--------------------------------------------------------------------------------------//* Function Name       : check_flash_erased//* Object              : check if the flash is erased. If not erase it.//*//* Input Parameters    : <*flash> = Pointer to Flash descriptor//*                       <*addr_base> = Pointer base address//*//* Output Parameters   : If flash erased TRUE, else FALSE//*--------------------------------------------------------------------------------------int check_flash_erased ( const FlashDef *flash,                         flash_word *addr_base )//* Begin{    int         i ;    flash_word  read_data ;    int         size = flash->flash_size ;    //* For each word of the flash    for ( i = 0 ; i < (size/2) ; i ++ )    {        //* Check erased value reading, if not        if (( read_data = *(addr_base + i)) != (flash_word)0xFFFF )        {            //* Display Error and return False            printf ( "Chip not erased !\n" ) ;            printf ( "Address 0x%08x, Value 0x%08x\n",                     (int)(addr_base + i), read_data ) ;            return ( FALSE ) ;        }        //* Endif    }    //* Endfor    //* Display Sector Erased    printf ( "Chip erased !\n" ) ;    //* Return True    return ( TRUE ) ;}//* End//*--------------------------------------------------------------------------------------//* Function Name       : erase_flash//* Object              : check if flash is erased if not erase it//*//* Input Parameters    : <*flash> = Pointer to Flash descriptor//*                       <*addr_base> = Pointer base address//*//* Output Parameters   : if flash erased TRUE, else FALSE//*--------------------------------------------------------------------------------------int erase_flash ( const FlashDef *flash,                  flash_word *base_addr )//* Begin{    int     trial = 0 ;    //* While flash is not erased or too much erasing performed    while (( check_flash_erased ( flash, base_addr ) == FALSE ) &&           ( trial++ < NB_TRIAL_ERASE ))    {        //* Display Erasing Sector        printf ( "Erasing Chip\n" ) ;        //* Enter Sector Erase Sequence codes        *(base_addr + FLASH_SEQ_ADD1) = FLASH_CODE1;        *(base_addr + FLASH_SEQ_ADD2) = FLASH_CODE2;        *(base_addr + FLASH_SEQ_ADD1) = ERASE_CODE;        *(base_addr + FLASH_SEQ_ADD1) = FLASH_CODE1;        *(base_addr + FLASH_SEQ_ADD2) = FLASH_CODE2;        *(base_addr + FLASH_SEQ_ADD1) = CHIP_ERASE_CODE;	wait ( DELAY ) ;        //* Wait for Flash Ready after Erase, if timeout        if ( wait_flash_ready ( base_addr ) == FALSE )        {            //* Display Timeout and return False            printf ( "Timeout while erasing\n" ) ;        }        //* Endif    }    //* EndWhile    //* Return True    return ( TRUE ) ;}//* End//*--------------------------------------------------------------------------------------//* Function Name       : main//* Object              : Get parameters//* Input Parameters    : <argc>    ://*                     : <*argv[]> : Base address of the flash memory//*//* Output Parameters   : none//*--------------------------------------------------------------------------------------int main ( int argc, char *argv[] )//* Begin{    flash_word      *base_addr ;    const FlashDef  *flash ;    //* Display Flash Eraser Header    printf ( "\n**** %s Flash Erasing Utility ****\n", FLASH_TYPE ) ;    //* If not enough arguments    if (argc < MIN_NO_OF_ARGS)    {        //* Display Help then Return False        display_help () ;        return ( FALSE ) ;    }    //* Endif    /* Get load address */    sscanf ( argv[1], "0x%x", &base_addr) ;    //* Display Flash Base Address    printf ( "Base address is:   %x \n", (int)base_addr ) ;    //* If FLASH Device is not recognised    if (( flash = flash_identify ( base_addr )) == (const FlashDef *)0 )    {        //* Display Error and exit        printf ( "Error - The Flash device is not recognised\n" ) ;        return ( FALSE );    }    //* Init PIO MCKO/P25 as busy ready input signal    init_flash_ready () ;    //* If Flash Erasing is not OK    if ( erase_flash ( flash, base_addr ) != TRUE )    {        //* Display Error and exit        printf ( "Error while erasing Flash\n" ) ;        return ( FALSE ) ;    }    //* If Erased flash is not checked    if ( check_flash_erased ( flash, base_addr ) != TRUE )    {        //* Display Error and exit        printf ( "Flash not checked after erasing\n" ) ;        return ( FALSE ) ;    }    //* Display Flash erased and checked    printf ( "Flash erased and checked\n" ) ;    return ( TRUE ) ;}//* End

⌨️ 快捷键说明

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