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

📄 wrlib.c

📁 基于C8051F340单片机的串行flash芯片SST25VF016B的应用,配合一般的串口调试程序即可完成对板上器件的读写操作.
💻 C
字号:
//ROM编程,配合串口调试程序可以完成任意代码的写操作
//首先由调试程序将要写的文件读入窗口;
//串口调试程序发代码0xfe,0x80,addr1,addr2,addr3;
//串口调试程序发送文件,发送结束后按 KEY1 退出。
//串口调试程序设置为: 9600,N,8,1
/*************************************
2008.04.09 Passed 
SST25VF016B(FLASH ROM)读写程序
*************************************/
#include  <C8051F340.h>

#define   uint unsigned  int
#define   uchar unsigned  char

uchar COM0buf[2048];
uchar Com0buf1[256];
unsigned long int SPIpt;
uint C0inpt;
uint C0outpt;
uchar C0sndpt;
uchar C0fulpt;
uchar ix,ix1,recvdat0,recvdat1;
uint cont1;
uchar numb3;

bit ok0;
bit ok3;
bit ik;
bit ik1;
bit overf;
bit key1flg;

sbit NSS  =  P0^3;
sbit KEY1 =  P2^3;
void delay(unsigned long int  wu);
void Init(void);
void fulbuf0(uchar Byte);
void PORT_Init (void);
void INIT_clk(void);
uchar inkey(void);
void save_SPI(uchar SPID);
void read_SPI(uchar addr1,uchar addr2,uchar addr3,uchar Len);
void cmd_SPI(uchar cmd,uchar addr1,uchar addr2,uchar addr3);
void writeAAI(void);
uchar wbyte(uchar Sbyte);
void rd_SPI(void);
uchar rstat(void);
void setime0(uint timevaul);
void wROMstat(uchar statbyte);
void writ_SPI(void);
void erase_all(void);
uchar readcom0(void);
void wcomd(uchar Cbyte);
void dis_SPI(void);
uchar readID(void);

main()
{
     PCA0MD = 0X8e;
	 delay(20000);  //it is need
	 INIT_clk();
	 PORT_Init ();
	 Init();
	 ok0 = ik = ik1 = 0;
	 C0inpt=C0outpt=C0sndpt=C0fulpt=0;
 	 NSS = 0;
 	 delay(20);
 	 wbyte(0x04);
 	 NSS = 1;
////////////////////////////////////////////////////////////
  	 EA = 1;
	 fulbuf0(0x55);
     do
     {
 	   if(C0inpt != C0outpt)
	    { ix = COM0buf[C0outpt];	//addr
		  C0outpt++;
		  C0outpt &= 0x07ff;
		  if(ik)
		   { ik=0;
		     switch(ix)
			  { case 0x80:
					writ_SPI();
			  		break;
				case 0x81: //读出0地址起始的内容送UART0
					  dis_SPI();
					  break;
				case 0x82: //读出128字节送UART0
					  read_SPI(0,0,0,0x7f);
 				      break;
				case 0x83: //整片擦除
					erase_all(); 
					break;
				case 0x84:
					  rstat();
					  break;
				case 0x85:	//读ID
					  readID();
			   default:break;	
	   		  };
			}
		  if(ix == 0xfe)
			 { ik = 1;
			   continue;
			 }
		}
  	 } while(1);
}
//************************************
void dis_SPI(void)
{ unsigned long int count1 = 0;
  KEY1 = 1;
  while(KEY1)
   { while( C0fulpt	 != C0sndpt );
     read_SPI(count1>>16,count1>>8,count1,0xff);
	 count1 += 0xff;
   }
}

//************************************
void fulbuf0(uchar ixchr)
{ if(ok0==0)
    { SBUF0 = ixchr;
	  ok0 = 1;
	}
   else
    { Com0buf1[C0fulpt] = ixchr;
  	  C0fulpt++;
	}
}

void  delay(unsigned long int  wu)
{
  for(wu;wu>0;wu--);
}

//*************************************************
static void tim0_isr (void) interrupt 4 using 1
{ 
  cont1++;
  if(cont1 >= 0x3ff)
   { overf = 1;
     numb3++;
	 cont1 =0; 
   }	
  TR0 = 1;
  TH0 = 0x2c;
  TL0 = 0x8f;
}

static void com_isr (void) interrupt 4 using 1
{ 
  if (RI0 != 0)		/* Received data interrupt */
  {
	RI0 = 0;
  	COM0buf[C0inpt] = SBUF0;
	C0inpt++;
	C0inpt&=0x07ff;
  }
/********************************************/
  if (TI0)		/* Transmitted data interrupt */
   { TI0 = 0;
      if(C0sndpt != C0fulpt)
	  { 
	    SBUF0 = Com0buf1[C0sndpt];
	    C0sndpt++;
		ok0 =1;
	  }
	  else ok0 = 0;
	}
}
//************************************************
void Init( )
{
     TR1 = 0;		/* stop timer 1 */
     ET1 = 0;		/* disable timer 1 interrupt */
	 SCON0=0X50; 	// 8位UART标准串口,允许接收 ,可变波特率
	 TMOD =0X20;  	//0x80=SMOD: set serial baudrate doubler
     PCON |= 0x80;  //
	 TH1=0x30;		// 48M 9600bps
	 TL1=0x30;
	 TR1=1;
     MCE0 = 0;
     REN0 = 1;	  	/* enable serial receiver */
     TI0 = 0;		  	/* clear transmit interrupt */
     RI0 = 0;		  	/* clear receiver interrupt */
     ES0 = 1;		  	/* enable serial interrupts */
//     PS = 0;		  	/* set ints to low priority */
}
// Configure the Crossbar and GPIO ports
void PORT_Init (void)
{
   XBR0    = 0x3;       // Enable UART0 & SPI0
   XBR1    = 0x40;		// Enable crossbar and weak pull-ups
   P0MDOUT |= 0x1d;     // enable TX0,MOSI,NSS,SCK as a push-pull output
   EMI0CF &= 0XF3;		// 只用内部XRAM
   NSS = 1;			    // ROM片选
   P2SKIP |= 0x20;
   P3MDIN &= 0xdc;
   P3SKIP |= 0x3;
//****** Init SPI0 *****   
   SPI0CFG = 0x40;	// 工作在主器件方式,SCK第一个边沿采样,SCK空闲时为高(Mode3)
   SPI0CN = 0x01;	// 三线方式,P03为ROM片选,SPI工作
   SPI0CKR = 1;	// 速率 12M
   NSS = 1;
//**** Init CPT1 ***************
   CPT1CN = 0x85;	// CPT1工作允许,正负回差都为5mv.
   CPT1MD = 0x3;	// 禁止中断,方式3
   CPT1MX = 0x11;	// +端输入P3.0,-端输入P3.1
}

void INIT_clk(void)
{ 
  OSCICN = 0x83;
  delay(20000);
  OSCICL=0x10;
  CLKMUL = 0x0;
  CLKMUL |= 0x80;
  delay(100);
  CLKMUL|=0xc0;
  while((CLKMUL & 0x20)==0);
  CKCON = 0x84;
  CLKSEL = 0x03;
  PFE0CN = 0x21;
}			

void wcomd(uchar Cbyte)
{ NSS = 0;
  wbyte(Cbyte);
  NSS = 1;
}
uchar wbyte(uchar Sbyte)
{ uchar tmp;
  SPI0CN &= 0x7f;
  SPI0DAT = Sbyte;
   while((SPI0CN & 0x80) == 0);
  tmp = SPI0DAT;
  return tmp;
}

void wROMstat(uchar statbyte)
{  wcomd(0x50);		//写SST25VF016B状态寄存器
   NSS = 1;
   wbyte(0xff);
   NSS = 0;
   wbyte(0x1);
   wbyte(statbyte);
   NSS = 1;
}
//读存储器状态
uchar rstat(void)
{ uchar tmp;
  NSS = 0;
  wbyte(0x5);				//Cmd = 5 is read state,Cmd = 0x9f is read ID.
  tmp = wbyte(0xff);
  NSS = 1;
  fulbuf0(tmp);
  return tmp;
}

uchar readID(void)
{ uchar tmp;
  NSS = 0;
  wbyte(0x9f);				//Cmd = 5 is read state,Cmd = 0x9f is read ID.
  for(tmp=0;tmp<3;tmp++)
    fulbuf0(wbyte(0xff));
  NSS = 1;
  return tmp;
}

void read_SPI(uchar addr3,uchar addr2,uchar addr1,uchar Len)
{ uchar ix;
  NSS = 0;
  wbyte(0x3);
  wbyte(addr3);
  wbyte(addr2);
  wbyte(addr1);
  for(ix1=0;ix1<Len;ix1++)
   { ix=wbyte(0xff);
	 fulbuf0(ix);
   }
   NSS=1;
} 

void writ_SPI(void)
{ uchar addr1,addr2,addr3;
  uchar ix2,ix3;
  addr3 = readcom0();
  addr2 = readcom0();
  addr1 = readcom0();
  ix2 = readcom0();
  ix3 = readcom0();
  wROMstat(0x2);
  wcomd(0x70);
  wcomd(0x06);
  NSS = 0;
  wbyte(0xad);
  wbyte(addr3);
  wbyte(addr2);
  wbyte(addr1);
  wbyte(ix2);
  wbyte(ix3);
  NSS = 1;
  while(key1flg==0)
  {	ix2 = readcom0();
    if(key1flg) return;
	ix3 = readcom0();
    if(key1flg) return;
	NSS = 0;
 	wbyte(0xad);
    wbyte(ix2);
    wbyte(ix3);
    NSS = 1;
  }
  wROMstat(0x9c);
}

uchar readcom0(void)
{ 
  uchar ix;
  key1flg = 0;
  while(KEY1)
  if (C0outpt != C0inpt)
  { ix = COM0buf[C0outpt];	//addr
    C0outpt++;
    C0outpt &= 0x7ff;
    return ix;
  }
  key1flg = 1;
  wcomd(0x80);
  wcomd(0x4);
  fulbuf0(0x5a);
  return 0xff;
} 

void erase_all(void)
{ wROMstat(0x2);
  wcomd(0x6);
  delay(2000);
  NSS = 0;
  wbyte(0x60);
  delay(0x3ffff);
  NSS = 1;
  wcomd(0x4);
}

⌨️ 快捷键说明

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