直接上代码:
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);
上一篇:stm32专题十八:详细分析SPI FLASH
下一篇:stm32专题十九:FatFs文件系统简介
推荐阅读最新更新时间:2024-11-09 11:21
设计资源 培训 开发板 精华推荐
- DC2157A,用于 LTM4630EY-1 双路 18A/单路 36A 降压模块稳压器的演示板,4.5V = VIN = 15V,Vout1 = 1.5V @ 18A,Vout2 = 1V @ 18A
- LTC4056-4.2 的典型应用 - 具有 ThinSOT 端接的线性锂离子充电器
- DIY点锡膏机
- INA219电流监控模块
- LTC5540 600MHz 至 1.3GHz 高动态范围下变频混频器的典型应用
- LTM4631IV 20A、1.2V 输出超薄降压型稳压器的典型应用电路
- MC33074DTBR2G 有源高 Q 陷波滤波器运算放大器的典型应用
- P1022COME-DS-PB、P1022COME-DS基于QorIQ P1022处理器和COM Express模块的开发系统
- LM2931AZ5低压差稳压器典型应用电路
- LTC4162IUFD-FST 9V 至 35V 2 节 3.2A 充电器的典型应用,具有 PowerPath 和 2A 输入限制