|
SPI1配置如下:
#define SPI_FLASH_SPI SPI1
#define SPI_FLASH_SPI_CLK RCC_APB2Periph_SPI1
#define SPI_FLASH_SPI_SCK_PIN GPIO_Pin_3 /* PB.03 */
#define SPI_FLASH_SPI_SCK_GPIO_PORT GPIOB/* GPIOB */
#define SPI_FLASH_SPI_SCK_GPIO_CLK RCC_AHB1Periph_GPIOB
#define SPI_FLASH_SPI_MISO_PIN GPIO_Pin_4/* PB.04 */
#define SPI_FLASH_SPI_MISO_GPIO_PORT GPIOB/* GPIOB */
#define SPI_FLASH_SPI_MISO_GPIO_CLK RCC_AHB1Periph_GPIOB
#define SPI_FLASH_SPI_MOSI_PIN GPIO_Pin_5 /* PB.05 */
#define SPI_FLASH_SPI_MOSI_GPIO_PORT GPIOB /* GPIOB */
#define SPI_FLASH_SPI_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOB
#define SPI_FLASH_CS_PIN GPIO_Pin_15 /* PA.15 */
#define SPI_FLASH_CS_GPIO_PORT GPIOA /* GPIOA */
#define SPI_FLASH_CS_GPIO_CLK RCC_AHB1Periph_GPIOA
void SPI_FLASH_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/*!< SPI_FLASH_SPI_CLK Periph clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
/*!< Configure SPI_FLASH_SPI_SCK_PIN pins: SCK */
GPIO_InitStructure.GPIO_Pin = SPI_FLASH_SPI_SCK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI_FLASH_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_SPI1);//Connect SPI pins to AF5
GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI1);
/*!< Configure SPI_FLASH_SPI_MOSI_PIN pins: MOSI */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Pin = SPI_FLASH_SPI_MOSI_PIN;
GPIO_Init(SPI_FLASH_CS_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI_MISO_PIN pins: MISO */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin = SPI_FLASH_SPI_MISO_PIN;
GPIO_Init(SPI_FLASH_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_CS_PIN pin: sFLASH Card CS pin */
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Pin = SPI_FLASH_CS_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI_FLASH_CS_GPIO_PORT, &GPIO_InitStructure);
//GPIO_PinAFConfig(SPI_FLASH_CS_GPIO_PORT, GPIO_PinSource15, GPIO_AF_SPI1);
/*!< Deselect the FLASH: Chip Select high */
S25FL132K_SPI_FLASH_CS_HIGH();
RCC_APB2PeriphClockCmd(SPI_FLASH_SPI_CLK, ENABLE);
/*!< SPI configuration */
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_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
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(SPI_FLASH_SPI, &SPI_InitStructure);
/*!< Enable the SPI_FLASH_SPI */
SPI_Cmd(SPI_FLASH_SPI, ENABLE);
}
u32 S25FL132K_SPI_FLASH_ReadID(void)
{
u32 Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;
S25FL132K_SPI_FLASH_WriteEnable();
/* Select the FLASH: Chip Select low */
S25FL132K_SPI_FLASH_CS_LOW();
/* Send "RDID " instruction */
S25FL132K_SPI_FLASH_SendByte(S25FL132K_JedecDeviceID);
/* Read a byte from the FLASH */
Temp0 = S25FL132K_SPI_FLASH_SendByte(Dummy_Byte);
/* Read a byte from the FLASH */
Temp1 = S25FL132K_SPI_FLASH_SendByte(Dummy_Byte);
/* Read a byte from the FLASH */
Temp2 = S25FL132K_SPI_FLASH_SendByte(Dummy_Byte);
/* Deselect the FLASH: Chip Select high */
S25FL132K_SPI_FLASH_CS_HIGH();
Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2;
return Temp;
}
读出来始终为0x00,郁闷啊,求大神指点!!! |
|