本帖最后由 jorry 于 2017-4-20 22:39 编辑
采用原子的代码,板子为硬件电路和原子该部分电路一样,但无法读出数据,单步提示图,调用代码如下#ifdef USING_FLASHDEVICE
const u8 TEXT_Buffer[]={"Explorer STM32F1 SPI TEST"};
#define SIZE sizeof(TEXT_Buffer)
u32 FLASH_SIZE=16*1024*1024; //FLASH ′óD¡Îa16×Ö½ú
u8 datatemp[SIZE];
#endif
#ifdef USING_FLASHDEVICE SPI_Flash_Init(); //SPI FLASH 3õê¼»ˉ
while(SPI_Flash_ReadID()!=W25Q128) //¼ì2a2»μ½W25Q128 ¿éòÔ¼ì2aμ½
{
USART1_Printfstr("Please Check!
");
delay_ms(100);
}
SPI_Flash_Write((u8*)TEXT_Buffer,FLASH_SIZE-100,SIZE); //′óμ1êyμú100¸öμØÖ·′|¿aê¼,D′èëSIZE3¤¶èμÄêy¾Y
delay_ms(100);
#endif
读取代码如下
#ifdef USING_FLASHDEVICE
SPI_Flash_Read(datatemp,FLASH_SIZE-100,SIZE); //′óμ1êyμú100¸öμØÖ·′|¿aê¼,¶á3öSIZE¸ö×Ö½ú
delay_ms(100);
sprintf(temp,"%s
",datatemp);
USART1_Printfstr(temp);
#endif
找不出问题,硬件经测量和原子电路一样,供电都为3.3V,请指导帮助!谢谢
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
请问原子哥,由于电路这部分采用的您的电路,所以代码移植是移植的您的代码,没找到不同点,现在去掉 SPI_Flash_Write((u8*)TEXT_Buffer,FLASH_SIZE-100,SIZE); 可一直读出0xFF,加上读不出任何数据,经示波器查看cs,mosi都在读的时候有跳变沿,miso却一直为低,sck却一直为高,请问我该如何调试这个spi时序,谢谢
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );//PORTBê±Öóê1Äü
RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );//SPI2ê±Öóê1Äü
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PB13/14/15¸′óÃíÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉGPIOB
GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); //PB13/14/15éÏà-
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //éèÖÃSPIμ¥Ïò»òÕßË«ÏòμÄêy¾YÄ£ê½:SPIéèÖÃÎaË«ÏßË«Ïòè«Ë«1¤
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //éèÖÃSPI1¤×÷Ä£ê½:éèÖÃÎaÖ÷SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //éèÖÃSPIμÄêy¾Y′óD¡:SPI·¢Ëí½óêÕ8λ֡½á11
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //′®DDí¬2½ê±ÖóμÄ¿ÕÏD×′ì¬Îa¸ßμçƽ
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //′®DDí¬2½ê±ÖóμÄμú¶t¸öìø±äÑØ£¨éÏéy»òϽ죩êy¾Y±»2éÑù
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSSDÅoÅóéó2¼t£¨NSS1ü½Å£©»1êÇèí¼t£¨ê1óÃSSI룩1üàí:Äú2¿NSSDÅoÅóDSSIλ¿ØÖÆ
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //¶¨òå2¨ìØÂêÔ¤·ÖÆμμÄÖμ:2¨ìØÂêÔ¤·ÖÆμÖμÎa256
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //Ö¸¶¨êy¾Y′«êä′óMSBλ»1êÇLSBλ¿aê¼:êy¾Y′«êä′óMSBλ¿aê¼
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRCÖμ¼ÆËãμĶàÏîê½
SPI_Init(SPI2, &SPI_InitStructure); //¸ù¾YSPI_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèSPIx¼Ä′æÆ÷
SPI_Cmd(SPI2, ENABLE); //ê1ÄüSPIíaéè
SPI2_ReadWriteByte(0xff);//Æô¶ˉ′«êä
}
u16 SPI_FLASH_TYPE=W25Q128;//ĬèϾíêÇ25Q128
//4KbytesÎaò»¸öSector
//16¸öéèÇøÎa1¸öBlock
//W25X16
//èYá¿Îa2M×Ö½ú,12óD32¸öBlock,512¸öSector
//3õê¼»ˉSPI FLASHμÄIO¿ú
void SPI_Flash_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );//PORTBê±Öóê1Äü
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_5); //Öøß1©μçflash
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; // PB12 íÆíì
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_12); //spi flash 2»Ñ¡ÖD
SPI2_Init(); //3õê¼»ˉSPI
SPI2_SetSpeed(SPI_BaudRatePrescaler_2);//éèÖÃÎa18Mê±Öó,¸ßËùÄ£ê½
SPI_FLASH_TYPE=SPI_Flash_ReadID();//¶áè¡FLASH ID.
}
//¶áè¡SPI_FLASHμÄ×′쬼Ä′æÆ÷
//BIT7 6 5 4 3 2 1 0
//SPR RV TB BP2 BP1 BP0 WEL BUSY
//SPR:ĬèÏ0,×′쬼Ä′æÆ÷±£»¤Î»,ÅäoÏWPê1óÃ
//TB,BP2,BP1,BP0:FLASHÇøóòD′±£»¤éèÖÃ
//WEL′ê1ÄüËø¶¨
//BUSY:Ã|±ê¼Çλ(1,Ã|;0,¿ÕÏD)
//ĬèÏ:0x00
u8 SPI_Flash_ReadSR(void)
{
u8 byte=0;
SPI_FLASH_CS=0; //ê1ÄüÆ÷¼t
SPI2_ReadWriteByte(W25X_ReadStatusReg); //·¢Ëí¶áè¡×′쬼Ä′æÆ÷Ãüáî
byte=SPI2_ReadWriteByte(0Xff); //¶áè¡ò»¸ö×Ö½ú
SPI_FLASH_CS=1; //è¡ÏûƬѡ
return byte;
}
//D′SPI_FLASH×′쬼Ä′æÆ÷
//Ö»óDSPR,TB,BP2,BP1,BP0(bit 7,5,4,3,2)¿éòÔD′!!!
void SPI_FLASH_Write_SR(u8 sr)
{
SPI_FLASH_CS=0; //ê1ÄüÆ÷¼t
SPI2_ReadWriteByte(W25X_WriteStatusReg); //·¢ËíD′è¡×′쬼Ä′æÆ÷Ãüáî
SPI2_ReadWriteByte(sr); //D′èëò»¸ö×Ö½ú
SPI_FLASH_CS=1; //è¡ÏûƬѡ
}
//SPI_FLASHD′ê1Äü
//½«WELÖÃλ
void SPI_FLASH_Write_Enable(void)
{
SPI_FLASH_CS=0; //ê1ÄüÆ÷¼t
SPI2_ReadWriteByte(W25X_WriteEnable); //·¢ËíD′ê1Äü
SPI_FLASH_CS=1; //è¡ÏûƬѡ
}
//SPI_FLASHD′½ûÖ1
//½«WELÇåáã
void SPI_FLASH_Write_Disable(void)
{
SPI_FLASH_CS=0; //ê1ÄüÆ÷¼t
SPI2_ReadWriteByte(W25X_WriteDisable); //·¢ËíD′½ûÖ1Ö¸áî
SPI_FLASH_CS=1; //è¡ÏûƬѡ
}
//¶áè¡D¾Æ¬ID W25X16μÄID:0XEF14
u16 SPI_Flash_ReadID(void)
{
u16 Temp = 0;
SPI_FLASH_CS=0;
SPI2_ReadWriteByte(0x90);//·¢Ëí¶áè¡IDÃüáî
SPI2_ReadWriteByte(0x00);
SPI2_ReadWriteByte(0x00);
SPI2_ReadWriteByte(0x00);
Temp|=SPI2_ReadWriteByte(0xFF)<<8;
Temp|=SPI2_ReadWriteByte(0xFF);
SPI_FLASH_CS=1;
return Temp;
}
//¶áè¡SPI FLASH
//ÔúÖ¸¶¨μØÖ·¿a꼶áè¡Ö¸¶¨3¤¶èμÄêy¾Y
//pBuffer:êy¾Y′æ′¢Çø
//ReadAddr:¿a꼶áè¡μÄμØÖ·(24bit)
//NumByteToRead:òa¶áè¡μÄ×Ö½úêy(×î′ó65535)
void SPI_Flash_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead)
{
u16 i;
SPI_FLASH_CS=0; //ê1ÄüÆ÷¼t
SPI2_ReadWriteByte(W25X_ReadData); //·¢Ëí¶áè¡Ãüáî
SPI2_ReadWriteByte((u8)((ReadAddr)>>16)); //·¢Ëí24bitμØÖ·
SPI2_ReadWriteByte((u8)((ReadAddr)>>8));
SPI2_ReadWriteByte((u8)ReadAddr);
for(i=0;i<NumByteToRead;i++)
{
pBuffer[i]=SPI2_ReadWriteByte(0XFF); //Ñ-»·¶áêy
}
SPI_FLASH_CS=1; //è¡ÏûƬѡ
}
//SPIÔúò»ò3(0~65535)ÄúD′èëéùóú256¸ö×Ö½úμÄêy¾Y
//ÔúÖ¸¶¨μØÖ·¿aê¼D′èë×î′ó256×Ö½úμÄêy¾Y
//pBuffer:êy¾Y′æ′¢Çø
//WriteAddr:¿aê¼D′èëμÄμØÖ·(24bit)
//NumByteToWrite:òaD′èëμÄ×Ö½úêy(×î′ó256),¸Ãêy2»ó|¸Ã3¬1y¸Ãò3μÄê£óà×Ö½úêy!!!
void SPI_Flash_Write_Page(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u16 i;
SPI_FLASH_Write_Enable(); //SET WEL
SPI_FLASH_CS=0; //ê1ÄüÆ÷¼t
SPI2_ReadWriteByte(W25X_PageProgram); //·¢ËíD′ò3Ãüáî
SPI2_ReadWriteByte((u8)((WriteAddr)>>16)); //·¢Ëí24bitμØÖ·
SPI2_ReadWriteByte((u8)((WriteAddr)>>8));
SPI2_ReadWriteByte((u8)WriteAddr);
for(i=0;i<NumByteToWrite;i++)SPI2_ReadWriteByte(pBuffer[i]);//Ñ-»·D′êy
SPI_FLASH_CS=1; //è¡ÏûƬѡ
SPI_Flash_Wait_Busy(); //μè′yD′èë½áêø
}
//ÎT¼ìÑéD′SPI FLASH
//±ØDëè·±£ËùD′μÄμØÖ··¶Î§ÄúμÄêy¾Yè«2¿Îa0XFF,·ñÔòÔú·Ç0XFF′|D′èëμÄêy¾Y½«ê§°ü!
//¾ßóD×Ô¶ˉ»»ò31|Äü
//ÔúÖ¸¶¨μØÖ·¿aê¼D′èëÖ¸¶¨3¤¶èμÄêy¾Y,μ«êÇòaè·±£μØÖ·2»Ô½½ç!
//pBuffer:êy¾Y′æ′¢Çø
//WriteAddr:¿aê¼D′èëμÄμØÖ·(24bit)
//NumByteToWrite:òaD′èëμÄ×Ö½úêy(×î′ó65535)
//CHECK OK
void SPI_Flash_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u16 pageremain;
pageremain=256-WriteAddr%256; //μ¥ò3ê£óàμÄ×Ö½úêy
if(NumByteToWrite<=pageremain)pageremain=NumByteToWrite;//2»′óóú256¸ö×Ö½ú
while(1)
{
SPI_Flash_Write_Page(pBuffer,WriteAddr,pageremain);
if(NumByteToWrite==pageremain)break;//D′èë½áêøáË
else //NumByteToWrite>pageremain
{
pBuffer+=pageremain;
WriteAddr+=pageremain;
NumByteToWrite-=pageremain; //¼õè¥òѾ-D′èëáËμÄ×Ö½úêy
if(NumByteToWrite>256)pageremain=256; //ò»′οéòÔD′èë256¸ö×Ö½ú
else pageremain=NumByteToWrite; //2»1»256¸ö×Ö½úáË
}
};
}
//D′SPI FLASH
//ÔúÖ¸¶¨μØÖ·¿aê¼D′èëÖ¸¶¨3¤¶èμÄêy¾Y
//¸Ãoˉêy′ø2á3y2ù×÷!
//pBuffer:êy¾Y′æ′¢Çø
//WriteAddr:¿aê¼D′èëμÄμØÖ·(24bit)
//NumByteToWrite:òaD′èëμÄ×Ö½úêy(×î′ó65535)
u8 SPI_FLASH_BUF[4096];
void SPI_Flash_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
u32 secpos;
u16 secoff;
u16 secremain;
u16 i;
secpos=WriteAddr/4096;//éèÇøμØÖ· 0~511 for w25x16
secoff=WriteAddr%4096;//ÔúéèÇøÄúμÄÆ«òÆ
secremain=4096-secoff;//éèÇøê£óà¿Õ¼ä′óD¡
if(NumByteToWrite<=secremain)secremain=NumByteToWrite;//2»′óóú4096¸ö×Ö½ú
while(1)
{
SPI_Flash_Read(SPI_FLASH_BUF,secpos*4096,4096);//¶á3öÕû¸öéèÇøμÄÄúèY
for(i=0;i<secremain;i++)//D£Ñéêy¾Y
{
if(SPI_FLASH_BUF[secoff+i]!=0XFF)break;//Dèòa2á3y
}
if(i<secremain)//Dèòa2á3y
{
SPI_Flash_Erase_Sector(secpos);//2á3yÕa¸öéèÇø
for(i=0;i<secremain;i++) //¸′ÖÆ
{
SPI_FLASH_BUF[i+secoff]=pBuffer[i];
}
SPI_Flash_Write_NoCheck(SPI_FLASH_BUF,secpos*4096,4096);//D′èëÕû¸öéèÇø
}else SPI_Flash_Write_NoCheck(pBuffer,WriteAddr,secremain);//D′òѾ-2á3yáËμÄ,Ö±½óD′èëéèÇøê£óàÇø¼ä.
if(NumByteToWrite==secremain)break;//D′èë½áêøáË
else//D′èëÎ′½áêø
{
secpos++;//éèÇøμØÖ·Ôö1
secoff=0;//Æ«òÆλÖÃÎa0
pBuffer+=secremain; //Ö¸ÕëÆ«òÆ
WriteAddr+=secremain;//D′μØÖ·Æ«òÆ
NumByteToWrite-=secremain; //×Ö½úêyμY¼õ
if(NumByteToWrite>4096)secremain=4096; //ÏÂò»¸öéèÇø»1êÇD′2»íê
else secremain=NumByteToWrite; //ÏÂò»¸öéèÇø¿éòÔD′íêáË
}
};
}
//2á3yÕû¸öD¾Æ¬
//ÕûƬ2á3yê±¼ä:
//W25X16:25s
//W25X32:40s
//W25X64:40s
//μè′yê±¼ä3¬3¤...
void SPI_Flash_Erase_Chip(void)
{
SPI_FLASH_Write_Enable(); //SET WEL
SPI_Flash_Wait_Busy();
SPI_FLASH_CS=0; //ê1ÄüÆ÷¼t
SPI2_ReadWriteByte(W25X_ChipErase); //·¢ËíƬ2á3yÃüáî
SPI_FLASH_CS=1; //è¡ÏûƬѡ
SPI_Flash_Wait_Busy(); //μè′yD¾Æ¬2á3y½áêø
}
//2á3yò»¸öéèÇø
//Dst_Addr:éèÇøμØÖ· 0~511 for w25x16
//2á3yò»¸öé½ÇøμÄ×îéùê±¼ä:150ms
void SPI_Flash_Erase_Sector(u32 Dst_Addr)
{
Dst_Addr*=4096;
SPI_FLASH_Write_Enable(); //SET WEL
SPI_Flash_Wait_Busy();
SPI_FLASH_CS=0; //ê1ÄüÆ÷¼t
SPI2_ReadWriteByte(W25X_SectorErase); //·¢ËíéèÇø2á3yÖ¸áî
SPI2_ReadWriteByte((u8)((Dst_Addr)>>16)); //·¢Ëí24bitμØÖ·
SPI2_ReadWriteByte((u8)((Dst_Addr)>>8));
SPI2_ReadWriteByte((u8)Dst_Addr);
SPI_FLASH_CS=1; //è¡ÏûƬѡ
SPI_Flash_Wait_Busy(); //μè′y2á3yíê3é
}
//μè′y¿ÕÏD
void SPI_Flash_Wait_Busy(void)
{
while ((SPI_Flash_ReadSR()&0x01)==0x01); // μè′yBUSYλÇå¿Õ
}
//½øèëμôμçÄ£ê½
void SPI_Flash_PowerDown(void)
{
SPI_FLASH_CS=0; //ê1ÄüÆ÷¼t
SPI2_ReadWriteByte(W25X_PowerDown); //·¢ËíμôμçÃüáî
SPI_FLASH_CS=1; //è¡ÏûƬѡ
delay_us(3); //μè′yTPD
}
//»½DÑ
void SPI_Flash_WAKEUP(void)
{
SPI_FLASH_CS=0; //ê1ÄüÆ÷¼t
SPI2_ReadWriteByte(W25X_ReleasePowerDown); // send W25X_PowerDown command 0xAB
SPI_FLASH_CS=1; //è¡ÏûƬѡ
delay_us(3); //μè′yTRES1
}
一周热门 更多>