STM32存储左右互搏 QSPI总线读写FLASH W25QXX

STM32存储左右互搏 QSPI总线读写FLASH W25QXX

FLASH是常用的一种非易失存储单元,W25QXX系列Flash有不同容量的型号,如W25Q64的容量为64Mbit,也就是8MByte。这里介绍STM32CUBEIDE开发平台HAL库Qual SPI总线操作W25Q各型号FLASH的例程。

W25QXX介绍

W25QXX的SOIC封装如下所示,在采用QUAL SPI而不是SPI时,管脚定义为:
在这里插入图片描述
即由片选(/CS), 时钟(CLK), 双向4根输入输出线(IO0, IO1, IO2, IO3)组成6线QSPI信号接口。VCC和GND提供电源和接地连接。

例程采用STM32H750VBT6芯片, FLASH可以选择为8/16/32/64/128/256/512/1024 Mbit的W25Q型号。

STM32工程配置

首先建立基本工程并设置时钟:
在这里插入图片描述
在这里插入图片描述
注意QSPI时钟在单独的时钟树支上:
在这里插入图片描述
QSPI接口可以配置为两个Bank(并行协同存储扩展),也可以配置为单个Bank,并且可以由4线(数据线)模式降级为单线/双线(数据线)模式。这里对于单个Flash,选择一个bank配置:
在这里插入图片描述
在这里插入图片描述
注意QSPI的片选信号是硬件自动控制,和SPI可以采用软件代码控制不同,所以不必单独指定一个GPIO作为片选:
在这里插入图片描述
不用DMA
在这里插入图片描述

配置串口UART1作为命令输入和打印输出接口:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
不用DMA:
在这里插入图片描述
保存并生成初始工程代码:
在这里插入图片描述

STM32工程代码

UART串口printf打印输出实现参考:STM32 UART串口printf函数应用及浮点打印代码空间节省 (HAL)

建立W25Q访问的库头文件W25QXX_QSPI.h:*

#ifndef INC_W25QXX_H_
#define INC_W25QXX_H_#include "main.h"//W25QXX serial chip list:
#define W25Q80_ID 	0XEF13
#define W25Q16_ID 	0XEF14
#define W25Q32_ID 	0XEF15
#define W25Q64_ID 	0XEF16
#define W25Q128_ID	0XEF17
#define W25Q256_ID 0XEF18
#define W25Q512_ID 0XEF19
#define W25Q1024_ID 0XEF20extern uint16_t W25QXX_TYPE; //To indicate W25QXX type used in this procedure//command table for W25QXX access
#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#define W25X_QUAD_QuadInputPageProgram 0x32
#define W25X_QUAD_FastReadQuadOutput 0x6B
#define W25X_QUAD_ManufactDeviceID 0x94
#define W25X_QUAD_FastRead 0xEB
#define W25X_QUAD_SetBurstwithWrap 0x77uint8_t W25QXX_Init(void);
uint16_t  W25QXX_ReadID(void);  	    		  //Read W25QXX ID
uint8_t W25QXX_ReadSR(uint8_t reg_num);           //Read from status register
void W25QXX_4ByteAddr_Enable(void);               //Enable 4-byte address mode
void W25QXX_Write_SR(uint8_t reg_num,uint8_t d);  //Write to status register
void W25QXX_Write_Enable(void);  		          //Write enable
void W25QXX_Write_Disable(void);		          //Write disable
void W25QXX_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite); //Write operation w/o check
void W25QXX_Read(uint8_t* pBuffer,uint32_t ReadAddr,uint16_t NumByteToRead);            //Read operation
void W25QXX_Write(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);         //Write operation
void W25QXX_Erase_Chip(void);    	  	                                                //Erase whole chip
void W25QXX_Erase_Sector(uint32_t Sector_Num);	                                        //Erase sector in specific sector number
void W25QXX_Wait_Busy(void);           	       //Wait idle status before next operation
void W25QXX_PowerDown(void);        	       //Enter power-down mode
void W25QXX_WAKEUP(void);				       //Wake-up#endif /* INC_W25QXX_H_ */

建立W25Q访问的库源文件W25QXX_QSPI.c:

#include "W25QXX_QSPI.h"extern QSPI_HandleTypeDef hqspi;
extern void PY_Delay_us_t(uint32_t Delay);
extern HAL_StatusTypeDef QSPI_Send_CMD(uint8_t cmd,uint32_t addr,uint8_t mode,uint8_t dmcycle);
extern HAL_StatusTypeDef QSPI_Receive(uint8_t* buf,uint32_t datalen);
extern HAL_StatusTypeDef QSPI_Transmit(uint8_t* buf,uint32_t datalen);#define Use_Quad_Line 1uint16_t W25QXX_TYPE=W25Q64_ID;//W25QXX initialization
uint8_t W25QXX_Init(void)
{uint8_t temp;W25QXX_TYPE=W25QXX_ReadID();if((W25QXX_TYPE==W25Q256_ID)||(W25QXX_TYPE==W25Q512_ID)||(W25QXX_TYPE==W25Q1024_ID)){temp=W25QXX_ReadSR(3);              //read status register 3if((temp&0X01)==0)			        //judge address mode and configure to 4-byte address mode{QSPI_Send_CMD(W25X_Enable4ByteAddr, 0x00, (0<<6)|(0<<4)|(0<<2)|(1<<0), 0);}}if((W25QXX_TYPE==0x0000)||(W25QXX_TYPE==0xFFFF)) return 0;else return 1;
}//Read status registers of W25QXX
//reg_num: register number from 1 to 3
//return: value of selected register//SR1 (default 0x00):
//BIT7  6   5   4   3   2   1   0
//SPR   RV  TB BP2 BP1 BP0 WEL BUSY
//SPR: default 0, status register protection bit used with WP
//TB,BP2,BP1,BP0: FLASH region write protection configuration
//WEL: write enable lock
//BUSY: busy flag (1: busy; 0: idle)//SR2:
//BIT7  6   5   4   3   2   1   0
//SUS   CMP LB3 LB2 LB1 (R) QE  SRP1//SR3:
//BIT7      6    5    4   3   2   1   0
//HOLD/RST  DRV1 DRV0 (R) (R) WPS ADP ADS
uint8_t W25QXX_ReadSR(uint8_t reg_num)
{uint8_t byte=0,command=0;switch(reg_num){case 1:command=W25X_ReadStatusReg1;    //To read status register 1break;case 2:command=W25X_ReadStatusReg2;    //To read status register 2break;case 3:command=W25X_ReadStatusReg3;    //To read status register 3break;default:command=W25X_ReadStatusReg1;break;}QSPI_Send_CMD(command, 0x00, (1<<6)|(0<<4)|(0<<2)|(1<<0), 0);  //send commandQSPI_Receive(&byte,1);                                         //read datareturn byte;
}//Write status registers of W25QXX
//reg_num: register number from 1 to 3
//d: data for updating status register
void W25QXX_Write_SR(uint8_t reg_num,uint8_t d)
{uint8_t command=0;switch(reg_num){case 1:command=W25X_WriteStatusReg1;    //To write status register 1break;case 2:command=W25X_WriteStatusReg2;    //To write status register 2break;case 3:command=W25X_WriteStatusReg3;    //To write status register 3break;default:command=W25X_WriteStatusReg1;break;}QSPI_Send_CMD(command, 0x00, (1<<6)|(0<<4)|(0<<2)|(1<<0), 0); //send commandQSPI_Transmit(&d, 1);                                         //write data
}
//W25QXX write enable
void W25QXX_Write_Enable(void)
{QSPI_Send_CMD(W25X_WriteEnable, 0x00, (0<<6)|(0<<4)|(0<<2)|(1<<0), 0); //send command
}
//W25QXX write disable
void W25QXX_Write_Disable(void)
{QSPI_Send_CMD(W25X_WriteDisable, 0x00, (0<<6)|(0<<4)|(0<<2)|(1<<0), 0); //send command
}//Read chip ID
//return:
//0XEF13 for W25Q80
//0XEF14 for W25Q16
//0XEF15 for W25Q32
//0XEF16 for W25Q64
//0XEF17 for W25Q128
//0XEF18 for W25Q256
uint16_t W25QXX_ReadID(void)
{uint16_t Temp = 0;uint8_t st;uint8_t TD[8];if(Use_Quad_Line){uint8_t extra_dummy = 4; //Adjust dummy here for I/O direction adjustment delayQSPI_Send_CMD(W25X_QUAD_ManufactDeviceID, 0x000000f0, (3<<6)|(3<<4)|(3<<2)|(1<<0), extra_dummy);   ///To read Manufacturer/Device ID in Quad modest = QSPI_Receive(TD, 2);if(st==0){Temp = (TD[0]<<8)|TD[1];}else{Temp = 0;}return Temp;}else{QSPI_Send_CMD(W25X_ManufactDeviceID, 0x00, (1<<6)|(0<<4)|(0<<2)|(1<<0), 0);  //To read Manufacturer/Device ID in single line modest = QSPI_Receive(TD, 5);if(st==0){Temp = (TD[3]<<8)|TD[4];}else{Temp = 0;}return Temp;}
}
//Read W25QXX from specific address for specific byte length
//pBuffer: data buffer
//ReadAddr: specific address
//NumByteToRead: specific byte length (max 65535)
void W25QXX_Read(uint8_t* pBuffer,uint32_t ReadAddr,uint16_t NumByteToRead)
{if(Use_Quad_Line){uint8_t extra_dummy = 8;    //Adjust dummy here for I/O direction adjustment delayif((W25QXX_TYPE==W25Q256_ID)||(W25QXX_TYPE==W25Q512_ID)||(W25QXX_TYPE==W25Q1024_ID)){QSPI_Send_CMD(W25X_QUAD_FastReadQuadOutput, ReadAddr, (3<<6)|(3<<4)|(1<<2)|(1<<0), extra_dummy);}else{QSPI_Send_CMD(W25X_QUAD_FastReadQuadOutput, ReadAddr, (3<<6)|(2<<4)|(1<<2)|(1<<0), extra_dummy);}QSPI_Receive(pBuffer, NumByteToRead); //read data}else{uint8_t extra_dummy = 8;    //Adjust dummy here for I/O direction adjustment delayif((W25QXX_TYPE==W25Q256_ID)||(W25QXX_TYPE==W25Q512_ID)||(W25QXX_TYPE==W25Q1024_ID)){QSPI_Send_CMD(W25X_ReadData, ReadAddr, (1<<6)|(3<<4)|(1<<2)|(1<<0), extra_dummy);}else{QSPI_Send_CMD(W25X_ReadData, ReadAddr, (1<<6)|(2<<4)|(1<<2)|(1<<0), extra_dummy);}QSPI_Receive(pBuffer, NumByteToRead); //read data}}//Write W25QXX not more than 1 page (256 bytes)
//pBuffer: data buffer
//WriteAddr: specific address
//NumByteToWrite: specific byte length (max 256)
void W25QXX_Write_Page(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
{W25QXX_Write_Enable();                                        //write enable//send write commandif(Use_Quad_Line){if((W25QXX_TYPE==W25Q256_ID)||(W25QXX_TYPE==W25Q512_ID)||(W25QXX_TYPE==W25Q1024_ID)){QSPI_Send_CMD(W25X_QUAD_QuadInputPageProgram, WriteAddr, (3<<6)|(3<<4)|(1<<2)|(1<<0), 0);}else{QSPI_Send_CMD(W25X_QUAD_QuadInputPageProgram, WriteAddr, (3<<6)|(2<<4)|(1<<2)|(1<<0), 0);}}else{if((W25QXX_TYPE==W25Q256_ID)||(W25QXX_TYPE==W25Q512_ID)||(W25QXX_TYPE==W25Q1024_ID)){QSPI_Send_CMD(W25X_PageProgram, WriteAddr, (1<<6)|(3<<4)|(1<<2)|(1<<0), 0);}else{QSPI_Send_CMD(W25X_PageProgram, WriteAddr, (1<<6)|(2<<4)|(1<<2)|(1<<0), 0);}}QSPI_Transmit(pBuffer, NumByteToWrite); //write dataW25QXX_Wait_Busy();
}//Write W25QXX w/o erase check and w/o byte number restriction
//pBuffer: data buffer
//WriteAddr: specific address
//NumByteToWrite: specific byte length (max 65535)
void W25QXX_Write_NoCheck(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
{uint16_t remained_byte_num_in_page;remained_byte_num_in_page=256-WriteAddr%256;                                                       //remained byte number in pageif( NumByteToWrite <= remained_byte_num_in_page ) remained_byte_num_in_page = NumByteToWrite;      //data can be written in single pagewhile(1){W25QXX_Write_Page(pBuffer,WriteAddr,remained_byte_num_in_page);if(NumByteToWrite==remained_byte_num_in_page)break;                                            //end write operationelse                                                                                           //NumByteToWrite>remained_byte_num_in_page{pBuffer+=remained_byte_num_in_page;WriteAddr+=remained_byte_num_in_page;NumByteToWrite-=remained_byte_num_in_page;if(NumByteToWrite>256) remained_byte_num_in_page=256;                                       //for whole page writeelse remained_byte_num_in_page=NumByteToWrite; 	                                           //for non-whole page write}};
}//Write W25QXX w/ erase after check and w/o byte number restriction
//pBuffer: data buffer
//WriteAddr: specific address
//NumByteToWrite: specific byte length (max 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;                                        //sector number (16 pages for 1 sector) for destination addresssecoff=WriteAddr%4096;                                        //offset address in sector for destination addresssecremain=4096-secoff;                                        //remained space for sectorif(NumByteToWrite<=secremain)secremain=NumByteToWrite;        //data can be written in single sectorwhile(1){W25QXX_Read(W25QXX_BUF,secpos*4096,4096);                 //read sector data for ease necessity judgmentfor(i=0;i<secremain;i++)                                  //check sector data status{if(W25QXX_BUF[secoff+i]!=0XFF) break;                 //ease necessary}if(i<secremain)                                           //for ease{W25QXX_Erase_Sector(secpos);                          //ease sectorfor(i=0;i<secremain;i++)	                          //data copy{W25QXX_BUF[i+secoff]=pBuffer[i];}W25QXX_Write_NoCheck(W25QXX_BUF,secpos*4096,4096);     //write sector}else W25QXX_Write_NoCheck(pBuffer,WriteAddr,secremain);   //write data for sector unnecessary to eraseif(NumByteToWrite==secremain)break;                        //for operation endelse                                                       //for operation continuing{secpos++;                                              //sector number + 1secoff=0;                                              //offset address from 0pBuffer+=secremain;                                    //pointer adjustmentWriteAddr+=secremain;                                  //write address adjustmentNumByteToWrite-=secremain;				               //write number adjustmentif(NumByteToWrite>4096) secremain=4096;	               //not last sectorelse secremain=NumByteToWrite;			               //last sector}};
}//Erase whole chip, long waiting...
void W25QXX_Erase_Chip(void)
{W25QXX_Write_Enable();                  //write enableW25QXX_Wait_Busy();QSPI_Send_CMD(W25X_ChipErase, 0x00, (0<<6)|(0<<4)|(0<<2)|(1<<0), 0); //send erase commandW25QXX_Wait_Busy();   				    //wait for erase complete
}//Erase one sector
//Sector_Num: sector number
void W25QXX_Erase_Sector(uint32_t Sector_Num)
{Sector_Num *= 4096;W25QXX_Write_Enable();                                     //write enableW25QXX_Wait_Busy();if((W25QXX_TYPE==W25Q256_ID)||(W25QXX_TYPE==W25Q512_ID)||(W25QXX_TYPE==W25Q1024_ID)) //send highest 8-bit address{QSPI_Send_CMD(W25X_SectorErase, Sector_Num, (0<<6)|(3<<4)|(1<<2)|(1<<0), 0); //send erase command}else{QSPI_Send_CMD(W25X_SectorErase, Sector_Num, (0<<6)|(2<<4)|(1<<2)|(1<<0), 0); //send erase command}W25QXX_Wait_Busy();   				                       //wait for erase complete
}//Wait idle status before next operation
void W25QXX_Wait_Busy(void)
{while((W25QXX_ReadSR(1)&0x01)==0x01);    //wait for busy flag cleared
}//Enter power-down mode
#define tDP_us 3
void W25QXX_PowerDown(void)
{QSPI_Send_CMD(W25X_PowerDown, 0, (0<<6)|(0<<4)|(0<<2)|(1<<0), 0);        //send power-down commandPY_Delay_us_t(tDP_us);                   //tDP
}
//Wake-up
#define tRES1_us 3
void W25QXX_WAKEUP(void)
{QSPI_Send_CMD(W25X_ReleasePowerDown, 0, (0<<6)|(0<<4)|(0<<2)|(1<<0), 0); //send release power-down commandPY_Delay_us_t(tRES1_us);                 //tRES1
}

main.c文件操作代码里包含几个QSPI操作函数定义如下:

#define page_byte_size 256
uint8_t sdbuffer[page_byte_size];/*
QSPI TX in block mode for command byte
cmd: command to be sent to device
addr: address to be sent to device
mode: operation mode set asmode[1:0] for command transmission mode ( 00: no command; 01: single-line transmission; 10: dual-line transmission; 11: four-line transmission )mode[3:2] for address transmission mode ( 00: no address; 01: single-line transmission; 10: dual-line transmission; 11: four-line transmission )mode[5:4] for address length ( 00:8-bit address; 01: 16-bit address; 10: 24-bit address; 11: 32-bit address )mode[7:6] for data transmission mode ( 00: no command; 01: single-line transmission; 10: dual-line transmission; 11: four-line transmission )
dmcycle: dummy clock cycle
*/
HAL_StatusTypeDef QSPI_Send_CMD(uint8_t cmd,uint32_t addr,uint8_t mode,uint8_t dmcycle)
{QSPI_CommandTypeDef Cmdhandler;Cmdhandler.Instruction=cmd;		//set cmdCmdhandler.Address=addr;		//set addressCmdhandler.DummyCycles=dmcycle; //set dummy circle number/*set cmd transmission mode*/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;/*set address transmission mode*/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;/*set address length*/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;/*set data transmission mode*/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;				/*Send instruction on every transaction*/Cmdhandler.AlternateByteMode=QSPI_ALTERNATE_BYTES_NONE;		/*No alternate bytes*/Cmdhandler.DdrMode=QSPI_DDR_MODE_DISABLE;					/*Double data rate mode disabled*/Cmdhandler.DdrHoldHalfCycle=QSPI_DDR_HHC_ANALOG_DELAY;      /*Delay the data output using analog delay in DDR mode*/return HAL_QSPI_Command(&hqspi,&Cmdhandler,5000);
}//QSPI RX in block mode for data
//buf : buffer address for RX data
//datalen : RX data length
//return : 0, OK
//         others, error code
HAL_StatusTypeDef QSPI_Receive(uint8_t* buf,uint32_t datalen)
{hqspi.Instance->DLR=datalen-1;return HAL_QSPI_Receive(&hqspi,buf,5000);
}//QSPI TX in block mode for data
//buf : buffer address for TX data
//datalen : TX data length
//return : 0, OK
//         others, error code
HAL_StatusTypeDef QSPI_Transmit(uint8_t* buf,uint32_t datalen)
{hqspi.Instance->DLR=datalen-1;return HAL_QSPI_Transmit(&hqspi,buf,5000);
}

main.c文件操作代码里实现串口接收1个字节的指令,实现FLASH的ID读取,一页的写入,一页的读出三个功能。其它功能可以根据需要自行增加。完整的main.c文件如下

/* USER CODE BEGIN Header */
/********************************************************************************* @file           : main.c* @brief          : Main program body******************************************************************************* @attention** Copyright (c) 2023 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
//Written by Pegasus Yu in 2023
//NOte: to access W25Qxx, send single line command at first to trigger consequent operation mode for address and data which could be 1-line or 4-line
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <string.h>
#include "usart.h"
#include "W25QXX_QSPI.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
__IO float usDelayBase;
void PY_usDelayTest(void)
{__IO uint32_t firstms, secondms;__IO uint32_t counter = 0;firstms = HAL_GetTick()+1;secondms = firstms+1;while(uwTick!=firstms) ;while(uwTick!=secondms) counter++;usDelayBase = ((float)counter)/1000;
}void PY_Delay_us_t(uint32_t Delay)
{__IO uint32_t delayReg;__IO uint32_t usNum = (uint32_t)(Delay*usDelayBase);delayReg = 0;while(delayReg!=usNum) delayReg++;
}void PY_usDelayOptimize(void)
{__IO uint32_t firstms, secondms;__IO float coe = 1.0;firstms = HAL_GetTick();PY_Delay_us_t(1000000) ;secondms = HAL_GetTick();coe = ((float)1000)/(secondms-firstms);usDelayBase = coe*usDelayBase;
}void PY_Delay_us(uint32_t Delay)
{__IO uint32_t delayReg;__IO uint32_t msNum = Delay/1000;__IO uint32_t usNum = (uint32_t)((Delay%1000)*usDelayBase);if(msNum>0) HAL_Delay(msNum);delayReg = 0;while(delayReg!=usNum) delayReg++;
}
/* 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;UART_HandleTypeDef huart1;/* USER CODE BEGIN PV */
uint8_t uart1_rx[16];
uint8_t cmd;
uint32_t Flash_Access_Addr = 0;uint8_t st;
uint16_t FID;
uint8_t TD[256];
/* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_QUADSPI_Init(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#define page_byte_size 256
uint8_t sdbuffer[page_byte_size];/*
QSPI TX in block mode for command byte
cmd: command to be sent to device
addr: address to be sent to device
mode: operation mode set asmode[1:0] for command transmission mode ( 00: no command; 01: single-line transmission; 10: dual-line transmission; 11: four-line transmission )mode[3:2] for address transmission mode ( 00: no address; 01: single-line transmission; 10: dual-line transmission; 11: four-line transmission )mode[5:4] for address length ( 00:8-bit address; 01: 16-bit address; 10: 24-bit address; 11: 32-bit address )mode[7:6] for data transmission mode ( 00: no command; 01: single-line transmission; 10: dual-line transmission; 11: four-line transmission )
dmcycle: dummy clock cycle
*/
HAL_StatusTypeDef QSPI_Send_CMD(uint8_t cmd,uint32_t addr,uint8_t mode,uint8_t dmcycle)
{QSPI_CommandTypeDef Cmdhandler;Cmdhandler.Instruction=cmd;		//set cmdCmdhandler.Address=addr;		//set addressCmdhandler.DummyCycles=dmcycle; //set dummy circle number/*set cmd transmission mode*/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;/*set address transmission mode*/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;/*set address length*/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;/*set data transmission mode*/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;				/*Send instruction on every transaction*/Cmdhandler.AlternateByteMode=QSPI_ALTERNATE_BYTES_NONE;		/*No alternate bytes*/Cmdhandler.DdrMode=QSPI_DDR_MODE_DISABLE;					/*Double data rate mode disabled*/Cmdhandler.DdrHoldHalfCycle=QSPI_DDR_HHC_ANALOG_DELAY;      /*Delay the data output using analog delay in DDR mode*/return HAL_QSPI_Command(&hqspi,&Cmdhandler,5000);
}//QSPI RX in block mode for data
//buf : buffer address for RX data
//datalen : RX data length
//return : 0, OK
//         others, error code
HAL_StatusTypeDef QSPI_Receive(uint8_t* buf,uint32_t datalen)
{hqspi.Instance->DLR=datalen-1;return HAL_QSPI_Receive(&hqspi,buf,5000);
}//QSPI TX in block mode for data
//buf : buffer address for TX data
//datalen : TX data length
//return : 0, OK
//         others, error code
HAL_StatusTypeDef QSPI_Transmit(uint8_t* buf,uint32_t datalen)
{hqspi.Instance->DLR=datalen-1;return HAL_QSPI_Transmit(&hqspi,buf,5000);
}
/* 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_USART1_UART_Init();MX_QUADSPI_Init();/* USER CODE BEGIN 2 */PY_usDelayTest();PY_usDelayOptimize();HAL_UART_Receive_IT(&huart1, uart1_rx, 1);W25QXX_Init();/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){if(cmd==0x01) //Read ID{cmd = 0;FID = W25QXX_ReadID();printf("Flash ID = 0x%x\r\n\r\n", FID);printf("W25Q80_ID: 0XEF13\r\n");printf("W25Q16_ID: 0XEF14\r\n");printf("W25Q32_ID: 0XEF15\r\n");printf("W25Q64_ID: 0XEF16\r\n");printf("W25Q128_ID: 0XEF17\r\n");printf("W25Q256_ID: 0XEF18\r\n");printf("W25Q512_ID: 0XEF18\r\n");printf("W25Q1024_ID: 0XEF20\r\n");}if(cmd==2) //Write one page{cmd = 0;for(uint32_t i=0;i<page_byte_size;i++){sdbuffer[i]=i;}Flash_Access_Addr = 0;W25QXX_Write(sdbuffer, Flash_Access_Addr, page_byte_size);printf("Write to W25QXX done!\r\n");}if(cmd==3)//Read one page{cmd = 0;memset(sdbuffer, 0, page_byte_size);printf("Start to read W25QXX......\r\n");Flash_Access_Addr = 0;W25QXX_Read(sdbuffer, Flash_Access_Addr, page_byte_size);for(uint32_t i=0; i<page_byte_size; i++){printf("%d ", sdbuffer[i]);}printf("\r\n");}/* 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)) {}__HAL_RCC_SYSCFG_CLK_ENABLE();__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);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_HSI;RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;RCC_OscInitStruct.PLL.PLLM = 4;RCC_OscInitStruct.PLL.PLLN = 60;RCC_OscInitStruct.PLL.PLLP = 2;RCC_OscInitStruct.PLL.PLLQ = 2;RCC_OscInitStruct.PLL.PLLR = 2;RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;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_DIV2;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != 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 = 9;hqspi.Init.FifoThreshold = 4;hqspi.Init.SampleShifting = QSPI_SAMPLE_SHIFTING_HALFCYCLE;hqspi.Init.FlashSize = 25;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 USART1 Initialization Function* @param None* @retval None*/
static void MX_USART1_UART_Init(void)
{/* USER CODE BEGIN USART1_Init 0 *//* USER CODE END USART1_Init 0 *//* USER CODE BEGIN USART1_Init 1 *//* USER CODE END USART1_Init 1 */huart1.Instance = USART1;huart1.Init.BaudRate = 115200;huart1.Init.WordLength = UART_WORDLENGTH_8B;huart1.Init.StopBits = UART_STOPBITS_1;huart1.Init.Parity = UART_PARITY_NONE;huart1.Init.Mode = UART_MODE_TX_RX;huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;huart1.Init.OverSampling = UART_OVERSAMPLING_16;huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;if (HAL_UART_Init(&huart1) != HAL_OK){Error_Handler();}if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK){Error_Handler();}if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK){Error_Handler();}if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK){Error_Handler();}/* USER CODE BEGIN USART1_Init 2 *//* USER CODE END USART1_Init 2 */}/*** @brief GPIO Initialization Function* @param None* @retval None*/
static void MX_GPIO_Init(void)
{/* GPIO Ports Clock Enable */__HAL_RCC_GPIOE_CLK_ENABLE();__HAL_RCC_GPIOB_CLK_ENABLE();__HAL_RCC_GPIOD_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();}/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{if(huart==&huart1){cmd = uart1_rx[0];HAL_UART_Receive_IT(&huart1, uart1_rx, 1);}}
/* 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 */

STM32例程测试

串口指令0x01测试效果如下:
在这里插入图片描述
串口指令0x02测试效果如下:
在这里插入图片描述

串口指令0x03测试效果如下:
在这里插入图片描述

STM32例程下载

STM32H750VBT6 QSPI总线读写FLASH W25QXX HAL库例程下载

–End–

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/249769.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python给word插入脚注

1.需求 最近因为工作需要&#xff0c;需要给大量文本的脚注插入内容&#xff0c;我就写了个小程序。 2.实现 下面程序是我已经给所有脚注插入了两次文本“幸福”&#xff0c;给脚注2到4再插入文本“幸福” from win32com import clientdef add_text_to_specific_footnotes(…

外包干了8个月,技术退步明显...

先说一下自己的情况&#xff0c;大专生&#xff0c;18年通过校招进入武汉某软件公司&#xff0c;干了接近4年的功能测试&#xff0c;今年年初&#xff0c;感觉自己不能够在这样下去了&#xff0c;长时间呆在一个舒适的环境会让一个人堕落! 而我已经在一个企业干了四年的功能测…

前端小案例——导航回顶部(HTML+CSS+JS, 附源码)

一、前言 实现功能&#xff1a; 这个案例实现了页面滚动到一定位置时显示"回到顶部"按钮&#xff0c;并且点击按钮能够平滑滚动回页面顶部的功能。 实现逻辑&#xff1a; 页面结构&#xff1a;通过HTML标签定义了页面的基本结构。页面主要由多个div.content组成&am…

【居然比GPT还好用】KnowLM:知识图谱 + 大模型,实现更有效的信息抽取和知识管理

KnowLM 知识图谱 大模型&#xff1a;实现信息抽取 KnowLM 原理KnowLM 部署KnowLM 应用1. 命名实体识别&#xff08;NER&#xff09;2. 关系抽取&#xff08;RE&#xff09;3. 事件抽取&#xff08;EE&#xff09; KnowLM 原理 代码&#xff1a;https://github.com/zjunlp/Kno…

如何使用内网穿透工具在公网实现实时监测DashDot服务器仪表盘

文章目录 1. 本地环境检查1.1 安装docker1.2 下载Dashdot镜像 2. 部署DashDot应用3. 本地访问DashDot服务4. 安装cpolar内网穿透5. 固定DashDot公网地址 本篇文章我们将使用Docker在本地部署DashDot服务器仪表盘&#xff0c;并且结合cpolar内网穿透工具可以实现公网实时监测服务…

2024年美国大学生数学建模F题思路分析 - 减少非法野生动物贸易

# 1 赛题 问题F&#xff1a;减少非法野生动物贸易 非法的野生动物贸易会对我们的环境产生负面影响&#xff0c;并威胁到全球的生物多样性。据估计&#xff0c;它每年涉及高达265亿美元&#xff0c;被认为是全球第四大非法交易。[1]你将开发一个由数据驱动的5年项目&#xff0c…

CCSIP中国网络安全行业全景册(第六版)发布 飞驰云联入选7大领域

2024年1月24日&#xff0c; FreeBuf咨询正式发布《CCSIP 2023中国网络安全行业全景册&#xff08;第六版&#xff09;》。Ftrans飞驰云联的产品凭借优秀的市场表现&#xff0c;强势入选网络隔离/网闸、工业网络隔离系统/网闸、数据安全管控&#xff08;平台型&#xff09;、数据…

[Tcpdump] 网络抓包工具使用教程

往期回顾 海思 tcpdump 移植开发详解海思 tcpdump 移植开发详解 前言 上一节&#xff0c;我们已经讲解了在海思平台如何基于静态库生成 tcpdump 工具&#xff0c;本节将作为上一节的拓展内容。 一、tcpdump 简介 「 tcpdump 」是一款强大的网络抓包工具&#xff0c;它基于…

deque容器的相关概念及常用接口

deque的基本概念 作用&#xff1a;作为双端数组&#xff0c;可以很方便的对头尾进行插入和删除操作 注意&#xff1a;适用deque时需包含头文件deque deque与vector的区别 1、vector对数组头部的插入和删除操作效率低&#xff0c;时间复杂度高。数据量越大&#xff0c;效率越…

Python学习03 -- 函数相关内容

1.def --- 这个是定义函数的关键字 \n --- 这个在print()函数中是换行符号 1.注意是x, 加个空格之后再y 1.形式参数数量是不受限制的&#xff08;参数间用&#xff0c;隔开&#xff09;&#xff0c;传实参给形参的时候要一一对应 返回值 --- 函数返还的结果捏 1.写None的时…

2023年09月CCF-GESP编程能力等级认证Python编程五级真题解析

Python等级认证GESP(1~6级)全部真题・点这里 一、单选题(共15题,共30分) 第1题 近年来,线上授课变得普遍,很多有助于改善教学效果的设备也逐渐流行,其中包括比较常用的手写板,那么它属于哪类设备?( ) A:输入 B:输出 C:控制 D:记录 答案:A 第2题 以下关于…

学习Android的第一天

目录 什么是 Android&#xff1f; Android 官网 Android 应用程序 Android 开发环境搭建 Android 平台架构 Android 应用程序组件 附件组件 Android 第一个程序 HelloWorld 什么是 Android&#xff1f; Android&#xff08;发音为[ˈnˌdrɔɪd]&#xff0c;非官方中文…

【selenium方式】获取微博指定用户指定日期内所有帖子详细数据

这篇文章主要放源代码&#xff0c;思路不会介绍特别清楚&#xff0c;详细思路可以看评论区的b站讲解视频。 1.场景需求 获取微博肖战超话内容部分用户的帖子数据&#xff0c;日期范围限定在近2个月&#xff0c;要求获得帖子的发布时间、帖子文本内容、转发数据、评论数据和点…

【Go 快速入门】包及依赖管理 | Go 第三方包发布 | 接口 | 反射

文章目录 包和依赖管理依赖管理go modgo get go.mod 文件go.sum 文件Go Modules 发布包 接口空接口接口值类型断言 反射reflect.TypeOfreflect.ValueOf结构体反射 项目代码地址&#xff1a;04-PackageInterfaceReflection 包和依赖管理 Go 使用包来支持代码模块化和代码复用&…

市场复盘总结 20240201

仅用于记录当天的市场情况&#xff0c;用于统计交易策略的适用情况&#xff0c;以便程序回测 短线核心&#xff1a;不参与任何级别的调整&#xff0c;采用龙空龙模式 一支股票 10%的时候可以操作&#xff0c; 90%的时间适合空仓等待 昨日主题投资 连板进级率 6/27 22.2% 二进…

AI 原生时代的云计算

本文整理自2023年 12 月 20 日举办的「2023 百度云智大会智算大会」主论坛&#xff0c;百度副总裁谢广军的主题演讲《AI 原生时代的云计算》。 &#xff08;视频回放链接&#xff1a;https://cloud.baidu.com/summit/aicomputing_2023/index.html&#xff09; 大模型的到来&…

一些大语言模型(LLM)相关的开源项目

一些大语言模型&#xff08;LLM&#xff09;相关的开源项目 更多文章访问: https://www.cyisme.top 因为站内限制问题&#xff0c;有些图片无法显示&#xff0c;导致阅读体验较差&#xff0c;可以访问原文&#xff1a;《一些大语言模型&#xff08;LLM&#xff09;相关的开源项…

揭秘远程控制APP的便捷之美!

在这个科技日新月异的时代&#xff0c;我们的生活被各种手机软件所包围。几乎每个人都有一个甚至多个手机&#xff0c;你是否也有遇到过需要远程操作自己某一台手机的场景呢&#xff1f;今天&#xff0c;我要向大家推荐一款神奇的手机远程操作神器&#xff0c;让你可以随时随地…

(2)(2.11) RFD900

文章目录 前言 1 概述 2 主要功能 3 状态LED指示灯 4 接口 5 使用Mission Planner进行配置 6 支持不同国家/地区 7 讨论论坛 前言 RFD900 无线电调制解调器是一款高功率 900Mhz ISM 波段无线电调制解调器&#xff0c;设计用于远距离串行通信。据报道&#xff0c;其通信…

数字孪生 三维建模方式以及细节步骤流程

对于数字孪生这个概念&#xff0c;三维建模不同行业认知都不尽相同。有的行业认为数字孪生重点在于建模&#xff0c;有的行业认为在于物联感知&#xff0c;还有部分认为是虚拟仿真。今天重点从建模角度和大家谈谈数字孪生技术常用的三维建模方式以及精细度分级。 数字孪生平台…