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

📄 sst39vf040.c.svn-base

📁 51单片机读写nor flash 读出flash 的ID
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
/* 39VF040.                                                             */
/*                                                                      */
/* NOTE:  It is VERY important the sector containing the byte to be     */
/*        programmed was ERASED first.                                  */
/*                                                                      */
/* Input:                                                               */
/*           Src      The BYTE which will be written to the 39VF040.     */
/*           Dst      DESTINATION address which will be written with the */
/*                    data passed in from Src                            */
///*        iSector  the sector number which will be written to         */                                                          */
/* Output:                                                              */
/*           None                                                       */
/************************************************************************/

void Program_One_Byte (BYTE SrcByte,  int iSector, BYTE xdata *Dst)
{
       int iBlockNum; 
       BYTE xdata  *Temp;
       BYTE xdata  *DestBuf;

       iBlockNum=iSector>>4;
       SELECT_PAGE(iBlockNum);
       DestBuf = Dst;
       DestBuf=((int)DestBuf)|(iSector%16<<12);
		//        DestBuf =(BYTE xdata*)0x500;        

        Temp =  (BYTE xdata *)0x5555; /* set up address to be A000:555h       */
        *Temp = 0xAA;                   /* write data 0xAA to the address       */
        Temp =  (BYTE xdata *)0x2AAA; /* set up address to be A000:2AAAh      */
        *Temp = 0x55;                   /* write data 0x55 to the address       */
        Temp =  (BYTE  xdata *)0x5555; /* set up address to be A000:5555h      */
        *Temp = 0xA0;                   /* write data 0xA0 to the address       */
        *DestBuf = SrcByte;             /* transfer the byte to destination     */
    	Delay_Nano_Seconds(250);
 	    Check_Toggle_Ready(DestBuf);    /* wait for TOGGLE bit to get ready     */

}


/************************************************************************/
/* PROCEDURE:   Program_One_Sector                                      */
/*                                                                      */
/* This procedure can be used to program a total of 4096 bytes of date  */
/* to the SST's 39VF040.                                                */
/*                                                                      */
/* Input:                                                               */
/*           Src     SOURCE address containing the data which will be   */
/*                   written to the 39VF040.                            */
/*           iSector the sector which  data will be written to          */
/*           iSector  0--128                                            */
/* Output:                                                              */
/*           None                                                       */
/************************************************************************/

void Program_One_Sector (BYTE  *Src, int iSector)
{
        BYTE xdata  *Temp;
        int iBlockNum; 
		BYTE xdata  *SourceBuf;
        BYTE xdata  *DestBuf;
        int Index;
        
        SourceBuf = Src;
        DestBuf =0; 
		DestBuf=(BYTE xdata*)(iSector%16<<12|((int)DestBuf));

        Erase_One_Sector(iSector);          /* erase the sector first */

        iBlockNum=iSector>>4;
    	SELECT_PAGE(iBlockNum);
	   
	    for (Index = 0; Index < SECTOR_SIZE; Index++)
        {
            Temp =  (BYTE xdata *)0x5555; /* set up address to be A000:555h           */
            *Temp = 0xAA;                   /* write data 0xAA to the address           */
            Temp =  (BYTE xdata*)0x2AAA; /* set up address to be A000:2AAAh          */
            *Temp = 0x55;                   /* write data 0x55 to the address           */
            Temp =  (BYTE xdata*)0x5555; /* set up address to be A000:5555h          */
            *Temp = 0xA0;                   /* write data 0xA0 to the address           */
            Temp = DestBuf;                 /* save the original Destination address    */
            *DestBuf++ = *SourceBuf++;      /* transfer data from source to destination */
            Check_Toggle_Ready(Temp);       /* wait for TOGGLE bit to get ready         */
        }
}





/************************************************************************/
/* PROCEDURE:    Check_Toggle_Ready                                     */
/*                                                                      */
/* During the internal program cycle, any consecutive read operation    */
/* on DQ6 will produce alternating 0's and 1's i.e. toggling between    */
/* 0 and 1. When the program cycle is completed, DQ6 of the data will   */
/* stop toggling. After the DQ6 data bit stops toggling, the device is  */
/* ready for next operation.                                            */
/*                                                                      */
/* Input:                                                               */
/*           Dst        must already set-up by the caller               */
/*                                                                      */
/* Output:                                                              */
/*           None                                                       */
/************************************************************************/

void Check_Toggle_Ready (BYTE xdata *Dst)
{
        BYTE Loop = TRUE;
        BYTE PreData;
        BYTE CurrData;
        unsigned long TimeOut = 0;

        PreData = *Dst;
        PreData = PreData & 0x40;
        while ((TimeOut< 0x07FFFFFF) && (Loop))
        {
            CurrData = *Dst;
            CurrData = CurrData & 0x40;
            if (PreData == CurrData)
                    Loop = FALSE;   /* ready to exit the while loop */
            PreData = CurrData;
            TimeOut++;
        }

		if(Loop==TRUE)
		 while(1);
}


/************************************************************************/
/* PROCEDURE:   Check_Data_Polling                                      */
/*                                                                      */
/* During the internal program cycle, any attempt to read DQ7 of the    */
/* last byte loaded during the page/byte-load cycle will receive the    */
/* complement of the true data.  Once the program cycle is completed,   */
/* DQ7 will show true data.                                             */
/*                                                                      */
/* Input:                                                               */
/*           Dst        must already set-up by the caller               */
/*           True       Datathis is the original (true) data            */
/*                                                                      */
/* Output:                                                              */
/*           None                                                       */
/************************************************************************/

void Check_Data_Polling (BYTE  *Dst, BYTE TrueData)
{
        BYTE Loop = TRUE;
        BYTE CurrData;
        unsigned long TimeOut = 0;

        TrueData = TrueData &  0x80;
        while ((TimeOut< 0x07FFFFFF) && (Loop))
        {
                CurrData = *Dst;
                CurrData = CurrData & 0x80;
                if (TrueData == CurrData)
                        Loop = FALSE;   /* ready to exit the while loop  */
                TimeOut++;
        }
}

/************************************************************************/
/* PROCEDURE:   Program_Flash                                           */
/*                                                                      */
/* This procedure can be used to program data to the                    */
/* 39VF040.                                                             */
/*                                                                      */
/* NOTE:  It is VERY important the sector containing the byte to be     */
/*        programmed was ERASED first.                                  */
/*                                                                      */
/* Input:                                                               */
/*           Src     The BYTE which will be written to the 39VF040.     */
/*           iSector   the sector which the data will be  written to    */
/*                     iSector 0--128                                   */
///*          ulLeng    the length which will be writted to flash       */                                                     */
/* Output:                                                              */
/*           None                                                       */
/************************************************************************/
int Program_Flash(BYTE * src,int iSector,unsigned long ulLength)
{
   unsigned int i,j;
   unsigned long len;
   unsigned int SectorStart,numSector;
   len=ulLength;
   SectorStart=iSector;   //起始sector
   numSector=ulLength/SECTOR_SIZE;               //待擦除扇区数
   if(ulLength%SECTOR_SIZE)
      numSector++;
   for(i=0;i<numSector;i++)
     {
     
	   Erase_One_Sector(i+SectorStart);  //擦除FLASH
       len-=i*SECTOR_SIZE;
       if(len>=SECTOR_SIZE)
          Program_One_Sector (src+i*SECTOR_SIZE, SectorStart+i);  //写整个扇区
       else
	    {  
	      for(j=0;len>0;j++,len--)
		    Program_One_Byte (*(src+i+j),SectorStart+i,(BYTE xdata*)j);//写剩余字节
         }
	  }
	return (0);
}
#endif

⌨️ 快捷键说明

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