#define THIS_FILE_ID 13 #include "includes.h" //4Kbytes为一个Sector //16个扇区为1个Block //W25X16 //容量为2M字节,共有32个Block,512个Sector //先要调用函数进行sector解保护,或者全局解保护后才能写入. //W25系列上电默认没有软件写保护,故可直接写入 #define sFLASH_CS_PIN GPIO_Pin_4 /* PA.04 */ #define sFLASH_CS_GPIO_PORT GPIOA /* GPIOA */ #define sFLASH_CS_GPIO_CLK RCC_APB2Periph_GPIOA #define sFLASH_SPI SPI1 #define sFLASH_SPI_CLK RCC_APB2Periph_SPI1 #define sFLASH_SPI_SCK_PIN GPIO_Pin_5 /* PA.05 */ #define sFLASH_SPI_SCK_GPIO_PORT GPIOA /* GPIOA */ #define sFLASH_SPI_SCK_GPIO_CLK RCC_APB2Periph_GPIOA #define sFLASH_SPI_MISO_PIN GPIO_Pin_6 /* PA.06 */ #define sFLASH_SPI_MISO_GPIO_PORT GPIOA /* GPIOA */ #define sFLASH_SPI_MISO_GPIO_CLK RCC_APB2Periph_GPIOA #define sFLASH_SPI_MOSI_PIN GPIO_Pin_7 /* PA.07 */ #define sFLASH_SPI_MOSI_GPIO_PORT GPIOA /* GPIOA */ #define sFLASH_SPI_MOSI_GPIO_CLK RCC_APB2Periph_GPIOA #define sFLASH_CS_LOW() GPIO_ResetBits(sFLASH_CS_GPIO_PORT, sFLASH_CS_PIN) #define sFLASH_CS_HIGH() GPIO_SetBits(sFLASH_CS_GPIO_PORT, sFLASH_CS_PIN) //指令表 #define W25Q64_WriteEnable 0x06 #define W25Q64_WriteDisable 0x04 #define W25Q64_ReadStatusReg 0x05 #define W25Q64_WriteStatusReg 0x01 #define W25Q64_ReadData 0x03 #define W25Q64_FastReadData 0x0B #define W25Q64_FastReadDual 0x3B //?? #define W25Q64_PageProgram 0x02 #define W25Q64_SectorErase 0x20 //erase 4k #define W25Q64_BlockErase 0x52 //erase 32k #define W25Q64_BigBlockErase 0xd8 //erase 64k #define W25Q64_ChipErase 0xC7 //0x60 #define W25Q64_PowerDown 0xB9 #define W25Q64_ReleasePowerDown 0xAB #define W25Q64_DeviceID 0x9F //#define W25Q64_UnprotectSector 0x39 //#define W25Q64_ProtectSector 0x36 uint32_t sFlash_ReadID(void); //读取FLASH ID u8 sFlash_ReadSR(void); //读取状态寄存器 void sFlash_Write_SR(u8 sr); //写状态寄存器 void sFlash_Write_Enable(void); //写使能 void sFlash_Write_Disable(void); //写保护 void sFlash_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead); //读取flash void sFlash_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite);//写入flash void sFlash_Erase_Chip(void); //整片擦除 void sFlash_Erase_Sector(u32 Dst_Addr);//扇区擦除 void sFlash_Wait_Busy(void); //等待空闲 void sFlash_PowerDown(void); //进入掉电模式 void sFlash_WAKEUP(void); //唤醒 //void sFlash_Protect_Sector(u32 Dst_Addr); //void sFlash_Unprotect_Sector(u32 Dst_Addr); void sFlash_SetProtectMode(unsigned char mode);//设置保护模式 0--寄存器方式 1--硬件WP操作方式 void sFlash_Global_Protect(void); void sFlash_Global_Unprotect(void); static unsigned char sFlashProtectMode=0; /********************************************************************** 1ms在8000~9000之间 ***********************************************************************/ void DelayMs(unsigned short ms) { unsigned short i; unsigned short t=ms; //IWDG_ReloadCounter(); while(t--){ for(i=0;i<6000;i++); //IWDG_ReloadCounter(); } } /********************************************************************** 1ms在8000~9000之间 fclk=24M 24*1.24=29.75M ***********************************************************************/ void DelayUs(unsigned short us) { unsigned short i; unsigned short t=us; while(t--){ __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop(); __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop(); __nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop();__nop(); } } /*************************************************************** W25Q64_PortInit ****************************************************************/ void W25Q64_PortInit(void) { GPIO_InitTypeDef GPIO_InitStructure; /*!< sFLASH_SPI_CS_GPIO, sFLASH_SPI_MOSI_GPIO, sFLASH_SPI_MISO_GPIO and sFLASH_SPI_SCK_GPIO Periph clock enable */ RCC_APB2PeriphClockCmd(sFLASH_CS_GPIO_CLK | sFLASH_SPI_MOSI_GPIO_CLK | sFLASH_SPI_MISO_GPIO_CLK | sFLASH_SPI_SCK_GPIO_CLK, ENABLE); /*!< sFLASH_SPI Periph clock enable */ RCC_APB2PeriphClockCmd(sFLASH_SPI_CLK, ENABLE); /*!< Configure sFLASH_SPI pins: SCK */ GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_SCK_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(sFLASH_SPI_SCK_GPIO_PORT, &GPIO_InitStructure); /*!< Configure sFLASH_SPI pins: MOSI */ GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_MOSI_PIN; GPIO_Init(sFLASH_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure); /*!< Configure sFLASH_SPI pins: MISO */ GPIO_InitStructure.GPIO_Pin = sFLASH_SPI_MISO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(sFLASH_SPI_MISO_GPIO_PORT, &GPIO_InitStructure); /*!< Configure sFLASH_CS_PIN pin: sFLASH Card CS pin */ GPIO_InitStructure.GPIO_Pin = sFLASH_CS_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(sFLASH_CS_GPIO_PORT, &GPIO_InitStructure); } /*************************************************************** W25Q64_Init ****************************************************************/ int W25Q64_Init(void) { uint32_t sFlashId; SPI_InitTypeDef SPI_InitStructure; //printf("sFlash Init...\r\n"); W25Q64_PortInit(); /*!< Deselect the FLASH: Chip Select high */ sFLASH_CS_HIGH(); /*!< SPI configuration */ 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_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(sFLASH_SPI, &SPI_InitStructure); // /*!< Enable the sFLASH_SPI */ SPI_Cmd(sFLASH_SPI, ENABLE); // DelayUs(1000); // sFlashId=sFlash_ReadID(); //printf("sFlash ID=%04x\r\n",sFlashId); //if(sFlashId==0x00000000 || sFlashId==0xffffffff){ // printf("sFlash Init Fail!\r\n"); // return 0; //} //printf("sFlash Init OK!\r\n"); return 1; } /********************************************************************** * ***********************************************************************/ u8 SPIx_ReadWriteByte(u8 byte) { // < Loop while DR register in not emplty while (SPI_I2S_GetFlagStatus(sFLASH_SPI, SPI_I2S_FLAG_TXE) == RESET); // !< Send byte through the SPI1 peripheral SPI_I2S_SendData(sFLASH_SPI, byte); // !< Wait to receive a byte while (SPI_I2S_GetFlagStatus(sFLASH_SPI, SPI_I2S_FLAG_RXNE) == RESET); // !< Return the byte read from the SPI bus return SPI_I2S_ReceiveData(sFLASH_SPI); } /* //SPIx 读写一个字节 //返回值:读取到的字节 u8 SPIx_ReadWriteByte(u8 TxData) { u8 retry=0; while((SPI1->SR&1<<1)==0)//等待发送区空 { retry++; if(retry>200)return 0; } SPI1->DR=TxData; //发送一个byte retry=0; while((SPI1->SR&1<<0)==0) //等待接收完一个byte { retry++; if(retry>200)return 0; } return SPI1->DR; //返回收到的数据 } */ //读取sFlash的状态寄存器 //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 u8 sFlash_ReadSR(void) { u8 byte=0; sFLASH_CS_LOW(); //使能器件 SPIx_ReadWriteByte(W25Q64_ReadStatusReg); //发送读取状态寄存器命令 byte=SPIx_ReadWriteByte(0Xff); //读取一个字节 sFLASH_CS_HIGH(); //取消片选 return byte; } //写sFlash状态寄存器 //只有SPR,TB,BP2,BP1,BP0(bit 7,5,4,3,2)可以写!!! void sFlash_Write_SR(u8 sr) { sFLASH_CS_LOW(); //使能器件 SPIx_ReadWriteByte(W25Q64_WriteStatusReg); //发送写取状态寄存器命令 SPIx_ReadWriteByte(sr); //写入一个字节 sFLASH_CS_HIGH(); //取消片选 } //sFlash写使能 //将WEL置位 void sFlash_Write_Enable(void) { sFLASH_CS_LOW(); //使能器件 SPIx_ReadWriteByte(W25Q64_WriteEnable); //发送写使能 sFLASH_CS_HIGH(); //取消片选 } //sFlash写禁止 //将WEL清零 void sFlash_Write_Disable(void) { sFLASH_CS_LOW(); //使能器件 SPIx_ReadWriteByte(W25Q64_WriteDisable); //发送写禁止指令 sFLASH_CS_HIGH(); //取消片选 } //读取芯片ID W25X16的ID:0XEF14 uint32_t sFlash_ReadID(void) { uint32_t Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0; sFLASH_CS_LOW(); SPIx_ReadWriteByte(W25Q64_DeviceID);//发送读取ID命令 Temp0=SPIx_ReadWriteByte(0xFF); Temp1=SPIx_ReadWriteByte(0xFF); Temp2=SPIx_ReadWriteByte(0xFF); sFLASH_CS_HIGH(); Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2; return Temp; } //读取SPI FLASH //在指定地址开始读取指定长度的数据 //pBuffer:数据存储区 //ReadAddr:开始读取的地址(24bit) //NumByteToRead:要读取的字节数(最大65535) void sFlash_Read(u8* pBuffer,u32 ReadAddr,u16 NumByteToRead) { u16 i; sFLASH_CS_LOW(); //使能器件 //SPIx_ReadWriteByte(W25Q64_ReadData); //发送读取命令 SPIx_ReadWriteByte(W25Q64_FastReadData);//快速读命令 SPIx_ReadWriteByte((u8)((ReadAddr)>>16)); //发送24bit地址 SPIx_ReadWriteByte((u8)((ReadAddr)>>8)); SPIx_ReadWriteByte((u8)ReadAddr); pBuffer[0]=SPIx_ReadWriteByte(0XFF);//快速读命令丢弃一个字节 for(i=0;i>16)); //发送24bit地址 SPIx_ReadWriteByte((u8)((WriteAddr)>>8)); SPIx_ReadWriteByte((u8)WriteAddr); for(i=0;ipageremain { pBuffer+=pageremain; WriteAddr+=pageremain; NumByteToWrite-=pageremain; //减去已经写入了的字节数 if(NumByteToWrite>256)pageremain=256; //一次可以写入256个字节 else pageremain=NumByteToWrite; //不够256个字节了 } }; } //写SPI FLASH //在指定地址开始写入指定长度的数据 //该函数带擦除操作! //pBuffer:数据存储区 //WriteAddr:开始写入的地址(24bit) //NumByteToWrite:要写入的字节数(最大65535) u8 sFlash_BUF[4096]; void sFlash_Write(u8* pBuffer,u32 WriteAddr,u16 NumByteToWrite) { u32 secpos; u16 secoff; u16 secremain; u16 i; secpos=WriteAddr/4096;//扇区地址 0~511 for w25x16 secoff=WriteAddr%4096;//在扇区内的偏移 secremain=4096-secoff;//扇区剩余空间大小 if(NumByteToWrite<=secremain)secremain=NumByteToWrite;//不大于4096个字节 while(1) { sFlash_Read(sFlash_BUF,secpos*4096,4096);//读出整个扇区的内容 for(i=0;i4096)secremain=4096; //下一个扇区还是写不完 else secremain=NumByteToWrite; //下一个扇区可以写完了 } } } //擦除整个芯片 //整片擦除时间: //W25X16:25s //W25X32:40s //W25X64:40s //等待时间超长... void sFlash_Erase_Chip(void) { sFlash_Write_Enable(); //SET WEL sFlash_Wait_Busy(); sFLASH_CS_LOW(); //使能器件 SPIx_ReadWriteByte(W25Q64_ChipErase); //发送片擦除命令 sFLASH_CS_HIGH(); //取消片选 sFlash_Wait_Busy(); //等待芯片擦除结束 } //void sFlash_EraseChip(void) //{ ///*!< Send write enable instruction */ //sFlash_Write_Enable(); //sFlash_WaitForWriteEnd(); ///*!< Bulk Erase */ ///*!< Select the FLASH: Chip Select low */ //W25X_FLASH_CS_LOW(); ///*!< Send Bulk Erase instruction */ //sFlash_SendByte(W25X_CMD_ChipErase); ///*!< Deselect the FLASH: Chip Select high */ //W25X_FLASH_CS_HIGH(); // ///*!< Wait the end of Flash writing */ //sFlash_WaitForWriteEnd(); //} //擦除一个扇区 //Dst_Addr:扇区地址 0~511 for w25x16 //擦除一个山区的最少时间:150ms void sFlash_Erase_Sector(u32 Dst_Addr) { Dst_Addr*=4096; sFlash_Write_Enable(); //SET WEL sFlash_Wait_Busy(); sFLASH_CS_LOW(); //使能器件 SPIx_ReadWriteByte(W25Q64_SectorErase); //发送扇区擦除指令 SPIx_ReadWriteByte((u8)((Dst_Addr)>>16)); //发送24bit地址 SPIx_ReadWriteByte((u8)((Dst_Addr)>>8)); SPIx_ReadWriteByte((u8)Dst_Addr); sFLASH_CS_HIGH(); //取消片选 sFlash_Wait_Busy(); //等待擦除完成 } //保护全芯片 void sFlash_Global_Protect(void) { //sFlash_Wait_Busy(); sFlash_Write_Enable(); //SET WEL sFlash_Wait_Busy(); sFlash_Write_SR(0x3c); //sFlash_Wait_Busy(); } //解保护全芯片 void sFlash_Global_Unprotect(void) { //sFlash_Wait_Busy(); sFlash_Write_Enable(); //SET WEL sFlash_Wait_Busy(); sFlash_Write_SR(0x80); //sFlash_Wait_Busy(); } //等待空闲 void sFlash_Wait_Busy(void) { while ((sFlash_ReadSR()&0x01)==0x01){ // 等待BUSY位清空 //IWDG_ReloadCounter();//喂狗 } } //进入掉电模式 void sFlash_PowerDown(void) { sFLASH_CS_LOW(); //使能器件 SPIx_ReadWriteByte(W25Q64_PowerDown); //发送掉电命令 sFLASH_CS_HIGH(); //取消片选 DelayUs(3); //等待TPD } //唤醒 void sFlash_WAKEUP(void) { sFLASH_CS_LOW(); //使能器件 SPIx_ReadWriteByte(W25Q64_ReleasePowerDown); // send W25Q64_PowerDown command 0xAB sFLASH_CS_HIGH(); //取消片选 DelayUs(3); //等待TRES1 } /********************************************* * ***********************************************/ void W25Q64Test(void) { uint32_t sFlashId; unsigned char sFlashSR; static int scStep=0; unsigned char t; char buf[200]; int i; switch(scStep){ case 0: sFlashId=sFlash_ReadID(); printf("sFlashID=%04x\r\n",sFlashId); break; case 1: memset(buf,0,sizeof(buf)); strcpy(buf,"333333333444444444455555555555\r\n"); printf("sFlash Write=%s",buf); sFlash_Write((unsigned char *)buf,0,sizeof(buf)); break; case 2: memset(buf,0,sizeof(buf)); sFlash_Read((unsigned char *)buf,0,sizeof(buf)); printf("sFlash Read=%s",buf); printf("sFlash write 8MB...\r\n"); for(i=0;i<200;i++){ buf[i]=i; } for(i=0;i<1000;i++){ sFlash_Write_Page((unsigned char *)buf,i*4096,200); } printf("sFlash write 8MB OK!\r\n"); break; case 3: break; } if(scStep<3)scStep++; }