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

📄 flashdev.c

📁 flash 的读写操作,对于目前主流的flash开发都有启发!
💻 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<num_words; i++)
        	*dest++=*src++;

	enable_int ();

    	return ( result);
}


// 将在FLASH中存在的flash的操作程序拷贝到ram空间中的相应数组中。
void FLASH_init ( void)
{  
   	FLASH_memcpy( flash_sector_erase_ram, (unsigned long far *)EraseBlock, FLASH_SECTOR_ERASE_SIZE);

   	FLASH_memcpy( flash_word_prog_ram, (unsigned long far *)ProgramBlock, FLASH_WORD_PROG_SIZE);	
}

//在调用本程序的时后,程序跳转到ram空间中的代码;
//	int a; 
//	unsigned char b;
void  EraseBlock_ram (unsigned long adr)

{

	adr=adr;
	
	__asm  {
		CALLS 0xE0, 0x0400	
			}
			
}

//在调用本程序的时后,程序跳转到ram空间中的代码;

int ProgramBlock_ram (unsigned long adr, void far *buf)
{
	adr=adr;
	buf=buf;
	__asm  {
		CALLS 0xE0, 0x0000
			}
}



 //将参数写入以FLASH_FOR_PARAM开始的flash空间。
	
void flash_write_par_into_flash(void)
{
	flash_test() ;
	memcpy(flash_buffer,&su200_param,sizeof(SU200_PARAM));
	EraseBlock (FLASH_FOR_PARAM);
//	ProgramBlock (FLASH_FOR_PARAM, flash_buffer);
//	ProgramBlock (FLASH_FOR_PARAM+128, flash_buffer+128);	
	ProgramBlock (FLASH_FOR_PARAM, (UBYTE *)(&su200_param));
	ProgramBlock (FLASH_FOR_PARAM+128,(UBYTE *)(&su200_param+128));	
	
	
	}



 //测试程序
void flash_test(void)
{
	unsigned char i;
	UBYTE *p;
	p=(UBYTE *)(&su200_param);
	for(i=0;i<0xa0;i++)
	{
		*p=i;
		p++;
//	flash_buffer[i]=i;
	}	
}


//将flash空间中的数据复制到ram空间中的su200_param

void flash_read_par_into_ram(void)
{
	 memcpy((UBYTE volatile *)(&su200_param_test),(UBYTE volatile *)FLASH_FOR_PARAM,sizeof(SU200_PARAM));
}



⌨️ 快捷键说明

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