直接上代码:
bsp_spi_flash.c
/**
******************************************************************************
* @file bsp_spi_flash.c
* @author STMicroelectronics
* @version V1.0
* @date 2019
* @brief SPI FLASH(W25Q64)应用函数bsp
******************************************************************************
*/
#include "bsp_spi_flash.h"
#include "./usart/bsp_usart.h"
static __IO uint32_t SPITimeout = SPIT_LONG_TIMEOUT;
static uint32_t SPI_TIMEOUT_UserCallback(uint8_t errorCode);
/**
* @brief SPI I/O配置
* @param 无
* @retval 无
*/
static void SPI_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能与 SPI 有关的时钟 */
FLASH_SPI_APBxClock_FUN(FLASH_SPI_CLK, ENABLE);
FLASH_SPI_GPIO_APBxClock_FUN(FLASH_SPI_GPIO_CLK, ENABLE);
/* MISO MOSI SCK */
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_SCK_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // SCK 推挽复用
GPIO_Init(FLASH_SPI_SCK_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MOSI_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // MOSI 推挽复用
GPIO_Init(FLASH_SPI_MOSI_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MISO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // MOSI 浮空输入
GPIO_Init(FLASH_SPI_MISO_PORT, &GPIO_InitStructure);
// 初始化CS引脚,使用软件控制,所以直接配置为推挽输出
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_CS_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // CS 推挽输出
GPIO_Init(FLASH_SPI_CS_PORT, &GPIO_InitStructure);
// 拉高CS,使SPI处于空闲状态
FLASH_SPI_CS_HIGH;
}
/**
* @brief SPI 工作模式配置
* @param 无
* @retval 无
*/
static void SPI_Mode_Config(void)
{
SPI_InitTypeDef SPI_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 = 0; // 不使用CRC功能,数值随便写
SPI_Init(FLASH_SPIx, &SPI_InitStructure);
SPI_Cmd(FLASH_SPIx, ENABLE);
}
/**
* @brief I2C 外设(EEPROM)初始化
* @param 无
* @retval 无
*/
void SPI_FLASH_Init(void)
{
SPI_GPIO_Config();
SPI_Mode_Config();
}
// 发送并接收一个字节
uint8_t SPI_FLASH_Send_Byte(uint8_t data)
{
SPITimeout = SPIT_FLAG_TIMEOUT;
// 检查并等待至TX缓冲区为空
while (SPI_I2S_GetFlagStatus(FLASH_SPIx, SPI_I2S_FLAG_TXE) == RESET)
{
if ((SPITimeout--) == 0)
{
return SPI_TIMEOUT_UserCallback(0);
}
}
// 程序执行到此处,TX缓冲区已空
SPI_I2S_SendData(FLASH_SPIx, data);
SPITimeout = SPIT_FLAG_TIMEOUT;
// 检查并等待至RX缓冲区非空
while (SPI_I2S_GetFlagStatus(FLASH_SPIx, SPI_I2S_FLAG_RXNE) == RESET)
{
if ((SPITimeout--) == 0)
{
return SPI_TIMEOUT_UserCallback(0);
}
}
// 程序执行到此处,说明数据发送完毕,并接收到一个字节
return SPI_I2S_ReceiveData(FLASH_SPIx);
}
// 接收一个字节
uint8_t SPI_FLASH_Read_Byte(void)
{
return SPI_FLASH_Send_Byte(DUMMY);
}
// 读取FLASH ID
uint32_t SPI_Read_ID(void)
{
uint32_t flash_id = 0;
// 片选使能
FLASH_SPI_CS_LOW;
SPI_FLASH_Send_Byte(JEDEC_ID); // 发送命令码
flash_id = SPI_FLASH_Read_Byte();
flash_id <<= 8;
flash_id |= SPI_FLASH_Read_Byte();
flash_id <<= 8;
flash_id |= SPI_FLASH_Read_Byte();
FLASH_SPI_CS_HIGH;
return flash_id;
}
// 写入
// Flash写入使能
void SPI_WriteEnable(void)
{
FLASH_SPI_CS_LOW;
SPI_FLASH_Send_Byte(WRITE_ENABLE); // 发送命令码
FLASH_SPI_CS_HIGH;
}
// 擦除Flash指定扇区
void SPI_Erase_Sector(uint32_t addr)
{
SPI_WriteEnable(); // 擦除之前先调用写使能
// 片选使能
FLASH_SPI_CS_LOW;
SPI_FLASH_Send_Byte(ERASE_SECTOR);
SPI_FLASH_Send_Byte((addr >> 16) & (0xFF)); // 发送命令码
SPI_FLASH_Send_Byte((addr >> 8) & (0xFF));
SPI_FLASH_Send_Byte((addr) & (0xFF));
FLASH_SPI_CS_HIGH;
// Flash的擦除需要时间,要通过读取状态寄存器,来判断是否擦除完成
SPI_WaitForWriteEnd();
}
// 读取Flash的内容
void SPI_Read_Data(uint32_t addr, uint8_t *read_buff, uint32_t numByteToRead)
{
// 片选使能
FLASH_SPI_CS_LOW;
SPI_FLASH_Send_Byte(READ_DATA);
SPI_FLASH_Send_Byte((addr >> 16) & (0xFF));
SPI_FLASH_Send_Byte((addr >> 8) & (0xFF));
SPI_FLASH_Send_Byte((addr) & (0xFF));
while (numByteToRead--)
{
*read_buff = SPI_FLASH_Read_Byte();
read_buff++;
}
FLASH_SPI_CS_HIGH;
}
// 向Flash中写入内容(一次最多写256字节)
void SPI_Write_Data(uint32_t addr, uint8_t *write_buff, uint32_t numByteToWrite)
{
SPI_WriteEnable(); // 写入之前先调用写使能
// 片选使能
FLASH_SPI_CS_LOW;
SPI_FLASH_Send_Byte(WRITE_DATA);
SPI_FLASH_Send_Byte((addr >> 16) & (0xFF));
SPI_FLASH_Send_Byte((addr >> 8) & (0xFF));
SPI_FLASH_Send_Byte((addr) & (0xFF));
while (numByteToWrite--)
{
SPI_FLASH_Send_Byte(*write_buff);
write_buff++;
}
FLASH_SPI_CS_HIGH;
SPI_WaitForWriteEnd();
}
// 等待Flash内部时序操作完成
void SPI_WaitForWriteEnd(void)
{
uint8_t status = 0;
// 片选使能
FLASH_SPI_CS_LOW;
SPI_FLASH_Send_Byte(STATUS); // 发送命令码
do
{
status = SPI_FLASH_Read_Byte();
}
while ((status & 0x01) == 1); // SPI 总线忙碌
FLASH_SPI_CS_HIGH;
}
/**
* @brief Basic management of the timeout situation.
* @param errorCode:错误代码,可以用来定位是哪个环节出错.
* @retval 返回0,表示SPI读取失败.
*/
static uint32_t SPI_TIMEOUT_UserCallback(uint8_t errorCode)
{
/* Block communication and all processes */
FLASH_ERROR("SPI 等待超时!errorCode = %d",errorCode);
return 0;
}
/*********************************************END OF FILE**********************/
bsp_spi_flash.h
#ifndef __BSP_SPI_FLASH_H
#define __BSP_SPI_FLASH_H
#include "stm32f10x.h"
/**************************I2C参数定义,I2C1或I2C2********************************/
#define FLASH_SPIx SPI1
#define FLASH_SPI_APBxClock_FUN RCC_APB2PeriphClockCmd
#define FLASH_SPI_CLK RCC_APB2Periph_SPI1
#define FLASH_SPI_GPIO_APBxClock_FUN RCC_APB2PeriphClockCmd
#define FLASH_SPI_GPIO_CLK (RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC)
#define FLASH_SPI_SCK_PORT GPIOA
#define FLASH_SPI_SCK_PIN GPIO_Pin_5
#define FLASH_SPI_MISO_PORT GPIOA
#define FLASH_SPI_MISO_PIN GPIO_Pin_6
#define FLASH_SPI_MOSI_PORT GPIOA
#define FLASH_SPI_MOSI_PIN GPIO_Pin_7
#define FLASH_SPI_CS_PORT GPIOC
#define FLASH_SPI_CS_PIN GPIO_Pin_0
#define DUMMY (0X00)
#define JEDEC_ID (0X9F)
#define ERASE_SECTOR (0X20)
#define STATUS (0X05)
#define READ_DATA (0X03)
#define WRITE_ENABLE (0X06)
#define WRITE_DATA (0X02)
/*等待超时时间*/
#define SPIT_FLAG_TIMEOUT ((uint32_t)0x1000)
#define SPIT_LONG_TIMEOUT ((uint32_t)(10 * SPIT_FLAG_TIMEOUT))
/* CS引脚配置 */
#define FLASH_SPI_CS_HIGH do{GPIO_SetBits(FLASH_SPI_CS_PORT,FLASH_SPI_CS_PIN);}while(0)
#define FLASH_SPI_CS_LOW do{GPIO_ResetBits(FLASH_SPI_CS_PORT,FLASH_SPI_CS_PIN);}while(0)
/*信息输出*/
#define FLASH_DEBUG_ON 0
#define FLASH_INFO(fmt,arg...) printf("<<-FLASH-INFO->> "fmt"\n",##arg)
#define FLASH_ERROR(fmt,arg...) printf("<<-FLASH-ERROR->> "fmt"\n",##arg)
#define FLASH_DEBUG(fmt,arg...) do{\
if(EEPROM_DEBUG_ON)\
printf("<<-FLASH-DEBUG->> [%d]"fmt"\n",__LINE__, ##arg);\
}while(0)
void SPI_FLASH_Init(void);
uint32_t SPI_Read_ID(void);
void SPI_Erase_Sector(uint32_t addr);
void SPI_Read_Data(uint32_t addr, uint8_t *read_buff, uint32_t numByteToRead);
void SPI_WaitForWriteEnd(void);
void SPI_Write_Data(uint32_t addr, uint8_t *write_buff, uint32_t numByteToWrite);
#endif /* __BSP_SPI_FLASH_H */
主函数测试代码:
main.c
/**
******************************************************************************
* @file main.c
* @author fire
* @version V1.0
* @date 2013-xx-xx
* @brief I2C EEPROM(AT24C02)测试,测试信息通过USART1打印在电脑的超级终端
******************************************************************************
* @attention
*
* 实验平台:野火 F103-指南者 STM32 开发板
* 论坛 :http://www.firebbs.cn
* 淘宝 :https://fire-stm32.taobao.com
*
******************************************************************************
*/
#include "stm32f10x.h"
#include "./led/bsp_led.h"
#include "./usart/bsp_usart.h"
#include "bsp_spi_flash.h"
#include <string.h>
uint8_t readBuff[4096]; // 这个直接放到主函数中,会导致内存溢出(硬件错误)
uint8_t writeBuff[4096];
/**
* @brief 主函数
* @param 无
* @retval 无
*/
int main(void)
{
uint32_t id = 0;
uint16_t i = 0;
LED_GPIO_Config();
LED_BLUE;
USART_Config();
SPI_FLASH_Init();
printf("\t\n这是一个SPI-FLASH读写测试例程\t\n");
id = SPI_Read_ID();
printf("\t\nID = %#X\t\n", id);
SPI_Erase_Sector(0);
for (i = 0; i < 4096; i++)
{
writeBuff[i] = i;
}
SPI_Write_Data(0, writeBuff, 4096);
SPI_Read_Data(0, readBuff, 4096);
for (i = 0; i < 4096; i++)
{
printf("%#X ", readBuff[i]);
if ((i % 10) == 0)
printf("\n");
}
while (1);
}
/*********************************************END OF FILE**********************/