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

📄 flash.c

📁 代码有点长,需细心阅读,仅供影音视听类产品的开发人员参考
💻 C
字号:
#include "config.h"
#include "regmap.h"
#include "global.h"
#include "sio.h"
#include "epp.h"

#ifdef SUPPORT_FLASH

static inline void FLUSHWB(void)
{
	volatile UINT32 *p = (UINT32 *) 0x80000000;

	(void)p[0];
}

static inline void SET_WP_0(void)
{
	UINT32  x;

	x = regs0->gpio_out[0];
	x &= ~0x08;
	regs0->gpio_out[0] = x;
}


static inline void SET_WP_1(void)
{
	UINT32  x;

	x = regs0->gpio_out[0];
	x |= 0x08;
	regs0->gpio_out[0] = x;
}

int flash_read_ID(volatile BYTE * p)
{
	UINT8   id;

	*p = 0x90;
	{
		int     i;

		for(i = 0; i < 2048; i++)
			asm volatile ("nop");
	}
	id = *p;
	*p = 0xff;
	return id;
}


int flash_eraseblock(volatile BYTE * p)
{
	UINT8   status;

	//SET_WP_1();  FLUSHWB();
	*p = 0x20;										//FLUSHWB();
	*p = 0xd0;										//FLUSHWB();


	// wait finish
	// write 0x70
	//*p = 0x70;  //FLUSHWB();

	status = 0;
	while((status & 0x80) == 0)
	{
		status = 0;
		status = *p;
	}

//  psprintf(linebuf,"erase status=%02x\n",status);
//  epp_write_wait(linebuf);

	{
		int     i;

		for(i = 0; i < 2048; i++)
			asm volatile ("nop");
	}

	*p = 0x50;										// clear status register

	*p = 0xFF;										//FLUSHWB();

	return 0;
}


int flash_prog(volatile BYTE * p, BYTE flash_write_data)
{
	UINT8   status;

	//
	//psprintf(linebuf, "prog 0x%08x with %02x\n", p, flash_write_data);
	//epp_write_wait(linebuf);

	// WPB=0
	//SET_WP_0(); FLUSHWB();

	// write 0x40
	*p = 0x40;										//FLUSHWB();

	// WPB=1;
	//SET_WP_1(); FLUSHWB();

	// write data
	*p = flash_write_data;				//FLUSHWB();


	// wait finish
	// write 0x70
	//*p = 0x70;  //FLUSHWB();
	status = 0;
	while((status & 0x80) == 0)
	{
		status = 0;
		status = *p;
//  psprintf(linebuf,"prog status=%02x\n",status);
//  epp_write_wait(linebuf);
	}



	{
		int     i;

		for(i = 0; i < 2048; i++)
			asm volatile ("nop");
	}

	// WPB=0;
	//SET_WP_0(); FLUSHWB();

	*p = 0x50;										// clear status register

	// write 0xFF: read array
	*p = 0xFF;										//FLUSHWB();

	//regs0->stamp = *p;

	return 0;
}


int test_flash_access(void)
{
	UINT32  u;

	//
	// force select following pins:
	// GPIOA[2]  MEMWE_B      GPIO_SEL_AUX[2]==0
	// GPIOA[3]  MEMWP_B      always
	// GPIOA[12] MEMOE_B      GPIO_SEL_AUX[3]==0
	// GPIOA[13] MEMCS1_B     GPIO_SEL_AUX[4]==0
	// GPIOA[14] MEMCS2_B     GPIO_SEL_AUX[5]==0
	// GPIOA[15] MEMCS3_B     GPIO_SEL_AUX[6]==0
	//
/*
  u = regs0->gpio_sel_aux;
  u &= ~(0x007c);
  regs0->gpio_sel_aux = u;
*/

	//
	// turn on output of MEMWP_B (GPIOA[3])
	//
//  SET_WP_0();         // default 0
	//SET_WP_1();         // default 0

/*
  u = regs0->gpio_oe[0];
  u |= 0x0008;
  regs0->gpio_oe[0] = u;

  u = regs0->gpio_master[0];
  u |= 0x0008;
  regs0->gpio_master[0] = u;
*/
	//
	// also setup flash_1, flash_2 address
	//
#define FLASH1_BASE_64      (0x10)	// 0x80100000
#define FLASH2_BASE_64      (0x20)	// 0x80200000
#define ROM0_BASE_U     (0xa0000000)
#define FLASH1_BASE_C       (0x80000000 | (FLASH1_BASE_64<<16))
#define FLASH2_BASE_C       (0x80000000 | (FLASH2_BASE_64<<16))
#define FLASH1_BASE_U       (0xbfc00000 | (FLASH1_BASE_64<<16))
#define FLASH2_BASE_U       (0xbfc00000 | (FLASH2_BASE_64<<16))

	regs0->rom1_base = FLASH1_BASE_64;
	regs0->rom2_base = FLASH2_BASE_64;	//FLASH2_BASE_64;

	//
	// now ready to issue flash command/data.
	//
#define TLEN    4096
	{
		int     i;
		volatile BYTE *p;
		UINT8   flash_id, read_byte1, read_byte2, read_byte3, read_byte4;

		// read id
		p = (volatile BYTE *)FLASH1_BASE_U;
		//epp_write_wait("flash read ID\n");
		flash_id = flash_read_ID(p);
		//psprintf(linebuf,"flash id = %02x\n",flash_id);
		//epp_write_wait(linebuf);

		*p = 0x50;									// clear status register
		// read it
/*
    p = (volatile BYTE *)FLASH1_BASE_U;
    epp_write_wait("flash content\n");
    for (i=0;i<32;)
    {
      psprintf(linebuf, "%02x ", p[i]); i++;
      epp_write_wait(linebuf);
      if ((i&0x0f)==0) epp_write_wait("\n");
    }


    p = (volatile BYTE *)FLASH1_BASE_U;
    epp_write_wait("flash content\n");
    for (i=0;i<32;)
    {
      psprintf(linebuf, "%02x ", p[i]); i++;
      epp_write_wait(linebuf);
      if ((i&0x0f)==0) epp_write_wait("\n");
    }
*/

		//
		p = (volatile BYTE *)(FLASH1_BASE_U);
		flash_eraseblock(p);

		flash_prog(p, 0xaa);
		flash_prog(p + 1, 0xbb);
		flash_prog(p + 2, 0xcc);
		flash_prog(p + 3, 0xdd);
		// write 0xFF: read array
//    *p = 0xFF;    //FLUSHWB();
		//  *p = 0xFF;    //FLUSHWB();

		//psprintf(linebuf, "read prog %08x %02x%02x%02x%02x (0xaabbccdd)\n", p,p[0],p[1],p[2],p[3]);
		//epp_write_wait(linebuf);

		flash_eraseblock(p);
		flash_prog(p + 0, 0xee);
		//psprintf(linebuf, "read prog %02x (0xee)\n", p[0]);
		//epp_write_wait(linebuf);
//    monitor();
//    return 0;

		//

		flash_eraseblock(p);
		for(i = 0; i < TLEN; i++)
			flash_prog(p + i, 0xff);
		for(i = 0; i < TLEN; i++)
		{
			if(p[i] != 0xff)
			{
				//psprintf(linebuf, "flash1 w0[%02x] 0xff (%02x)failed\n", i, p[i]);
				//epp_write_wait(linebuf);
			}
		}
		// epp_write_wait("TEST 1 OK..\n");  

		flash_eraseblock(p);
		for(i = 0; i < TLEN; i++)
			flash_prog(p + i, 0x00);
		for(i = 0; i < TLEN; i++)
		{
			if(p[i] != 0x00)
			{
				// psprintf(linebuf, "flash1 w1[%02x] 0x00 (%02x) failed\n", i, p[i]);
				// epp_write_wait(linebuf);
			}
		}
		// epp_write_wait("TEST 2 OK..\n");

		flash_eraseblock(p);
		for(i = 0; i < TLEN; i++)
			flash_prog(p + i, i);
		for(i = 0; i < TLEN; i++)
		{
			if(p[i] != (i & 0x00ff))
			{
				//psprintf(linebuf, "flash1 w2[%02x] 0x%02x (%02x)failed\n", i, i, p[i]);
				//epp_write_wait(linebuf);
			}
		}
		//epp_write_wait("TEST 3 OK..\n");
		// epp_write_wait("flash prg ended and OK..\n");
//    monitor();

	}
	return 0;
}

#endif //#ifdef SUPPORT_FLASH

⌨️ 快捷键说明

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