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

📄 flash.c

📁 This an example of how to install and use interrupts on AT91M4800A-embedded systems when building an
💻 C
📖 第 1 页 / 共 2 页
字号:
    // Enter Programming code sequence
    *(base_addr + FLASH_SEQ_ADD1) = FLASH_CODE1 ;
    *(base_addr + FLASH_SEQ_ADD2) = FLASH_CODE2 ;
    *(base_addr + FLASH_SEQ_ADD1) = WRITE_CODE ;
    *load_addr = data ;

    // Wait for Flash ready after erasing, if timeout

    // While two consecutive read don't give same value or timeout
    for (i = 0; (i< TIME_OUT) && ( *load_addr != data ); i++) {};

    // If Data written does not equal data
    read_data = *load_addr;
    if (( read_data != data ) || ( i == TIME_OUT )) {
        // Display Error and return False
        printf ( "Program Error\n" ) ;
        printf ( "Address 0x%08x / Data 0x%04x / 0x%04x \n", (int)load_addr, data, read_data ) ;
        return ( FALSE );
    }
    else {
        return ( TRUE ) ;
    }
}


//*--------------------------------------------------------------------------------------
//* Function Name       : download_file_to_flash
//* Object              : Read data from file and write it into flash memory
//* Input Parameters    : <addr_base> base flash address
//*                       <addr_load> address to load
//*
//* Output Parameters   : TRUE or FALSE
//*--------------------------------------------------------------------------------------
int download_file_to_flash ( FILE *image, const FlashAt49BVDef *flash, flash_word *addr_base, flash_word *addr_load )
{
    flash_word  *addr_prg_sector = addr_base ;
    int         block = 0 ;
    int         nb_sector = 0 ;
    int         sector_id = 0 ;
    int         sector_found = FALSE ;
    int         dataCount = 0;
    int         kbyteCount = 0;
    int         happyProgramming = TRUE;

    unsigned short  *RAMDataPtr ;
    flash_word      *flashDataPtr;

    //int sectorNum;
    unsigned int sectorSize;
    unsigned int numWordsRead;
    unsigned int wordCount;

    // Find the starting flash sector
    sector_found = FALSE ;
    while ( sector_found == FALSE ) {
        // compute the end address + 1 of the current sector
        addr_prg_sector += (flash->flash_org[block].sector_size/2) ;
        // If program address lower than this, we start programming in this sector
        if (addr_prg_sector > addr_load ) {
            // Display First sector to program
            printf ( "First Sector to program:\t\t%d \n", sector_id ) ;
            // If the first address to program is within a sector
            if ( addr_load > addr_prg_sector ) {
                // Display Warning : first address within a sector
                printf ( "The first address to program is within a sector.\n" ) ;
                printf ( "First Data of the sector will be lost!\n" ) ;
            }
            sector_found = TRUE ;
            break; // leave the while loop
        }
        else {
            // Increment the Sector Identifier
            sector_id ++ ;
            // Increment the sector number
            nb_sector++ ;
            // If last sector in block tested
            if ( nb_sector >= flash->flash_org[block].sector_number ) {
                // Re-initialize sector number in block
                nb_sector = 0 ;
                // Increment block number
                block ++ ;
                // If last block tested
                if ( block >= flash->flash_block_nb ) {
                    // Display Error and Return False
                    printf ( "Error : Programming address is not within the Flash device's address space\n" ) ;
                    happyProgramming = FALSE;
                    break; // leave the while loop
                }
            } //if ( nb_sector...
        } //if (( addr_prg_sector
    } //while ( sector_found


    /* Loop over each sector within each block, erase it, then program it */
    while (happyProgramming == TRUE) {
        // Erase the current Sector
        printf ( "Programming Sector %d\n", sector_id ) ;
        if ( erase_sector( addr_base, addr_load, flash->flash_org[block].sector_size, sector_id ) != TRUE ) {
            happyProgramming = FALSE;
            break; // while (happyProgramming...
        }

        // read a whole sector from the file into RAM
        sectorSize = flash->flash_org[block].sector_size;
        numWordsRead = fread ( RAMSectorData, 2, sectorSize/2, image );

        RAMDataPtr = RAMSectorData;
        flashDataPtr = addr_load ;
        for (wordCount = 0; wordCount < numWordsRead; wordCount ++) {
            if ( write_flash ( addr_base, flashDataPtr, *RAMDataPtr ) != TRUE ) {
                happyProgramming = FALSE;
                break;
            }
            flashDataPtr++;
            RAMDataPtr++;
        }

        /* Verify the flash sector we just wrote with the RAM data */
        RAMDataPtr = RAMSectorData;
        flashDataPtr = addr_load ;
        for (wordCount = 0; wordCount < numWordsRead; wordCount ++) {
            // print a dot for the user
            dataCount += 2;
            if (dataCount%1024 == 0) {
                kbyteCount++;
                //printf("%d kbytes\n", kbyteCount);
                printf(".");
            }
            if (*RAMDataPtr++ != *flashDataPtr++) {
                happyProgramming = FALSE;
                break; // leave the while loop
            }
        }
        addr_load = flashDataPtr;
        printf("\n");

        if (numWordsRead < sectorSize/2) {
            // There is no more data in the file
            break; // leave the while loop
        }

        // Increment the Sector Identifier
        sector_id ++ ;
        // Increment the sector number
        nb_sector++ ;
        // If last sector in block tested
        if ( nb_sector >= flash->flash_org[block].sector_number ) {
            // Re-initialize sector number in block
            nb_sector = 0 ;
            // Increment block number
            block ++ ;
            // If last block tested
            if ( block >= flash->flash_block_nb ) {
                // Display Error and Return False
                printf ( "Error : Binary file too large for flash device\n" ) ;
                happyProgramming = FALSE;
                break; // leave the while loop
            }
        } //if ( nb_sector...

    } // while (happyProgramm...

    return ( happyProgramming ) ;
}


//*--------------------------------------------------------------------------------------
//* Function Name       : main
//* Object              : Get parameter
//* Input Parameters    : none
//* Output Parameters   : none
//*--------------------------------------------------------------------------------------
int main ( int argc, char *argv[] )
{
    flash_word      *base_addr ;
    flash_word      *load_addr ;
    FILE            *image ;
    const FlashAt49BVDef  *flash ;
    char str[30];
    char name[256];

        //* Display Flash Downloader Header
        //* Display Flash Downloader Header
        printf ( "\n**** %s Flash Programming Utility ****\n", TARGET_ID ) ;
        printf ( "**** Advise load at 0x01100000  with STD selection ****\n") ;
        printf("\n**** Load address (0x01100000) ****\n");

        /* Get load address */
        gets(str);
        sscanf(str,"0x%x",&load_addr);

        printf("\n**** File to download ****\n");
        /* Get File */
        gets(name);

        //* If Error while opening the external Flash Image file
        if (( image = fopen ( name, "rb" )) == NULL )
        {
            //* Display Error then exit
            printf ( "Error - Cannot open file image \"%s\"\n", name ) ;
            return ( FALSE ) ;
        }
        //* Else
        else
        {
            //* Display input file name
            printf ( "Input file is  :   %s \n", name ) ;
        }

        //* Display Load Address
        printf ( "Load address is:   %x \n", (int)load_addr ) ;

        // If FLASH Device is not recognized
        if (( flash = flash_identify( load_addr )) == (const FlashAt49BVDef *)0 )
        {
            // Display Error and exit
            printf ( "Error - The Flash device is not recognized\n" ) ;
            return ( FALSE ) ;
        }
        else
        {
            // Initialize Flash Base Address
            base_addr = (flash_word *) ((int)load_addr & ~(flash->flash_mask)) ;
            // Display Flash Base Address
            printf ( "Base address is:   %x \n", (int)base_addr ) ;

            // Init PIO MCKO/P25 as busy ready input signal
            init_flash_ready () ;

            // If File Download into flash is not OK
            if ( download_file_to_flash ( image, flash, base_addr, load_addr ) != TRUE )
            {
                // Display Error and exit
                printf ( "Error while programming Flash\n" ) ;
                return ( FALSE ) ;
            }
        }

    /* Close the external file and exit the program */
    fclose ( image ) ;

    // Display Flash written and exit
    printf ( "Flash written and verified\n" ) ;
    return ( TRUE ) ;
}

⌨️ 快捷键说明

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