关于在写SPI通信死机的问题,写好了个SPI通信的W25Q128,但是老是出现死机,不知道是啥回事

2019-08-20 15:54发布

昨天,弄了一整天的的SPI通信但是没有成功,我在看W25Q128的相关函数之后发现没有错误,我在想是不是还有的一些关于W25Q128的指令我不知道才会是这样的,或者是写入失败才会一直的占用这SPI,看看有没有哪位大神知道是啥回事实在是弄不出了。知道的感激不尽。
u8  Read_Write_Reg(u8 data)//SPI
{
u8  h=0,num=0;
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE)==RESET)
{
  num++;
  if(num>255)
  {
   return 0;
   //break;
  }
}
SPI_I2S_SendData(SPI2,data);
//SPI_I2S_ClearFlag(SPI2, SPI_I2S_FLAG_TXE);
num=0;
while(SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE )==RESET)
{
  num++;
  if(num>255)
  {
   return 0;
   //break;
  }
}
h=SPI_I2S_ReceiveData(SPI2);
//SPI_I2S_ClearFlag(SPI2, SPI_I2S_FLAG_RXNE);
return  h;
}
void   W25Q128_WriteEndable()
{
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
  Read_Write_Reg(0x06);
GPIO_SetBits(GPIOB,GPIO_Pin_12);
}
void   Clear_SectorErase(u32 adder1)
{
W25Q128_WriteEndable();
W25QXX_Wait_Busy();
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
  Read_Write_Reg(0x20);
Read_Write_Reg((u8)(adder1>>16));
Read_Write_Reg((u8)(adder1>>8));
Read_Write_Reg((u8)(adder1));
GPIO_SetBits(GPIOB,GPIO_Pin_12);
W25QXX_Wait_Busy();
}
void   W25Q128_WriteDisable()
{
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
  Read_Write_Reg(0x04);
GPIO_SetBits(GPIOB,GPIO_Pin_12);
}
/*do{
   for(i=0;i<Sector;i++)
   {
   BufferRead[i]=Read_Write_Reg(0xFF);
   }
  if(Page!=0)
  {
   Page--;
   Sector=4096;
  }
}while(Page!=0);*/
//&Otilde;&acirc;&cedil;&ouml;&sup2;&raquo;&Oacute;&Atilde;&micro;&frac12;&Otilde;&acirc;&cedil;&ouml;&pound;&not;&Ograve;ò&Icirc;&ordf;&Ocirc;&Uacute;&para;&Aacute;&micro;&Auml;&Ecirc;±&micro;&Oslash;&Ouml;·&raquo;á×&Ocirc;&para;&macr;&micro;&Auml;&Ocirc;&ouml;&frac14;&Oacute;&micro;&Auml;
//&Ouml;&raquo;&ETH;è&Ograve;&ordf;°&acute;&Euml;&sup3;&ETH;ò&para;&Aacute;&frac14;&acute;&iquest;&Eacute;
void  W25Q128_Read(u8 *BufferRead,u32 adder1,u16 number1)
{
  u8 i=0;
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
//Read_Write_Reg(0x04);
Read_Write_Reg(0x03);
Read_Write_Reg((u8)(adder1>>16));
Read_Write_Reg((u8)(adder1>>8));
Read_Write_Reg((u8)(adder1));
for(i=0;i<number1;i++)
  {
   BufferRead[i]=Read_Write_Reg(0xFF);
  }
GPIO_SetBits(GPIOB,GPIO_Pin_12);
}
u8 W25QXX_ReadSR(void)   
{  
u8 byte=0;   
GPIO_ResetBits(GPIOB, GPIO_Pin_12);                         //&Ecirc;&sup1;&Auml;&Uuml;&AElig;÷&frac14;&thorn;   
Read_Write_Reg(0x05); //·&cent;&Euml;&Iacute;&para;&Aacute;&Egrave;&iexcl;×&acute;&Igrave;&not;&frac14;&Auml;&acute;&aelig;&AElig;÷&Atilde;ü&Aacute;&icirc;   
byte=Read_Write_Reg(0Xff);          //&para;&Aacute;&Egrave;&iexcl;&Ograve;&raquo;&cedil;&ouml;×&Ouml;&frac12;&Uacute;  
GPIO_SetBits(GPIOB,GPIO_Pin_12);                         //&Egrave;&iexcl;&Iuml;&ucirc;&AElig;&not;&Ntilde;&iexcl;     
return byte;        
}
void W25QXX_Wait_Busy(void)     
{      
while((W25QXX_ReadSR()&0x01)==0x01);     // &micro;&Egrave;&acute;&yacute;BUSY&Icirc;&raquo;&Ccedil;&aring;&iquest;&Otilde;
}
//&Ouml;&raquo;&Auml;&Uuml;&Ocirc;&Uacute;&Atilde;&iquest;&cedil;&ouml;&Ograve;&sup3;&ETH;&acute;256&pound;&not;&Otilde;&acirc;&Ecirc;&Ccedil;×&icirc;&acute;ó&micro;&Auml;&Ecirc;&yacute;&Ouml;&micro;&iexcl;&pound;
//&Ocirc;&Uacute;&Atilde;&iquest;&cedil;&ouml;&micro;&Oslash;·&frac12;&Ouml;&raquo;&Auml;&Uuml;·&Aring;&Atilde;&raquo;&Oacute;&ETH;&ETH;&acute;&micro;&Auml;&Ecirc;&yacute;&frac34;&Yacute;&sup2;&raquo;&Egrave;&raquo;&ETH; &raquo;á&Ecirc;§°&Uuml;
//&micro;&Ouml;&Ouml;&AElig;&frac34;&szlig;&Oacute;&ETH;×&Ocirc;&Ocirc;&ouml;
void  W25Q128_NOchecWrite(u8 *Buffer1,u32 adder2,u16 number2)//&Otilde;&acirc;&Agrave;&iuml;&Ouml;&raquo;&Auml;&Uuml;&ETH;&acute;&Ecirc;&reg;&Aacute;ù&cedil;&ouml;
{//&Ocirc;&Uacute;&Otilde;&acirc;&Agrave;&iuml;&ETH;&acute;&para;à&Aacute;&Euml;&sup2;&raquo;&ETH;&ETH;
u16 i=0;
int  pagemain=0;
pagemain=256-number2%256;
W25Q128_WriteEndable();
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
Read_Write_Reg(0x02);//&Ograve;&sup3;&ETH;&acute;
Read_Write_Reg((u8)(adder2>>16));
Read_Write_Reg((u8)(adder2>>8));
Read_Write_Reg((u8)(adder2));
while(number2>0)
{
if(pagemain>=number2)//
{
for(i=0;i<number2;i++)
{
  Read_Write_Reg(Buffer1[i]);
}
}
else
{
  for(i=0;i<pagemain;i++)
   {
   Read_Write_Reg(Buffer1[i]);
   }
  }
  number2-=pagemain;
  pagemain=256-number2%256;
}
// W25QXX_Wait_Busy();
  GPIO_SetBits(GPIOB,GPIO_Pin_12);
   //W25Q128_WriteDisable();
W25QXX_Wait_Busy();
}
void W25Q128_Write(u8 *Buffer,u32 adder,u16 number)
{
u8  *BufferReadd;
int i=0,Sector=0,sectormain=0;
int  Page=0,h=0,h1=0;
Page=number/4096;
Sector=number%4096;
sectormain=4096-Sector;
  W25Q128_WriteEndable();
do{
  W25Q128_Read(BufferReadd,adder,number);//&Ocirc;&Uacute;&Otilde;&acirc;&Agrave;&iuml;&para;&Aacute;
  for(i=0;i<Sector;i++)//&Ocirc;&Uacute;&Otilde;&acirc;&Agrave;&iuml;&frac12;&oslash;&ETH;&ETH;±&Egrave;&frac12;&Iuml;
    {
     if(BufferReadd[i]!=0xFF) break;
    }
  if(i!=Sector)
   {
  for(i=0;i<Sector;i++)// fu shi
    {
     Buffer[i+Sector]=BufferReadd[i];
    }   
    printf("%c",'A');
  Clear_SectorErase(adder);//clear  sector
  while(Sector>0)
  {
   if(sectormain>=Sector)//
    {
       W25Q128_NOchecWrite(Buffer,adder,Sector);
     Sector=0;
    }
    else
     {
      W25Q128_NOchecWrite(Buffer,adder,sectormain);
     number-=sectormain;
     Sector=number%4096;
        sectormain=4096-Sector;
     adder+=sectormain;
      /*Sector-=256;
      adder+=256;
      Buffer+=256;
      h1=256-Sector%256;*/
    }
  }
}
else
  {
   if(sectormain>=Sector)
  {
    W25Q128_NOchecWrite(Buffer,adder,Sector);
   Sector=0;
  }
  else
  {
    W25Q128_NOchecWrite(Buffer,adder,sectormain);
   number-=sectormain;
   Sector=number%4096;
    sectormain=4096-Sector;
   adder+=sectormain;
  }
  }
  if(Page!=0)
  {
   Sector=4096;
   Buffer+=4096;
   adder+=4096;
   number=4096;
  }
}while(Page--);
W25Q128_WriteDisable();
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。