使用STM32F103RET6写一个使用SPI驱动W25Q64的代码
时间: 2024-05-15 09:19:20 浏览: 136
以下是使用STM32F103RET6写一个使用SPI驱动W25Q64的代码的示例:
```c
#include "stm32f10x.h"
#define W25Q64_CS_GPIO_CLK RCC_APB2Periph_GPIOA
#define W25Q64_CS_GPIO_PORT GPIOA
#define W25Q64_CS_GPIO_PIN GPIO_Pin_4
#define W25Q64_SPI_CLK RCC_APB2Periph_SPI1
#define W25Q64_SPI SPI1
#define W25Q64_SPI_SLOW 2000000
#define W25Q64_SPI_FAST 8000000
#define W25Q64_CMD_WRITE_ENABLE 0x06
#define W25Q64_CMD_WRITE_DISABLE 0x04
#define W25Q64_CMD_READ_STATUS_REG1 0x05
#define W25Q64_CMD_READ_DATA 0x03
#define W25Q64_CMD_PAGE_PROGRAM 0x02
#define W25Q64_CMD_ERASE_SECTOR 0x20
#define W25Q64_CMD_ERASE_BLOCK 0xD8
#define W25Q64_CMD_ERASE_CHIP 0xC7
void W25Q64_Init(void);
void W25Q64_ReadID(uint16_t *manufacturer_id, uint16_t *device_id);
void W25Q64_ReadData(uint32_t address, uint8_t *data, uint16_t length);
void W25Q64_WriteData(uint32_t address, uint8_t *data, uint16_t length);
void W25Q64_EraseSector(uint32_t address);
void W25Q64_EraseBlock(uint32_t address);
void W25Q64_EraseChip(void);
void W25Q64_WriteEnable(void);
void W25Q64_WriteDisable(void);
uint8_t W25Q64_ReadStatusReg1(void);
void W25Q64_WaitForReady(void);
void W25Q64_Select(void);
void W25Q64_Deselect(void);
void W25Q64_SendByte(uint8_t byte);
uint8_t W25Q64_ReceiveByte(void);
void W25Q64_SendCommand(uint8_t command);
void W25Q64_SendAddress(uint32_t address);
void W25Q64_SendData(uint8_t *data, uint16_t length);
void W25Q64_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
// enable clocks
RCC_APB2PeriphClockCmd(W25Q64_CS_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(W25Q64_SPI_CLK, ENABLE);
// configure chip select pin
GPIO_InitStructure.GPIO_Pin = W25Q64_CS_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(W25Q64_CS_GPIO_PORT, &GPIO_InitStructure);
// configure SPI pins
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// configure SPI
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_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(W25Q64_SPI, &SPI_InitStructure);
SPI_Cmd(W25Q64_SPI, ENABLE);
}
void W25Q64_ReadID(uint16_t *manufacturer_id, uint16_t *device_id)
{
uint8_t tx_data[4] = {0x90, 0, 0, 0};
uint8_t rx_data[4];
W25Q64_Select();
W25Q64_SendData(tx_data, 4);
rx_data[0] = W25Q64_ReceiveByte();
rx_data[1] = W25Q64_ReceiveByte();
rx_data[2] = W25Q64_ReceiveByte();
rx_data[3] = W25Q64_ReceiveByte();
W25Q64_Deselect();
*manufacturer_id = (rx_data[1] << 8) | rx_data[2];
*device_id = rx_data[3];
}
void W25Q64_ReadData(uint32_t address, uint8_t *data, uint16_t length)
{
uint8_t tx_data[4] = {W25Q64_CMD_READ_DATA, (address >> 16) & 0xFF, (address >> 8) & 0xFF, address & 0xFF};
W25Q64_Select();
W25Q64_SendData(tx_data, 4);
W25Q64_SendData(NULL, length);
for (int i = 0; i < length; i++) {
data[i] = W25Q64_ReceiveByte();
}
W25Q64_Deselect();
}
void W25Q64_WriteData(uint32_t address, uint8_t *data, uint16_t length)
{
uint8_t tx_data[4] = {W25Q64_CMD_PAGE_PROGRAM, (address >> 16) & 0xFF, (address >> 8) & 0xFF, address & 0xFF};
W25Q64_WriteEnable();
W25Q64_Select();
W25Q64_SendData(tx_data, 4);
W25Q64_SendData(data, length);
W25Q64_Deselect();
W25Q64_WaitForReady();
}
void W25Q64_EraseSector(uint32_t address)
{
uint8_t tx_data[4] = {W25Q64_CMD_ERASE_SECTOR, (address >> 16) & 0xFF, (address >> 8) & 0xFF, address & 0xFF};
W25Q64_WriteEnable();
W25Q64_Select();
W25Q64_SendData(tx_data, 4);
W25Q64_Deselect();
W25Q64_WaitForReady();
}
void W25Q64_EraseBlock(uint32_t address)
{
uint8_t tx_data[4] = {W25Q64_CMD_ERASE_BLOCK, (address >> 16) & 0xFF, (address >> 8) & 0xFF, address & 0xFF};
W25Q64_WriteEnable();
W25Q64_Select();
W25Q64_SendData(tx_data, 4);
W25Q64_Deselect();
W25Q64_WaitForReady();
}
void W25Q64_EraseChip(void)
{
W25Q64_WriteEnable();
W25Q64_Select();
W25Q64_SendCommand(W25Q64_CMD_ERASE_CHIP);
W25Q64_Deselect();
W25Q64_WaitForReady();
}
void W25Q64_WriteEnable(void)
{
W25Q64_Select();
W25Q64_SendCommand(W25Q64_CMD_WRITE_ENABLE);
W25Q64_Deselect();
}
void W25Q64_WriteDisable(void)
{
W25Q64_Select();
W25Q64_SendCommand(W25Q64_CMD_WRITE_DISABLE);
W25Q64_Deselect();
}
uint8_t W25Q64_ReadStatusReg1(void)
{
uint8_t tx_data = W25Q64_CMD_READ_STATUS_REG1;
uint8_t rx_data;
W25Q64_Select();
W25Q64_SendData(&tx_data, 1);
rx_data = W25Q64_ReceiveByte();
W25Q64_Deselect();
return rx_data;
}
void W25Q64_WaitForReady(void)
{
while ((W25Q64_ReadStatusReg1() & 0x01) == 0x01);
}
void W25Q64_Select(void)
{
GPIO_ResetBits(W25Q64_CS_GPIO_PORT, W25Q64_CS_GPIO_PIN);
}
void W25Q64_Deselect(void)
{
GPIO_SetBits(W25Q64_CS_GPIO_PORT, W25Q64_CS_GPIO_PIN);
}
void W25Q64_SendByte(uint8_t byte)
{
while (SPI_I2S_GetFlagStatus(W25Q64_SPI, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(W25Q64_SPI, byte);
while (SPI_I2S_GetFlagStatus(W25Q64_SPI, SPI_I2S_FLAG_RXNE) == RESET);
SPI_I2S_ReceiveData(W25Q64_SPI);
}
uint8_t W25Q64_ReceiveByte(void)
{
W25Q64_SendByte(0xFF);
return SPI_I2S_ReceiveData(W25Q64_SPI);
}
void W25Q64_SendCommand(uint8_t command)
{
W25Q64_SendByte(command);
}
void W25Q64_SendAddress(uint32_t address)
{
W25Q64_SendByte((address >> 16) & 0xFF);
W25Q64_SendByte((address >> 8) & 0xFF);
W25Q64_SendByte(address & 0xFF);
}
void W25Q64_SendData(uint8_t *data, uint16_t length)
{
for (int i = 0; i < length; i++) {
W25Q64_SendByte(data[i]);
}
}
```
在主函数中调用上述函数即可完成对W25Q64的读写操作。注意,在使用SPI进行通信时,需要配置SPI的时钟分频和相位极性等参数,以确保通信正常。同时,为了避免在通信过程中产生噪声,需要在CS引脚上使用外部上拉电阻。
阅读全文