W25Qxx系列中的xx代表容量,单位为Mb。 W25Qxx系列的Flash内部是按照Page(页)、Sector(扇区)、Block(块)的结构来划分的。 每页(Page)为256个字节, 每个扇区(Sector)有16页(Page)也就是4KB, 每个块(Block)有16个扇区(Sector)也就是64KB, W25Qxx最小擦除单位为一个扇区(Sector),也就是每次至少擦除4KB。 最大写入单位是一页(Page),也就是一次最多写入256个字节。
以W25q128为例。 The W25Q128JW array is organized into 65,536 programmable pages of 256-bytes each. Up to 256 bytes can be programmed at a time. Pages can be erased in groups of 16 (4KB sector erase), groups of 128 (32KB block erase), groups of 256 (64KB block erase) or the entire chip (chip erase). The W 25Q128JW has 4,096 erasable sectors and 256 erasable blocks respectively. The small 4KB sectors allow for greater flexibility in applications that require data and parameter storage. W25Q128的容量为128Mb,也就是16MB。包含了有256个可擦除块,4096个可擦除扇区,32768个可编程页。一次最多可编程256字节。页面可以按16组(4KB扇区擦除)、128组(32KB块擦除)、256组(64KB块擦除)或整个芯片(芯片擦除)擦除。 框图如下。
ID读取函数如下 //读取芯片ID //返回值如下: //0XEF13,表示芯片型号为W25Q80 //0XEF14,表示芯片型号为W25Q16 //0XEF15,表示芯片型号为W25Q32 //0XEF16,表示芯片型号为W25Q64 //0XEF17,表示芯片型号为W25Q128 //0XEF18,表示芯片型号为W25Q256 uint8_t W25qxx_ReadID(void) { uint8_t Temp0 = 0,Temp1 = 0; W25qxx_cs(0); W25qxx_SPIReadWrite(W25X_ManufactDeviceID);//发送读取ID命令 W25qxx_SPIReadWrite(0x00); W25qxx_SPIReadWrite(0x00); W25qxx_SPIReadWrite(0x00); Temp0=W25qxx_SPIReadWrite(W25QXX_DUMMY_BYTE); Temp1=W25qxx_SPIReadWrite(W25QXX_DUMMY_BYTE); W25qxx_cs(1); if(Temp0 == 0xef && Temp1>= 0x10 && Temp1<= 0x19) { return Temp1; } else return 0; } void W25qxx_EraseSector(uint32_t SectorAddr) { W25qxx_WaitForWriteEnd(); SectorAddr = SectorAddr * W25X_SectorSize; W25qxx_WriteEnable(); W25qxx_cs(0); if (w25qxx.ID >= W25Q256) { W25qxx_SPIReadWrite(0x21); W25qxx_SPIReadWrite((SectorAddr & 0xFF000000) >> 24); } else { W25qxx_SPIReadWrite(W25X_SectorErase); } W25qxx_SPIReadWrite((SectorAddr & 0xFF0000) >> 16); W25qxx_SPIReadWrite((SectorAddr & 0xFF00) >> 8); W25qxx_SPIReadWrite(SectorAddr & 0xFF); W25qxx_cs(1); W25qxx_WaitForWriteEnd(); W25qxx_Delay(1); } //在指定地址开始读取指定长度的数据 //pBuffer:数据存储区 //ReadAddr:开始读取的地址(24bit) //NumByteToRead:要读取的字节数(最大65535) void W25qxx_ReadBytes(uint8_t *pBuffer, uint32_t ReadAddr, uint32_t NumByteToRead) { W25qxx_cs(0) if (w25qxx.ID >= W25Q256) { W25qxx_SPIReadWrite(0x0C); W25qxx_SPIReadWrite((ReadAddr & 0xFF000000) >> 24); } else { W25qxx_SPIReadWrite(W25X_FastReadData); } W25qxx_SPIReadWrite((ReadAddr & 0xFF0000) >> 16); W25qxx_SPIReadWrite((ReadAddr & 0xFF00) >> 8); W25qxx_SPIReadWrite(ReadAddr & 0xFF); W25qxx_SPIReadWrite(0); for(uint32_t i=0;i<NumByteToRead;i++) { pBuffer[i]=W25qxx_SPIReadWrite(0XFF); //循环读数 } W25qxx_cs(1); } //在指定地址开始写入不大于一页的数据,写入前确保扇区已经擦除 //pBuffer: 要写入的数据 //Page_Address: 要写入的页地址(第几页) //OffsetInByte: 在页内的偏移字节 //NumByteToWrite_up_to_PageSize: 要写入的字节数(最大256),该数不应该超过该页的剩余字节数!!! void W25qxx_WritePage(uint8_t *pBuffer, uint32_t Page_Address, uint32_t OffsetInByte, uint32_t NumByteToWrite_up_to_PageSize) { if (((NumByteToWrite_up_to_PageSize + OffsetInByte) > W25X_PageSize) || (NumByteToWrite_up_to_PageSize == 0)) //判断要写入的数据是否合法 NumByteToWrite_up_to_PageSize = W25X_PageSize - OffsetInByte; W25qxx_WaitForWriteEnd(); W25qxx_WriteEnable(); W25qxx_cs(0); Page_Address = (Page_Address * W25X_PageSize) + OffsetInByte; if (w25qxx.ID >= W25Q256) { W25qxx_SPIReadWrite(0x12); W25qxx_SPIReadWrite((Page_Address & 0xFF000000) >> 24); } else { W25qxx_SPIReadWrite(W25X_PageProgram); } W25qxx_SPIReadWrite((Page_Address & 0xFF0000) >> 16); W25qxx_SPIReadWrite((Page_Address & 0xFF00) >> 8); W25qxx_SPIReadWrite(Page_Address & 0xFF); W25qxx_SPIWrite(pBuffer, NumByteToWrite_up_to_PageSize); W25qxx_cs(1); W25qxx_WaitForWriteEnd(); W25qxx_Delay(1); }
|