野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10109|回复: 5

W25Q128 问题?可以读 但不能写!

[复制链接]
发表于 2017-6-9 11:35:15 | 显示全部楼层 |阅读模式
w25q.PNG
读id 正常 !  写入不正常!
度扇区都是0xff!
程序如下
/**
  ******************************************************************************
  * @file    USART/Printf/main.c
  * @author  MCD Application Team
  * @version V3.5.0
  * @date    08-April-2011
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include "stm32f10x_usart.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include <stddef.h>
#include "stm32f10x.h"
#include <stdlib.h>
#include <string.h>
#include "bsp_spi_flash.h"
#define  FLASH_WriteAddress     0x00000
#define  FLASH_ReadAddress      FLASH_WriteAddress
#define  FLASH_SectorToErase    FLASH_WriteAddress
int qidong;
SPI_InitTypeDef  SPI_InitStructure;

void spicon(){
        SystemInit();
        // Create Structs
        GPIO_InitTypeDef GPIO_InitStructure;

        USART_InitTypeDef USART_InitStructure;
        USART_ClockInitTypeDef USART_ClockInitStructure;

        // Enable clocks
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1|RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

        // USART1 RX-Pin initialize
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
//        GPIO_InitStructure.GPIO_Speed=  GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        // USART1 TX-Pin initialize
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
//        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
//        GPIO_SetBits(GPIOA,GPIO_Pin_9);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;  // PB12 í&#198;íì
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  //í&#198;íìê&#228;3&#246;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
//        GPIO_SetBits(GPIOA,GPIO_Pin_4);
        // USART and clock initialize
        USART_ClockStructInit(&USART_ClockInitStructure);
        USART_ClockInit(USART1, &USART_ClockInitStructure);


// USART and clock initialize
       
        USART_InitStructure.USART_BaudRate = 9600;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
        USART_InitStructure.USART_Parity = USART_Parity_No;
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_Init(USART1, &USART_InitStructure);
  USART_Cmd(USART1, ENABLE);
  SPI_FLASH_Init();
  qidong=1;       
}


int main(void)
{
spicon();
qidong =0;
u8 size4k[4096]={0};

u32        Temp = SPI_FLASH_ReadID();       
FlagStatus rf ;


int io=0;
while (io<8){
rf=        USART_GetFlagStatus(USART1, USART_FLAG_TC);
   if( rf != RESET){
                 char pp[8];
           sprintf(pp , "%X" ,Temp);

                 USART_SendData(USART1,pp[io]);
                 io++;
         }
}

                FlagStatus Tf ;
         u16 rxdata;
        int longzi=0;
        while(1){

  Tf=USART_GetFlagStatus(USART1,USART_FLAG_RXNE);
                if(Tf ==SET){
                  rxdata= USART_ReceiveData(USART1);
      size4k[longzi]=(char)rxdata;
                        longzi++;
                        if (rxdata==0x000D){
                  longzi=0;
                 int yaofadezi=0;
       SPI_FLASH_SectorErase(FLASH_SectorToErase);
                        // SPI_FLASH_BulkErase();
                        //                                SPI_FLASH_WriteEnable();
                         
                      SPI_FLASH_PageWrite(size4k,FLASH_WriteAddress,SPI_FLASH_PageSize);
                     SPI_FLASH_BufferRead(size4k,0,256);
                               
                        // SPI_Flash_SR();
                        while(yaofadezi<256){
                         rf=USART_GetFlagStatus(USART1, USART_FLAG_TC);
                         if (rf !=RESET){
                                 char jj;
                                  sprintf(&jj , "%d" ,size4k[yaofadezi]);
                           USART_SendData(USART1,jj);
                                  yaofadezi++;
                         }
                       
                       
                  }
                        memset(size4k,0,4096*sizeof(char));
                        }
                }

                rf=        USART_GetFlagStatus(USART1, USART_FLAG_TC);
   if( (rf != RESET)&&(rxdata!=NULL) ){
               
                 USART_SendData(USART1,rxdata);
           rxdata=NULL;
             }
            
        }
}

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/



回复

使用道具 举报

 楼主| 发表于 2017-6-9 11:36:31 | 显示全部楼层
驱动 如下:
/**
  ******************************************************************************
  * @file    bsp_xxx.c
  * @author  STMicroelectronics
  * @version V1.0
  * @date    2013-xx-xx
  * @brief   spi flash μ×2&#227;ó|ó&#195;oˉêybsp
  ******************************************************************************
  * @attention
  *
  * êμ&#209;é&#198;&#189;ì¨:ò°&#187;e iSO STM32 &#191;a·¢°&#229;
  * &#194;&#219;ì3    :http://www.chuxue123.com
  * ì&#212;±|    :http://firestm32.taobao.com
  *
  ******************************************************************************
  */
  
#include "bsp_spi_flash.h"
#include <string.h>
#include <stdio.h>

u8 SPI_Flash_SR(void){
  u8 byte=0;
        SPI_FLASH_CS_LOW();
        SPI_FLASH_SendByte(W25X_ReadStatusReg);
        byte=SPI_FLASH_SendByte(0Xff);
        SPI_FLASH_CS_HIGH();
        return byte;
}
// ′í&#206;ó′òó&#161;
void eerping(char * errsr){
  int Num=0;
        FlagStatus dexian; //ê&#199;·&#241;μ&#195;&#207;D·¢£&#161;
   while (Num<strlen(errsr)+1){
           dexian= USART_GetFlagStatus(USART1, USART_FLAG_TC);
    if( dexian != RESET){
                 USART_SendData(USART1,errsr[Num]);
                 Num++;
                }
         }
}
/**
  * @brief  SPI_FLASH3&#245;ê&#188;&#187;ˉ
  * @param  &#206;T
  * @retval &#206;T
  */

void SPI_FLASH_Init(void)
{
  SPI_InitTypeDef  SPI_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
       
        /* ê1&#196;üSPIê±&#214;ó */
        SPI_APBxClock_FUN ( SPI_CLK, ENABLE );

  /* &#197;&#228;&#214;&#195;SPIμ&#196; CSòy&#189;&#197;£&#172;&#198;&#213;í¨IO&#188;′&#191;é */
        SPI_CS_APBxClock_FUN ( SPI_CS_CLK, ENABLE );
  GPIO_InitStructure.GPIO_Pin = SPI_CS_PIN;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(SPI_CS_PORT, &GPIO_InitStructure);
       
  /* &#197;&#228;&#214;&#195;SPIμ&#196; SCKòy&#189;&#197;*/
        SPI_SCK_APBxClock_FUN ( SPI_SCK_CLK, ENABLE );
  GPIO_InitStructure.GPIO_Pin = SPI_SCK_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(SPI_SCK_PORT, &GPIO_InitStructure);

  /* &#197;&#228;&#214;&#195;SPIμ&#196; MISOòy&#189;&#197;*/
        SPI_MISO_APBxClock_FUN ( SPI_MISO_CLK, ENABLE );
  GPIO_InitStructure.GPIO_Pin = SPI_MISO_PIN;
  GPIO_Init(SPI_MISO_PORT, &GPIO_InitStructure);

  /* &#197;&#228;&#214;&#195;SPIμ&#196; MOSIòy&#189;&#197;*/
        SPI_MOSI_APBxClock_FUN ( SPI_MOSI_CLK, ENABLE );
  GPIO_InitStructure.GPIO_Pin = SPI_MOSI_PIN;
  GPIO_Init(SPI_MOSI_PORT, &GPIO_InitStructure);

  /* í£&#214;1D&#197;o&#197; FLASH: CSòy&#189;&#197;&#184;&#223;μ&#231;&#198;&#189;*/
  SPI_FLASH_CS_HIGH();

  /* SPI &#196;£ê&#189;&#197;&#228;&#214;&#195; */
  // FLASHD&#190;&#198;&#172; &#214;§3&#214;SPI&#196;£ê&#189;0&#188;°&#196;£ê&#189;3£&#172;&#190;Y′&#203;éè&#214;&#195;CPOL CPHA
  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_4;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPIx , &SPI_InitStructure);

  /* ê1&#196;ü SPI  */
  SPI_Cmd(SPIx , ENABLE);
       
}
/**
  * @brief  2á3yFLASHéè&#199;&#248;
  * @param  SectorAddr£oòa2á3yμ&#196;éè&#199;&#248;μ&#216;&#214;·
  * @retval &#206;T
  */
void SPI_FLASH_SectorErase(u32 SectorAddr)
{
  /* ·¢&#203;íFLASHD′ê1&#196;ü&#195;üá&#238; */
  SPI_FLASH_WriteEnable();
  SPI_FLASH_WaitForWriteEnd();
  /* 2á3yéè&#199;&#248; */
  /* &#209;&#161;&#212;&#241;FLASH: CSμíμ&#231;&#198;&#189; */
  SPI_FLASH_CS_LOW();
  /* ·¢&#203;íéè&#199;&#248;2á3y&#214;&#184;á&#238;*/
  SPI_FLASH_SendByte(W25X_SectorErase);
  /*·¢&#203;í2á3yéè&#199;&#248;μ&#216;&#214;·μ&#196;&#184;&#223;&#206;&#187;*/
  SPI_FLASH_SendByte((u8)((SectorAddr & 0xFF0000) >> 16));
  /* ·¢&#203;í2á3yéè&#199;&#248;μ&#216;&#214;·μ&#196;&#214;D&#206;&#187; */
  SPI_FLASH_SendByte((u8)((SectorAddr & 0xFF00) >> 8));
  /* ·¢&#203;í2á3yéè&#199;&#248;μ&#216;&#214;·μ&#196;μí&#206;&#187; */
  SPI_FLASH_SendByte((u8)(SectorAddr & 0xFF));
  /* í£&#214;1D&#197;o&#197; FLASH: CS &#184;&#223;μ&#231;&#198;&#189; */
  SPI_FLASH_CS_HIGH();
  /* μè′y2á3yíê±&#207;*/
  SPI_FLASH_WaitForWriteEnd();
}

/**
  * @brief  2á3yFLASHéè&#199;&#248;£&#172;&#213;&#251;&#198;&#172;2á3y
  * @param  &#206;T
  * @retval &#206;T
  */
void SPI_FLASH_BulkErase(void)
{
  /* ·¢&#203;íFLASHD′ê1&#196;ü&#195;üá&#238; */
  SPI_FLASH_WriteEnable();

  /* &#213;&#251;&#191;é Erase */
  /* &#209;&#161;&#212;&#241;FLASH: CSμíμ&#231;&#198;&#189; */
  SPI_FLASH_CS_LOW();
  /* ·¢&#203;í&#213;&#251;&#191;é2á3y&#214;&#184;á&#238;*/
  SPI_FLASH_SendByte(W25X_ChipErase);
  /* í£&#214;1D&#197;o&#197; FLASH: CS &#184;&#223;μ&#231;&#198;&#189; */
  SPI_FLASH_CS_HIGH();

  /* μè′y2á3yíê±&#207;*/
  SPI_FLASH_WaitForWriteEnd();
}

/**
  * @brief  &#182;&#212;FLASH°′ò3D′è&#235;êy&#190;Y£&#172;μ÷ó&#195;±&#190;oˉêyD′è&#235;êy&#190;Y&#199;°Dèòa&#207;è2á3yéè&#199;&#248;
  * @param        pBuffer£&#172;òaD′è&#235;êy&#190;Yμ&#196;&#214;&#184;&#213;&#235;
  * @param WriteAddr£&#172;D′è&#235;μ&#216;&#214;·
  * @param  NumByteToWrite£&#172;D′è&#235;êy&#190;Y3¤&#182;è£&#172;±&#216;D&#235;D&#161;óúμèóúSPI_FLASH_PerWritePageSize
  * @retval &#206;T
  */
void SPI_FLASH_PageWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
{
  /* ·¢&#203;íFLASHD′ê1&#196;ü&#195;üá&#238; */
  SPI_FLASH_WriteEnable();

  /* &#209;&#161;&#212;&#241;FLASH: CSμíμ&#231;&#198;&#189; */
  SPI_FLASH_CS_LOW();
  /* D′ò3D′&#214;&#184;á&#238;*/
  SPI_FLASH_SendByte(W25X_PageProgram);
  /*·¢&#203;íD′μ&#216;&#214;·μ&#196;&#184;&#223;&#206;&#187;*/
  SPI_FLASH_SendByte((u8)((WriteAddr & 0xFF0000) >> 16));
  /*·¢&#203;íD′μ&#216;&#214;·μ&#196;&#214;D&#206;&#187;*/
  SPI_FLASH_SendByte((u8)((WriteAddr & 0xFF00) >> 8));
  /*·¢&#203;íD′μ&#216;&#214;·μ&#196;μí&#206;&#187;*/
  SPI_FLASH_SendByte((u8)(WriteAddr & 0xFF));

  if(NumByteToWrite > SPI_FLASH_PerWritePageSize)
  {
     NumByteToWrite = SPI_FLASH_PerWritePageSize;
  //   FLASH_ERROR("SPI_FLASH_PageWrite too large!");
                 char * Pagestr ="SPI_FLASH_PageWrite too large!";
                 eerping(Pagestr);
  }
  /* D′è&#235;êy&#190;Y*/
  while (NumByteToWrite--)
  {
    /* ·¢&#203;íμ±&#199;°òaD′è&#235;μ&#196;×&#214;&#189;úêy&#190;Y */
    char xie=8;
                xie=*pBuffer;
    SPI_FLASH_SendByte(*pBuffer);
    /* &#214;&#184;&#207;ò&#207;&#194;ò&#187;×&#214;&#189;úêy&#190;Y */
    pBuffer++;
  }

  /* í£&#214;1D&#197;o&#197; FLASH: CS &#184;&#223;μ&#231;&#198;&#189; */
  SPI_FLASH_CS_HIGH();

  /* μè′yD′è&#235;íê±&#207;*/
  SPI_FLASH_WaitForWriteEnd();
}

/**
  * @brief  &#182;&#212;FLASHD′è&#235;êy&#190;Y£&#172;μ÷ó&#195;±&#190;oˉêyD′è&#235;êy&#190;Y&#199;°Dèòa&#207;è2á3yéè&#199;&#248;
  * @param        pBuffer£&#172;òaD′è&#235;êy&#190;Yμ&#196;&#214;&#184;&#213;&#235;
  * @param  WriteAddr£&#172;D′è&#235;μ&#216;&#214;·
  * @param  NumByteToWrite£&#172;D′è&#235;êy&#190;Y3¤&#182;è
  * @retval &#206;T
  */
void SPI_FLASH_BufferWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite)
{
  u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0;
       
        /*mod&#212;&#203;&#203;&#227;&#199;óóà£&#172;è&#244;writeAddrê&#199;SPI_FLASH_PageSize&#213;&#251;êy±&#182;£&#172;&#212;&#203;&#203;&#227;&#189;á1&#251;Addr&#214;μ&#206;a0*/
  Addr = WriteAddr % SPI_FLASH_PageSize;
       
        /*2&#238;count&#184;&#246;êy&#190;Y&#214;μ£&#172;&#184;&#213;o&#195;&#191;éò&#212;&#182;&#212;&#198;&#235;μ&#189;ò3μ&#216;&#214;·*/
  count = SPI_FLASH_PageSize - Addr;
        /*&#188;&#198;&#203;&#227;3&#246;òaD′&#182;àéù&#213;&#251;êyò3*/
  NumOfPage =  NumByteToWrite / SPI_FLASH_PageSize;
        /*mod&#212;&#203;&#203;&#227;&#199;óóà£&#172;&#188;&#198;&#203;&#227;3&#246;ê£óà2&#187;&#194;úò&#187;ò3μ&#196;×&#214;&#189;úêy*/
  NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;
       
        /* Addr=0,&#212;òWriteAddr &#184;&#213;o&#195;°′ò3&#182;&#212;&#198;&#235; aligned  */
  if (Addr == 0)
  {
                /* NumByteToWrite < SPI_FLASH_PageSize */
    if (NumOfPage == 0)
    {
      SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
    }
    else /* NumByteToWrite > SPI_FLASH_PageSize */
    {
                        /*&#207;è°&#209;&#213;&#251;êyò3&#182;&#188;D′á&#203;*/
      while (NumOfPage--)
      {
        SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
        WriteAddr +=  SPI_FLASH_PageSize;
        pBuffer += SPI_FLASH_PageSize;
      }
                        /*è&#244;óD&#182;àóàμ&#196;2&#187;&#194;úò&#187;ò3μ&#196;êy&#190;Y£&#172;°&#209;&#203;üD′íê*/
      SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
    }
  }
        /* è&#244;μ&#216;&#214;·ó&#235; SPI_FLASH_PageSize 2&#187;&#182;&#212;&#198;&#235;  */
  else
  {
                /* NumByteToWrite < SPI_FLASH_PageSize */
    if (NumOfPage == 0)
    {
                        /*μ±&#199;°ò3ê£óàμ&#196;count&#184;&#246;&#206;&#187;&#214;&#195;±èNumOfSingleD&#161;£&#172;ò&#187;ò3D′2&#187;íê*/
      if (NumOfSingle > count)
      {
        temp = NumOfSingle - count;
                                /*&#207;èD′&#194;úμ±&#199;°ò3*/
        SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);
                               
        WriteAddr +=  count;
        pBuffer += count;
                                /*&#212;ùD′ê£óàμ&#196;êy&#190;Y*/
        SPI_FLASH_PageWrite(pBuffer, WriteAddr, temp);
      }
      else /*μ±&#199;°ò3ê£óàμ&#196;count&#184;&#246;&#206;&#187;&#214;&#195;&#196;üD′íêNumOfSingle&#184;&#246;êy&#190;Y*/
      {
        SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
      }
    }
    else /* NumByteToWrite > SPI_FLASH_PageSize */
    {
                        /*μ&#216;&#214;·2&#187;&#182;&#212;&#198;&#235;&#182;à3&#246;μ&#196;count·&#214;&#191;a′|àí£&#172;2&#187;&#188;óè&#235;&#213;a&#184;&#246;&#212;&#203;&#203;&#227;*/
      NumByteToWrite -= count;
      NumOfPage =  NumByteToWrite / SPI_FLASH_PageSize;
      NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;
                       
                        /* &#207;èD′íêcount&#184;&#246;êy&#190;Y£&#172;&#206;aμ&#196;ê&#199;è&#195;&#207;&#194;ò&#187;′&#206;òaD′μ&#196;μ&#216;&#214;·&#182;&#212;&#198;&#235; */
      SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);
                       
                        /* &#189;ó&#207;&#194;à′&#190;í&#214;&#216;&#184;′μ&#216;&#214;·&#182;&#212;&#198;&#235;μ&#196;&#199;é&#191;&#246; */
      WriteAddr +=  count;
      pBuffer += count;
                        /*°&#209;&#213;&#251;êyò3&#182;&#188;D′á&#203;*/
      while (NumOfPage--)
      {
        SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
        WriteAddr +=  SPI_FLASH_PageSize;
        pBuffer += SPI_FLASH_PageSize;
      }
                        /*è&#244;óD&#182;àóàμ&#196;2&#187;&#194;úò&#187;ò3μ&#196;êy&#190;Y£&#172;°&#209;&#203;üD′íê*/
      if (NumOfSingle != 0)
      {
        SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
      }
    }
  }
}

/**
  * @brief  &#182;áè&#161;FLASHêy&#190;Y
  * @param         pBuffer£&#172;′&#230;′¢&#182;á3&#246;êy&#190;Yμ&#196;&#214;&#184;&#213;&#235;
  * @param   ReadAddr£&#172;&#182;áè&#161;μ&#216;&#214;·
  * @param   NumByteToRead£&#172;&#182;áè&#161;êy&#190;Y3¤&#182;è
  * @retval &#206;T
  */
void SPI_FLASH_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead)
{
  /* &#209;&#161;&#212;&#241;FLASH: CSμíμ&#231;&#198;&#189; */
  SPI_FLASH_CS_LOW();

  /* ·¢&#203;í &#182;á &#214;&#184;á&#238; */
  SPI_FLASH_SendByte(W25X_ReadData);

  /* ·¢&#203;í &#182;á μ&#216;&#214;·&#184;&#223;&#206;&#187; */
  SPI_FLASH_SendByte((u8)((ReadAddr & 0xFF0000) >> 16));
  /* ·¢&#203;í &#182;á μ&#216;&#214;·&#214;D&#206;&#187; */
  SPI_FLASH_SendByte((u8)((ReadAddr& 0xFF00) >> 8));
  /* ·¢&#203;í &#182;á μ&#216;&#214;·μí&#206;&#187; */
  SPI_FLASH_SendByte((u8)(ReadAddr & 0xFF));
       
        /* &#182;áè&#161;êy&#190;Y */

        while (NumByteToRead--) /* while there is data to be read */
  {
    /* &#182;áè&#161;ò&#187;&#184;&#246;×&#214;&#189;ú*/
               
   *pBuffer = SPI_FLASH_SendByte(0Xff);
    /* &#214;&#184;&#207;ò&#207;&#194;ò&#187;&#184;&#246;×&#214;&#189;ú&#187;o3&#229;&#199;&#248; */
     pBuffer++;
  }

  /* í£&#214;1D&#197;o&#197; FLASH: CS &#184;&#223;μ&#231;&#198;&#189; */
  SPI_FLASH_CS_HIGH();
}

/**
  * @brief  &#182;áè&#161;FLASH ID
  * @param         &#206;T
  * @retval FLASH ID
  */
u32 SPI_FLASH_ReadID(void)
{
  u32 Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;

  /* &#191;aê&#188;í¨&#209;&#182;£oCSμíμ&#231;&#198;&#189; */
  SPI_FLASH_CS_LOW();

  /* ·¢&#203;íJEDEC&#214;&#184;á&#238;£&#172;&#182;áè&#161;ID */
  SPI_FLASH_SendByte(W25X_JedecDeviceID);

  /* &#182;áè&#161;ò&#187;&#184;&#246;×&#214;&#189;úêy&#190;Y */
  Temp0 = SPI_FLASH_SendByte(Dummy_Byte);

  /* &#182;áè&#161;ò&#187;&#184;&#246;×&#214;&#189;úêy&#190;Y */
  Temp1 = SPI_FLASH_SendByte(Dummy_Byte);

  /* &#182;áè&#161;ò&#187;&#184;&#246;×&#214;&#189;úêy&#190;Y */
  Temp2 = SPI_FLASH_SendByte(Dummy_Byte);

/* í£&#214;1í¨&#209;&#182;£oCS&#184;&#223;μ&#231;&#198;&#189; */
  SPI_FLASH_CS_HIGH();

  /*°&#209;êy&#190;Y×éo&#207;&#198;eà′£&#172;×÷&#206;aoˉêyμ&#196;·μ&#187;&#216;&#214;μ*/
        Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2;

  return Temp;
}
/**
  * @brief  &#182;áè&#161;FLASH Device ID
  * @param         &#206;T
  * @retval FLASH Device ID
  */
u32 SPI_FLASH_ReadDeviceID(void)
{
  u32 Temp = 0;

  /* Select the FLASH: Chip Select low */
  SPI_FLASH_CS_LOW();

  /* Send "RDID " instruction */
  SPI_FLASH_SendByte(W25X_DeviceID);
  SPI_FLASH_SendByte(Dummy_Byte);
  SPI_FLASH_SendByte(Dummy_Byte);
  SPI_FLASH_SendByte(Dummy_Byte);
  
  /* Read a byte from the FLASH */
  Temp = SPI_FLASH_SendByte(Dummy_Byte);

  /* Deselect the FLASH: Chip Select high */
  SPI_FLASH_CS_HIGH();

  return Temp;
}
/*******************************************************************************
* Function Name  : SPI_FLASH_StartReadSequence
* Description    : Initiates a read data byte (READ) sequence from the Flash.
*                  This is done by driving the /CS line low to select the device,
*                  then the READ instruction is transmitted followed by 3 bytes
*                  address. This function exit and keep the /CS line low, so the
*                  Flash still being selected. With this technique the whole
*                  content of the Flash is read with a single READ instruction.
* Input          : - ReadAddr : FLASH's internal address to read from.
* Output         : None
* Return         : None
*******************************************************************************/
void SPI_FLASH_StartReadSequence(u32 ReadAddr)
{
  /* Select the FLASH: Chip Select low */
  SPI_FLASH_CS_LOW();

  /* Send "Read from Memory " instruction */
  SPI_FLASH_SendByte(W25X_ReadData);

  /* Send the 24-bit address of the address to read from -----------------------*/
  /* Send ReadAddr high nibble address byte */
  SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
  /* Send ReadAddr medium nibble address byte */
  SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
  /* Send ReadAddr low nibble address byte */
  SPI_FLASH_SendByte(ReadAddr & 0xFF);
}


/**
  * @brief  ê1ó&#195;SPI&#182;áè&#161;ò&#187;&#184;&#246;×&#214;&#189;úμ&#196;êy&#190;Y
  * @param  &#206;T
  * @retval ·μ&#187;&#216;&#189;óê&#213;μ&#189;μ&#196;êy&#190;Y
  */
u8 SPI_FLASH_ReadByte(void)
{
  return (SPI_FLASH_SendByte(Dummy_Byte));
}

/**
  * @brief  ê1ó&#195;SPI·¢&#203;íò&#187;&#184;&#246;×&#214;&#189;úμ&#196;êy&#190;Y
  * @param  byte£oòa·¢&#203;íμ&#196;êy&#190;Y
  * @retval ·μ&#187;&#216;&#189;óê&#213;μ&#189;μ&#196;êy&#190;Y
  */
u8 SPI_FLASH_SendByte(u8 byte)
{
  /* μè′y·¢&#203;í&#187;o3&#229;&#199;&#248;&#206;a&#191;&#213;£&#172;TXEê&#194;&#188;t */
  while (SPI_I2S_GetFlagStatus(SPIx , SPI_I2S_FLAG_TXE) == RESET);

  /* D′è&#235;êy&#190;Y&#188;&#196;′&#230;&#198;÷£&#172;°&#209;òaD′è&#235;μ&#196;êy&#190;YD′è&#235;·¢&#203;í&#187;o3&#229;&#199;&#248; */
  SPI_I2S_SendData(SPIx , byte);

  /* μè′y&#189;óê&#213;&#187;o3&#229;&#199;&#248;·&#199;&#191;&#213;£&#172;RXNEê&#194;&#188;t */
  while (SPI_I2S_GetFlagStatus(SPIx , SPI_I2S_FLAG_RXNE) == RESET);

  /* &#182;áè&#161;êy&#190;Y&#188;&#196;′&#230;&#198;÷£&#172;&#187;&#241;è&#161;&#189;óê&#213;&#187;o3&#229;&#199;&#248;êy&#190;Y */
  return SPI_I2S_ReceiveData(SPIx );
}

/**
  * @brief  ê1ó&#195;SPI·¢&#203;íá&#189;&#184;&#246;×&#214;&#189;úμ&#196;êy&#190;Y
  * @param  byte£oòa·¢&#203;íμ&#196;êy&#190;Y
  * @retval ·μ&#187;&#216;&#189;óê&#213;μ&#189;μ&#196;êy&#190;Y
  */
u16 SPI_FLASH_SendHalfWord(u16 HalfWord)
{
  /* μè′y·¢&#203;í&#187;o3&#229;&#199;&#248;&#206;a&#191;&#213;£&#172;TXEê&#194;&#188;t */
  while (SPI_I2S_GetFlagStatus(SPIx , SPI_I2S_FLAG_TXE) == RESET);

  /* D′è&#235;êy&#190;Y&#188;&#196;′&#230;&#198;÷£&#172;°&#209;òaD′è&#235;μ&#196;êy&#190;YD′è&#235;·¢&#203;í&#187;o3&#229;&#199;&#248; */
  SPI_I2S_SendData(SPIx , HalfWord);

  /* μè′y&#189;óê&#213;&#187;o3&#229;&#199;&#248;·&#199;&#191;&#213;£&#172;RXNEê&#194;&#188;t */
  while (SPI_I2S_GetFlagStatus(SPIx , SPI_I2S_FLAG_RXNE) == RESET);

  /* &#182;áè&#161;êy&#190;Y&#188;&#196;′&#230;&#198;÷£&#172;&#187;&#241;è&#161;&#189;óê&#213;&#187;o3&#229;&#199;&#248;êy&#190;Y */
  return SPI_I2S_ReceiveData(SPIx );
}

/**
  * @brief  &#207;òFLASH·¢&#203;í D′ê1&#196;ü &#195;üá&#238;
  * @param  none
  * @retval none
  */
void SPI_FLASH_WriteEnable(void)
{
  /* í¨&#209;&#182;&#191;aê&#188;£oCSμí */
  SPI_FLASH_CS_LOW();

  /* ·¢&#203;íD′ê1&#196;ü&#195;üá&#238;*/
  SPI_FLASH_SendByte(W25X_WriteEnable);

  /*í¨&#209;&#182;&#189;áê&#248;£oCS&#184;&#223; */
  SPI_FLASH_CS_HIGH();
}

/* WIP(busy)±ê&#214;&#190;£&#172;FLASH&#196;ú2&#191;&#213;y&#212;úD′è&#235; */
#define WIP_Flag                  0x01

/**
  * @brief  μè′yWIP(BUSY)±ê&#214;&#190;±&#187;&#214;&#195;0£&#172;&#188;′μè′yμ&#189;FLASH&#196;ú2&#191;êy&#190;YD′è&#235;íê±&#207;
  * @param  none
  * @retval none
  */
void SPI_FLASH_WaitForWriteEnd(void)
{
  u8 FLASH_Status = 0;

  /* &#209;&#161;&#212;&#241; FLASH: CS μí */
  SPI_FLASH_CS_LOW();

  /* ·¢&#203;í &#182;á×′ì&#172;&#188;&#196;′&#230;&#198;÷ &#195;üá&#238; */
  SPI_FLASH_SendByte(W25X_ReadStatusReg);

  /* è&#244;FLASH&#195;|&#194;μ£&#172;&#212;òμè′y */
  do
  {
                /* &#182;áè&#161;FLASHD&#190;&#198;&#172;μ&#196;×′ì&#172;&#188;&#196;′&#230;&#198;÷ */
    FLASH_Status = SPI_FLASH_SendByte(Dummy_Byte);         
  }
  while ((FLASH_Status & WIP_Flag) == SET);  /* &#213;y&#212;úD′è&#235;±ê&#214;&#190; */

  /* í£&#214;1D&#197;o&#197;  FLASH: CS &#184;&#223; */
  SPI_FLASH_CS_HIGH();
}


//&#189;&#248;è&#235;μ&#244;μ&#231;&#196;£ê&#189;
void SPI_Flash_PowerDown(void)   
{
  /* í¨&#209;&#182;&#191;aê&#188;£oCSμí */
  SPI_FLASH_CS_LOW();

  /* ·¢&#203;í μ&#244;μ&#231; &#195;üá&#238; */
  SPI_FLASH_SendByte(W25X_PowerDown);

  /*í¨&#209;&#182;&#189;áê&#248;£oCS&#184;&#223; */
  SPI_FLASH_CS_HIGH();
}   

//&#187;&#189;D&#209;
void SPI_Flash_WAKEUP(void)   
{
  /*&#209;&#161;&#212;&#241; FLASH: CS μí */
  SPI_FLASH_CS_LOW();

  /* ·¢&#203;í é&#207;μ&#231; &#195;üá&#238; */
  SPI_FLASH_SendByte(W25X_ReleasePowerDown);

   /* í£&#214;1D&#197;o&#197; FLASH: CS &#184;&#223; */
  SPI_FLASH_CS_HIGH();
}   
   
/*********************************************END OF FILE**********************/
回复 支持 反对

使用道具 举报

发表于 2017-6-9 14:34:55 | 显示全部楼层
有没有用我们配套的程序试一试         ?
回复 支持 反对

使用道具 举报

发表于 2017-6-13 18:07:38 | 显示全部楼层
jiabin 发表于 2017-6-9 11:36
驱动 如下:
/**
  ******************************************************************************
...

SPI的速度不能太快,SPI_BaudRatePrescaler改为64,128,256会好点
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-6-18 18:23:46 | 显示全部楼层
hehuibo 发表于 2017-6-13 18:07
SPI的速度不能太快,SPI_BaudRatePrescaler改为64,128,256会好点

谢谢! 我换了个芯片可以了!  
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-11-23 18:47 , Processed in 0.034278 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表