4427 views|2 replies

3191

Posts

0

Resources
The OP
 

STM32 SPI2 read and write W25X20 problem [Copy link]

This post was last edited by chenbingjy on 2017-11-14 11:28 I use STM32F103C8T6 to read and write W25X20, and the read ID is always 0.Schematic diagram: Program: Main function: SPI_Flash_Init(); 。 。 。 。 。 。 。 。 。 。 id=SPI_Flash_ReadID(); flash.c u16 SPI_FLASH_TYPE=W25X20; void SPI_Flash_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //SPI CS GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); //GPIO_SetBits(GPIOB,GPIO_Pin_15|GPIO_Pin_13|GPIO_Pin_14); GPIO_SetBits(GPIOB,GPIO_Pin_12); SPI2_Init(); SPI2_SetSpeed(SPI_BaudRatePrescaler_4); SPI_FLASH_TYPE=SPI_Flash_ReadID(); } SPI.C SPI_InitTypeDef SPI_InitStructure; void SPI2_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; //RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB|RCC_APB1Periph_SPI2, ENABLE ); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //??GPIOB?? RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);//??SPI2?? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14| GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); //?????SPI???? RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2,ENABLE);//??SPI1 RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2,DISABLE);//??SPI1 GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14| GPIO_Pin_15); 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_2; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI2, &SPI_InitStructure); SPI_Cmd(SPI2,ENABLE);

        SPI2_ReadWriteByte(0xff);         
}   

u8 SPI2_ReadWriteByte(u8 TxData)
{               
        u8 retry=0;                                         
        while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET)
                {
                retry++;
                if(retry>200)return 0;
                }                          
        SPI_I2S_SendData(SPI2, TxData);
        retry=0;

        while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET)
                {
                retry++;
                if(retry>200)return 0;
                }                                                              
        return SPI_I2S_ReceiveData(SPI2);                                    
}

FLASH.H
#define W25Q80         0XEF13         
#define W25Q16         0XEF14
#define W25Q32         0XEF15
#define W25Q64         0XEF16
#define W25X20         0XEF9F

extern u16 SPI_FLASH_TYPE;               
#define        SPI_FLASH_CS PBout(12)  

////////////////////////////////////////////////////////////////////////////

extern u8 SPI_FLASH_BUF[4096];

#define FLASH_ID 0XEF9F

spi.h
请高手看看,哪里有错?谢谢!


This post is from stm32/stm8

Latest reply

First check whether mosi and clk have output waveforms; then check whether miso has response data. If garbled data is received, adjust the data clock polarity and data edge. It will definitely work.   Details Published on 2017-11-14 15:42
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 

4005

Posts

0

Resources
2
 
First check whether mosi and clk have output waveforms; then check whether miso has response data. If garbled data is received, adjust the data clock polarity and data edge. It will definitely work.
This post is from stm32/stm8

Comments

Already done, thanks!  Details Published on 2017-11-14 15:44
 
 

3191

Posts

0

Resources
3
 
huo_hu posted on 2017-11-14 15:42 First check whether mosi and clk have output waveforms; then check whether miso has response data. If garbled data is received, adjust the data clock polarity and number...
It has been done, thank you!
This post is from stm32/stm8
 
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list