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

📄 flash_16x4_ads.c

📁 ATMEL的ADS例子1
💻 C
📖 第 1 页 / 共 2 页
字号:
//*--------------------------------------------------------------------------------------
//*      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               : flash_16x4_ads.c
//* Object                  : FLASH programmer for :
//*                             AT49BV1604/AT49BV1604T
//*                             AT49BV1614/AT49BV1604T
//*                             AT49BV1604A/AT49BV1604AT
//*
//* 1.0 10/01/02    PFi     : Creation
//* 1.0 18/Feb/03   JPP     : Add default value in internal ram at 0x40 & 0x44
//* 1.1 03/Apr/03   NCy     : Add AT49BV1604A/AT49BV1604AT
//*--------------------------------------------------------------------------------------

/* 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 TARGET_ID           "Flash 16x4"

/* Trials Number to erase a sector */
#define NB_TRIAL_ERASE      10

typedef volatile unsigned short flash_word ;

#define MIN_NO_OF_ARGS      3

/* Timeout loop count */
#define TIME_OUT            10000000
/* Loop count for delay after sequence */
#define DELAY               10000

/* Wait for flash ready by polling 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)

/* Flash Type */
#define ATMEL_MANUFACTURED          0x001F
#define FLASH_AT49BV16x4A           0x00C0
#define FLASH_AT49BV16x4AT          0x00C2

#define FLASH_IDENT_DEFAULT         0x0000
#define FLASH_IDENT_AT49BV16x4A     0x00C8

/* 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_SECTOR_CODE1  ((flash_word)(0x80))
#define CHIP_SECTOR_CODE1   ((flash_word)(0x10))
#define ERASE_SECTOR_CODE2  ((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
    }
};

 OrgDef OrgAT49BV16x4A[] =
{
    /* 8 x 8kbytes sectors */
    {
        8,
        8*1024
    },
    /* 31 x 64 kbytes sectors */
    {
        31,
        64*1024,
    }
};

OrgDef OrgAT49BV16x4AT[] =
{
    /* 31 x 64 kbytes sectors */
    {
        31,
        64*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_extra_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,
        FLASH_IDENT_DEFAULT,
        0x001FFFFF,
        OrgAT49BV16x4,
        sizeof(OrgAT49BV16x4)/sizeof(OrgDef)
    },
    {
        2*1024*1024,
        "AT49BV16x4T",
        0x001F,
//      0x161F,
        0x00C2,
//      0x16C2,
        FLASH_IDENT_DEFAULT,
        0x001FFFFF,
        OrgAT49BV16x4T,
        sizeof(OrgAT49BV16x4T)/sizeof(OrgDef)
    },
    {
        2*1024*1024,
        "AT49BV16x4A",
        ATMEL_MANUFACTURED,
        FLASH_AT49BV16x4A,
        FLASH_IDENT_AT49BV16x4A,
        0x001FFFFF,
        OrgAT49BV16x4A,
        sizeof(OrgAT49BV16x4A)/sizeof(OrgDef)
    },
    {
        2*1024*1024,
        "AT49BV16x4AT",
        ATMEL_MANUFACTURED,
        FLASH_AT49BV16x4AT,
        FLASH_IDENT_AT49BV16x4A,        
        0x001FFFFF,
        OrgAT49BV16x4AT,
        sizeof(OrgAT49BV16x4AT)/sizeof(OrgDef)
    }
};

/* Defines number of flash supported */
#define NB_FLASH_SUPPORTED  sizeof(FlashTable)/sizeof(FlashDef)


//*--------------------------------------------------------------------------------------
//* Function Name       : display_help
//* Object              : Display help when an error occurs
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : none
//*--------------------------------------------------------------------------------------
void display_help ( void )
//* Begin
{
    //* Display Error
    printf ( "\n" ) ;
    printf ( "Error in arguments, correct syntax is :\n" ) ;
    //* Dispaly Syntax
    printf ( "\tload flash <filename> <address>\n" ) ;
    printf ( "\twhere:\n" ) ;
    //* Display arguments
    printf ( "\t\t<filename> - binary file to program into flash\n" ) ;
    printf ( "\t\t<address>  - beginning address to download\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 ;
    flash_word      extra_id ;
    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) ;
        extra_id = *(base_addr + 3);

        //* 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 )&&
            ( flash_pt->flash_extra_id == extra_id ))
        {
            //* 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, flash_word data )
{
//* 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 != data ) && ( i++ < TIME_OUT )) ;
#endif

    //* If timeout
    if ( i < TIME_OUT )
    {
        //printf ( "Counter = %d\n", i ) ;
        //* Return True
        return ( TRUE ) ;
    }
    //* Else
    else
    {
        //printf ( "Timeout\n" ) ;
        //* Return False
        return ( FALSE ) ;
    }
    //* Endif
}
//* End

//*--------------------------------------------------------------------------------------
//* Function Name       : check_sector_erased
//* Object              : check if sector is erased. If not erase it.
//*
//* Input Parameters    : <sector_addr> base sector address
//*                       <size> sector size in byte
//*                       <sector_id> sector ID
//*
//* Output Parameters   : If data sector erase TRUE, else FALSE
//*--------------------------------------------------------------------------------------
int check_sector_erased ( flash_word *sector_addr, int size, int sector_id )
//* Begin
{
    int     i ;
    flash_word  read_data ;

    //* For each word of the sector
    for ( i = 0 ; i < (size/2) ; i ++ )
    {
        //* Check erased value reading, if not
        if (( read_data = *(sector_addr + i)) != (flash_word)0xFFFF )
        {
            printf ( "Sector %d not erased !\n", sector_id ) ;
            printf ( "Sector %d, Address 0x%08x, Value 0x%08x\n",
                     sector_id, (int)(sector_addr + i), read_data ) ;
            return ( FALSE ) ;
        }
        //* Endif
    }
    //* Endfor

    //* Display Sector Erased
    printf ( "Sector %d erased !\n", sector_id ) ;

    //* Return True
    return ( TRUE ) ;
}
//* End

//*--------------------------------------------------------------------------------------
//* Function Name       : erase_sector
//* Object              : check if sector is erased if not erase
//* Input Parameters    : <base_addr> Flash base address

⌨️ 快捷键说明

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