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

📄 flash111.c

📁 这是基于ARM平台的一个FLASH操作的一个例子
💻 C
字号:

#include <c:\arm\include\stdlib.h>
#include <c:\arm\include\string.h>
#include <c:\arm\include\stdio.h>
#include "flash.h"

//* function declare
int main(int argc, char * argv [ ]);
void cpu_init(void);
void help(void);
void dly(void);
int identify(void);
int erase_chip(void);
int erase_chk(void);
int Bsy2Rdy(void);
int download_file(FILE * file);
int write_flash(flash_word *addr, flash_word data);
int flash_comp_file(FILE * file);

/**********************************************************
 *				User define options							    *
 **********************************************************/
#define DEV_NAME 		"SST39VF400A"
#define DEV_SIZE  		(512*1024)			//unit: Byte
#define DEV_MAN_CODE 	0x00bf
#define DEV_ID_CODE		0x2780
#define SEC_NUM               128					//sector number
#define SEC_SIZE               (DEV_SIZE/SEC_NUM)	//=4KB
#define START_ADDRESS   0					//download flash start address
#define END_ADDRESS       (START_ADDRESS + (DEV_SIZE >> 1))
//** end of usr defination **


int main (int argc, char  *argv[])
{
    
  //  FILE     *image ;
   // dly();
    dly();
	
    //cpu_init ();
    
    printf ("*****    Flash Programming Utility   *****\n");
   //  dly();
     dly();
    printf ("\n > target on ARM development board\n");
   //  dly();
     
     dly();
   
}

/*   
void cpu_init(void)			//initialize cpu
{
    rWTCON = 0x0;				//disable watchdog
    rINTMSK = 0x07ffffff;		//disable all interrupt

    rLOCKTIME = 0xfff;
    rPLLCON = ((0x34<<12)+(0x3<<4)+0x1);	//Fin=10MHz; Fout=60MHz
    rCLKCON = 0x7ff8;						//all unit block clk on

    rPCONA = 0x3ff;			//config GPIOA as Addr. bus
    rPCONB = 0x7ff;			//config GPIOB as mem controller interface
    rPCONC = 0xaaaaaaaa;		//config GPIOC as data bus high bits

    mLEDSET = 0x0;			//disable LED light
}
*/
void dly(void)
    {
    unsigned int i, j;  
    for(i=0; i<200; i++)				//wait a long period to handle chip erase opration
    	{
         for(j=0; j<10000; j++);
        		
    	}
    }
void help(void)
{
    printf ( "\n" ) ;
    printf ( ": Argument error - enter a correct binary file please.\n" ) ;
}


int identify(void)				//check and identify exist flash type
{
    flash_word   ManCode, DevCode;
    unsigned int i;

    //enter software query mode
    FLASH_SEQ_ADD1 = FLASH_CODE1;
    for(i = 0; i < 2; i++);
    FLASH_SEQ_ADD2 = FLASH_CODE2;
    FLASH_SEQ_ADD1 = ID_IN_CODE;
    for(i = 0; i < 2; i++);				// wait tIDA
    //read infor
    ManCode = *(flash_word *)0x00;		// address 0x00 << 1
    DevCode = *(flash_word *)0x02;		// address 0x01 << 1
    //exit software query mode
    FLASH_SEQ_ADD1 = FLASH_CODE1;
    for(i = 0; i < 2; i++);
    FLASH_SEQ_ADD2 = FLASH_CODE2;
    FLASH_SEQ_ADD1 = ID_OUT_CODE;

    if((ManCode == DEV_MAN_CODE) && (DevCode == DEV_ID_CODE))
    	{
         return (TRUE);
    	}
    else
    	{
    	 return (FALSE);
    	}
}


//* use chip-erase operation
int erase_chip(void)
{
    unsigned int i, j;
     
    FLASH_SEQ_ADD1 = FLASH_CODE1;
    for(i = 0; i < 2; i++);
    FLASH_SEQ_ADD2 = FLASH_CODE2;
    FLASH_SEQ_ADD1 = FLASH_READY;	//--|
    for(i=0;i<2;i++);					//  ensure no optimization will be done by compiler
    FLASH_SEQ_ADD1 = FLASH_CODE1;	//--|
    FLASH_SEQ_ADD2 = FLASH_CODE2;
    for(i = 0; i < 2; i++);
    FLASH_SEQ_ADD1 = ERASE_CHIP_CODE;
    
    printf(" : .\n");

    for(i=0; i<18; i++)				//wait a long period to handle chip erase opration
    	{
         for(j=0; j<TIME_OUT / 100; j++);
         printf ("   .\n");				//display processing information
    	}

    printf ("\n");

    return(Bsy2Rdy());
}


//* wait chip exit from busy status to ready status: use polling toggle bit method
int Bsy2Rdy(void)
{
    flash_word   TestData1, TestData2;
    unsigned int i;
    
    for (i = 0; i < TIME_OUT; i++)
    	{
         TestData1 = *((flash_word *) 0x12) & 0x0040;		//read address 0x12 ~ get bit Q[6], 0x12 is a random address
         TestData2 = *((flash_word *) 0x12) & 0x0040;
         if(TestData1 == TestData2)
         	return(TRUE);
    	}
    return (FALSE);
}


//* check whether the whole chip is earsed to 0xffff
int erase_chk(void)
{
    flash_word  * chk_addr = (flash_word *) START_ADDRESS;
    flash_word  * end_addr = (flash_word *) END_ADDRESS; 
   
    for(;chk_addr <= end_addr; chk_addr++)
    	{
         if(*chk_addr != (flash_word)0xffff)
		return(FALSE);
    	}
    return(TRUE);
}


//* write specified file to flash memory
int download_file(FILE * file)
{
    flash_word  *  pgm_addr = (flash_word *) START_ADDRESS;
    flash_word  *  end_addr  = (flash_word *) END_ADDRESS; 
    unsigned short	data;
    unsigned short	count = 0;

    printf(" :  .\n");
    
    fseek (file, 0, 0);				//locats the point to the beginning of a file

    while (fread(&data,1,2,file) == 2)	//read 2 bytes from file
    	{
	  if(pgm_addr > end_addr)
	  	{
	  	  printf ("\n ! Program error - flash size overflow.\n");
	  	  return(FALSE);
	  	}	  
    	  if(write_flash(pgm_addr, data) == FALSE)		//flash word(16-bit) program
    	  	{
 		  printf(" \n ! Program error - can't write flash at 0x%x.\n", (int)pgm_addr);
 		  return(FALSE);
    	  	}
    	  
    	  pgm_addr ++;				//point to next flash address
    	  count += 2;

    	  if(count == SEC_SIZE)		//a sector is programmed 
    	  	{
		  count = 0;
		  printf ("    .\n");
    	  	}
    	}
    
    return (TRUE);
}


//*wirte a 16-bit word to flash
int write_flash (flash_word * addr, flash_word data)
{
    unsigned i;
    //make flash into word programming mode
    FLASH_SEQ_ADD1 = FLASH_CODE1;
    for(i = 0; i < 2; i++);
    FLASH_SEQ_ADD2 = FLASH_CODE2;
    FLASH_SEQ_ADD1 = WRITE_CODE;
    
    *(addr) = data;			//programming
    
    return(Bsy2Rdy());
}


//*check programmed flash with source file
int flash_comp_file(FILE * file)
{
    flash_word * rd_addr;			//rom address
    unsigned short data;

    rd_addr = (flash_word *)START_ADDRESS;

    fseek (file, 0, 0);				//locats the point to the beginning of a file

    // For each word read from the file
    while ( fread (&data, 1, 2, file) == 2 )
    {
        // If data from file is different from data in flash
        if ( *rd_addr != data )
        {
            return ( FALSE ) ;
        }
        rd_addr++;					//point to next flash address
    }

    return(TRUE);
}

⌨️ 快捷键说明

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