学前班
最后登录1970-1-1
在线时间 小时
注册时间2018-8-28
|
我用火哥的SPI——FLASH的程序读写W25Q128的FLASH,读取ID都是FFFFFF,读取里面的数据都是00,搞不懂什么情况了,搞了2天还是没有搞出来。。。。那个大神知道情况
[mw_shl_code=c,true]#include "flash.h"
/************************************flash.c*****************************/
void SPI_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
SPI_GPIO_CMD(SPI_NSS_CLK | SPI_SCK_CLK | SPI_MISO_CLK | SPI_MOSI_CLK | SPI_MODE_CLK, ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin = SPI_NSS_PIN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI_NSS_PORT, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin = SPI_SCK_PIN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI_SCK_PORT, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Pin = SPI_MISO_PIN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI_MISO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin = SPI_MOSI_PIN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI_MOSI_PORT, &GPIO_InitStruct);
GPIO_SetBits(SPI_NSS_PORT, GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);
}
void SPI_MODE_Config(void)
{
SPI_InitTypeDef SPI_InitStruct;
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_High;
SPI_InitStruct.SPI_CRCPolynomial = 0;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
SPI_Init(SPIx, &SPI_InitStruct);
SPI_Cmd(SPIx, ENABLE);
}
void SPI_Config(void)
{
SPI_GPIO_Config();
SPI_MODE_Config();
}
uint8_t SPI_FLASH_Sendbyte(uint8_t data)
{
while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPIx, data);
while(SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPIx);
}
uint8_t SPI_FLASH_Receivebyte(void)
{
return SPI_FLASH_Sendbyte(DUMMY);
}
uint32_t SPI_FLASH_GetId(void)
{
uint32_t flashid = 0;
SPI_NSS_RESET;
SPI_FLASH_Sendbyte(0x9F);
flashid = SPI_FLASH_Sendbyte(0X00);
flashid <<= 8;
flashid |= SPI_FLASH_Sendbyte(0X00);
flashid <<= 8;
flashid |= SPI_FLASH_Sendbyte(0X00);
SPI_NSS_SET;
return flashid;
}[/mw_shl_code]
/**********************************flash.h*********************/
[mw_shl_code=c,true]#ifndef __FLASH_H
#define __FLASH_H
#include "stm32f10x.h"
#define SPI_NSS_CLK RCC_APB2Periph_GPIOA
#define SPI_NSS_PIN GPIO_Pin_4
#define SPI_NSS_PORT GPIOA
#define SPI_SCK_CLK RCC_APB2Periph_GPIOA
#define SPI_SCK_PIN GPIO_Pin_5
#define SPI_SCK_PORT GPIOA
#define SPI_MISO_CLK RCC_APB2Periph_GPIOA
#define SPI_MISO_PIN GPIO_Pin_6
#define SPI_MISO_PORT GPIOA
#define SPI_MOSI_CLK RCC_APB2Periph_GPIOA
#define SPI_MOSI_PIN GPIO_Pin_7
#define SPI_MOSI_PORT GPIOA
#define SPI_MODE_CLK RCC_APB2Periph_SPI1
#define SPI_GPIO_CMD RCC_APB2PeriphClockCmd
#define SPI_MODE_CMD RCC_APB2PeriphClockCmd
#define SPI_MODE_CMD RCC_APB2PeriphClockCmd
#define SPI_NSS_SET GPIO_SetBits(SPI_NSS_PORT, SPI_NSS_PIN)
#define SPI_NSS_RESET GPIO_ResetBits(SPI_NSS_PORT, SPI_NSS_PIN)
#define DUMMY 0X00
#define SPIx SPI1
void SPI_Config(void);
uint8_t SPI_FLASH_Sendbyte(uint8_t data);
uint8_t SPI_FLASH_Receivebyte(void);
uint32_t SPI_FLASH_GetId(void);
#endif
[/mw_shl_code]
|
|