问题描述
STM32CubeMX生成F411CEUx USB MSC 工程 采用SPI2与w25qxx结合做8MB容量的Flash时
无法读取 或者读取容量对不上
硬件:STM32F411CEU6
w25q64
引脚配置:
参考资料:
1、"Fyra-BH"的【STM32学习】基于STM32F411CEU6的USB储存设备@Fyra-BH
2、“zhe_boy_is_z” 的 F411-WeAct(一)SPi 驱动外部Flash(W25Q64)@zhe_boy_is_z
参考以上两位博主的帖子做了MSC的Flash 但是发现在SPI2 的通道下相同的设置电脑显示FLASH大小只有520KB
usbd_storage_if.c文件设置的容量是:8MB
#define STORAGE_LUN_NBR 1
#define STORAGE_BLK_NBR 0x10000
#define STORAGE_BLK_SIZ 0x200
/* USER CODE BEGIN PRIVATE_DEFINES */
#define USER_STORAGE_LUN_NBR 1
#define USER_STORAGE_BLK_NBR 2048
#define USER_STORAGE_BLK_SIZ 4096 //实际使用的定义
但是在SPI1 或者SPI3等通道上就可以完整的显示8MB 况且可以任意“扩容”
修改参数显示16MB容量
问题探索过程
1、怀疑是w25qxx库的问题
参考 Pzkkkkkk :STM32Cube MX USB虚拟U盘+FATFS+W25Q128
直接用现成的代码复制粘贴过来读取也还是520KB
而且在调试过程中发现如下的参数影响电脑上的容量显示
#define STORAGE_BLK_SIZ 0x200
规律为:
STORAGE_BLK_SIZ | 显示容量 | 格式化时容量 |
---|---|---|
512 | 48.5kB | 68.5KB |
1024 | 117kB | 137kB |
2048 | 250kB | 274kB |
4096 | 520kB | 548kB |
存储块大小改变的时候 格式化时显示的总容量都是137的倍数
我后来也没找到这个137是哪里来的
现在怀疑可能是CubeMx在生成库时有点问题,具体如何调 我也是刚上手不太熟练。等以后解决了之后再开贴
2、怀疑电路设计问题
换一片F411CEU6的板子问题一样没解决,而且还出现无法格式化的弹窗:
看芯片手册
看手册里SPI部分的说明也没找到关键信息
DMA通道问题
去掉DMA之后也还是不行
问题复现过程
usbd_storage_if.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : usbd_storage_if.c
* @version : v1.0_Cube
* @brief : Memory management layer.
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2021 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "usbd_storage_if.h"
/* USER CODE BEGIN INCLUDE */
#include "w25qxx.h"
/* USER CODE END INCLUDE */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
* @brief Usb device.
* @{
*/
/** @defgroup USBD_STORAGE
* @brief Usb mass storage device module
* @{
*/
/** @defgroup USBD_STORAGE_Private_TypesDefinitions
* @brief Private types.
* @{
*/
/* USER CODE BEGIN PRIVATE_TYPES */
/* USER CODE END PRIVATE_TYPES */
/**
* @}
*/
/** @defgroup USBD_STORAGE_Private_Defines
* @brief Private defines.
* @{
*/
#define STORAGE_LUN_NBR 1
#define STORAGE_BLK_NBR 0x10000
#define STORAGE_BLK_SIZ 0x200
/* USER CODE BEGIN PRIVATE_DEFINES */
#define USER_STORAGE_LUN_NBR 1
#define USER_STORAGE_BLK_NBR 2048
#define USER_STORAGE_BLK_SIZ 4096
//#define USER_STORAGE_BLK_SIZ 2048
/* USER CODE END PRIVATE_DEFINES */
/**
* @}
*/
/** @defgroup USBD_STORAGE_Private_Macros
* @brief Private macros.
* @{
*/
/* USER CODE BEGIN PRIVATE_MACRO */
/* USER CODE END PRIVATE_MACRO */
/**
* @}
*/
/** @defgroup USBD_STORAGE_Private_Variables
* @brief Private variables.
* @{
*/
/* USER CODE BEGIN INQUIRY_DATA_FS */
/** USB Mass storage Standard Inquiry Data. */
const int8_t STORAGE_Inquirydata_FS[] = {/* 36 */
/* LUN 0 */
0x00,
0x80,
0x02,
0x02,
(STANDARD_INQUIRY_DATA_LEN - 5),
0x00,
0x00,
0x00,
'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
'P', 'r', 'o', 'd', 'u', 'c', 't', ' ', /* Product : 16 Bytes */
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
'0', '.', '0' ,'1' /* Version : 4 Bytes */
};
/* USER CODE END INQUIRY_DATA_FS */
/* USER CODE BEGIN PRIVATE_VARIABLES */
/* USER CODE END PRIVATE_VARIABLES */
/**
* @}
*/
/** @defgroup USBD_STORAGE_Exported_Variables
* @brief Public variables.
* @{
*/
extern USBD_HandleTypeDef hUsbDeviceFS;
/* USER CODE BEGIN EXPORTED_VARIABLES */
/* USER CODE END EXPORTED_VARIABLES */
/**
* @}
*/
/** @defgroup USBD_STORAGE_Private_FunctionPrototypes
* @brief Private functions declaration.
* @{
*/
static int8_t STORAGE_Init_FS(uint8_t lun);
static int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size);
static int8_t STORAGE_IsReady_FS(uint8_t lun);
static int8_t STORAGE_IsWriteProtected_FS(uint8_t lun);
static int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
static int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len);
static int8_t STORAGE_GetMaxLun_FS(void);
/* USER CODE BEGIN PRIVATE_FUNCTIONS_DECLARATION */
/* USER CODE END PRIVATE_FUNCTIONS_DECLARATION */
/**
* @}
*/
USBD_StorageTypeDef USBD_Storage_Interface_fops_FS =
{
STORAGE_Init_FS,
STORAGE_GetCapacity_FS,
STORAGE_IsReady_FS,
STORAGE_IsWriteProtected_FS,
STORAGE_Read_FS,
STORAGE_Write_FS,
STORAGE_GetMaxLun_FS,
(int8_t *)STORAGE_Inquirydata_FS
};
/* Private functions ---------------------------------------------------------*/
/**
* @brief Initializes over USB FS IP
* @param lun:
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_Init_FS(uint8_t lun)
{
/* USER CODE BEGIN 2 */
W25QXX_Init();
return (USBD_OK);
/* USER CODE END 2 */
}
/**
* @brief .
* @param lun: .
* @param block_num: .
* @param block_size: .
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
/* USER CODE BEGIN 3 */
*block_num = USER_STORAGE_BLK_NBR;
*block_size = USER_STORAGE_BLK_SIZ;
return (USBD_OK);
/* USER CODE END 3 */
}
/**
* @brief .
* @param lun: .
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_IsReady_FS(uint8_t lun)
{
/* USER CODE BEGIN 4 */
return (USBD_OK);
/* USER CODE END 4 */
}
/**
* @brief .
* @param lun: .
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_IsWriteProtected_FS(uint8_t lun)
{
/* USER CODE BEGIN 5 */
return (USBD_OK);
/* USER CODE END 5 */
}
/**
* @brief .
* @param lun: .
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
/* USER CODE BEGIN 6 */
W25QXX_Read(buf, blk_addr * USER_STORAGE_BLK_SIZ, blk_len * USER_STORAGE_BLK_SIZ);
return (USBD_OK);
/* USER CODE END 6 */
}
/**
* @brief .
* @param lun: .
* @retval USBD_OK if all operations are OK else USBD_FAIL
*/
int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
/* USER CODE BEGIN 7 */
W25QXX_Write(buf, blk_addr * USER_STORAGE_BLK_SIZ, blk_len * USER_STORAGE_BLK_SIZ);
return (USBD_OK);
/* USER CODE END 7 */
}
/**
* @brief .
* @param None
* @retval .
*/
int8_t STORAGE_GetMaxLun_FS(void)
{
/* USER CODE BEGIN 8 */
return (USER_STORAGE_LUN_NBR - 1);
/* USER CODE END 8 */
}
/* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */
/* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
w25qxx.c
#include "w25qxx.h"
/
//本程序只供学习使用,未经作者许可,不得用于其它任何用途
//ALIENTEK STM32F103开发板
//W25QXX驱动代码
//正点原子@ALIENTEK
//技术论坛:www.openedv.com
//创建日期:2017/5/30
//版本:V1.0
//版权所有,盗版必究。
//Copyright(C) 广州市星翼电子科技有限公司 2014-2024
//All rights reserved
//
uint16_t W25QXX_TYPE = W25Q64; //默认是W25Q128
//4Kbytes为一个Sector
//16个扇区为1个Block
//W25Q256
//容量为32M字节,共有512个Block,8192个Sector
//初始化SPI FLASH的IO口
void W25QXX_Init(void)
{
uint8_t temp;
W25QXX_CS(1); //SPI FLASH不选中
// SPI1_Init(); //已经在main.c中初始化
W25QXX_TYPE = W25QXX_ReadID(); //读取FLASH ID.
if (W25QXX_TYPE == W25Q128) //SPI FLASH为W25Q256
{
temp = W25QXX_ReadSR(3); //读取状态寄存器3,判断地址模式
if ((temp & 0X01) == 0) //如果不是4字节地址模式,则进入4字节地址模式
{
W25QXX_CS(0); //选中
SPIx_ReadWriteByte(W25X_Enable4ByteAddr); //发送进入4字节地址模式指令
uint8_t data = W25X_Enable4ByteAddr;
SPIx_WriteByte(&data, 1);
W25QXX_CS(1); //取消片选
}
}
}
//读取W25QXX的状态寄存器,W25QXX一共有3个状态寄存器
//状态寄存器1:
//BIT7 6 5 4 3 2 1 0
//SPR RV TB BP2 BP1 BP0 WEL BUSY
//SPR:默认0,状态寄存器保护位,配合WP使用
//TB,BP2,BP1,BP0:FLASH区域写保护设置
//WEL:写使能锁定
//BUSY:忙标记位(1,忙;0,空闲)
//默认:0x00
//状态寄存器2:
//BIT7 6 5 4 3 2 1 0
//SUS CMP LB3 LB2 LB1 (R) QE SRP1
//状态寄存器3:
//BIT7 6 5 4 3 2 1 0
//HOLD/RST DRV1 DRV0 (R) (R) WPS ADP ADS
//regno:状态寄存器号,范:1~3
//返回值:状态寄存器值
uint8_t W25QXX_ReadSR(uint8_t regno)
{
uint8_t byte = 0, command = 0;
switch (regno)
{
case 1:
command = W25X_ReadStatusReg1; //读状态寄存器1指令
break;
case 2:
command = W25X_ReadStatusReg2; //读状态寄存器2指令
break;
case 3:
command = W25X_ReadStatusReg3; //读状态寄存器3指令
break;
default:
command = W25X_ReadStatusReg1;
break;
}
W25QXX_CS(0); //使能器件
SPIx_ReadWriteByte(command); //发送读取状态寄存器命令
SPIx_ReadByte(&byte, 1);
W25QXX_CS(1); //取消片选
return byte;
}
//写W25QXX状态寄存器
void W25QXX_Write_SR(uint8_t regno, uint8_t sr)
{
uint8_t command = 0;
switch (regno)
{
case 1:
command = W25X_WriteStatusReg1; //写状态寄存器1指令
break;
case 2:
command = W25X_WriteStatusReg2; //写状态寄存器2指令
break;
case 3:
command = W25X_WriteStatusReg3; //写状态寄存器3指令
break;
default:
command = W25X_WriteStatusReg1;
break;
}
W25QXX_CS(0); //使能器件
SPIx_ReadWriteByte(command); //发送写取状态寄存器命令
SPIx_ReadWriteByte(sr); //写入一个字节
W25QXX_CS(1); //取消片选
}
//W25QXX写使能
//将WEL置位
void W25QXX_Write_Enable(void)
{
W25QXX_CS(0); //使能器件
SPIx_ReadWriteByte(W25X_WriteEnable); //发送写使能
W25QXX_CS(1); //取消片选
}
//W25QXX写禁止
//将WEL清零
void W25QXX_Write_Disable(void)
{
W25QXX_CS(0); //使能器件
SPIx_ReadWriteByte(W25X_WriteDisable); //发送写禁止指令
W25QXX_CS(1); //取消片选
}
//读取芯片ID
//返回值如下:
//0XEF13,表示芯片型号为W25Q80
//0XEF14,表示芯片型号为W25Q16
//0XEF15,表示芯片型号为W25Q32
//0XEF16,表示芯片型号为W25Q64
//0XEF17,表示芯片型号为W25Q128
//0XEF18,表示芯片型号为W25Q256
uint16_t W25QXX_ReadID(void)
{
uint16_t Temp = 0;
uint8_t byte = 0;
W25QXX_CS(0);
SPIx_ReadWriteByte(0x90); //发送读取ID命令
SPIx_ReadWriteByte(0x00);
SPIx_ReadWriteByte(0x00);
SPIx_ReadWriteByte(0x00);
SPIx_ReadByte(&byte, 1);
Temp |= byte;
SPIx_ReadByte(&byte, 1);
Temp |= byte;
W25QXX_CS(1);
return Temp;
}
//读取SPI FLASH
//在指定地址开始读取指定长度的数据
//pBuffer:数据存储区
//ReadAddr:开始读取的地址(24bit)
//NumByteToRead:要读取的字节数(最大65535)
void W25QXX_Read(uint8_t *pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead)
{
W25QXX_CS(0); //使能器件
SPIx_ReadWriteByte(W25X_ReadData); //发送读取命令
if (W25QXX_TYPE == W25Q256) //如果是W25Q256的话地址为4字节的,要发送最高8位
{
SPIx_ReadWriteByte((uint8_t)((ReadAddr) >> 24));
}
SPIx_ReadWriteByte((uint8_t)((ReadAddr) >> 16)); //发送24bit地址
SPIx_ReadWriteByte((uint8_t)((ReadAddr) >> 8));
SPIx_ReadWriteByte((uint8_t)ReadAddr);
SPIx_ReadByte(pBuffer, NumByteToRead);
uint16_t count = 10000;
while ((HAL_SPI_GetState(&SPI_CH) == HAL_SPI_STATE_BUSY_RX)&&count)
{
count --;
}
W25QXX_CS(1);
}
//SPI在一页(0~65535)内写入少于256个字节的数据
//在指定地址开始写入最大256字节的数据
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(24bit)
//NumByteToWrite:要写入的字节数(最大256),该数不应该超过该页的剩余字节数!!!
void W25QXX_Write_Page(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite)
{
// uint16_t i;
W25QXX_Write_Enable(); //SET WEL
W25QXX_CS(0); //使能器件
SPIx_ReadWriteByte(W25X_PageProgram); //发送写页命令
if (W25QXX_TYPE == W25Q256) //如果是W25Q256的话地址为4字节的,要发送最高8位
{
SPIx_ReadWriteByte((uint8_t)((WriteAddr) >> 24));
}
SPIx_ReadWriteByte((uint8_t)((WriteAddr) >> 16)); //发送24bit地址
SPIx_ReadWriteByte((uint8_t)((WriteAddr) >> 8));
SPIx_ReadWriteByte((uint8_t)WriteAddr);
SPIx_WriteByte(pBuffer, NumByteToWrite);
uint16_t count = 10000;
while ((HAL_SPI_GetState(&SPI_CH) == HAL_SPI_STATE_BUSY_TX)&&count)
{
count --;
}
// for (i = 0; i < NumByteToWrite; i++)
// SPIx_ReadWriteByte(pBuffer[i]); //循环写数
W25QXX_CS(1); //取消片选
W25QXX_Wait_Busy(); //等待写入结束
}
//无检验写SPI FLASH
//必须确保所写的地址范围内的数据全部为0XFF,否则在非0XFF处写入的数据将失败!
//具有自动换页功能
//在指定地址开始写入指定长度的数据,但是要确保地址不越界!
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(24bit)
//NumByteToWrite:要写入的字节数(最大65535)
//CHECK OK
void W25QXX_Write_NoCheck(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite)
{
uint16_t pageremain;
pageremain = 256 - WriteAddr % 256; //单页剩余的字节数
if (NumByteToWrite <= pageremain)
pageremain = NumByteToWrite; //不大于256个字节
while (1)
{
W25QXX_Write_Page(pBuffer, WriteAddr, pageremain);
if (NumByteToWrite == pageremain)
break; //写入结束了
else //NumByteToWrite>pageremain
{
pBuffer += pageremain;
WriteAddr += pageremain;
NumByteToWrite -= pageremain; //减去已经写入了的字节数
if (NumByteToWrite > 256)
pageremain = 256; //一次可以写入256个字节
else
pageremain = NumByteToWrite; //不够256个字节了
}
};
}
//写SPI FLASH
//在指定地址开始写入指定长度的数据
//该函数带擦除操作!
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(24bit)
//NumByteToWrite:要写入的字节数(最大65535)
uint8_t W25QXX_BUFFER[4096];
void W25QXX_Write(uint8_t *pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite)
{
uint32_t secpos;
uint16_t secoff;
uint16_t secremain;
uint16_t i;
uint8_t *W25QXX_BUF;
W25QXX_BUF = W25QXX_BUFFER;
secpos = WriteAddr / 4096; //扇区地址
secoff = WriteAddr % 4096; //在扇区内的偏移
secremain = 4096 - secoff; //扇区剩余空间大小
//printf("ad:%X,nb:%X\r\n",WriteAddr,NumByteToWrite);//测试用
// HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
if (NumByteToWrite <= secremain)
secremain = NumByteToWrite; //不大于4096个字节
while (1)
{
W25QXX_Read(W25QXX_BUF, secpos * 4096, 4096); //读出整个扇区的内容
for (i = 0; i < secremain; i++) //校验数据
{
if (W25QXX_BUF[secoff + i] != 0XFF)
break; //需要擦除
}
if (i < secremain) //需要擦除
{
W25QXX_Erase_Sector(secpos); //擦除这个扇区
for (i = 0; i < secremain; i++) //复制
{
W25QXX_BUF[i + secoff] = pBuffer[i];
}
W25QXX_Write_NoCheck(W25QXX_BUF, secpos * 4096, 4096); //写入整个扇区
}
else
W25QXX_Write_NoCheck(pBuffer, WriteAddr, secremain); //写已经擦除了的,直接写入扇区剩余区间.
if (NumByteToWrite == secremain)
break; //写入结束了
else //写入未结束
{
secpos++; //扇区地址增1
secoff = 0; //偏移位置为0
pBuffer += secremain; //指针偏移
WriteAddr += secremain; //写地址偏移
NumByteToWrite -= secremain; //字节数递减
if (NumByteToWrite > 4096)
secremain = 4096; //下一个扇区还是写不完
else
secremain = NumByteToWrite; //下一个扇区可以写完了
}
};
}
//擦除整个芯片
//等待时间超长...
void W25QXX_Erase_Chip(void)
{
W25QXX_Write_Enable(); //SET WEL
W25QXX_Wait_Busy();
W25QXX_CS(0); //使能器件
SPIx_ReadWriteByte(W25X_ChipErase); //发送片擦除命令
W25QXX_CS(1); //取消片选
W25QXX_Wait_Busy(); //等待芯片擦除结束
}
//擦除一个扇区
//Dst_Addr:扇区地址 根据实际容量设置
//擦除一个扇区的最少时间:150ms
void W25QXX_Erase_Sector(uint32_t Dst_Addr)
{
//监视falsh擦除情况,测试用
//printf("fe:%x\r\n",Dst_Addr);
Dst_Addr *= 4096;
W25QXX_Write_Enable(); //SET WEL
W25QXX_Wait_Busy();
W25QXX_CS(0); //使能器件
SPIx_ReadWriteByte(W25X_SectorErase); //发送扇区擦除指令
if (W25QXX_TYPE == W25Q256) //如果是W25Q256的话地址为4字节的,要发送最高8位
{
SPIx_ReadWriteByte((uint8_t)((Dst_Addr) >> 24));
}
SPIx_ReadWriteByte((uint8_t)((Dst_Addr) >> 16)); //发送24bit地址
SPIx_ReadWriteByte((uint8_t)((Dst_Addr) >> 8));
SPIx_ReadWriteByte((uint8_t)Dst_Addr);
W25QXX_CS(1); //取消片选
W25QXX_Wait_Busy(); //等待擦除完成
}
//等待空闲
void W25QXX_Wait_Busy(void)
{
while ((W25QXX_ReadSR(1) & 0x01) == 0x01)
; // 等待BUSY位清空
}
//进入掉电模式
void W25QXX_PowerDown(void)
{
W25QXX_CS(0); //使能器件
SPIx_ReadWriteByte(W25X_PowerDown); //发送掉电命令
W25QXX_CS(1); //取消片选
HAL_Delay(3); //等待TPD
}
//唤醒
void W25QXX_WAKEUP(void)
{
W25QXX_CS(0); //使能器件
SPIx_ReadWriteByte(W25X_ReleasePowerDown); // send W25X_PowerDown command 0xAB
W25QXX_CS(1); //取消片选
HAL_Delay(3); //等待TRES1
}
uint8_t SPIx_ReadWriteByte(uint8_t TxData)
{
uint8_t Rxdata;
#if (USE_DMA)
HAL_SPI_TransmitReceive_DMA(&SPI_CH,&TxData,&Rxdata,1);
#else
HAL_SPI_TransmitReceive(&SPI_CH,&TxData,&Rxdata,1, 1000);
#endif
return Rxdata; //返回收到的数据
}
void SPIx_WriteByte(uint8_t *pBuffer, uint16_t NumByteToWrite)
{
#if (USE_DMA)
HAL_SPI_Transmit_DMA(&SPI_CH, pBuffer, NumByteToWrite);
#else
HAL_SPI_Transmit(&SPI_CH, pBuffer, NumByteToWrite, 1000);
#endif
}
void SPIx_ReadByte(uint8_t *pBuffer, uint16_t NumByteToRead)
{
#if (USE_DMA)
HAL_SPI_Receive_DMA(&SPI_CH, pBuffer, NumByteToRead); // DMA channel
#else
HAL_SPI_Receive(&SPI_CH, pBuffer, NumByteToRead, 1000); //
#endif
}
w25qxx.h
//
//Usage:
//1.config SPI channel:
// #define SPI_CH hspix
//2.if USE SPI DMA chanel set:
// #define USE_DMA 1
//3.config w25qxx CS pin:
// #define W25QXX_CS_GPIO_Port F_CS_GPIO_Port
// #define W25QXX_CS_Pin F_CS_Pin
//
#ifndef __W25QXX_H
#define __W25QXX_H
#include <stdint.h>
#include "stm32f4xx_hal.h"
#include "main.h"
#define SPI_CH hspi2
#define USE_DMA 0 //read or write SPI use DMA:1 else:0;
#define W25QXX_CS_GPIO_Port F_CS_GPIO_Port
#define W25QXX_CS_Pin F_CS_Pin
extern SPI_HandleTypeDef SPI_CH;
//W25X系列/Q系列芯片列表
#define W25Q80 0XEF13
#define W25Q16 0XEF14
#define W25Q32 0XEF15
#define W25Q64 0XEF16
#define W25Q128 0XEF17
#define W25Q256 0XEF18
extern uint16_t W25QXX_TYPE; //定义W25QXX芯片型号
#define W25QXX_CS(state) HAL_GPIO_WritePin(W25QXX_CS_GPIO_Port, W25QXX_CS_Pin, (GPIO_PinState)state)
//
//指令表
#define W25X_WriteEnable 0x06
#define W25X_WriteDisable 0x04
#define W25X_ReadStatusReg1 0x05
#define W25X_ReadStatusReg2 0x35
#define W25X_ReadStatusReg3 0x15
#define W25X_WriteStatusReg1 0x01
#define W25X_WriteStatusReg2 0x31
#define W25X_WriteStatusReg3 0x11
#define W25X_ReadData 0x03
#define W25X_FastReadData 0x0B
#define W25X_FastReadDual 0x3B
#define W25X_PageProgram 0x02
#define W25X_BlockErase 0xD8
#define W25X_SectorErase 0x20
#define W25X_ChipErase 0xC7
#define W25X_PowerDown 0xB9
#define W25X_ReleasePowerDown 0xAB
#define W25X_DeviceID 0xAB
#define W25X_ManufactDeviceID 0x90
#define W25X_JedecDeviceID 0x9F
#define W25X_Enable4ByteAddr 0xB7
#define W25X_Exit4ByteAddr 0xE9
uint8_t SPIx_ReadWriteByte(uint8_t TxData);
void SPIx_WriteByte(uint8_t *pBuffer, uint16_t NumByteToWrite);
void SPIx_ReadByte(uint8_t *pBuffer, uint16_t NumByteToRead);
void W25QXX_Init(void);
uint16_t W25QXX_ReadID(void); //读取FLASH ID
uint8_t W25QXX_ReadSR(uint8_t regno); //读取状态寄存器
void W25QXX_4ByteAddr_Enable(void); //使能4字节地址模式
void W25QXX_Write_SR(uint8_t regno,uint8_t sr); //写状态寄存器
void W25QXX_Write_Enable(void); //写使能
void W25QXX_Write_Disable(void); //写保护
void W25QXX_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);
void W25QXX_Read(uint8_t* pBuffer,uint32_t ReadAddr,uint16_t NumByteToRead); //读取flash
void W25QXX_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);//写入flash
void W25QXX_Erase_Chip(void); //整片擦除
void W25QXX_Erase_Sector(uint32_t Dst_Addr); //扇区擦除
void W25QXX_Wait_Busy(void); //等待空闲
void W25QXX_PowerDown(void); //进入掉电模式
void W25QXX_WAKEUP(void);
#endif