经过几天的研究终于完成了用Quad SPI模式对W25Q64的读写。我用的W25Q64JVSSIQ,后缀Q表示QE位已经置1.附上代码供大家参考指正,小的刚接触编程不久,肯定有很多疏漏之处,欢迎大家批评指正。void QSPI_Init(void)
{
spi_parameter_struct spi_init_struct;
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_SPI0);
gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
gpio_bit_set(GPIOA,GPIO_PIN_4);
/* SPI0 parameter config */
spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
spi_init_struct.device_mode = SPI_MASTER;
spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT;
spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE;
spi_init_struct.nss = SPI_NSS_SOFT;
spi_init_struct.prescale = SPI_PSC_2;
spi_init_struct.endian = SPI_ENDIAN_MSB;
spi_init(SPI0, &spi_init_struct);
spi_quad_enable(SPI0);
spi_quad_io23_output_enable(SPI0);
spi_enable(SPI0);
}
//W25QXX写使能
void W25QXX_Write_Enable(void)
{
gpio_bit_reset(GPIOA,GPIO_PIN_4);
QSPI_Send_CMD(W25X_WriteEnable); //写使能指令
gpio_bit_set(GPIOA,GPIO_PIN_4);
}
//W25Qxx读型号ID
uint16_t W25QXX_ReadID(void)
{
unsigned char temp[2];
uint32_t deviceid;
gpio_bit_reset(GPIOA,GPIO_PIN_4);
QSPI_Send_CMD(W25X_ManufactDeviceID);
for(deviceid=6;deviceid>0;deviceid--)
QSPI_Send_DAT(0);
QSPI_Receive(temp,2);
deviceid=(temp[0]<<8)+temp[1];
gpio_bit_set(GPIOA,GPIO_PIN_4);
return deviceid;
}
//读取SPI FLASH
//在指定地址开始读取指定长度的数据
//pBuffer:数据存储区
//ReadAddr:开始读取的地址(最大32bit)
//NumByteToRead:要读取的字节数(最大65535)
void W25QXX_Read(unsigned char* pBuffer,uint32_t ReadAddr,uint16_t NumByteToRead)
{
gpio_bit_reset(GPIOA,GPIO_PIN_4);
QSPI_Send_CMD(W25X_FastReadQuad);
QSPI_Send_DAT((unsigned char)(ReadAddr>>16));
QSPI_Send_DAT((unsigned char)(ReadAddr>>8));
QSPI_Send_DAT((unsigned char)ReadAddr);
QSPI_Send_DAT(0);
QSPI_Send_DAT(0);
QSPI_Send_DAT(0);
QSPI_Receive(pBuffer,NumByteToRead);
gpio_bit_set(GPIOA,GPIO_PIN_4);
}
//SPI在一页(0~65535)内写入少于256个字节的数据
//在指定地址开始写入最大256字节的数据
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(最大32bit)
//NumByteToWrite:要写入的字节数(最大256),该数不应该超过该页的剩余字节数!!!
void W25QXX_Write_Page(unsigned char* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
{
unsigned char i;
W25QXX_Write_Enable(); //写使能
gpio_bit_reset(GPIOA,GPIO_PIN_4);
QSPI_Send_CMD(W25X_PageProgram); //QPI,页写指令,地址为WriteAddr
for (i=0;i<12;i++)
{
QSPI_Send_DAT((((WriteAddr>>(23-i*2))&0x01)<<4)|((WriteAddr>>(22-i*2))&0x01));
}
QSPI_Transmit(pBuffer,NumByteToWrite);
gpio_bit_set(GPIOA,GPIO_PIN_4);
W25QXX_Wait_Busy(); //等待写入结束
}
//无检验写SPI FLASH
//必须确保所写的地址范围内的数据全部为0XFF,否则在非0XFF处写入的数据将失败!
//具有自动换页功能
//在指定地址开始写入指定长度的数据,但是要确保地址不越界!
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(最大32bit)
//NumByteToWrite:要写入的字节数(最大65535)
//CHECK OK
void W25QXX_Write_NoCheck(unsigned char* 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
{
pBuffer+=pageremain;
WriteAddr+=pageremain;
NumByteToWrite-=pageremain; //减去已经写入了的字节数
if(NumByteToWrite>256)pageremain=256; //一次可以写入256个字节
else pageremain=NumByteToWrite; //不够256个字节了
}
};
}
//写SPI FLASH
//在指定地址开始写入指定长度的数据
//该函数带擦除操作!
//pBuffer:数据存储区
//WriteAddr:开始写入的地址(最大32bit)
//NumByteToWrite:要写入的字节数(最大65535)
unsigned char W25QXX_BUFFER[4096];
void W25QXX_Write(unsigned char* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite)
{
uint32_t secpos;
uint16_t secoff;
uint16_t secremain;
uint16_t i;
secpos=WriteAddr/4096;//扇区地址
secoff=WriteAddr%4096;//在扇区内的偏移
secremain=4096-secoff;//扇区剩余空间大小
if(NumByteToWrite<=secremain)secremain=NumByteToWrite;//不大于4096个字节
while(1)
{
W25QXX_Read(W25QXX_BUFFER,secpos*4096,4096);//读出整个扇区的内容
for(i=0;i<secremain;i++)//校验数据
{
if(W25QXX_BUFFER[secoff+i]!=0XFF)break;//需要擦除
}
if(i<secremain)//需要擦除
{
W25QXX_Erase_Sector(secpos);//擦除这个扇区
for(i=0;i<secremain;i++) //复制
{
W25QXX_BUFFER[i+secoff]=pBuffer[i];
}
W25QXX_Write_NoCheck(W25QXX_BUFFER,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_Wait_Busy();
gpio_bit_reset(GPIOA,GPIO_PIN_4);
QSPI_Send_CMD(W25X_ChipErase);//QPI,写全片擦除指令
gpio_bit_set(GPIOA,GPIO_PIN_4);
W25QXX_Wait_Busy(); //等待芯片擦除结束
}
//擦除一个扇区
//Dst_Addr:扇区地址 根据实际容量设置
//擦除一个扇区的最少时间:150ms
void W25QXX_Erase_Sector(uint32_t Dst_Addr)
{
unsigned char i;
Dst_Addr*=4096;
W25QXX_Write_Enable(); //SET WEL
W25QXX_Wait_Busy();
gpio_bit_reset(GPIOA,GPIO_PIN_4);
QSPI_Send_CMD(W25X_SectorErase);//QPI,写扇区擦除指令
for (i=0;i<12;i++)
{
QSPI_Send_DAT((((Dst_Addr>>(23-i*2))&0x01)<<4)|((Dst_Addr>>(22-i*2))&0x01));
}
gpio_bit_set(GPIOA,GPIO_PIN_4);
W25QXX_Wait_Busy(); //等待擦除完成
}
//等待空闲
void W25QXX_Wait_Busy(void)
{
unsigned char buf[4];
while(1)
{
gpio_bit_reset(GPIOA,GPIO_PIN_4);
QSPI_Send_CMD(W25X_ReadStatusReg1);
QSPI_Receive(buf,4);
gpio_bit_set(GPIOA,GPIO_PIN_4);
if ((buf[3]&0x02)==0)// 等待BUSY位清空
break;
}
}
//发送命令
void QSPI_Send_CMD(uint32_t instruction)
{
while (spi_i2s_flag_get(SPI0,SPI_STAT_TRANS)){}
spi_quad_write_enable(SPI0);
QSPI_Send_DAT((unsigned char)(instruction>>24));
QSPI_Send_DAT((unsigned char)(instruction>>16));
QSPI_Send_DAT((unsigned char)(instruction>>8));
QSPI_Send_DAT((unsigned char)instruction);
}
//发送数据
unsigned char QSPI_Send_DAT(unsigned char data)
{
while (spi_i2s_flag_get(SPI0,SPI_STAT_TBE)==RESET){}
SPI_DATA(SPI0)=(uint32_t)data;
while (spi_i2s_flag_get(SPI0,SPI_STAT_RBNE)==RESET){}
return SPI_DATA(SPI0);
}
//QSPI接收指定长度的数据
//buf:接收数据缓冲区首地址
//datalen:要传输的数据长度
void QSPI_Receive(unsigned char* buf,uint32_t datalen)
{
uint32_t i;
while (spi_i2s_flag_get(SPI0,SPI_STAT_TRANS)){}
spi_quad_read_enable(SPI0);
for(i=0;i<datalen;i++)
{
while (spi_i2s_flag_get(SPI0,SPI_STAT_TBE)==RESET){}
SPI_DATA(SPI0)=0x000000ff;
while (spi_i2s_flag_get(SPI0,SPI_STAT_RBNE)==RESET){}
buf[i]=(unsigned char)SPI_DATA(SPI0);
}
}
//QSPI发送指定长度的数据
//buf:发送数据缓冲区首地址
//datalen:要传输的数据长度
void QSPI_Transmit(unsigned char* buf,uint32_t datalen)
{
uint16_t i;
for(i=0;i<datalen;i++)
{
QSPI_Send_DAT(buf[i]);
}
}
//指令表
#define W25X_WriteEnable 0x110 //0x06 写使能
#define W25X_ReadStatusReg1 0x101 //0x05 读状态寄存器1
#define W25X_PageProgram 0x110010 //0x32 页写
#define W25X_SectorErase 0x100000 //0x20 扇区擦除
#define W25X_ChipErase 0x11000111 //0xc7 整片擦除
#define W25X_ManufactDeviceID 0x10010100 //0x94 读ID
#define W25X_FastReadQuad 0x11101011 //0xEB 读数据
void QSPI_Init(void); //初始化QSPI
void QSPI_Send_CMD(uint32_t instruction); //QSPI发送命令
unsigned char QSPI_Send_DAT(unsigned char data);
void QSPI_Receive(unsigned char* buf,uint32_t datalen); //QSPI接收数据
void QSPI_Transmit(unsigned char* buf,uint32_t datalen); //QSPI发送数据
uint16_t W25QXX_ReadID(void); //读取FLASH ID
void W25QXX_Write_Enable(void); //写使能
void W25QXX_Write_Page(unsigned char* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite); //页编程
void W25QXX_Write_NoCheck(unsigned char* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);//写flash,不校验
void W25QXX_Write(unsigned char* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite);//写入flash
void W25QXX_Read(unsigned char* pBuffer,uint32_t ReadAddr,uint16_t NumByteToRead); //读取flash
void W25QXX_Erase_Chip(void); //整片擦除
void W25QXX_Erase_Sector(uint32_t Dst_Addr); //扇区擦除
void W25QXX_Wait_Busy(void); //等待空闲
|
|