导航: 老古网老古论坛XMOS公共讨论区XMOS开源项目区单片机程序设计嵌入式系统广告区域
→51   对128M flash blcok 0  eras[hypee]

 *第35157篇: 51 对128M flash blcok 0 erase/program/read?

  
楼 主:hypee 2005年10月19日09:35
 51   对128M flash blcok 0  erase/program/read?
有没现成的程序?
给份给我?
谢谢


hypee@163.com

  
2楼:hypee 2005年10月20日15:03
 付上程序!
#include     <REG51.H>  
#include     <string.h>  
#include     <cmd.h>  
#include     <absacc.h>  
#include     <flash_cmd.h>  

#define    uchar     unsigned char      // 8bit3
#define    uint      unsigned int       // 16bit
#define    ulong     unsigned long      // 32bit

#define              cmd_len              3
#define              tol_len              2
#define              data_len             17

uchar    data         cmd_buff[cmd_len];   //store the cmd data   
uchar    data         len_buff[tol_len];   //store the data total length
uchar    data         data_buff[data_len]; // sotre the data

uchar    data         count1 ;             // count1 for low  byte
uchar    data         count2 ;             // count2 for high byte
 
ulong                 n;                   // write the data to external ram; for counter
ulong                 k;                   // read/verify the data for external ram;

uchar    data         count;               // counter

bit                   send_flag;           // finished received, set this flag

bit                   rec_cmd;             // receive cmd  flag
bit                   rec_len;             // receive len  flag 
bit                   rec_data;            // receive data flag

sbit                  P36=P3^6;            // write            // for external flash
sbit                  P37=P3^7;            // read

sbit                  P13=P1^3;            // for flash A0
sbit                  P14=P1^4;            // for CE0          // for internal falsh
sbit                  P15=P1^5;            // for CE1
sbit                  P16=P1^6;            // for CE2

sbit                  P33=P3^3;            // for RP
sbit                  P34=P3^4;            // for BYTE 

void init_serialcom(void)
{
  SCON|=0x50;                   // serial: model 2
  TMOD|=0x20;                   // time1:  model 2
  PCON|=0x80;                   // SMOD :  0
  TH1|=0x0d9;                   // 18M     2400 bps
  TR1=1;                        // start the time1
}

// send one byte 
void  send_byte(uchar  in_byte) 
   {      
     SBUF=in_byte;     
     while (TI==0);
     TI=0;     
   } 

// send the string in read/verify
void  send_string(uchar  *str,ulong  strlen)
  {
     ulong  k=4;
     do
      {
       send_byte(*(str+k));
       k=k+1;
      }while(k  <=strlen);
  } 
//**********************************************************//
//==  mcu prot define for internal 128M flash processing  ==//
//**********************************************************//
void write_flash (uint addr, uchar cmd_data)  
  {     
     XBYTE[addr]=cmd_data;     
  }

void WaitComplete()
   {
     ulong     iWait;
     ulong     i=0,j=0;
     //uchar     status=0;
     for(iWait=0;iWait  <0xFFFFFFFF;iWait++)         
           { 
             //write_flash(addr,READ_STS_CMD);
             //status=XBYTE[addr];
             i++;
             j++;
             i++;
             j++;
             i++;
             j++;
             i++;
             j++;
             i++;
             j++;
             i=0;
             j=0;
           } 
          
     //write_flash(addr, CLEAR_STS_CMD);
     
   }

void flash_erase (uint addr)
  {  
     write_flash ( addr, ERASE_CMD);
     write_flash ( addr, ERASE_CONFIRM_CMD);          
     WaitComplete( );
  }      
 
   
//*************************************************************//   
//*************************************************************//
void serial_int() interrupt 4 using 1
{       EA=0;
        ES=0;              
        if(RI){   ulong    data   i;                   // write the data to external ram; for counter
                  uchar    data m;         // write counter for each time                            
                  uchar    data rec;       // receive data for temp   
                  if(rec_cmd)               // receive command
                    {                                                                            
                      RI=0;  
                      rec=SBUF; 
                      P1=SBUF;

                      cmd_buff[count]=rec;
                      count=count+1;
                      if(count==cmd_len)
                          {                           
                           count=0;
                           rec_cmd=0; 
                           send_flag=1;                          
                          }
                      else
                          {
                           rec_cmd=1;
                           send_flag=0;
                          }
                    }
              
                  else if(rec_len)          // receive data length
                     {
                      RI=0;  
                      rec=SBUF; 
                      P1=SBUF;

                      len_buff[count]=rec;
                      count=count+1;
                      if(count==tol_len)
                          {                           
                           count=0;
                           rec_len=0;
                           rec_data=1;                  
                           send_byte(ACK);                           
                          }
                      else
                          {
                           rec_len=1;                           
                          }
                     }   
                 
                  else if(rec_data)           //  receive data flow
                    { 
                      RI=0;  
                      rec=SBUF;                             // count1 low
                      P1=SBUF;                              // count2 high

                      data_buff[count]=rec;

                      if(count==0);
                      else
                        count1=count1+1;

                      count=count+1;                            
                       
                      if((count2==len_buff[1])&&(count1==len_buff[0]))   
                        {                        
                           if(data_buff[0]==count-1)     
                             {
                              send_byte(PASS);
                              // **************************** //   
                              // add the flash operat: write 
                              // **************************** // 
                               P37=1;  
                               //XBYTE[n]=0x40;                             
                               for(m=1;m  <count;m=m+1)
                                    {  
                                       P15=0; 
                                       k=n;
                                       XBYTE[n]=0x40;
                                       XBYTE[n]=data_buff[m];
                                       for(i=0;i  <=5;i++);
                                       n=n+1; 
                                       P15=1;                                       
                                    }  
                               P37=0;
                               n=4;             //******* for n counter  start addr                                          
                             //******************   end  *********************//                              
                              rec_cmd=1;
                              rec_len=0;
                              rec_data=0;
                              send_flag=0;

                              count=0; 
                              count1=0;
                              count2=0;                        
                              P15=0;
                             }
                              
                           else
                             {
                              send_byte(ERROR);
                              rec_data=0;
                              rec_cmd=1;
                              rec_len=0;
                              send_flag=0;

                              count=0; 
                              count1=0;
                              count2=0;
                              
                             }
                        }
                       else if (count1==0xff)
                          { 
                            
                            count2=count2+1;       
                 }
                       else if (count==data_len)
                        {  
                           // **************************** // 
                           // add the flash operat: write
                           // **************************** // 
                                
                               P37=1;  
                               //XBYTE[n]=0x40;                             
                               for(m=1;m  <count;m=m+1)
                                    {  
                                       P15=0; 
                                       k=n;
                                       XBYTE[n]=0x40;
                                       XBYTE[n]=data_buff[m];
                                       for(i=0;i  <=5;i++);
                                       n=n+1; 
                                       P15=1;                                       
                                    }  
                               P37=0;                                               
                           // ******************   end  ********************* //
                           send_byte(ACK);   
        
                           rec_data=1;
                           rec_cmd=0;
                           rec_len=0;
                           send_flag=0;                           
                           count=0;                         
                        }
                        else
                             {    
                               rec_data=1;
                               rec_cmd=0;
                               rec_len=0;
                               send_flag=0;  
                             }                      
                        
                                                                                                                        
                                                                          
                    }
               }
        EA=1; 
        ES=1;
}

main()
{     
      uchar  idata  temp;  //for read ID NO
      EA=1;
      ES=1;   
      P13=0;        //for address A0  not used in 8 model, only choice 8 bit mode
      P36=1;
      P37=1;

     
      P14=0;       // control the flash  CE0
      P15=0;       // control the flash  CE1
      P16=0;       // control the flash  CE2

      P33=1;       //RB
      P34=0;       //BYTE 8/16 bit

      send_flag=0;
      count=0;
      count1=0;
      count2=0;

      n=4;        // start addr
      k=0;      

      rec_cmd=1; 
      rec_len=0; 
      rec_data=0;
            
      init_serialcom(); 

   while(1)
  {   
       if(send_flag)
        {     //*********************************************************************************//
              //************************ micro flash 128M bit ***********************************//
              //*********************************************************************************//
              if((cmd_buff[0]==MICRON)&&(cmd_buff[1]==MT28F128J3_ID)&&(cmd_buff[2]==chip_write)) 
              {
                send_byte(ACK);
                send_flag=0;
                count=0;
                P15=1;
                //P33=1;       //RB           
                //P34=0;       //BYTE 8/16 bit
                k=0;
                rec_len=1;    
              }
              
              else if ((cmd_buff[0]==MICRON)&&(cmd_buff[1]==MT28F128J3_ID)&&(cmd_buff[2]==chip_erase))              
              { // ************************************** //
                // add the operate code for erase the flash 
                // ************************************** // 
                //P15=0;       // chip enable        
                //P33=1;       // RB           
                //P34=0;       // BYTE 8/16 bit
                //P37=1;P36=0;
                flash_erase (0);                           
                //P15=1;
                //**************** end *******************//
                send_byte(ACK);
                send_flag=0;
                count=0;
                rec_cmd=1;                
                k=0;
              }
              
              else if ((cmd_buff[0]==MICRON)&&(cmd_buff[1]==MT28F128J3_ID)&&(cmd_buff[2]==chip_verify))                               
              { // ************************************** //
                // add the operate code for erase the flash
                // ************************************** //
                //P15=0;                
                //P33=1;       //RB           
                //P34=0;       //BYTE 8/16 bit
                //P37=0;P36=1;
                XBYTE[n]=0x0ff;
                send_string(XBYTE,k);
                //P15=1;
                //**************** end *******************//
                //total_length=0;
                send_flag=0;
                count=0;
                rec_cmd=1;
              }
              else if ((cmd_buff[0]==MICRON)&&(cmd_buff[1]==MT28F128J3_ID)&&(cmd_buff[2]==chip_read))          
              { // ************************************** //
                // add the operate code for erase the flash
                // ************************************** //
                //P15=0;                
                //P33=1;       //RB           
                //P34=0;       //BYTE 8/16 bit
                //P37=0;P36=1;
                XBYTE[n]=0x0ff;
                send_string(XBYTE,k);
                //P15=1;
                //**************** end *******************//
                //total_length=0; 
                send_flag=0;
                count=0;
                rec_cmd=1;
              }
              else if ((cmd_buff[0]==MICRON)&&(cmd_buff[1]==MT28F128J3_ID)&&(cmd_buff[2]==read_id))
              {
                // ************************************** //
                // add the operate code for erase the flash
                // ************************************** //
                //P15=0;                
                //P33=1;       //RB           
                //P34=0;       //BYTE 8/16 bit
                //P37=0;P36=1;
                //send_byte(ACK);
                XBYTE[0]=0x90;
                temp=XBYTE[0];
                send_byte(temp);
                P15=1;
                //**************** end *******************//
                //total_length=0; 
                send_flag=0;
                count=0;
                rec_cmd=1;
              }
              
              else
                   {
                    send_byte(NACK);
                    send_flag=0;
                    count=0;
                    rec_cmd=1;
                   }
         }         
       
 }  
   
}

  
3楼:down 2005年11月8日13:51
 nand ;flash
nand flash的一个PAGE就有528 byte,用标准51可以实现读写操作,但是这没有什么意义,因为你无法保存。

VCD/DVD/MP3工程师论坛


>>>>>>对该主题发表你的看法

本主题贴数3,分页: [第1页]


[上一篇主题]:各位大虾,能否推荐几本学单片机外围电路设计的好书

[下一篇主题]:[求助]为何LCD(RT12864A-1)的背光不亮??