| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923 |
- #include "w25qxx.h"
- #include "w25qxxConf.h"
- #include "w25qxxPort.h"
- #include "string.h"
- #if (_W25QXX_DEBUG == 1)
- #include "stdio.h"
- #define W25QXX_DEBUG_PRINTF(...) \
- do{\
- printf(__VA_ARGS__);\
- printf("\r\n");\
- }while(0)
- #endif
- #define W25QXX_DUMMY_BYTE 0xA5
- w25qxx_t w25qxx;
- // ###################################################################################################################
- uint32_t W25qxx_ReadID(void)
- {
- uint32_t Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0;
- W25qxx_Select();
- W25qxx_Spi(0x9F);
- Temp0 = W25qxx_Spi(W25QXX_DUMMY_BYTE);
- Temp1 = W25qxx_Spi(W25QXX_DUMMY_BYTE);
- Temp2 = W25qxx_Spi(W25QXX_DUMMY_BYTE);
- W25qxx_Deselect();
- Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2;
- return Temp;
- }
- // ###################################################################################################################
- void W25qxx_ReadUniqID(void)
- {
- W25qxx_Select();
- W25qxx_Spi(0x4B);
- for (uint8_t i = 0; i < 4; i++)
- W25qxx_Spi(W25QXX_DUMMY_BYTE);
- for (uint8_t i = 0; i < 8; i++)
- w25qxx.UniqID[i] = W25qxx_Spi(W25QXX_DUMMY_BYTE);
- W25qxx_Deselect();
- }
- // ###################################################################################################################
- void W25qxx_WriteEnable(void)
- {
- W25qxx_Select();
- W25qxx_Spi(0x06);
- W25qxx_Deselect();
- W25qxx_Delay(1);
- }
- // ###################################################################################################################
- void W25qxx_WriteDisable(void)
- {
- W25qxx_Select();
- W25qxx_Spi(0x04);
- W25qxx_Deselect();
- W25qxx_Delay(1);
- }
- // ###################################################################################################################
- uint8_t W25qxx_ReadStatusRegister(uint8_t SelectStatusRegister_1_2_3)
- {
- uint8_t status = 0;
- W25qxx_Select();
- if (SelectStatusRegister_1_2_3 == 1)
- {
- W25qxx_Spi(0x05);
- status = W25qxx_Spi(W25QXX_DUMMY_BYTE);
- w25qxx.StatusRegister1 = status;
- }
- else if (SelectStatusRegister_1_2_3 == 2)
- {
- W25qxx_Spi(0x35);
- status = W25qxx_Spi(W25QXX_DUMMY_BYTE);
- w25qxx.StatusRegister2 = status;
- }
- else
- {
- W25qxx_Spi(0x15);
- status = W25qxx_Spi(W25QXX_DUMMY_BYTE);
- w25qxx.StatusRegister3 = status;
- }
- W25qxx_Deselect();
- return status;
- }
- // ###################################################################################################################
- void W25qxx_WriteStatusRegister(uint8_t SelectStatusRegister_1_2_3, uint8_t Data)
- {
- W25qxx_Select();
- if (SelectStatusRegister_1_2_3 == 1)
- {
- W25qxx_Spi(0x01);
- w25qxx.StatusRegister1 = Data;
- }
- else if (SelectStatusRegister_1_2_3 == 2)
- {
- W25qxx_Spi(0x31);
- w25qxx.StatusRegister2 = Data;
- }
- else
- {
- W25qxx_Spi(0x11);
- w25qxx.StatusRegister3 = Data;
- }
- W25qxx_Spi(Data);
- W25qxx_Deselect();
- }
- // ###################################################################################################################
- void W25qxx_WaitForWriteEnd(void)
- {
- W25qxx_Delay(1);
- W25qxx_Select();
- W25qxx_Spi(0x05);
- do
- {
- w25qxx.StatusRegister1 = W25qxx_Spi(W25QXX_DUMMY_BYTE);
- W25qxx_Delay(1);
- } while ((w25qxx.StatusRegister1 & 0x01) == 0x01);
- W25qxx_Deselect();
- }
- // ###################################################################################################################
- bool W25qxx_Init(void)
- {
- W25qxx_SPI_Init();
- w25qxx.Lock = 1;
- W25qxx_Delay(100);
- W25qxx_Deselect();
- W25qxx_Delay(100);
- uint32_t id;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx Init Begin...");
- #endif
- id = W25qxx_ReadID();
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx ID:0x%06lx", id);
- #endif
- switch (id & 0x0000FFFF)
- {
- case 0x401A: // w25q512
- w25qxx.ID = W25Q512;
- w25qxx.BlockCount = 1024;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q512");
- #endif
- break;
- case 0x4019: // w25q256
- w25qxx.ID = W25Q256;
- w25qxx.BlockCount = 512;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q256");
- #endif
- break;
- case 0x4018: // w25q128
- w25qxx.ID = W25Q128;
- w25qxx.BlockCount = 256;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q128");
- #endif
- break;
- case 0x4017: // w25q64
- w25qxx.ID = W25Q64;
- w25qxx.BlockCount = 128;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q64");
- #endif
- break;
- case 0x4016: // w25q32
- w25qxx.ID = W25Q32;
- w25qxx.BlockCount = 64;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q32");
- #endif
- break;
- case 0x4015: // w25q16
- w25qxx.ID = W25Q16;
- w25qxx.BlockCount = 32;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q16");
- #endif
- break;
- case 0x4014: // w25q80
- w25qxx.ID = W25Q80;
- w25qxx.BlockCount = 16;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q80");
- #endif
- break;
- case 0x4013: // w25q40
- w25qxx.ID = W25Q40;
- w25qxx.BlockCount = 8;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q40");
- #endif
- break;
- case 0x4012: // w25q20
- w25qxx.ID = W25Q20;
- w25qxx.BlockCount = 4;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q20");
- #endif
- break;
- case 0x4011: // w25q10
- w25qxx.ID = W25Q10;
- w25qxx.BlockCount = 2;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx Chip: w25q10");
- #endif
- break;
- default:
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx Unknown ID");
- #endif
- w25qxx.Lock = 0;
- return false;
- }
- w25qxx.PageSize = 256;
- w25qxx.SectorSize = 0x1000;
- w25qxx.SectorCount = w25qxx.BlockCount * 16;
- w25qxx.PageCount = (w25qxx.SectorCount * w25qxx.SectorSize) / w25qxx.PageSize;
- w25qxx.BlockSize = w25qxx.SectorSize * 16;
- w25qxx.CapacityInKiloByte = (w25qxx.SectorCount * w25qxx.SectorSize) / 1024;
- W25qxx_ReadUniqID();
- W25qxx_ReadStatusRegister(1);
- W25qxx_ReadStatusRegister(2);
- W25qxx_ReadStatusRegister(3);
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx Page Size: %d Bytes", w25qxx.PageSize);
- W25QXX_DEBUG_PRINTF("w25qxx Page Count: %ld", w25qxx.PageCount);
- W25QXX_DEBUG_PRINTF("w25qxx Sector Size: %ld Bytes", w25qxx.SectorSize);
- W25QXX_DEBUG_PRINTF("w25qxx Sector Count: %ld", w25qxx.SectorCount);
- W25QXX_DEBUG_PRINTF("w25qxx Block Size: %ld Bytes", w25qxx.BlockSize);
- W25QXX_DEBUG_PRINTF("w25qxx Block Count: %ld", w25qxx.BlockCount);
- W25QXX_DEBUG_PRINTF("w25qxx Capacity: %ld KiloBytes", w25qxx.CapacityInKiloByte);
- W25QXX_DEBUG_PRINTF("w25qxx Init Done");
- #endif
- w25qxx.Lock = 0;
- return true;
- }
- // ###################################################################################################################
- void W25qxx_EraseChip(void)
- {
- while (w25qxx.Lock == 1)
- W25qxx_Delay(1);
- w25qxx.Lock = 1;
- #if (_W25QXX_DEBUG == 1)
- uint32_t StartTime = W25qxx_Ticks();
- W25QXX_DEBUG_PRINTF("w25qxx EraseChip Begin...");
- #endif
- W25qxx_WriteEnable();
- W25qxx_Select();
- W25qxx_Spi(0xC7);
- W25qxx_Deselect();
- W25qxx_WaitForWriteEnd();
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx EraseBlock done after %ld ms!", W25qxx_Ticks() - StartTime);
- #endif
- W25qxx_Delay(10);
- w25qxx.Lock = 0;
- }
- // ###################################################################################################################
- void W25qxx_EraseSector(uint32_t SectorAddr)
- {
- while (w25qxx.Lock == 1)
- W25qxx_Delay(1);
- w25qxx.Lock = 1;
- #if (_W25QXX_DEBUG == 1)
- uint32_t StartTime = W25qxx_Ticks();
- W25QXX_DEBUG_PRINTF("w25qxx EraseSector %ld Begin...", SectorAddr);
- #endif
- W25qxx_WaitForWriteEnd();
- SectorAddr = SectorAddr * w25qxx.SectorSize;
- W25qxx_WriteEnable();
- W25qxx_Select();
- W25qxx_Spi(0x20);
- if (w25qxx.ID >= W25Q256)
- W25qxx_Spi((SectorAddr & 0xFF000000) >> 24);
- W25qxx_Spi((SectorAddr & 0xFF0000) >> 16);
- W25qxx_Spi((SectorAddr & 0xFF00) >> 8);
- W25qxx_Spi(SectorAddr & 0xFF);
- W25qxx_Deselect();
- W25qxx_WaitForWriteEnd();
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx EraseSector done after %ld ms", W25qxx_Ticks() - StartTime);
- #endif
- W25qxx_Delay(1);
- w25qxx.Lock = 0;
- }
- // ###################################################################################################################
- void W25qxx_EraseBlock(uint32_t BlockAddr)
- {
- while (w25qxx.Lock == 1)
- W25qxx_Delay(1);
- w25qxx.Lock = 1;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx EraseBlock %ld Begin...", BlockAddr);
- W25qxx_Delay(100);
- uint32_t StartTime = W25qxx_Ticks();
- #endif
- W25qxx_WaitForWriteEnd();
- BlockAddr = BlockAddr * w25qxx.SectorSize * 16;
- W25qxx_WriteEnable();
- W25qxx_Select();
- W25qxx_Spi(0xD8);
- if (w25qxx.ID >= W25Q256)
- W25qxx_Spi((BlockAddr & 0xFF000000) >> 24);
- W25qxx_Spi((BlockAddr & 0xFF0000) >> 16);
- W25qxx_Spi((BlockAddr & 0xFF00) >> 8);
- W25qxx_Spi(BlockAddr & 0xFF);
- W25qxx_Deselect();
- W25qxx_WaitForWriteEnd();
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx EraseBlock done after %ld ms", W25qxx_Ticks() - StartTime);
- W25qxx_Delay(100);
- #endif
- W25qxx_Delay(1);
- w25qxx.Lock = 0;
- }
- // ###################################################################################################################
- uint32_t W25qxx_PageToSector(uint32_t PageAddress)
- {
- return ((PageAddress * w25qxx.PageSize) / w25qxx.SectorSize);
- }
- // ###################################################################################################################
- uint32_t W25qxx_PageToBlock(uint32_t PageAddress)
- {
- return ((PageAddress * w25qxx.PageSize) / w25qxx.BlockSize);
- }
- // ###################################################################################################################
- uint32_t W25qxx_SectorToBlock(uint32_t SectorAddress)
- {
- return ((SectorAddress * w25qxx.SectorSize) / w25qxx.BlockSize);
- }
- // ###################################################################################################################
- uint32_t W25qxx_SectorToPage(uint32_t SectorAddress)
- {
- return (SectorAddress * w25qxx.SectorSize) / w25qxx.PageSize;
- }
- // ###################################################################################################################
- uint32_t W25qxx_BlockToPage(uint32_t BlockAddress)
- {
- return (BlockAddress * w25qxx.BlockSize) / w25qxx.PageSize;
- }
- // ###################################################################################################################
- bool W25qxx_IsEmptyPage(uint32_t Page_Address, uint32_t OffsetInByte, uint32_t NumByteToCheck_up_to_PageSize)
- {
- while (w25qxx.Lock == 1)
- W25qxx_Delay(1);
- w25qxx.Lock = 1;
- if (((NumByteToCheck_up_to_PageSize + OffsetInByte) > w25qxx.PageSize) || (NumByteToCheck_up_to_PageSize == 0))
- NumByteToCheck_up_to_PageSize = w25qxx.PageSize - OffsetInByte;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx CheckPage:%ld, Offset:%ld, Bytes:%ld begin...", Page_Address, OffsetInByte, NumByteToCheck_up_to_PageSize);
- W25qxx_Delay(100);
- uint32_t StartTime = W25qxx_Ticks();
- #endif
- uint8_t pBuffer[32];
- uint32_t WorkAddress;
- uint32_t i;
- for (i = OffsetInByte; i < w25qxx.PageSize; i += sizeof(pBuffer))
- {
- W25qxx_Select();
- WorkAddress = (i + Page_Address * w25qxx.PageSize);
- W25qxx_Spi(0x0B);
- if (w25qxx.ID >= W25Q256)
- W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
- W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
- W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
- W25qxx_Spi(WorkAddress & 0xFF);
- W25qxx_Spi(W25QXX_DUMMY_BYTE);
- W25qxx_Spi_Receive(pBuffer, sizeof(pBuffer), 100);
- W25qxx_Deselect();
- for (uint8_t x = 0; x < sizeof(pBuffer); x++)
- {
- if (pBuffer[x] != 0xFF)
- goto NOT_EMPTY;
- }
- }
- if ((w25qxx.PageSize + OffsetInByte) % sizeof(pBuffer) != 0)
- {
- i -= sizeof(pBuffer);
- for (; i < w25qxx.PageSize; i++)
- {
- W25qxx_Select();
- WorkAddress = (i + Page_Address * w25qxx.PageSize);
- W25qxx_Spi(0x0B);
- if (w25qxx.ID >= W25Q256)
- W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
- W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
- W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
- W25qxx_Spi(WorkAddress & 0xFF);
- W25qxx_Spi(W25QXX_DUMMY_BYTE);
- W25qxx_Spi_Receive(pBuffer, 1, 100);
- W25qxx_Deselect();
- if (pBuffer[0] != 0xFF)
- goto NOT_EMPTY;
- }
- }
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx CheckPage is Empty in %ld ms", W25qxx_Ticks() - StartTime);
- W25qxx_Delay(100);
- #endif
- w25qxx.Lock = 0;
- return true;
- NOT_EMPTY:
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx CheckPage is Not Empty in %ld ms", W25qxx_Ticks() - StartTime);
- W25qxx_Delay(100);
- #endif
- w25qxx.Lock = 0;
- return false;
- }
- // ###################################################################################################################
- bool W25qxx_IsEmptySector(uint32_t Sector_Address, uint32_t OffsetInByte, uint32_t NumByteToCheck_up_to_SectorSize)
- {
- while (w25qxx.Lock == 1)
- W25qxx_Delay(1);
- w25qxx.Lock = 1;
- if ((NumByteToCheck_up_to_SectorSize > w25qxx.SectorSize) || (NumByteToCheck_up_to_SectorSize == 0))
- NumByteToCheck_up_to_SectorSize = w25qxx.SectorSize;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx CheckSector:%ld, Offset:%ld, Bytes:%ld begin...", Sector_Address, OffsetInByte, NumByteToCheck_up_to_SectorSize);
- W25qxx_Delay(100);
- uint32_t StartTime = W25qxx_Ticks();
- #endif
- uint8_t pBuffer[32];
- uint32_t WorkAddress;
- uint32_t i;
- for (i = OffsetInByte; i < w25qxx.SectorSize; i += sizeof(pBuffer))
- {
- W25qxx_Select();
- WorkAddress = (i + Sector_Address * w25qxx.SectorSize);
- W25qxx_Spi(0x0B);
- if (w25qxx.ID >= W25Q256)
- W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
- W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
- W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
- W25qxx_Spi(WorkAddress & 0xFF);
- W25qxx_Spi(W25QXX_DUMMY_BYTE);
- W25qxx_Spi_Receive(pBuffer, sizeof(pBuffer), 100);
- W25qxx_Deselect();
- for (uint8_t x = 0; x < sizeof(pBuffer); x++)
- {
- if (pBuffer[x] != 0xFF)
- goto NOT_EMPTY;
- }
- }
- if ((w25qxx.SectorSize + OffsetInByte) % sizeof(pBuffer) != 0)
- {
- i -= sizeof(pBuffer);
- for (; i < w25qxx.SectorSize; i++)
- {
- W25qxx_Select();
- WorkAddress = (i + Sector_Address * w25qxx.SectorSize);
- W25qxx_Spi(0x0B);
- if (w25qxx.ID >= W25Q256)
- W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
- W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
- W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
- W25qxx_Spi(WorkAddress & 0xFF);
- W25qxx_Spi(W25QXX_DUMMY_BYTE);
- W25qxx_Spi_Receive(pBuffer, 1, 100);
- W25qxx_Deselect();
- if (pBuffer[0] != 0xFF)
- goto NOT_EMPTY;
- }
- }
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx CheckSector is Empty in %ld ms", W25qxx_Ticks() - StartTime);
- W25qxx_Delay(100);
- #endif
- w25qxx.Lock = 0;
- return true;
- NOT_EMPTY:
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx CheckSector is Not Empty in %ld ms", W25qxx_Ticks() - StartTime);
- W25qxx_Delay(100);
- #endif
- w25qxx.Lock = 0;
- return false;
- }
- // ###################################################################################################################
- bool W25qxx_IsEmptyBlock(uint32_t Block_Address, uint32_t OffsetInByte, uint32_t NumByteToCheck_up_to_BlockSize)
- {
- while (w25qxx.Lock == 1)
- W25qxx_Delay(1);
- w25qxx.Lock = 1;
- if ((NumByteToCheck_up_to_BlockSize > w25qxx.BlockSize) || (NumByteToCheck_up_to_BlockSize == 0))
- NumByteToCheck_up_to_BlockSize = w25qxx.BlockSize;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx CheckBlock:%ld, Offset:%ld, Bytes:%ld begin...", Block_Address, OffsetInByte, NumByteToCheck_up_to_BlockSize);
- W25qxx_Delay(100);
- uint32_t StartTime = W25qxx_Ticks();
- #endif
- uint8_t pBuffer[32];
- uint32_t WorkAddress;
- uint32_t i;
- for (i = OffsetInByte; i < w25qxx.BlockSize; i += sizeof(pBuffer))
- {
- W25qxx_Select();
- WorkAddress = (i + Block_Address * w25qxx.BlockSize);
- W25qxx_Spi(0x0B);
- if (w25qxx.ID >= W25Q256)
- W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
- W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
- W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
- W25qxx_Spi(WorkAddress & 0xFF);
- W25qxx_Spi(W25QXX_DUMMY_BYTE);
- W25qxx_Spi_Receive(pBuffer, sizeof(pBuffer), 100);
- W25qxx_Deselect();
- for (uint8_t x = 0; x < sizeof(pBuffer); x++)
- {
- if (pBuffer[x] != 0xFF)
- goto NOT_EMPTY;
- }
- }
- if ((w25qxx.BlockSize + OffsetInByte) % sizeof(pBuffer) != 0)
- {
- i -= sizeof(pBuffer);
- for (; i < w25qxx.BlockSize; i++)
- {
- W25qxx_Select();
- WorkAddress = (i + Block_Address * w25qxx.BlockSize);
- W25qxx_Spi(0x0B);
- if (w25qxx.ID >= W25Q256)
- W25qxx_Spi((WorkAddress & 0xFF000000) >> 24);
- W25qxx_Spi((WorkAddress & 0xFF0000) >> 16);
- W25qxx_Spi((WorkAddress & 0xFF00) >> 8);
- W25qxx_Spi(WorkAddress & 0xFF);
- W25qxx_Spi(W25QXX_DUMMY_BYTE);
- W25qxx_Spi_Receive(pBuffer, 1, 100);
- W25qxx_Deselect();
- if (pBuffer[0] != 0xFF)
- goto NOT_EMPTY;
- }
- }
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx CheckBlock is Empty in %ld ms", W25qxx_Ticks() - StartTime);
- W25qxx_Delay(100);
- #endif
- w25qxx.Lock = 0;
- return true;
- NOT_EMPTY:
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx CheckBlock is Not Empty in %ld ms", W25qxx_Ticks() - StartTime);
- W25qxx_Delay(100);
- #endif
- w25qxx.Lock = 0;
- return false;
- }
- // ###################################################################################################################
- void W25qxx_WriteByte(uint8_t pBuffer, uint32_t WriteAddr_inBytes)
- {
- while (w25qxx.Lock == 1)
- W25qxx_Delay(1);
- w25qxx.Lock = 1;
- #if (_W25QXX_DEBUG == 1)
- uint32_t StartTime = W25qxx_Ticks();
- W25QXX_DEBUG_PRINTF("w25qxx WriteByte 0x%02X at address %ld begin...", pBuffer, WriteAddr_inBytes);
- #endif
- W25qxx_WaitForWriteEnd();
- W25qxx_WriteEnable();
- W25qxx_Select();
- W25qxx_Spi(0x02);
- if (w25qxx.ID >= W25Q256)
- W25qxx_Spi((WriteAddr_inBytes & 0xFF000000) >> 24);
- W25qxx_Spi((WriteAddr_inBytes & 0xFF0000) >> 16);
- W25qxx_Spi((WriteAddr_inBytes & 0xFF00) >> 8);
- W25qxx_Spi(WriteAddr_inBytes & 0xFF);
- W25qxx_Spi(pBuffer);
- W25qxx_Deselect();
- W25qxx_WaitForWriteEnd();
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx WriteByte done after %ld ms", W25qxx_Ticks() - StartTime);
- #endif
- w25qxx.Lock = 0;
- }
- // ###################################################################################################################
- void W25qxx_WritePage(uint8_t *pBuffer, uint32_t Page_Address, uint32_t OffsetInByte, uint32_t NumByteToWrite_up_to_PageSize)
- {
- while (w25qxx.Lock == 1)
- W25qxx_Delay(1);
- w25qxx.Lock = 1;
- if (((NumByteToWrite_up_to_PageSize + OffsetInByte) > w25qxx.PageSize) || (NumByteToWrite_up_to_PageSize == 0))
- NumByteToWrite_up_to_PageSize = w25qxx.PageSize - OffsetInByte;
- if ((OffsetInByte + NumByteToWrite_up_to_PageSize) > w25qxx.PageSize)
- NumByteToWrite_up_to_PageSize = w25qxx.PageSize - OffsetInByte;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx WritePage:%ld, Offset:%ld ,Writes %ld Bytes, begin...", Page_Address, OffsetInByte, NumByteToWrite_up_to_PageSize);
- W25qxx_Delay(100);
- uint32_t StartTime = W25qxx_Ticks();
- #endif
- W25qxx_WaitForWriteEnd();
- W25qxx_WriteEnable();
- W25qxx_Select();
- W25qxx_Spi(0x02);
- Page_Address = (Page_Address * w25qxx.PageSize) + OffsetInByte;
- if (w25qxx.ID >= W25Q256)
- W25qxx_Spi((Page_Address & 0xFF000000) >> 24);
- W25qxx_Spi((Page_Address & 0xFF0000) >> 16);
- W25qxx_Spi((Page_Address & 0xFF00) >> 8);
- W25qxx_Spi(Page_Address & 0xFF);
- W25qxx_Spi_Transmit(pBuffer, NumByteToWrite_up_to_PageSize, 100);
- W25qxx_Deselect();
- W25qxx_WaitForWriteEnd();
- #if (_W25QXX_DEBUG == 1)
- StartTime = W25qxx_Ticks() - StartTime;
- W25QXX_DEBUG_PRINTF("w25qxx WritePage done after %ld ms", StartTime);
- W25qxx_Delay(100);
- #endif
- W25qxx_Delay(1);
- w25qxx.Lock = 0;
- }
- // ###################################################################################################################
- void W25qxx_WriteSector(uint8_t *pBuffer, uint32_t Sector_Address, uint32_t OffsetInByte, uint32_t NumByteToWrite_up_to_SectorSize)
- {
- if ((NumByteToWrite_up_to_SectorSize > w25qxx.SectorSize) || (NumByteToWrite_up_to_SectorSize == 0))
- NumByteToWrite_up_to_SectorSize = w25qxx.SectorSize;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("+++w25qxx WriteSector:%ld, Offset:%ld ,Write %ld Bytes, begin...", Sector_Address, OffsetInByte, NumByteToWrite_up_to_SectorSize);
- W25qxx_Delay(100);
- #endif
- if (OffsetInByte >= w25qxx.SectorSize)
- {
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("---w25qxx WriteSector Faild!");
- W25qxx_Delay(100);
- #endif
- return;
- }
- uint32_t StartPage;
- int32_t BytesToWrite;
- uint32_t LocalOffset;
- if ((OffsetInByte + NumByteToWrite_up_to_SectorSize) > w25qxx.SectorSize)
- BytesToWrite = w25qxx.SectorSize - OffsetInByte;
- else
- BytesToWrite = NumByteToWrite_up_to_SectorSize;
- StartPage = W25qxx_SectorToPage(Sector_Address) + (OffsetInByte / w25qxx.PageSize);
- LocalOffset = OffsetInByte % w25qxx.PageSize;
- do
- {
- W25qxx_WritePage(pBuffer, StartPage, LocalOffset, BytesToWrite);
- StartPage++;
- BytesToWrite -= w25qxx.PageSize - LocalOffset;
- pBuffer += w25qxx.PageSize - LocalOffset;
- LocalOffset = 0;
- } while (BytesToWrite > 0);
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("---w25qxx WriteSector Done");
- W25qxx_Delay(100);
- #endif
- }
- // ###################################################################################################################
- void W25qxx_WriteBlock(uint8_t *pBuffer, uint32_t Block_Address, uint32_t OffsetInByte, uint32_t NumByteToWrite_up_to_BlockSize)
- {
- if ((NumByteToWrite_up_to_BlockSize > w25qxx.BlockSize) || (NumByteToWrite_up_to_BlockSize == 0))
- NumByteToWrite_up_to_BlockSize = w25qxx.BlockSize;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("+++w25qxx WriteBlock:%ld, Offset:%ld ,Write %ld Bytes, begin...", Block_Address, OffsetInByte, NumByteToWrite_up_to_BlockSize);
- W25qxx_Delay(100);
- #endif
- if (OffsetInByte >= w25qxx.BlockSize)
- {
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("---w25qxx WriteBlock Faild!");
- W25qxx_Delay(100);
- #endif
- return;
- }
- uint32_t StartPage;
- int32_t BytesToWrite;
- uint32_t LocalOffset;
- if ((OffsetInByte + NumByteToWrite_up_to_BlockSize) > w25qxx.BlockSize)
- BytesToWrite = w25qxx.BlockSize - OffsetInByte;
- else
- BytesToWrite = NumByteToWrite_up_to_BlockSize;
- StartPage = W25qxx_BlockToPage(Block_Address) + (OffsetInByte / w25qxx.PageSize);
- LocalOffset = OffsetInByte % w25qxx.PageSize;
- do
- {
- W25qxx_WritePage(pBuffer, StartPage, LocalOffset, BytesToWrite);
- StartPage++;
- BytesToWrite -= w25qxx.PageSize - LocalOffset;
- pBuffer += w25qxx.PageSize - LocalOffset;
- LocalOffset = 0;
- } while (BytesToWrite > 0);
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("---w25qxx WriteBlock Done");
- W25qxx_Delay(100);
- #endif
- }
- // ###################################################################################################################
- void W25qxx_ReadByte(uint8_t *pBuffer, uint32_t Bytes_Address)
- {
- while (w25qxx.Lock == 1)
- W25qxx_Delay(1);
- w25qxx.Lock = 1;
- #if (_W25QXX_DEBUG == 1)
- uint32_t StartTime = W25qxx_Ticks();
- W25QXX_DEBUG_PRINTF("w25qxx ReadByte at address %ld begin...", Bytes_Address);
- #endif
- W25qxx_Select();
- W25qxx_Spi(0x0B);
- if (w25qxx.ID >= W25Q256)
- W25qxx_Spi((Bytes_Address & 0xFF000000) >> 24);
- W25qxx_Spi((Bytes_Address & 0xFF0000) >> 16);
- W25qxx_Spi((Bytes_Address & 0xFF00) >> 8);
- W25qxx_Spi(Bytes_Address & 0xFF);
- W25qxx_Spi(W25QXX_DUMMY_BYTE);
- *pBuffer = W25qxx_Spi(W25QXX_DUMMY_BYTE);
- W25qxx_Deselect();
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx ReadByte 0x%02X done after %ld ms", *pBuffer, W25qxx_Ticks() - StartTime);
- #endif
- w25qxx.Lock = 0;
- }
- // ###################################################################################################################
- void W25qxx_ReadBytes(uint8_t *pBuffer, uint32_t ReadAddr, uint32_t NumByteToRead)
- {
- while (w25qxx.Lock == 1)
- W25qxx_Delay(1);
- w25qxx.Lock = 1;
- #if (_W25QXX_DEBUG == 1)
- uint32_t StartTime = W25qxx_Ticks();
- W25QXX_DEBUG_PRINTF("w25qxx ReadBytes at Address:%ld, %ld Bytes begin...", ReadAddr, NumByteToRead);
- #endif
- W25qxx_Select();
- W25qxx_Spi(0x0B);
- if (w25qxx.ID >= W25Q256)
- W25qxx_Spi((ReadAddr & 0xFF000000) >> 24);
- W25qxx_Spi((ReadAddr & 0xFF0000) >> 16);
- W25qxx_Spi((ReadAddr & 0xFF00) >> 8);
- W25qxx_Spi(ReadAddr & 0xFF);
- W25qxx_Spi(W25QXX_DUMMY_BYTE);
- W25qxx_Spi_Receive(pBuffer, NumByteToRead, 2000);
- W25qxx_Deselect();
- #if (_W25QXX_DEBUG == 1)
- StartTime = W25qxx_Ticks() - StartTime;
- W25QXX_DEBUG_PRINTF("w25qxx ReadBytes done after %ld ms", StartTime);
- W25qxx_Delay(100);
- #endif
- W25qxx_Delay(1);
- w25qxx.Lock = 0;
- }
- // ###################################################################################################################
- void W25qxx_ReadPage(uint8_t *pBuffer, uint32_t Page_Address, uint32_t OffsetInByte, uint32_t NumByteToRead_up_to_PageSize)
- {
- while (w25qxx.Lock == 1)
- W25qxx_Delay(1);
- w25qxx.Lock = 1;
- if ((NumByteToRead_up_to_PageSize > w25qxx.PageSize) || (NumByteToRead_up_to_PageSize == 0))
- NumByteToRead_up_to_PageSize = w25qxx.PageSize;
- if ((OffsetInByte + NumByteToRead_up_to_PageSize) > w25qxx.PageSize)
- NumByteToRead_up_to_PageSize = w25qxx.PageSize - OffsetInByte;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx ReadPage:%ld, Offset:%ld ,Read %ld Bytes, begin...", Page_Address, OffsetInByte, NumByteToRead_up_to_PageSize);
- W25qxx_Delay(100);
- uint32_t StartTime = W25qxx_Ticks();
- #endif
- Page_Address = Page_Address * w25qxx.PageSize + OffsetInByte;
- W25qxx_Select();
- W25qxx_Spi(0x0B);
- if (w25qxx.ID >= W25Q256)
- W25qxx_Spi((Page_Address & 0xFF000000) >> 24);
- W25qxx_Spi((Page_Address & 0xFF0000) >> 16);
- W25qxx_Spi((Page_Address & 0xFF00) >> 8);
- W25qxx_Spi(Page_Address & 0xFF);
- W25qxx_Spi(W25QXX_DUMMY_BYTE);
- W25qxx_Spi_Receive(pBuffer, NumByteToRead_up_to_PageSize, 100);
- W25qxx_Deselect();
- #if (_W25QXX_DEBUG == 1)
- StartTime = W25qxx_Ticks() - StartTime;
- W25QXX_DEBUG_PRINTF("w25qxx ReadPage done after %ld ms", StartTime);
- W25qxx_Delay(100);
- #endif
- W25qxx_Delay(1);
- w25qxx.Lock = 0;
- }
- // ###################################################################################################################
- void W25qxx_ReadSector(uint8_t *pBuffer, uint32_t Sector_Address, uint32_t OffsetInByte, uint32_t NumByteToRead_up_to_SectorSize)
- {
- if ((NumByteToRead_up_to_SectorSize > w25qxx.SectorSize) || (NumByteToRead_up_to_SectorSize == 0))
- NumByteToRead_up_to_SectorSize = w25qxx.SectorSize;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("+++w25qxx ReadSector:%ld, Offset:%ld ,Read %ld Bytes, begin...", Sector_Address, OffsetInByte, NumByteToRead_up_to_SectorSize);
- W25qxx_Delay(100);
- #endif
- if (OffsetInByte >= w25qxx.SectorSize)
- {
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("---w25qxx ReadSector Faild!");
- W25qxx_Delay(100);
- #endif
- return;
- }
- uint32_t StartPage;
- int32_t BytesToRead;
- uint32_t LocalOffset;
- if ((OffsetInByte + NumByteToRead_up_to_SectorSize) > w25qxx.SectorSize)
- BytesToRead = w25qxx.SectorSize - OffsetInByte;
- else
- BytesToRead = NumByteToRead_up_to_SectorSize;
- StartPage = W25qxx_SectorToPage(Sector_Address) + (OffsetInByte / w25qxx.PageSize);
- LocalOffset = OffsetInByte % w25qxx.PageSize;
- do
- {
- W25qxx_ReadPage(pBuffer, StartPage, LocalOffset, BytesToRead);
- StartPage++;
- BytesToRead -= w25qxx.PageSize - LocalOffset;
- pBuffer += w25qxx.PageSize - LocalOffset;
- LocalOffset = 0;
- } while (BytesToRead > 0);
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("---w25qxx ReadSector Done");
- W25qxx_Delay(100);
- #endif
- }
- // ###################################################################################################################
- void W25qxx_ReadBlock(uint8_t *pBuffer, uint32_t Block_Address, uint32_t OffsetInByte, uint32_t NumByteToRead_up_to_BlockSize)
- {
- if ((NumByteToRead_up_to_BlockSize > w25qxx.BlockSize) || (NumByteToRead_up_to_BlockSize == 0))
- NumByteToRead_up_to_BlockSize = w25qxx.BlockSize;
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("+++w25qxx ReadBlock:%ld, Offset:%ld ,Read %ld Bytes, begin...", Block_Address, OffsetInByte, NumByteToRead_up_to_BlockSize);
- W25qxx_Delay(100);
- #endif
- if (OffsetInByte >= w25qxx.BlockSize)
- {
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("w25qxx ReadBlock Faild!");
- W25qxx_Delay(100);
- #endif
- return;
- }
- uint32_t StartPage;
- int32_t BytesToRead;
- uint32_t LocalOffset;
- if ((OffsetInByte + NumByteToRead_up_to_BlockSize) > w25qxx.BlockSize)
- BytesToRead = w25qxx.BlockSize - OffsetInByte;
- else
- BytesToRead = NumByteToRead_up_to_BlockSize;
- StartPage = W25qxx_BlockToPage(Block_Address) + (OffsetInByte / w25qxx.PageSize);
- LocalOffset = OffsetInByte % w25qxx.PageSize;
- do
- {
- W25qxx_ReadPage(pBuffer, StartPage, LocalOffset, BytesToRead);
- StartPage++;
- BytesToRead -= w25qxx.PageSize - LocalOffset;
- pBuffer += w25qxx.PageSize - LocalOffset;
- LocalOffset = 0;
- } while (BytesToRead > 0);
- #if (_W25QXX_DEBUG == 1)
- W25QXX_DEBUG_PRINTF("---w25qxx ReadBlock Done");
- W25qxx_Delay(100);
- #endif
- }
- // ###################################################################################################################
- void W25qxx_SelfTest(void)
- {
- uint8_t write_data[256];
- uint8_t read_data[sizeof(write_data)];
- uint32_t page_address = 0; // 使用第一页进行测试
- int passed = 0;
- // 初始化
- W25qxx_Init();
- for (int i = 0; i < 16; i++)
- {
- #if _W25QXX_DEBUG
- W25QXX_DEBUG_PRINTF("flash test, cycle:%d", i);
- #endif
- memset(write_data, i, sizeof(write_data));
- memset(read_data, i + 1, sizeof(read_data));
- // 擦除0扇区
- #if _W25QXX_DEBUG
- W25QXX_DEBUG_PRINTF("Erasing Flash...");
- #endif
- W25qxx_EraseSector(0);
- // 写入数据到 Flash
- #if _W25QXX_DEBUG
- W25QXX_DEBUG_PRINTF("Writing data to Flash...");
- #endif
- W25qxx_WritePage(write_data, page_address, 0, sizeof(write_data));
- // 读取数据
- #if _W25QXX_DEBUG
- W25QXX_DEBUG_PRINTF("Reading data from Flash...");
- #endif
- W25qxx_ReadPage(read_data, page_address, 0, sizeof(read_data));
- // 验证数据是否一致
- bool success = true;
- for (int i = 0; i < sizeof(read_data); i++)
- {
- if (read_data[i] != write_data[i])
- {
- success = false;
- break;
- }
- }
- if (success)
- {
- #if _W25QXX_DEBUG
- W25QXX_DEBUG_PRINTF("Flash test passed! Data matches.");
- #endif
- passed++;
- }
- #if _W25QXX_DEBUG
- else
- {
- W25QXX_DEBUG_PRINTF("Flash test failed! Data mismatch.");
- }
- W25QXX_DEBUG_PRINTF("###################################################");
- #endif
- }
- #if _W25QXX_DEBUG
- W25QXX_DEBUG_PRINTF("Flash test finished. %d/256 passed.", passed);
- #endif
- }
- // ###################################################################################################################
|