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

📄 flashsemihosting.c

📁 使用arm7芯片
💻 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               : FlashSemiHosting.c
//* Object                  : FLASH programmer for SAM7 product
//*
//* 1.0 01/Jul/04 JPP       : Creation
//*--------------------------------------------------------------------------------------

/* Include Standard c Libraries to allow stand alone compiling and operation */
#include <stdio.h>
#include <stdlib.h>

#include "flash.h"
#include "Board.h"

/* Target Identification */
#define TARGET_ID           "SAM7 "


unsigned int RAMSectorData[FLASH_PAGE_SIZE_LONG];


// #define TEST_UNI

//*--------------------------------------------------------------------------------------
//* Function Name       : flash_identify
//* Object              : Read the flash Version
//*--------------------------------------------------------------------------------------
int flash_identify ( void )
{
#ifndef TEST_UNI
    if (*AT91C_MC_FVR == AT91C_MC_FLASH_VERSION) return true;
    else return false ;
#else
return true;
#endif
}

//*----------------------------------------------------------------------------
//* \fn    AT91F_Flash_Write
//* \brief Write in one Flash page located in AT91C_IFLASH,  size in 32 bits
//*----------------------------------------------------------------------------
int AT91F_Flash_Write( unsigned int Flash_Address ,int size ,unsigned int * buff)
{
    //* set the Flasc controler base address
    AT91PS_MC ptMC = AT91C_BASE_MC;
    unsigned int i, page;
    unsigned int * Flash;
    //* init flash pointer
        Flash = (unsigned int *) Flash_Address;
    //* Get the Flasg page number
        page = ((Flash_Address - (unsigned int)AT91C_IFLASH) /FLASH_PAGE_SIZE_LONG);
    //* copy the new value
	for (i=0; (i < FLASH_PAGE_SIZE_LONG) & (size > 0) ;i++, Flash++,buff++,size-- ){
	//* copy the flash to the write buffer ensure that code generation
	    *Flash=*buff;
	}
    //* Write the Errase_All command
        ptMC->MC_FCR = AT91C_MC_CORRECT_KEY | AT91C_MC_FCMD_START_PROG | (AT91C_MC_PAGEN & (page <<8)) ;
    //* Wait the and of command
        while ( (ptMC->MC_FSR & AT91C_MC_FRDY) != 0) {};
    //* Check the result
        if ( (ptMC->MC_FSR & ( AT91C_MC_PROGE | AT91C_MC_LOCKE ))!=0) return false;
  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, unsigned int Address )
{

    int         wordCount ;
    int         sector_id;
    unsigned int numWordsRead;
    unsigned int *flashData ;

    numWordsRead = 1;

    while (numWordsRead !=0) {
        // Display  sector to program
        //* Get the Flasg page number
#ifndef TEST_UNI
        sector_id =  ((Address - (unsigned int)AT91C_IFLASH) /FLASH_PAGE_SIZE_LONG);
#else
        sector_id =  ((Address - (unsigned int)0x10000) /FLASH_PAGE_SIZE_LONG);

#endif
        // read a whole sector from the file into RAM
        numWordsRead = fread ( RAMSectorData, 4, FLASH_PAGE_SIZE_LONG, image );
        if (numWordsRead != 0)
        {
            printf ( "Programming Sector %d\n", sector_id) ;
            if ( ! AT91F_Flash_Write ( Address, numWordsRead, RAMSectorData ) ) {
                printf ( "Programming Sector %d Error\n", sector_id ) ;
                return false ;
            }
            flashData =(unsigned int *)Address ;
            for (wordCount = 0; wordCount < numWordsRead; wordCount ++) {
                if ( RAMSectorData[wordCount] != flashData[wordCount] ) {
                  printf ( "Verify Sector %d Error\n", sector_id ) ;
                  return false;
                }
            }// End for
	    } // End If
       Address+= numWordsRead ;
    } // End while
    return (true ) ;
}

//*--------------------------------------------------------------------------------------
//* Function Name       : main
//* Object              : Get parameter
//* Input Parameters    : none
//* Output Parameters   : none
//*--------------------------------------------------------------------------------------
int main ( int argc, char *argv[] )
{
    unsigned int    load_addr ;
    FILE            *image ;

    char name[30];

        printf ("\n**** %s Flash Programming Utility ****\n", TARGET_ID ) ;
        printf("\n**** This Utility work for external SAM7 board  !\n");
        /* Set load address */
        load_addr = 0x00100000;

   //* Set Flash Waite sate
	//  Single Cycle Access at Up to 30 MHz, or 40
	//  if MCK = 47923200 I have 50 Cycle for 1 useconde  flied MC_FMR->FMCN
#ifndef TEST_UNI
	AT91C_BASE_MC->MC_FMR = ((AT91C_MC_FMCN)&(50 <<16)) | AT91C_MC_FWS_1FWS ;
        printf("\n**** Please Enter return for start !\n");
        gets(name);
        printf("\n**** File to download at 0x%x**** \n",load_addr);
        /* Get File */
        gets(name);
#else
        sprintf (name,"c:/tmp/m.txt");
        load_addr = 0x00010000;
#endif

        //* 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 ( true ) ;
        }
        //* 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_identify( ) )
        {  // Display Error and exit
            printf ( "Error - The Flash device is not recognized\n" ) ;
            return ( false ) ;
        }
        else
        {
            // If File Download into flash is not OK
            if ( ! download_file_to_flash ( image, load_addr ) )
            {   // 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 + -