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

📄 flash.c

📁 28F128J系列FLASH驱动
💻 C
字号:
/*  ----------------------------------------------------------------------
                                                           
              Filename  	:  	flash.c
	      			Version   	:  	1.0  
              Designed By :		
              Time				:		2004.10.25          
   
   -----------------------------------------------------------------------*/
#include "vxWorks.h"
#include "config.h"

#include "sysdata.h"
#include "Flash.h"
#include "user_ext.h"


/***********************************************************/
/***********************************************************/
void 	FlashInit();
void 	FlashEraseAllBlock(void);
void 	FlashEraseBlock(unsigned char BlockNum);
void 	FlashLockBlock(unsigned char BlockNum);
void 	FlashReadStatus(unsigned char BlockNum);
void 	FlashUnlockBlock(unsigned char BlockNum);
void 	FlashWriteShort(unsigned char BlockNum,unsigned int Addr,unsigned short data);
unsigned short 	FlashReadShort (unsigned char BlockNum,unsigned int Addr);


/***********************************************************/
/***********************************************************/
void FlashInit()
{
		unsigned char i;
		
		for (i=0;i<BLOCK_NUM;i++)		FlashUnlockBlock(i);
}


/***********************************************************/
/***********************************************************/
void FlashUnlockBlock(unsigned char BlockNum)
{   
		if (BlockNum<BLOCK_NUM)
		{  
						*(unsigned short *)(FLASH_16M_POS+BlockNum*BLOCK_SIZE)=SET_BLOCK;			/*w	SET_BLOCK  			0x6060;		FLASH_16M_POS 	0x90000000*/
						*(unsigned short *)(FLASH_16M_POS+BlockNum*BLOCK_SIZE)=CONFIRM_ERASE;	/*w	CONFIRM_ERASE		0xd0d0;		BLOCK_SIZE  		0x40000*/
						
						FlashReadStatus(BlockNum);  
		}
}


/***********************************************************/
/***********************************************************/
void FlashReadStatus(unsigned char BlockNum)
{     
		unsigned short j;
		
		if (BlockNum<BLOCK_NUM)
		{ 
						*(unsigned short *)(FLASH_16M_POS+BlockNum*BLOCK_SIZE)=READ_STATUS;		/*READ_STATUS		0x7070*/
						j=*(unsigned short *)(FLASH_16M_POS+BlockNum*BLOCK_SIZE);
						
						while ((j&WSM_READY)!=WSM_READY)				/*WSM_READY	0x0080*/
						{ 
										j=*(unsigned short *)(FLASH_16M_POS+BlockNum*BLOCK_SIZE);     
						}
						
						*(unsigned short *)(FLASH_16M_POS+BlockNum*BLOCK_SIZE)=READ_ARRAY;		/*READ_ARRAY	0xffff*/
		}  
}


/***********************************************************/
/***********************************************************/
void FlashEraseBlock(unsigned char BlockNum)
{
		unsigned short j;
		
		if (BlockNum<BLOCK_NUM)
		{
						*(unsigned short *)(FLASH_16M_POS+BlockNum*BLOCK_SIZE)=SETUP_ERASE;			/*SETUP_ERASE		0x2020*/
						*(unsigned short *)(FLASH_16M_POS+BlockNum*BLOCK_SIZE)=CONFIRM_ERASE;		/*CONFIRM_ERASE		0xd0d0*/
						FlashReadStatus(BlockNum);
		}
}


/***********************************************************/
/***********************************************************/                                                 
void FlashWriteShort(unsigned char BlockNum,unsigned int Addr,unsigned short data)  /*一次写2个字节 short */
{     
		if (BlockNum<BLOCK_NUM&&(2*Addr<BLOCK_SIZE))
		{ 
						*(unsigned short *)(FLASH_16M_POS+BlockNum*BLOCK_SIZE)=SETUP_WRITE;       /*w	SETUP_WRITE	0x4040*/      
						*(unsigned short *)(FLASH_16M_POS+BlockNum*BLOCK_SIZE+Addr*2)=data;
						FlashReadStatus(BlockNum);
		}
}


/***********************************************************/
/***********************************************************/
void FlashEraseAllBlock(void)
{  
		unsigned char i;
		unsigned short j;
		
		for (i=0;i<BLOCK_NUM;i++)
		{  
						*(unsigned short *)(FLASH_16M_POS+i*BLOCK_SIZE)=SETUP_ERASE;			/*SETUP_ERASE		0x2020*/
						*(unsigned short *)(FLASH_16M_POS+i*BLOCK_SIZE)=CONFIRM_ERASE;		/*CONFIRM_ERASE		0xd0d0*/
						FlashReadStatus(i);
		}
		
		printf("Erase All Block Ok\n");      
}


/***********************************************************/
/***********************************************************/
void FlashLockBlock(unsigned char BlockNum)
{   
		if (BlockNum<BLOCK_NUM)
		{ 
						*(unsigned short *)(FLASH_16M_POS+BlockNum*BLOCK_SIZE)=SET_BLOCK;		/*SET_BLOCK  0x6060*/
						*(unsigned short *)(FLASH_16M_POS+BlockNum*BLOCK_SIZE)=0x0101;
						FlashReadStatus(BlockNum);     
		}
}


/***********************************************************/
/***********************************************************/
unsigned short FlashReadShort(unsigned char BlockNum,unsigned int Addr)
{     
		if (BlockNum<BLOCK_NUM&&(2*Addr<BLOCK_SIZE))
		{ 
						*(unsigned short *)(FLASH_16M_POS+BlockNum*BLOCK_SIZE)=READ_ARRAY; 			/*w	READ_ARRAY	0xffff*/
						return *(unsigned short *)(FLASH_16M_POS+BlockNum*BLOCK_SIZE+Addr*2);
		}
		else		return 0; 
}

⌨️ 快捷键说明

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