/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2022 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "bsp_delay.h"
#include "bsp_code_flash.h"
#include "bsp_w25qxx.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
QSPI_HandleTypeDef hqspi;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_QUADSPI_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_QUADSPI_Init();
/* USER CODE BEGIN 2 */
delay_init(400);
#define DATA_SIZE 10000
#define DATA_ADDR 380
//delay_ms(5000);
NORFLASH_Init();
static uint8_t pData_wr[DATA_SIZE] = {22,33,44};
uint32_t i;
for(i = 0; i < DATA_SIZE; i++)
{
pData_wr[i] = i;
}
NORFLASH_Write(pData_wr, DATA_ADDR, DATA_SIZE);
static uint8_t pData_rd[DATA_SIZE];
NORFLASH_Read((uint8_t*)pData_rd, DATA_ADDR, DATA_SIZE);
for(i = 0; i < DATA_SIZE; i++)
{
if((pData_wr[i] & 0xff) != (pData_rd[i] & 0xff)) break;
}
if(i < DATA_SIZE)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
}
else
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Supply configuration update enable
*/
HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 5;
RCC_OscInitStruct.PLL.PLLN = 160;
RCC_OscInitStruct.PLL.PLLP = 2;
RCC_OscInitStruct.PLL.PLLQ = 4;
RCC_OscInitStruct.PLL.PLLR = 2;
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
RCC_OscInitStruct.PLL.PLLFRACN = 0;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
|RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV4;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief QUADSPI Initialization Function
* @param None
* @retval None
*/
static void MX_QUADSPI_Init(void)
{
/* USER CODE BEGIN QUADSPI_Init 0 */
/* USER CODE END QUADSPI_Init 0 */
/* USER CODE BEGIN QUADSPI_Init 1 */
/* USER CODE END QUADSPI_Init 1 */
/* QUADSPI parameter configuration*/
hqspi.Instance = QUADSPI;
hqspi.Init.ClockPrescaler = 2;
hqspi.Init.FifoThreshold = 4;
hqspi.Init.SampleShifting = QSPI_SAMPLE_SHIFTING_HALFCYCLE;
hqspi.Init.FlashSize = 22;
hqspi.Init.ChipSelectHighTime = QSPI_CS_HIGH_TIME_5_CYCLE;
hqspi.Init.ClockMode = QSPI_CLOCK_MODE_0;
hqspi.Init.FlashID = QSPI_FLASH_ID_1;
hqspi.Init.DualFlash = QSPI_DUALFLASH_DISABLE;
if (HAL_QSPI_Init(&hqspi) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN QUADSPI_Init 2 */
/* USER CODE END QUADSPI_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
/*Configure GPIO pin : PB0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
#include "bsp_w25qxx.h"
#include "bsp_qspi.h"
uint16_t NORFLASH_TYPE=W25Q256; //默认是W25Q256
uint8_t NORFLASH_QPI_MODE=0; //QSPI模式标志:0,SPI模式;1,QPI模式.
//4Kbytes为一个Sector
//16个扇区为1个Block
//W25Q64
//容量为8M字节,共有128个Block,2048个Sector
//初始化SPI FLASH的IO口
void NORFLASH_Init(void)
{
uint8_t temp;
//QSPI_Init(); //初始化QSPI
NORFLASH_Qspi_Enable(); //使能QSPI模式
NORFLASH_TYPE=NORFLASH_ReadID();//读取FLASH ID.
if(NORFLASH_TYPE==W25Q64)
{
NORFLASH_Write_Enable(); //写使能
QSPI_Send_CMD(W25X_SetReadParam,0,(3<<6)|(0<<4)|(0<<2)|(3<<0),0); //QPI,设置读参数指令,地址为0,4线传数据_8位地址_无地址_4线传输指令,无空周期,1个字节数据
temp=3<<4; //设置P4&P5=11,8个dummy clocks,104M
QSPI_Transmit(&temp,1); //发送1个字节
}
//printf("ID:%x\r\n",NORFLASH_TYPE);
}
//W25QXX进入QSPI模式
void NORFLASH_Qspi_Enable(void)
{
uint8_t stareg2=0;
stareg2=NORFLASH_ReadSR(2); //先读出状态寄存器2的原始值
// printf("stareg2:%x\r\n",stareg2);
if((stareg2&0X02)==0) //QE位未使能
{
NORFLASH_Write_Enable(); //写使能
stareg2|=1<<1; //使能QE位
NORFLASH_Write_SR(2,stareg2); //写状态寄存器2
}
QSPI_Send_CMD(W25X_EnterQPIMode,0,(0<<6)|(0<<4)|(0<<2)|(1<<0),0); //写command指令,地址为0,无数据_8位地址_无地址_单线传输指令,无空周期,0个字节数据
NORFLASH_QPI_MODE=1; //标记QSPI模式
}
//W25QXX退出QSPI模式
void NORFLASH_Qspi_Disable(void)
{
QSPI_Send_CMD(W25X_ExitQPIMode,0,(0<<6)|(0<<4)|(0<<2)|(3<<0),0); //写command指令,地址为0,无数据_8位地址_无地址_4线传输指令,无空周期,0个字节数据
NORFLASH_QPI_MODE=0; //标记SPI模式
}
//读取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 NORFLASH_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;
}
if(NORFLASH_QPI_MODE)QSPI_Send_CMD(command,0,(3<<6)|(0<<4)|(0<<2)|(3<<0),0);//QPI,写command指令,地址为0,4线传数据_8位地址_无地址_4线传输指令,无空周期,1个字节数据
else QSPI_Send_CMD(command,0,(1<<6)|(0<<4)|(0<<2)|(1<<0),0); //SPI,写command指令,地址为0,单线传数据_8位地址_无地址_单线传输指令,无空周期,1个字节数据
QSPI_Receive(&byte,1);
return byte;
}
//写W25QXX状态寄存器
void NORFLASH_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;
}
if(NORFLASH_QPI_MODE)QSPI_Send_CMD(command,0,(3<<6)|(0<<4)|(0<<2)|(3<<0),0);//QPI,写command指令,地址为0,4线传数据_8位地址_无地址_4线传输指令,无空周期,1个字节数据
else QSPI_Send_CMD(command,0,(1<<6)|(0<<4)|(0<<2)|(1<<0),0); //SPI,写command指令,地址为0,单线传数据_8位地址_无地址_单线传输指令,无空周期,1个字节数据
QSPI_Transmit(&sr,1);
}
//W25QXX写使能
//将S1寄存器的WEL置位
void NORFLASH_Write_Enable(void)
{
if(NORFLASH_QPI_MODE)QSPI_Send_CMD(W25X_WriteEnable,0,(0<<6)|(0<<4)|(0<<2)|(3<<0),0); //QPI,写使能指令,地址为0,无数据_8位地址_无地址_4线传输指令,无空周期,0个字节数据
else QSPI_Send_CMD(W25X_WriteEnable,0,(0<<6)|(0<<4)|(0<<2)|(1<<0),0); //SPI,写使能指令,地址为0,无数据_8位地址_无地址_单线传输指令,无空周期,0个字节数据
}
//W25QXX写禁止
//将WEL清零
void NORFLASH_Write_Disable(void)
{
if(NORFLASH_QPI_MODE)QSPI_Send_CMD(W25X_WriteDisable,0,(0<<6)|(0<<4)|(0<<2)|(3<<0),0); //QPI,写禁止指令,地址为0,无数据_8位地址_无地址_4线传输指令,无空周期,0个字节数据
else QSPI_Send_CMD(W25X_WriteDisable,0,(0<<6)|(0<<4)|(0<<2)|(1<<0),0); //SPI,写禁止指令,地址为0,无数据_8位地址_无地址_单线传输指令,无空周期,0个字节数据
}
//返回值如下:
//0XEF13,表示芯片型号为W25Q80
//0XEF14,表示芯片型号为W25Q16
//0XEF15,表示芯片型号为W25Q32
//0XEF16,表示芯片型号为W25Q64
//0XEF17,表示芯片型号为W25Q128
//0XEF18,表示芯片型号为W25Q256
uint16_t NORFLASH_ReadID(void)
{
uint8_t temp[2];
uint16_t deviceid;
if(NORFLASH_QPI_MODE)QSPI_Send_CMD(W25X_ManufactDeviceID,0,(3<<6)|(2<<4)|(3<<2)|(3<<0),0); //QPI,读id,地址为0,4线传输数据_24位地址_4线传输地址_4线传输指令,无空周期,2个字节数据
else QSPI_Send_CMD(W25X_ManufactDeviceID,0,(1<<6)|(2<<4)|(1<<2)|(1<<0),0); //SPI,读id,地址为0,单线传输数据_24位地址_单线传输地址_单线传输指令,无空周期,2个字节数据
QSPI_Receive(temp,2);
deviceid=(temp[0]<<8)|temp[1];
return deviceid;
}
//读取SPI FLASH,仅支持QPI模式
//在指定地址开始读取指定长度的数据
//pBuffer:数据存储区
//ReadAddr:开始读取的地址(最大32bit)
//NumByteToRead:要读取的字节数(最大65535)
void NORFLASH_Read(uint8_t* pBuffer,uint32_t ReadAddr,uint16_t NumByteToRead)
{
QSPI_Send_CMD(W25X_FastReadData,ReadAddr,(3<<6)|(2<<4)|(3<<2)|(3<<0),8); //QPI,快速读数据,地址为ReadAddr,4线传输数据_24位地址_4线传输地址_4线传输指令,8空周期,NumByteToRead个数据
QSPI_Receive(pBuffer,NumByteToRead);
}
//SPI在一页(0~65535)内写入少于256个字节的数据
//在指定地址开始写入最大256字节的数据
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(最大32bit)
//NumByteToWrite:要写入的字节数(最大256),该数不应该超过该页的剩余字节数!!!
void NORFLASH_Write_Page(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
{
NORFLASH_Write_Enable(); //写使能
QSPI_Send_CMD(W25X_PageProgram,WriteAddr,(3<<6)|(2<<4)|(3<<2)|(3<<0),0); //QPI,页写指令,地址为WriteAddr,4线传输数据_24位地址_4线传输地址_4线传输指令,无空周期,NumByteToWrite个数据
QSPI_Transmit(pBuffer,NumByteToWrite);
NORFLASH_Wait_Busy(); //等待写入结束
}
//无检验写SPI FLASH
//必须确保所写的地址范围内的数据全部为0XFF,否则在非0XFF处写入的数据将失败!
//具有自动换页功能
//在指定地址开始写入指定长度的数据,但是要确保地址不越界!
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(最大32bit)
//NumByteToWrite:要写入的字节数(最大65535)
//CHECK OK
void NORFLASH_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)
{
NORFLASH_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:开始写入的地址(最大32bit)
//NumByteToWrite:要写入的字节数(最大65535)
uint8_t NORFLASH_BUFFER[4096];
void NORFLASH_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
{
uint32_t secpos;
uint16_t secoff;
uint16_t secremain;
uint16_t i;
uint8_t * NORFLASH_BUF;
NORFLASH_BUF=NORFLASH_BUFFER;
secpos=WriteAddr/4096;//扇区地址
secoff=WriteAddr%4096;//在扇区内的偏移
secremain=4096-secoff;//扇区剩余空间大小
//printf("ad:%X,nb:%X\r\n",WriteAddr,NumByteToWrite);//测试用
if(NumByteToWrite<=secremain)secremain=NumByteToWrite;//不大于4096个字节
while(1)
{
NORFLASH_Read(NORFLASH_BUF,secpos*4096,4096);//读出整个扇区的内容
for(i=0;i<secremain;i++)//校验数据
{
if(NORFLASH_BUF[secoff+i]!=0XFF)break;//需要擦除
}
if(i<secremain)//需要擦除
{
NORFLASH_Erase_Sector(secpos);//擦除这个扇区
for(i=0;i<secremain;i++) //复制
{
NORFLASH_BUF[i+secoff]=pBuffer[i];
}
NORFLASH_Write_NoCheck(NORFLASH_BUF,secpos*4096,4096); //写入整个扇区
}else NORFLASH_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 NORFLASH_Erase_Chip(void)
{
NORFLASH_Write_Enable(); //SET WEL
NORFLASH_Wait_Busy();
QSPI_Send_CMD(W25X_ChipErase,0,(0<<6)|(0<<4)|(0<<2)|(3<<0),0);//QPI,写全片擦除指令,地址为0,无数据_8位地址_无地址_4线传输指令,无空周期,0个字节数据
NORFLASH_Wait_Busy(); //等待芯片擦除结束
}
//擦除一个扇区
//Dst_Addr:扇区地址 根据实际容量设置
//擦除一个扇区的最少时间:150ms
void NORFLASH_Erase_Sector(uint32_t Dst_Addr)
{
//printf("fe:%x\r\n",Dst_Addr); //监视falsh擦除情况,测试用
Dst_Addr*=4096;
NORFLASH_Write_Enable(); //SET WEL
NORFLASH_Wait_Busy();
QSPI_Send_CMD(W25X_SectorErase,Dst_Addr,(0<<6)|(2<<4)|(3<<2)|(3<<0),0);//QPI,写扇区擦除指令,地址为0,无数据_24位地址_4线传输地址_4线传输指令,无空周期,0个字节数据
NORFLASH_Wait_Busy(); //等待擦除完成
}
//等待空闲
void NORFLASH_Wait_Busy(void)
{
while((NORFLASH_ReadSR(1)&0x01)==0x01); // 等待BUSY位清空
}
#include "bsp_qspi.h"
#define QSPI_Handler hqspi
//等待状态标志
//flag:需要等待的标志位
//sta:需要等待的状态
//wtime:等待时间
//返回值:0,等待成功.
// 1,等待失败.
uint8_t QSPI_Wait_Flag(uint32_t flag,uint8_t sta,uint32_t wtime)
{
uint8_t flagsta=0;
while(wtime)
{
flagsta=(QUADSPI->SR&flag)?1:0;
if(flagsta==sta)break;
wtime--;
}
if(wtime)return 0;
else return 1;
}
//QSPI发送命令
//cmd:要发送的指令
//addr:发送到的目的地址
//mode:模式,详细位定义如下:
// mode[1:0]:指令模式;00,无指令;01,单线传输指令;10,双线传输指令;11,四线传输指令.
// mode[3:2]:地址模式;00,无地址;01,单线传输地址;10,双线传输地址;11,四线传输地址.
// mode[5:4]:地址长度;00,8位地址;01,16位地址;10,24位地址;11,32位地址.
// mode[7:6]:数据模式;00,无数据;01,单线传输数据;10,双线传输数据;11,四线传输数据.
//dmcycle:空指令周期数
void QSPI_Send_CMD(uint8_t cmd,uint32_t addr,uint8_t mode,uint8_t dmcycle)
{
QSPI_CommandTypeDef Cmdhandler;
Cmdhandler.Instruction=cmd; //指令
Cmdhandler.Address=addr; //地址
Cmdhandler.DummyCycles=dmcycle; //设置空指令周期数
if(((mode>>0)&0x03) == 0)
Cmdhandler.InstructionMode=QSPI_INSTRUCTION_NONE; //指令模式
else if(((mode>>0)&0x03) == 1)
Cmdhandler.InstructionMode=QSPI_INSTRUCTION_1_LINE; //指令模式
else if(((mode>>0)&0x03) == 2)
Cmdhandler.InstructionMode=QSPI_INSTRUCTION_2_LINES; //指令模式
else if(((mode>>0)&0x03) == 3)
Cmdhandler.InstructionMode=QSPI_INSTRUCTION_4_LINES; //指令模式
if(((mode>>2)&0x03) == 0)
Cmdhandler.AddressMode=QSPI_ADDRESS_NONE; //地址模式
else if(((mode>>2)&0x03) == 1)
Cmdhandler.AddressMode=QSPI_ADDRESS_1_LINE; //地址模式
else if(((mode>>2)&0x03) == 2)
Cmdhandler.AddressMode=QSPI_ADDRESS_2_LINES; //地址模式
else if(((mode>>2)&0x03) == 3)
Cmdhandler.AddressMode=QSPI_ADDRESS_4_LINES; //地址模式
if(((mode>>4)&0x03) == 0)
Cmdhandler.AddressSize=QSPI_ADDRESS_8_BITS; //地址长度
else if(((mode>>4)&0x03) == 1)
Cmdhandler.AddressSize=QSPI_ADDRESS_16_BITS; //地址长度
else if(((mode>>4)&0x03) == 2)
Cmdhandler.AddressSize=QSPI_ADDRESS_24_BITS; //地址长度
else if(((mode>>4)&0x03) == 3)
Cmdhandler.AddressSize=QSPI_ADDRESS_32_BITS; //地址长度
if(((mode>>6)&0x03) == 0)
Cmdhandler.DataMode=QSPI_DATA_NONE; //数据模式
else if(((mode>>6)&0x03) == 1)
Cmdhandler.DataMode=QSPI_DATA_1_LINE; //数据模式
else if(((mode>>6)&0x03) == 2)
Cmdhandler.DataMode=QSPI_DATA_2_LINES; //数据模式
else if(((mode>>6)&0x03) == 3)
Cmdhandler.DataMode=QSPI_DATA_4_LINES; //数据模式
Cmdhandler.SIOOMode=QSPI_SIOO_INST_EVERY_CMD; //每次都发送指令
Cmdhandler.AlternateByteMode=QSPI_ALTERNATE_BYTES_NONE; //无交替字节
Cmdhandler.DdrMode=QSPI_DDR_MODE_DISABLE; //关闭DDR模式
Cmdhandler.DdrHoldHalfCycle=QSPI_DDR_HHC_ANALOG_DELAY;
HAL_QSPI_Command(&QSPI_Handler,&Cmdhandler,5000);
}
//QSPI接收指定长度的数据
//buf:接收数据缓冲区首地址
//datalen:要传输的数据长度
//返回值:0,正常
// 其他,错误代码
uint8_t QSPI_Receive(uint8_t* buf,uint32_t datalen)
{
QSPI_Handler.Instance->DLR=datalen-1; //配置数据长度
if(HAL_QSPI_Receive(&QSPI_Handler,buf,5000)==HAL_OK) return 0; //接收数据
else return 1;
}
//QSPI发送指定长度的数据
//buf:发送数据缓冲区首地址
//datalen:要传输的数据长度
//返回值:0,正常
// 其他,错误代码
uint8_t QSPI_Transmit(uint8_t* buf,uint32_t datalen)
{
QSPI_Handler.Instance->DLR=datalen-1; //配置数据长度
if(HAL_QSPI_Transmit(&QSPI_Handler,buf,5000)==HAL_OK) return 0; //发送数据
else return 1;
}
总结:
1、
2、
3、
4、
5、clock prescaler:W25Q256最高104MHz时钟,所以分频设置为2,采用时钟200MHz/(2+1);
FLASH SIZE:22,W25Q64为8MB大小,2的(22+1)次方
Clock Mode:Low,CLK空闲时为低电平
Chip Select:High Time设为5,最低50ns,5ns35 > 50ns
6、所有QSPI引脚都要设置为very high,NCS脚(PB6)必须设置为PULL-UP,否则会有问题(为什么?不懂),所以干脆把所有引脚都设置为PULL-UP
7、
8、
9、W25Q64与W25Q256的区别:①W25Q64只能24位地址,W25Q256可以24位地址、也可以32位地址;②W25Q64读状态寄存器为05h、35h,W25Q256为05h、35h、15h;③W25Q64写状态寄存器为01h,W25Q256为01h、31h、11h;等等