stm32f103c8t6 硬件spi驱动及w25q64读写
时间: 2023-08-24 19:08:00 浏览: 146
树莓派spi读写取w25q64芯片
以下是基于STM32F103C8T6芯片的硬件SPI驱动及W25Q64读写的示例代码,供参考。
SPI驱动:
```
#include "stm32f10x.h"
#define SPI_PORT GPIOA
#define SPI_SCK GPIO_Pin_5
#define SPI_MOSI GPIO_Pin_7
#define SPI_MISO GPIO_Pin_6
void SPI_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitStructure.GPIO_Pin = SPI_SCK | SPI_MOSI;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(SPI_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = SPI_MISO;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(SPI_PORT, &GPIO_InitStructure);
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_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
uint8_t SPI_SendByte(uint8_t byte)
{
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, byte);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
return SPI_I2S_ReceiveData(SPI1);
}
```
W25Q64读写:
```
#include "stm32f10x.h"
#define W25Q64_CS GPIO_Pin_4
void W25Q64_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = W25Q64_CS;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA, W25Q64_CS);
}
void W25Q64_WriteEnable(void)
{
GPIO_ResetBits(GPIOA, W25Q64_CS);
SPI_SendByte(0x06);
GPIO_SetBits(GPIOA, W25Q64_CS);
}
void W25Q64_ReadStatus(uint8_t *status)
{
GPIO_ResetBits(GPIOA, W25Q64_CS);
SPI_SendByte(0x05);
*status = SPI_SendByte(0xFF);
GPIO_SetBits(GPIOA, W25Q64_CS);
}
void W25Q64_WriteStatus(uint8_t status)
{
GPIO_ResetBits(GPIOA, W25Q64_CS);
SPI_SendByte(0x01);
SPI_SendByte(status);
GPIO_SetBits(GPIOA, W25Q64_CS);
}
void W25Q64_EraseChip(void)
{
W25Q64_WriteEnable();
GPIO_ResetBits(GPIOA, W25Q64_CS);
SPI_SendByte(0xC7);
GPIO_SetBits(GPIOA, W25Q64_CS);
}
void W25Q64_EraseSector(uint32_t address)
{
W25Q64_WriteEnable();
GPIO_ResetBits(GPIOA, W25Q64_CS);
SPI_SendByte(0x20);
SPI_SendByte((address >> 16) & 0xFF);
SPI_SendByte((address >> 8) & 0xFF);
SPI_SendByte(address & 0xFF);
GPIO_SetBits(GPIOA, W25Q64_CS);
}
void W25Q64_WritePage(uint32_t address, uint8_t *data, uint32_t length)
{
uint32_t i;
W25Q64_WriteEnable();
GPIO_ResetBits(GPIOA, W25Q64_CS);
SPI_SendByte(0x02);
SPI_SendByte((address >> 16) & 0xFF);
SPI_SendByte((address >> 8) & 0xFF);
SPI_SendByte(address & 0xFF);
for (i = 0; i < length; i++) {
SPI_SendByte(data[i]);
}
GPIO_SetBits(GPIOA, W25Q64_CS);
}
void W25Q64_ReadData(uint32_t address, uint8_t *data, uint32_t length)
{
uint32_t i;
GPIO_ResetBits(GPIOA, W25Q64_CS);
SPI_SendByte(0x03);
SPI_SendByte((address >> 16) & 0xFF);
SPI_SendByte((address >> 8) & 0xFF);
SPI_SendByte(address & 0xFF);
for (i = 0; i < length; i++) {
data[i] = SPI_SendByte(0xFF);
}
GPIO_SetBits(GPIOA, W25Q64_CS);
}
```
阅读全文