stm32读取w25Q128的8字节ID
时间: 2024-04-29 19:22:50 浏览: 104
读取stm32单片机ID
3星 · 编辑精心推荐
以下是一个示例代码,使用SPI接口读取W25Q128的8字节ID:
```
#include "stm32f4xx.h"
#include "stm32f4xx_spi.h"
#include "stm32f4xx_gpio.h"
#define SPIx SPI1
#define SPIx_CLK RCC_APB2Periph_SPI1
#define SPIx_CS_PIN GPIO_Pin_4
#define SPIx_CS_GPIO_PORT GPIOA
#define SPIx_CS_GPIO_CLK RCC_AHB1Periph_GPIOA
#define SPIx_MOSI_PIN GPIO_Pin_7
#define SPIx_MOSI_GPIO_PORT GPIOA
#define SPIx_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOA
#define SPIx_MOSI_SOURCE GPIO_PinSource7
#define SPIx_MOSI_AF GPIO_AF_SPI1
#define SPIx_MISO_PIN GPIO_Pin_6
#define SPIx_MISO_GPIO_PORT GPIOA
#define SPIx_MISO_GPIO_CLK RCC_AHB1Periph_GPIOA
#define SPIx_MISO_SOURCE GPIO_PinSource6
#define SPIx_MISO_AF GPIO_AF_SPI1
#define SPIx_SCK_PIN GPIO_Pin_5
#define SPIx_SCK_GPIO_PORT GPIOA
#define SPIx_SCK_GPIO_CLK RCC_AHB1Periph_GPIOA
#define SPIx_SCK_SOURCE GPIO_PinSource5
#define SPIx_SCK_AF GPIO_AF_SPI1
void SPIx_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* SPIx_CS_GPIO, SPIx_MOSI_GPIO, SPIx_MISO_GPIO and SPIx_SCK_GPIO Periph clock enable */
RCC_AHB1PeriphClockCmd(SPIx_CS_GPIO_CLK | SPIx_MOSI_GPIO_CLK | SPIx_MISO_GPIO_CLK | SPIx_SCK_GPIO_CLK, ENABLE);
/* SPIx Periph clock enable */
RCC_APB2PeriphClockCmd(SPIx_CLK, ENABLE);
/* Configure SPIx_CS_PIN in output pushpull mode */
GPIO_InitStructure.GPIO_Pin = SPIx_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(SPIx_CS_GPIO_PORT, &GPIO_InitStructure);
/* Configure SPIx pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = SPIx_SCK_PIN | SPIx_MISO_PIN | SPIx_MOSI_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect SPIx pins to AF */
GPIO_PinAFConfig(SPIx_SCK_GPIO_PORT, SPIx_SCK_SOURCE, SPIx_SCK_AF);
GPIO_PinAFConfig(SPIx_MISO_GPIO_PORT, SPIx_MISO_SOURCE, SPIx_MISO_AF);
GPIO_PinAFConfig(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_SOURCE, SPIx_MOSI_AF);
/* SPIx configuration ------------------------------------------------------*/
SPI_I2S_DeInit(SPIx);
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_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPIx, &SPI_InitStructure);
/* Enable SPIx */
SPI_Cmd(SPIx, ENABLE);
}
uint8_t SPIx_ReadWriteByte(uint8_t data)
{
/* Wait until the transmit buffer is empty */
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
/* Send the byte */
SPI_I2S_SendData(SPIx, data);
/* Wait until a data is received */
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
/* Get the received data */
return SPI_I2S_ReceiveData(SPIx);
}
uint32_t W25Q128_ReadID(void)
{
uint32_t id = 0;
SPIx_CS_GPIO_PORT->BSRRH = SPIx_CS_PIN; // set CS low
SPIx_ReadWriteByte(0x90); // send read ID command
SPIx_ReadWriteByte(0x00); // send address byte 1
SPIx_ReadWriteByte(0x00); // send address byte 2
SPIx_ReadWriteByte(0x00); // send address byte 3
id |= SPIx_ReadWriteByte(0xFF) << 16;
id |= SPIx_ReadWriteByte(0xFF) << 8;
id |= SPIx_ReadWriteByte(0xFF);
SPIx_CS_GPIO_PORT->BSRRL = SPIx_CS_PIN; // set CS high
return id;
}
int main(void)
{
SPIx_Init();
uint32_t id = W25Q128_ReadID();
return 0;
}
```
阅读全文