SPI FLASH 读写都为0

2019-07-20 22:22发布

初学者请教,读ID,写数组再读写,显示都为0,是怎么回事?望大神赐教,代码如下:
[mw_shl_code=c,true]#include "usart.h"
#include "spi_flash.h"

//pin
#define SPI_SCLK_PIN   GPIO_Pin_3
#define SPI_MISO_PIN   GPIO_Pin_4
#define SPI_MOSI_PIN   GPIO_Pin_5
#define SPI_CS_PIN  GPIO_Pin_6
#define SPI_GPIO_PORT GPIOB

#define SPI_CS_LOW() GPIO_ResetBits(SPI_GPIO_PORT,SPI_CS_PIN) //Chip Select pin set low
#define SPI_CS_HIGH() GPIO_SetBits(SPI_GPIO_PORT,SPI_CS_PIN)  //Chip Select pin set low

static u8 SPI_FLASH_SendByte(u8 byte);


static void SPI_GPIO_Configuration(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        SPI_InitTypeDef SPI_InitStructure;       
       
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3,ENABLE);

        GPIO_InitStructure.GPIO_Pin = SPI_SCLK_PIN | SPI_MOSI_PIN | SPI_MISO_PIN;
        GPIO_InitStructure.GPIO_Mode =   GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd =  GPIO_PuPd_NOPULL;
        GPIO_Init(SPI_GPIO_PORT,&GPIO_InitStructure);    //init CLK MO MI

        GPIO_PinAFConfig(SPI_GPIO_PORT, SPI_SCLK_PIN, GPIO_AF_SPI3);
        GPIO_PinAFConfig(SPI_GPIO_PORT, SPI_MOSI_PIN, GPIO_AF_SPI3);
        GPIO_PinAFConfig(SPI_GPIO_PORT, SPI_MISO_PIN, GPIO_AF_SPI3);
       
       
        GPIO_InitStructure.GPIO_Pin = SPI_CS_PIN;   //init CS
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStructure.GPIO_PuPd =  GPIO_PuPd_UP;
        GPIO_Init(GPIOB,&GPIO_InitStructure);  
        GPIO_SetBits(GPIOB,GPIO_Pin_6);    //CS==1

        SPI_Cmd(SPI3, DISABLE);
        SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
        SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
        SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
        SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
        SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
        SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
        SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
        SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;   
        SPI_InitStructure.SPI_CRCPolynomial = 7;   
        SPI_Init(SPI3, &SPI_InitStructure);
        SPI_Cmd(SPI3, ENABLE);       
       
}


static u8 SPI_FLASH_SendByte(u8 byte)
{
        while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET);
        SPI_I2S_SendData(SPI3, byte);
        while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET);
        return SPI_I2S_ReceiveData(SPI3);
}


u32 Read_AT25_ID(void)
{
        u32 device_id = 0;

        SPI_CS_LOW();

        SPI_FLASH_SendByte(AT25_READ_ID);
        device_id |= SPI_FLASH_SendByte(0xff)<<16;
        device_id |= SPI_FLASH_SendByte(0xff)<<8;
        device_id |= SPI_FLASH_SendByte(0xff);
       
        SPI_CS_HIGH();

        return device_id;
}

static void SPI_FLASH_Write_Enable(void)
{
        SPI_CS_LOW();

        SPI_FLASH_SendByte(AT25_WRITE_ENABLE);

        SPI_CS_HIGH();
}

static  void SPI_FLASH_WaitForWriteEnd(void)
{
        uint8_t FLASH_Status = 0;

        SPI_CS_LOW();
        SPI_FLASH_SendByte(AT25_READ_STATUSREGISTER);

        do
        {
                FLASH_Status = SPI_FLASH_SendByte(0xff);
        }while((FLASH_Status&0x01) == 0x01);

        SPI_CS_HIGH();
}

static void SPI_FLASH_Write_StatusRegister1(void)
{
//        uint8_t FLASH_Status = 0;

        SPI_FLASH_Write_Enable();
        SPI_FLASH_WaitForWriteEnd();

        SPI_CS_LOW();

        SPI_FLASH_SendByte(AT25_WRITE_STATUSREGISTER_BYTE1);
        SPI_FLASH_SendByte(0x00);

        SPI_CS_HIGH();
}

//2á3yò&#187;&#184;&#246;4k&#191;é
//Dst_Addr:&#191;éμ&#216;&#214;· &#184;ù&#190;Yêμ&#188;êèYá&#191;éè&#214;&#195;
//2á3yò&#187;&#184;&#246;é&#189;&#199;&#248;μ&#196;×&#238;éùê±&#188;&#228;:150ms
static void SPI_FLASH_Erase_Block(u32 Block_Addr)
{
        Block_Addr*=4096;
        SPI_FLASH_Write_Enable();
    SPI_FLASH_WaitForWriteEnd();

        SPI_CS_LOW();
       
        SPI_FLASH_SendByte(AT25_ERASE_4K);
    SPI_FLASH_SendByte((u8)((Block_Addr)>>16));  //·¢&#203;í24bitμ&#216;&#214;·   
    SPI_FLASH_SendByte((u8)((Block_Addr)>>8));   
    SPI_FLASH_SendByte((u8)Block_Addr);  
       
        SPI_CS_HIGH();

        SPI_FLASH_WaitForWriteEnd();
}

static void SPI_FLASH_Erase_Chip(void)
{
        SPI_FLASH_Write_Enable();
        SPI_FLASH_WaitForWriteEnd();

        SPI_CS_LOW();
        SPI_FLASH_SendByte(AT25_ERASE_CHIP);
        SPI_CS_HIGH();

        SPI_FLASH_WaitForWriteEnd();
}


//&#182;áè&#161;SPI FLASH  
//&#212;ú&#214;&#184;&#182;¨μ&#216;&#214;·&#191;aê&#188;&#182;áè&#161;&#214;&#184;&#182;¨3¤&#182;èμ&#196;êy&#190;Y
//pBuffer:êy&#190;Y′&#230;′¢&#199;&#248;
//ReadAddr:&#191;aê&#188;&#182;áè&#161;μ&#196;μ&#216;&#214;·(24bit)
//NumByteToRead:òa&#182;áè&#161;μ&#196;×&#214;&#189;úêy(×&#238;′ó65535)
void AT25DF_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead)   
{
        u16 i;                                                                                       
        SPI_CS_LOW();                            //ê1&#196;ü&#198;÷&#188;t   
    SPI_FLASH_SendByte(AT25_READ_ARRAY_0B);         //·¢&#203;í&#182;áè&#161;&#195;üá&#238;   
    SPI_FLASH_SendByte((u8)((ReadAddr)>>16));  //·¢&#203;í24bitμ&#216;&#214;·   
    SPI_FLASH_SendByte((u8)((ReadAddr)>>8));   
    SPI_FLASH_SendByte((u8)ReadAddr);   
    for(i=0;i<NumByteToRead;i++)
        {
        pBuffer=SPI_FLASH_SendByte(0XFF);   //&#209;-&#187;·&#182;áêy  
    }
        SPI_CS_HIGH();                                                   
}  
//SPI&#212;úò&#187;ò3(0~65535)&#196;úD′è&#235;éùóú256&#184;&#246;×&#214;&#189;úμ&#196;êy&#190;Y
//&#212;ú&#214;&#184;&#182;¨μ&#216;&#214;·&#191;aê&#188;D′è&#235;×&#238;′ó256×&#214;&#189;úμ&#196;êy&#190;Y
//pBuffer:êy&#190;Y′&#230;′¢&#199;&#248;
//WriteAddr:&#191;aê&#188;D′è&#235;μ&#196;μ&#216;&#214;·(24bit)
//NumByteToWrite:òaD′è&#235;μ&#196;×&#214;&#189;úêy(×&#238;′ó256),&#184;&#195;êy2&#187;ó|&#184;&#195;3&#172;1y&#184;&#195;ò3μ&#196;ê£óà×&#214;&#189;úêy!!!         
void AT25DF_Write_Page(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)
{
        u16 i;  
    SPI_FLASH_Write_Enable();                  //SET WEL
        SPI_CS_LOW();                            //ê1&#196;ü&#198;÷&#188;t   
    SPI_FLASH_SendByte(AT25_PAGE_PROGRAME);      //·¢&#203;íD′ò3&#195;üá&#238;   
    SPI_FLASH_SendByte((u8)((WriteAddr)>>16)); //·¢&#203;í24bitμ&#216;&#214;·   
    SPI_FLASH_SendByte((u8)((WriteAddr)>>8));   
    SPI_FLASH_SendByte((u8)WriteAddr);   
    for(i=0;i<NumByteToWrite;i++)SPI_FLASH_SendByte(pBuffer);//&#209;-&#187;·D′êy  
        SPI_CS_HIGH();                            //è&#161;&#207;&#251;&#198;&#172;&#209;&#161;
        SPI_FLASH_WaitForWriteEnd();                                           //μè′yD′è&#235;&#189;áê&#248;
}
//&#206;T&#188;ì&#209;éD′SPI FLASH
//±&#216;D&#235;è·±£&#203;ùD′μ&#196;μ&#216;&#214;··&#182;&#206;§&#196;úμ&#196;êy&#190;Yè&#171;2&#191;&#206;a0XFF,·&#241;&#212;ò&#212;ú·&#199;0XFF′|D′è&#235;μ&#196;êy&#190;Y&#189;&#171;꧰ü!
//&#190;&#223;óD×&#212;&#182;ˉ&#187;&#187;ò31|&#196;ü
//&#212;ú&#214;&#184;&#182;¨μ&#216;&#214;·&#191;aê&#188;D′è&#235;&#214;&#184;&#182;¨3¤&#182;èμ&#196;êy&#190;Y,μ&#171;ê&#199;òaè·±£μ&#216;&#214;·2&#187;&#212;&#189;&#189;&#231;!
//pBuffer:êy&#190;Y′&#230;′¢&#199;&#248;
//WriteAddr:&#191;aê&#188;D′è&#235;μ&#196;μ&#216;&#214;·(24bit)
//NumByteToWrite:òaD′è&#235;μ&#196;×&#214;&#189;úêy(×&#238;′ó65535)
//CHECK OK
void AT25DF_Write_NoCheck(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)   
{                                           
        u16 pageremain;          
        pageremain=256-WriteAddr%256; //μ¥ò3ê£óàμ&#196;×&#214;&#189;úêy                             
        if(NumByteToWrite<=pageremain)pageremain=NumByteToWrite;//2&#187;′óóú256&#184;&#246;×&#214;&#189;ú
        while(1)   //one time write =pageremain
        {          
                AT25DF_Write_Page(pBuffer,WriteAddr,pageremain);  //&#207;èó&#195;á&#227;&#203;éμ&#196;&#191;&#213;&#188;&#228;
                if(NumByteToWrite==pageremain)break;//D′è&#235;&#189;áê&#248;á&#203;
                 else //NumByteToWrite>pageremain
                {
                        pBuffer+=pageremain;
                        WriteAddr+=pageremain;       

                        NumByteToWrite-=pageremain;                          //&#188;&#245;è¥ò&#209;&#190;-D′è&#235;á&#203;μ&#196;×&#214;&#189;úêy
                        if(NumByteToWrite>256)pageremain=256; //ò&#187;′&#206;&#191;éò&#212;D′è&#235;256&#184;&#246;×&#214;&#189;ú
                        else pageremain=NumByteToWrite;           //2&#187;1&#187;256&#184;&#246;×&#214;&#189;úá&#203;
                }
        };            
}
//D′SPI FLASH  
//&#212;ú&#214;&#184;&#182;¨μ&#216;&#214;·&#191;aê&#188;D′è&#235;&#214;&#184;&#182;¨3¤&#182;èμ&#196;êy&#190;Y
//&#184;&#195;oˉêy′&#248;2á3y2ù×÷!
//pBuffer:êy&#190;Y′&#230;′¢&#199;&#248;
//WriteAddr:&#191;aê&#188;D′è&#235;μ&#196;μ&#216;&#214;·(24bit)                                               
//NumByteToWrite:òaD′è&#235;μ&#196;×&#214;&#189;úêy(×&#238;′ó65535)   
u8 AT25DF_BUFFER[4096];                 
void AT25DF_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite)   
{
        u32 blockpos;
        u16 blockoff;
        u16 blockremain;          
        u16 i;   
        u8 * AT25DF_BUF;          
           AT25DF_BUF=AT25DF_BUFFER;             
        blockpos=WriteAddr/4096;//&#191;éμ&#216;&#214;·  
        blockoff=WriteAddr%4096;//&#212;ú&#191;é&#196;úμ&#196;&#198;&#171;ò&#198;
        blockremain=4096-blockoff;//&#191;éê£óà&#191;&#213;&#188;&#228;′óD&#161;   
        printf("ad:%X,nb:%X ",WriteAddr,NumByteToWrite);//2aê&#212;ó&#195;
        if(NumByteToWrite<=blockremain)blockremain=NumByteToWrite;//2&#187;′óóú4096&#184;&#246;×&#214;&#189;ú
        while(1)
        {       
                AT25DF_Read(AT25DF_BUF,blockpos*4096,4096);//&#182;á3&#246;&#213;&#251;&#184;&#246;&#191;éμ&#196;&#196;úèY
                for(i=0;i<blockremain;i++)//D£&#209;éêy&#190;Y
                {
                        if(AT25DF_BUF[blockoff+i]!=0XFF)break;//Dèòa2á3y            
                }
                if(i<blockremain)//Dèòa2á3y
                {
                        SPI_FLASH_Erase_Block(blockpos); //2á3y&#213;a&#184;&#246;&#191;é
                        for(i=0;i<blockremain;i++)           //&#184;′&#214;&#198;
                        {
                                AT25DF_BUF[i+blockoff]=pBuffer;          
                        }
                        AT25DF_Write_NoCheck(AT25DF_BUF,blockpos*4096,4096);//D′è&#235;&#213;&#251;&#184;&#246;&#191;é  

                }else AT25DF_Write_NoCheck(pBuffer,WriteAddr,blockremain);//D′ò&#209;&#190;-2á3yá&#203;μ&#196;,&#214;±&#189;óD′è&#235;&#191;éê£óà&#199;&#248;&#188;&#228;.                                   
                if(NumByteToWrite==blockremain)break;//D′è&#235;&#189;áê&#248;á&#203;
                else//D′è&#235;&#206;′&#189;áê&#248;
                {
                        blockpos++;//&#191;éμ&#216;&#214;·&#212;&#246;1
                        blockoff=0;//&#198;&#171;ò&#198;&#206;&#187;&#214;&#195;&#206;a0          

                           pBuffer+=blockremain;  //&#214;&#184;&#213;&#235;&#198;&#171;ò&#198;
                        WriteAddr+=blockremain;//D′μ&#216;&#214;·&#198;&#171;ò&#198;          
                           NumByteToWrite-=blockremain;                                //×&#214;&#189;úêyμY&#188;&#245;
                        if(NumByteToWrite>4096)blockremain=4096;        //&#207;&#194;ò&#187;&#184;&#246;&#191;é&#187;1ê&#199;D′2&#187;íê
                        else blockremain=NumByteToWrite;                        //&#207;&#194;ò&#187;&#184;&#246;&#191;é&#191;éò&#212;D′íêá&#203;
                }         
        }         
}

void spi_flash_sample(void)
{
        char text[]={"i am a chinese"};
        char text2[15];
       
        SPI_GPIO_Configuration();
        printf("spi init finished ");

        printf("flash id is: %d ",Read_AT25_ID());
       
        SPI_FLASH_Erase_Chip();
        printf("spi erase finished ");
       
       
        AT25DF_Write((u8*)text,0x00,sizeof(text));
        printf("spi write finished ");       
       
        AT25DF_Read((u8*)text2,0x00,sizeof(text));       
        printf("spi read data : %s ",text2);       
       
}
[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。