stm32 w25q128驱动代码
时间: 2023-06-20 11:05:16 浏览: 124
以下是STM32驱动W25Q128的代码示例,使用SPI接口进行通信:
```c
#include "stm32f10x.h"
#define W25Q128_CS_GPIO GPIOA
#define W25Q128_CS_PIN GPIO_Pin_4
#define W25Q128_WRITE_ENABLE 0x06
#define W25Q128_WRITE_DISABLE 0x04
#define W25Q128_READ_STATUS_REG 0x05
#define W25Q128_WRITE_STATUS_REG 0x01
#define W25Q128_READ_DATA 0x03
#define W25Q128_PAGE_PROGRAM 0x02
#define W25Q128_SECTOR_ERASE 0x20
#define W25Q128_BLOCK_ERASE_32K 0x52
#define W25Q128_BLOCK_ERASE_64K 0xD8
#define W25Q128_CHIP_ERASE 0xC7
#define W25Q128_POWER_DOWN 0xB9
#define W25Q128_READ_DEVICE_ID 0x90
#define W25Q128_READ_MANUFACTURER_ID 0x9F
void W25Q128_Init(void);
void W25Q128_CS_Enable(void);
void W25Q128_CS_Disable(void);
void W25Q128_WriteEnable(void);
void W25Q128_WriteDisable(void);
void W25Q128_ReadStatusReg(uint8_t *status);
void W25Q128_WriteStatusReg(uint8_t status);
void W25Q128_ReadData(uint8_t *data, uint32_t addr, uint16_t size);
void W25Q128_PageProgram(uint8_t *data, uint32_t addr, uint16_t size);
void W25Q128_SectorErase(uint32_t addr);
void W25Q128_BlockErase32K(uint32_t addr);
void W25Q128_BlockErase64K(uint32_t addr);
void W25Q128_ChipErase(void);
void W25Q128_PowerDown(void);
void W25Q128_ReadDeviceID(uint8_t *id);
void W25Q128_ReadManufacturerID(uint8_t *id);
void W25Q128_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin = W25Q128_CS_PIN;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(W25Q128_CS_GPIO, &GPIO_InitStruct);
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStruct);
SPI_Cmd(SPI1, ENABLE);
}
void W25Q128_CS_Enable(void)
{
GPIO_ResetBits(W25Q128_CS_GPIO, W25Q128_CS_PIN);
}
void W25Q128_CS_Disable(void)
{
GPIO_SetBits(W25Q128_CS_GPIO, W25Q128_CS_PIN);
}
void W25Q128_WriteEnable(void)
{
W25Q128_CS_Enable();
SPI_I2S_SendData(SPI1, W25Q128_WRITE_ENABLE);
W25Q128_CS_Disable();
}
void W25Q128_WriteDisable(void)
{
W25Q128_CS_Enable();
SPI_I2S_SendData(SPI1, W25Q128_WRITE_DISABLE);
W25Q128_CS_Disable();
}
void W25Q128_ReadStatusReg(uint8_t *status)
{
W25Q128_CS_Enable();
SPI_I2S_SendData(SPI1, W25Q128_READ_STATUS_REG);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
*status = SPI_I2S_ReceiveData(SPI1);
W25Q128_CS_Disable();
}
void W25Q128_WriteStatusReg(uint8_t status)
{
W25Q128_CS_Enable();
SPI_I2S_SendData(SPI1, W25Q128_WRITE_STATUS_REG);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, status);
W25Q128_CS_Disable();
}
void W25Q128_ReadData(uint8_t *data, uint32_t addr, uint16_t size)
{
W25Q128_CS_Enable();
SPI_I2S_SendData(SPI1, W25Q128_READ_DATA);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, (addr >> 16) & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, (addr >> 8) & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, addr & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
while (size--) {
SPI_I2S_SendData(SPI1, 0);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
*data++ = SPI_I2S_ReceiveData(SPI1);
}
W25Q128_CS_Disable();
}
void W25Q128_PageProgram(uint8_t *data, uint32_t addr, uint16_t size)
{
W25Q128_WriteEnable();
W25Q128_CS_Enable();
SPI_I2S_SendData(SPI1, W25Q128_PAGE_PROGRAM);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, (addr >> 16) & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, (addr >> 8) & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, addr & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
while (size--) {
SPI_I2S_SendData(SPI1, *data++);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
}
W25Q128_CS_Disable();
}
void W25Q128_SectorErase(uint32_t addr)
{
W25Q128_WriteEnable();
W25Q128_CS_Enable();
SPI_I2S_SendData(SPI1, W25Q128_SECTOR_ERASE);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, (addr >> 16) & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, (addr >> 8) & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, addr & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
W25Q128_CS_Disable();
}
void W25Q128_BlockErase32K(uint32_t addr)
{
W25Q128_WriteEnable();
W25Q128_CS_Enable();
SPI_I2S_SendData(SPI1, W25Q128_BLOCK_ERASE_32K);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, (addr >> 16) & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, (addr >> 8) & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, addr & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
W25Q128_CS_Disable();
}
void W25Q128_BlockErase64K(uint32_t addr)
{
W25Q128_WriteEnable();
W25Q128_CS_Enable();
SPI_I2S_SendData(SPI1, W25Q128_BLOCK_ERASE_64K);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, (addr >> 16) & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, (addr >> 8) & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, addr & 0xFF);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
W25Q128_CS_Disable();
}
void W25Q128_ChipErase(void)
{
W25Q128_WriteEnable();
W25Q128_CS_Enable();
SPI_I2S_SendData(SPI1, W25Q128_CHIP_ERASE);
W25Q128_CS_Disable();
}
void W25Q128_PowerDown(void)
{
W25Q128_CS_Enable();
SPI_I2S_SendData(SPI1, W25Q128_POWER_DOWN);
W25Q128_CS_Disable();
}
void W25Q128_ReadDeviceID(uint8_t *id)
{
W25Q128_CS_Enable();
SPI_I2S_SendData(SPI1, W25Q128_READ_DEVICE_ID);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, 0);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
*id++ = SPI_I2S_ReceiveData(SPI1);
SPI_I2S_SendData(SPI1, 0);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
*id++ = SPI_I2S_ReceiveData(SPI1);
SPI_I2S_SendData(SPI1, 0);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
*id++ = SPI_I2S_ReceiveData(SPI1);
SPI_I2S_SendData(SPI1, 0);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
*id++ = SPI_I2S_ReceiveData(SPI1);
W25Q128_CS_Disable();
}
void W25Q128_ReadManufacturerID(uint8_t *id)
{
W25Q128_CS_Enable();
SPI_I2S_SendData(SPI1, W25Q128_READ_MANUFACTURER_ID);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, 0);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
*id++ = SPI_I2S_ReceiveData(SPI1);
SPI_I2S_SendData(SPI1, 0);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
*id++ = SPI_I2S_ReceiveData(SPI1);
W25Q128_CS_Disable();
}
```
注意,在使用前需要先初始化SPI接口和CS引脚。
阅读全文