stm32f103标准库开发:w25q128读写-程序源码
时间: 2023-09-08 13:04:04 浏览: 153
stm32f103标准库是一种在STM32F103系列微控制器上进行开发的库文件。该库文件提供了丰富的函数和接口,可以方便地进行外设的控制与操作。在使用stm32f103标准库进行开发时,可以利用该库来实现对W25Q128闪存芯片的读写操作。
下面是一个示例的程序源码,演示了如何使用stm32f103标准库来实现W25Q128的读写功能:
#include "stm32f10x.h"
#include "stm32f10x_spi.h"
#include "stm32f10x_gpio.h"
#define SPI1_CS_PIN GPIO_Pin_4
#define SPI1_CS_PORT GPIOA
#define SPI1_MOSI_PIN GPIO_Pin_5
#define SPI1_MOSI_PORT GPIOA
#define SPI1_MISO_PIN GPIO_Pin_6
#define SPI1_MISO_PORT GPIOA
#define SPI1_SCK_PIN GPIO_Pin_7
#define SPI1_SCK_PORT GPIOA
void SPI1_Configuration(void) {
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
// 配置SPI引脚
GPIO_InitStructure.GPIO_Pin = SPI1_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI1_CS_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPI1_MISO_PIN | SPI1_SCK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI1_MISO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPI1_MOSI_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SPI1_MOSI_PORT, &GPIO_InitStructure);
// 配置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_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
void W25Q128_Read(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead) {
GPIO_ResetBits(SPI1_CS_PORT, SPI1_CS_PIN); // 使能闪存芯片
SPI1->DR = 0x03; // 发送读命令
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI1->DR = (ReadAddr & 0xFF0000) >> 16; // 发送地址
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI1->DR = (ReadAddr & 0xFF00) >> 8;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI1->DR = ReadAddr & 0xFF;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
while (NumByteToRead--) {
SPI1->DR = 0xFF; // 发送读数据命令
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
*pBuffer = SPI1->DR;
pBuffer++;
}
GPIO_SetBits(SPI1_CS_PORT, SPI1_CS_PIN); // 禁能闪存芯片
}
void W25Q128_Write(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) {
GPIO_ResetBits(SPI1_CS_PORT, SPI1_CS_PIN); // 使能闪存芯片
SPI1->DR = 0x06; // 发送写使能命令
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
GPIO_SetBits(SPI1_CS_PORT, SPI1_CS_PIN); // 禁能闪存芯片
GPIO_ResetBits(SPI1_CS_PORT, SPI1_CS_PIN); // 使能闪存芯片
SPI1->DR = 0x02; // 发送写命令
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI1->DR = (WriteAddr & 0xFF0000) >> 16; // 发送地址
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI1->DR = (WriteAddr & 0xFF00) >> 8;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI1->DR = WriteAddr & 0xFF;
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
while (NumByteToWrite--) {
SPI1->DR = *pBuffer; // 发送写数据命令
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
pBuffer++;
}
GPIO_SetBits(SPI1_CS_PORT, SPI1_CS_PIN); // 禁能闪存芯片
}
int main(void) {
// 初始化SPI
SPI1_Configuration();
// 读取数据
uint32_t addr = 0x000000;
uint8_t buffer[60];
W25Q128_Read(buffer, addr, sizeof(buffer));
// 写入数据
uint8_t data_to_write[] = "Hello, World!";
uint16_t size = sizeof(data_to_write) - 1;
W25Q128_Write(data_to_write, addr, size);
while(1);
return 0;
}
上述程序实现了对W25Q128闪存芯片的读写功能。通过配置SPI1来连接STM32F103与W25Q128,在读取数据时使用了读命令和地址,读取完数据后将其存储在指定的缓冲区中。在写入数据时同样使用了写命令和地址,然后逐个字节地将数据写入闪存芯片。
阅读全文