📄 flash.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 : flash.c
//* Object : FLASH programmer for :
//* AT91SAM7S32
//* AT91SAM7S64
//*
//* 1.0 29/Nov/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 "AT91SAM7S64"
unsigned int numWordsRead;
unsigned int RAMSectorData[FLASH_PAGE_SIZE_LONG];
//*----------------------------------------------------------------------------
//* \fn AT91F_Flash_Unlock
//* \brief Clear the Non Volatile Memory Bits and set at 1 FSR bit=0
//* \input page number (0-1023) a same region have some page (16)
//* \output Region
//*----------------------------------------------------------------------------
__inline int AT91F_Flash_Unlock(unsigned int Flash_Lock_Page)
{
//* Write the Errase All command
AT91C_BASE_MC->MC_FCR = AT91C_MC_CORRECT_KEY | AT91C_MC_FCMD_UNLOCK | (AT91C_MC_PAGEN & (Flash_Lock_Page << 8) ) ;
//* Wait the and of command
while ((AT91C_BASE_MC->MC_FSR & AT91C_MC_FRDY) != AT91C_MC_FRDY ) {};
return (true);
}
//*----------------------------------------------------------------------------
//* \fn AT91F_Flash_Write
//* \brief Write in one Flash page located in AT91C_IFLASH, size in 32 bits
//* \input Flash_Address: start at 0x0010 0000 size: in byte
//*----------------------------------------------------------------------------
int AT91F_Flash_Write( unsigned int Flash_Address ,int size ,unsigned int * buff)
{
//* set the Flasc controler base address
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_BYTE);
//* copy the new value
for (i=0; (i < FLASH_PAGE_SIZE_BYTE) & (size > 0) ;i++, Flash++,buff++,size-=4 ){
//* copy the flash to the write buffer ensure that code generation
*Flash=*buff;
}
//* Write the Errase_All command
AT91C_BASE_MC->MC_FCR = AT91C_MC_CORRECT_KEY | AT91C_MC_FCMD_START_PROG | (AT91C_MC_PAGEN & (page <<8)) ;
//* Wait the end of command
while ((AT91C_BASE_MC->MC_FSR & AT91C_MC_FRDY) != AT91C_MC_FRDY ) {};
//* Check the result
if ( (AT91C_BASE_MC->MC_FSR & ( AT91C_MC_PROGE | AT91C_MC_LOCKE ))!=0) return false;
return true;
}
//*----------------------------------------------------------------------------
//* \fn AT91F_Flash_Write
//* \brief Write in one Flash page located in AT91C_IFLASH, size in byte
//* \input Start address (base=AT91C_IFLASH) size (in byte ) and buff address
//*----------------------------------------------------------------------------
__inline int AT91F_Flash_Write_all( unsigned int Flash_Address ,int size ,unsigned int * buff)
{
int next, status;
unsigned int dest;
unsigned int * src;
dest = Flash_Address;
src = buff;
status = true;
while( (status == true) & (size > 0) )
{
//* Check the size
if (size <= FLASH_PAGE_SIZE_BYTE) next = size;
else next = FLASH_PAGE_SIZE_BYTE;
//* Unlock current sector base address - curent address by sector size
AT91F_Flash_Unlock((dest - (unsigned int)AT91C_IFLASH ) /FLASH_PAGE_SIZE_BYTE);
//* Write page and get status
status = AT91F_Flash_Write( dest ,next ,src);
// * get next page param
size -= next;
src += FLASH_PAGE_SIZE_BYTE/4;
dest += FLASH_PAGE_SIZE_BYTE;
}
return status;
}
//*--------------------------------------------------------------------------------------
//* 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 page;
unsigned int *flashData ;
numWordsRead = 1;
while (numWordsRead !=0) {
// Display page to program
//* Get the Flasg page number
#ifdef DEBUG
printf ("Read\n") ;
#endif
page = (Address - (unsigned int)AT91C_IFLASH ) /FLASH_PAGE_SIZE_BYTE;
// read a whole page from the file into RAM
numWordsRead = fread ( RAMSectorData, 4, FLASH_PAGE_SIZE_LONG, image );
if (numWordsRead != 0)
{
printf ( "Page %d\n", page) ;
if ( ! AT91F_Flash_Write_all ( Address, numWordsRead*4, RAMSectorData ) ) {
printf ( "Page %d Error\n", page ) ;
return false ;
}
flashData =(unsigned int *)Address ;
for (wordCount = 0; wordCount < numWordsRead; wordCount ++) {
if ( RAMSectorData[wordCount] != flashData[wordCount] ) {
printf ( "Verify page %d Error\n", page ) ;
return false;
}
}// End for
} // End If
Address+= numWordsRead*4 ;
} // End while
return (true ) ;
}
//*--------------------------------------------------------------------------------------
//* Function Name : main
//* Object : Get parameter
//* Input Parameters : none
//* Output Parameters : none
//*--------------------------------------------------------------------------------------
int main ( void )
{
unsigned int load_addr ;
FILE *image ;
#ifdef DEBUG
char str[30];
#endif
char name[256];
char *name1 ;
unsigned int *load,i ;
load =(unsigned int*) *((unsigned int*)0x00200000);
name1=(char *) 0x00200004 ;
//* Display Flash Downloader Header
printf ( "\n%s Utility\n", TARGET_ID ) ;
printf("Load address: 0x%08X\n",(int) load);
/* Get load address */
#ifdef DEBUG
gets(str);
sscanf(str,"0x%x",&load);
/* Affect Load Address */
if ( load == 0) load =(unsigned int*) *((unsigned int*)0x40);
#endif
load_addr =(unsigned int) load;
printf("\nFile: %s \n",name1);
printf("Enter or new file name");
/* Get File */
gets(name);
if (name[0]==0){
for ( i=0; i < sizeof(name);i++)
{
if( (name1[i]==0x20) | (name1[i]==0x0a) | (name1[i]==0x0d))
name[i]=0;
else
name[i]=name1[i];
};
}
//* Display Flash Downloader Header
printf ( "\nProgramming %s: 0x%08X\n", name,(int) load_addr) ;
//* If Error while opening the external Flash Image file
if (( image = fopen ( name, "rb" )) == NULL )
{ //* Display Error then exit
printf ( "Error Cannot open\"%s\"\n", name ) ;
return ( true ) ;
}
//* Else
#ifdef DEBUG
else
{ //* Display input file name
printf ( "Input file: %s\n", name ) ;
}
//* Display Load Address
printf ( "Load address: %x\n", (int)load_addr ) ;
#endif
// If FLASH Device is not recognized
if ( ! download_file_to_flash ( image, load_addr ) )
{ // Display Error and exit
printf ( "Error programming Flash\n" ) ;
return ( false ) ;
}
/* Close the external file and exit the program */
fclose ( image ) ;
// Display Flash written and exit
printf ( "Written and verified\n" ) ;
return ( true ) ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -