www.pudn.com > flash.rar > FlashDev.c


/**************************************************************************** 
* MODULE:       FLASH 
* 
* --------------------------------------------------------------------------- 
* HISTORY: 
* 
* --------------------------------- ------------------------------------------ 
* (c) XJGC HVDC 2008			huhuan 
* 
* Copying of this document and giving it to others and the use or 
* communication of the contents thereof are forbidden without express 
* authority. Offenders are liable to the payment of damages. All rights 
* are reserved in the event of the grant of patent or the registration 
* of a utility model or design. 
****************************************************************************/ 
 
#include"flashdev.h" 
 
#define	flash_word_prog_ram MARRAY(unsigned long,0xE00000) 
#define	flash_sector_erase_ram MARRAY(unsigned long,0xE00400) 
 
#define FLASH_SECTOR_ERASE_SIZE ((0x92 - 0x6e + 40) / 4) 
#define FLASH_WORD_PROG_SIZE ((0xc8-0x6e + 40)/4) 
 
//参数存储的首地址,这部分地址空间应该受保护,不能被改写 
#define FLASH_FOR_PARAM        0xC20000 
 
//UBYTE flash_buffer[0xa0];	 //flash读写缓冲器 
UBYTE flash_buffer[sizeof(SU200_PARAM)]; 
SU200_PARAM  su200_param_test; 
 
 
 
 
void disable_int(void) 
{ 
	 PSW_IEN        =  0;   
} 
 
 
void enable_int(void) 
{ 
	 PSW_IEN        =  1;   
} 
 
/* 
 *  Program Block in Flash Memory 
 *    Parameter:      adr:  Block Address 
 *                    buf:  Block Data 
 *    Return Value:   0 - OK,  1 - Failed 
 */ 
 
int ProgramBlock (unsigned long adr, void far *buf) { 
  	unsigned int cnt; 
  
  // Enter Page Mode 
  	HVAR(unsigned short, 0xC10000 | 0xAA) = 0x50; 
  	HVAR(unsigned short, adr) = 0xAA; 
 
  // Load Page 
  	for (cnt = 0; cnt < 64; cnt++)  { 
    		HVAR(unsigned short, 0xC10000 | 0xF2) = *((unsigned short near *) buf); 
   		buf = (unsigned short near *) buf+1; 
  } 
 
  // Write Page 
  	HVAR(unsigned short, 0xC10000 | 0xAA) = 0xA0;  
  	HVAR(unsigned short, 0xC10000 | 0x5A) = 0xAA; 
 
  	return (1);                            // Check until Device Ready 
} 
 
 
/* 
 *  Erase Block in Flash Memory 
 *    Parameter:      adr:  Block Address 
 *    Return Value:   0 - OK,  1 - Failed 
 */ 
 
int EraseBlock (unsigned long adr) { 
  	HVAR(unsigned short, 0xC10000 | 0xAA) = 0x80;    // Erase Sector (1. Cycle) 
  	HVAR(unsigned short, 0xC10000 | 0x54) = 0xAA;    // Erase Sector (2. Cycle) 
  	HVAR(unsigned short, adr)         = 0x33;    // Erase Sector (3. Cycle) 
  	return (1);                            // Check until Device Ready 
} 
 
 
 //将flash空间中的代码拷贝到指定的ram空间 
 
BOOL8 FLASH_memcpy ( unsigned long far *dest, unsigned long far *src, UINT num_words) 
{ 
   	UINT i; 
    	BOOL8 result = TRUE; 
		 
	/* Various checks: Even source and dest address, non-zero word-count */ 
	if (num_words == 0)  return 1; 
 
	disable_int (); 
 
    	for (i=0; i