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

📄 progflash.c

📁 powerPC866 系列平台BSP移植开发的参考代码
💻 C
字号:
/*
This file is used to program flash--te28f640
*/
#include "vxWorks.h"
#include "stdio.h"

#include "rru852flash.h"

/*
 *	flash read array command
 */
void flash_read_array()
{
	unsigned short * flash_ptr;
	
	flash_ptr = (unsigned short *)FLASH_BASE_ADDRESS;
	*flash_ptr = (unsigned short)FLASH_READ_ARRAY;
}

/*
 *	check flash type
 */
#define MANUFCTURE_CODE		0x0089
#define DEVICE_CODE			0x0017

void checkft(unsigned short * manid, unsigned short * devid)
{
	unsigned short * flash_ptr;

	flash_ptr = (unsigned short *)FLASH_BASE_ADDRESS;
	*flash_ptr = (unsigned short)FLASH_READ_ID;

	flash_ptr = (unsigned short *)(FLASH_BASE_ADDRESS + FLASH_READ_MAID);
	*manid = *flash_ptr;

	flash_ptr = (unsigned short *)(FLASH_BASE_ADDRESS + FLASH_READ_DEID);
	*devid = *flash_ptr;

	flash_read_array();
}

/*
 *	erase flash block
 */
int erase_flash(unsigned int blockaddress)
{
	unsigned short * flash_ptr;
	unsigned short status;
	int ix;
	
	if((blockaddress > HIGH_FLASH_ADDRESS) || (blockaddress < LOW_FLASH_ADDRESS))
		return -1;

	flash_ptr = (unsigned short *)blockaddress;
	*flash_ptr = (unsigned short)FLASH_ERASE_BLOCK_CMD1;
	*flash_ptr = (unsigned short)FLASH_ERASE_BLOCK_CMD2;
	
	status = 0;
	ix = 0;
	while( 0 == (status & FLASH_STATUS_OK))
	{
		status = *flash_ptr;
		taskDelay(1);
		ix++;
		if(ix >= WAIT_TIMES)
			break;
	}

	flash_read_array();
	
	if(0 == (status & FLASH_STATUS_OK))
	{
		printf("status = %x.\n",status);
		return -1;
	}

	printf("ix = %d\n",ix);
	return 0;

}

/*
 *	erase flash from startaddress to endaddress
 */
int erase_flash_range(unsigned int startaddress, unsigned int endaddress)
{
	unsigned int eraseaddress;

	if((startaddress < LOW_FLASH_ADDRESS) || (endaddress > HIGH_FLASH_ADDRESS))
		return -1;
	if(endaddress < startaddress)
		return -1;

	eraseaddress = FLASH_BLOCK_SIZE - 1;
	eraseaddress = (unsigned int)0xffffffff - eraseaddress;
	eraseaddress += 1;

	eraseaddress = startaddress & eraseaddress;

	while(eraseaddress <= endaddress)
	{
		if( 0 != erase_flash(eraseaddress))
			return -1;
		eraseaddress += FLASH_BLOCK_SIZE;
	}

	return 0;
}

/*
 *	program flash whith one half word
 */
int prog_flash_hw(unsigned int address, unsigned short pdata)
{
	unsigned short * flash_ptr;
	unsigned short status;
	int ix;

	if((address < LOW_FLASH_ADDRESS) || (address > HIGH_FLASH_ADDRESS))
		return -1;

	flash_ptr = (unsigned short *)address;
	*flash_ptr = (unsigned short)FLASH_PROG_CMD;

	*flash_ptr = pdata;

	status = *flash_ptr;
	ix = 0;
	while( 0 == (status & FLASH_STATUS_OK))
	{
		status = *flash_ptr;
/*		taskDelay(1);
*/		ix++;
		if(ix >= PROG_WAIT_TIMES)
			break;
	}
	
	flash_read_array();
	
	if(0 == (status & FLASH_STATUS_OK))
	{
		printf("status = %x.\n",status);
		return -1;
	}

	status = *flash_ptr;

	if(status != pdata)
	{
		printf("prog err. pdata=%x, read=%x.\n", pdata, status);
		return -1;
	}
	
	printf("ix = %d\n",ix);
	return 0;

}

/*
 *	program flash with a buf
 */
int prog_flash(unsigned int adrs, int len, unsigned char * pbuf)
{
	unsigned int progaddress;
	int ix;
	unsigned short *spbuf;

	if((adrs < LOW_FLASH_ADDRESS) || (adrs > HIGH_FLASH_ADDRESS))
		return -1;
	
	progaddress = adrs;
	spbuf = (unsigned short *)pbuf;

	for(ix = 0; ix < len; ix+=2)
	{
		if(0 != prog_flash_hw(progaddress, *spbuf))
			return -1;
		spbuf++;
		progaddress += 2;
	}
}

void testflash()
{
	unsigned short manid;
	unsigned short devid;
	int ix;
	unsigned char mybuf[0x200];
	unsigned char mych;

	checkft(&manid, &devid);
	printf("manid=%x, devid=%x.\n",manid, devid);

	mych = 0x00;
	for(ix = 0; ix < 0x100; ix++)
	{
		mybuf[ix] = mych;
		mych++;
	}

	erase_flash_range(0xfc000000, 0xfc000000);

	ix = prog_flash(0xfc000000, 0x100, mybuf);
	printf("ix = %d,\n",ix);
}

⌨️ 快捷键说明

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