123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534 |
- #define INCLUDE_FROM_DATAFLASHMANAGER_C
- #include "DataflashManager.h"
- void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
- const uint32_t BlockAddress,
- uint16_t TotalBlocks)
- {
- uint16_t CurrDFPage = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) / DATAFLASH_PAGE_SIZE);
- uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);
- uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);
- bool UsingSecondBuffer = false;
-
- Dataflash_SelectChipFromPage(CurrDFPage);
- #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
-
- Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1);
- Dataflash_SendAddressBytes(CurrDFPage, 0);
- Dataflash_WaitWhileBusy();
- #endif
-
- Dataflash_SendByte(DF_CMD_BUFF1WRITE);
- Dataflash_SendAddressBytes(0, CurrDFPageByte);
-
- if (Endpoint_WaitUntilReady())
- return;
- while (TotalBlocks)
- {
- uint8_t BytesInBlockDiv16 = 0;
-
- while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
- {
-
- if (!(Endpoint_IsReadWriteAllowed()))
- {
-
- Endpoint_ClearOUT();
-
- if (Endpoint_WaitUntilReady())
- return;
- }
-
- if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
- {
-
- Dataflash_WaitWhileBusy();
- Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE : DF_CMD_BUFF1TOMAINMEMWITHERASE);
- Dataflash_SendAddressBytes(CurrDFPage, 0);
-
- CurrDFPageByteDiv16 = 0;
- CurrDFPage++;
-
- if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS))
- UsingSecondBuffer = !(UsingSecondBuffer);
-
- Dataflash_SelectChipFromPage(CurrDFPage);
- #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
-
- if ((TotalBlocks * (VIRTUAL_MEMORY_BLOCK_SIZE >> 4)) < (DATAFLASH_PAGE_SIZE >> 4))
- {
-
- Dataflash_WaitWhileBusy();
- Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2 : DF_CMD_MAINMEMTOBUFF1);
- Dataflash_SendAddressBytes(CurrDFPage, 0);
- Dataflash_WaitWhileBusy();
- }
- #endif
-
- Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE : DF_CMD_BUFF1WRITE);
- Dataflash_SendAddressBytes(0, 0);
- }
-
- Dataflash_SendByte(Endpoint_Read_8());
- Dataflash_SendByte(Endpoint_Read_8());
- Dataflash_SendByte(Endpoint_Read_8());
- Dataflash_SendByte(Endpoint_Read_8());
- Dataflash_SendByte(Endpoint_Read_8());
- Dataflash_SendByte(Endpoint_Read_8());
- Dataflash_SendByte(Endpoint_Read_8());
- Dataflash_SendByte(Endpoint_Read_8());
- Dataflash_SendByte(Endpoint_Read_8());
- Dataflash_SendByte(Endpoint_Read_8());
- Dataflash_SendByte(Endpoint_Read_8());
- Dataflash_SendByte(Endpoint_Read_8());
- Dataflash_SendByte(Endpoint_Read_8());
- Dataflash_SendByte(Endpoint_Read_8());
- Dataflash_SendByte(Endpoint_Read_8());
- Dataflash_SendByte(Endpoint_Read_8());
-
- CurrDFPageByteDiv16++;
-
- BytesInBlockDiv16++;
-
- if (MSInterfaceInfo->State.IsMassStoreReset)
- return;
- }
-
- TotalBlocks--;
- }
-
- Dataflash_WaitWhileBusy();
- Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE : DF_CMD_BUFF1TOMAINMEMWITHERASE);
- Dataflash_SendAddressBytes(CurrDFPage, 0x00);
- Dataflash_WaitWhileBusy();
-
- if (!(Endpoint_IsReadWriteAllowed()))
- Endpoint_ClearOUT();
-
- Dataflash_DeselectChip();
- }
- void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
- const uint32_t BlockAddress,
- uint16_t TotalBlocks)
- {
- uint16_t CurrDFPage = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) / DATAFLASH_PAGE_SIZE);
- uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);
- uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);
-
- Dataflash_SelectChipFromPage(CurrDFPage);
-
- Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
- Dataflash_SendAddressBytes(CurrDFPage, CurrDFPageByte);
- Dataflash_SendByte(0x00);
- Dataflash_SendByte(0x00);
- Dataflash_SendByte(0x00);
- Dataflash_SendByte(0x00);
-
- if (Endpoint_WaitUntilReady())
- return;
- while (TotalBlocks)
- {
- uint8_t BytesInBlockDiv16 = 0;
-
- while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
- {
-
- if (!(Endpoint_IsReadWriteAllowed()))
- {
-
- Endpoint_ClearIN();
-
- if (Endpoint_WaitUntilReady())
- return;
- }
-
- if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
- {
-
- CurrDFPageByteDiv16 = 0;
- CurrDFPage++;
-
- Dataflash_SelectChipFromPage(CurrDFPage);
-
- Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
- Dataflash_SendAddressBytes(CurrDFPage, 0);
- Dataflash_SendByte(0x00);
- Dataflash_SendByte(0x00);
- Dataflash_SendByte(0x00);
- Dataflash_SendByte(0x00);
- }
-
- Endpoint_Write_8(Dataflash_ReceiveByte());
- Endpoint_Write_8(Dataflash_ReceiveByte());
- Endpoint_Write_8(Dataflash_ReceiveByte());
- Endpoint_Write_8(Dataflash_ReceiveByte());
- Endpoint_Write_8(Dataflash_ReceiveByte());
- Endpoint_Write_8(Dataflash_ReceiveByte());
- Endpoint_Write_8(Dataflash_ReceiveByte());
- Endpoint_Write_8(Dataflash_ReceiveByte());
- Endpoint_Write_8(Dataflash_ReceiveByte());
- Endpoint_Write_8(Dataflash_ReceiveByte());
- Endpoint_Write_8(Dataflash_ReceiveByte());
- Endpoint_Write_8(Dataflash_ReceiveByte());
- Endpoint_Write_8(Dataflash_ReceiveByte());
- Endpoint_Write_8(Dataflash_ReceiveByte());
- Endpoint_Write_8(Dataflash_ReceiveByte());
- Endpoint_Write_8(Dataflash_ReceiveByte());
-
- CurrDFPageByteDiv16++;
-
- BytesInBlockDiv16++;
-
- if (MSInterfaceInfo->State.IsMassStoreReset)
- return;
- }
-
- TotalBlocks--;
- }
-
- if (!(Endpoint_IsReadWriteAllowed()))
- Endpoint_ClearIN();
-
- Dataflash_DeselectChip();
- }
- void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress,
- uint16_t TotalBlocks,
- const uint8_t* BufferPtr)
- {
- uint16_t CurrDFPage = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) / DATAFLASH_PAGE_SIZE);
- uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);
- uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);
- bool UsingSecondBuffer = false;
-
- Dataflash_SelectChipFromPage(CurrDFPage);
- #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
-
- Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1);
- Dataflash_SendAddressBytes(CurrDFPage, 0);
- Dataflash_WaitWhileBusy();
- #endif
-
- Dataflash_SendByte(DF_CMD_BUFF1WRITE);
- Dataflash_SendAddressBytes(0, CurrDFPageByte);
- while (TotalBlocks)
- {
- uint8_t BytesInBlockDiv16 = 0;
-
- while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
- {
-
- if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
- {
-
- Dataflash_WaitWhileBusy();
- Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE : DF_CMD_BUFF1TOMAINMEMWITHERASE);
- Dataflash_SendAddressBytes(CurrDFPage, 0);
-
- CurrDFPageByteDiv16 = 0;
- CurrDFPage++;
-
- if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS))
- UsingSecondBuffer = !(UsingSecondBuffer);
-
- Dataflash_SelectChipFromPage(CurrDFPage);
- #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE)
-
- if ((TotalBlocks * (VIRTUAL_MEMORY_BLOCK_SIZE >> 4)) < (DATAFLASH_PAGE_SIZE >> 4))
- {
-
- Dataflash_WaitWhileBusy();
- Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2 : DF_CMD_MAINMEMTOBUFF1);
- Dataflash_SendAddressBytes(CurrDFPage, 0);
- Dataflash_WaitWhileBusy();
- }
- #endif
-
- Dataflash_ToggleSelectedChipCS();
- Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE : DF_CMD_BUFF1WRITE);
- Dataflash_SendAddressBytes(0, 0);
- }
-
- for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
- Dataflash_SendByte(*(BufferPtr++));
-
- CurrDFPageByteDiv16++;
-
- BytesInBlockDiv16++;
- }
-
- TotalBlocks--;
- }
-
- Dataflash_WaitWhileBusy();
- Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE : DF_CMD_BUFF1TOMAINMEMWITHERASE);
- Dataflash_SendAddressBytes(CurrDFPage, 0x00);
- Dataflash_WaitWhileBusy();
-
- Dataflash_DeselectChip();
- }
- void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress,
- uint16_t TotalBlocks,
- uint8_t* BufferPtr)
- {
- uint16_t CurrDFPage = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) / DATAFLASH_PAGE_SIZE);
- uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE);
- uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4);
-
- Dataflash_SelectChipFromPage(CurrDFPage);
-
- Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
- Dataflash_SendAddressBytes(CurrDFPage, CurrDFPageByte);
- Dataflash_SendByte(0x00);
- Dataflash_SendByte(0x00);
- Dataflash_SendByte(0x00);
- Dataflash_SendByte(0x00);
- while (TotalBlocks)
- {
- uint8_t BytesInBlockDiv16 = 0;
-
- while (BytesInBlockDiv16 < (VIRTUAL_MEMORY_BLOCK_SIZE >> 4))
- {
-
- if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4))
- {
-
- CurrDFPageByteDiv16 = 0;
- CurrDFPage++;
-
- Dataflash_SelectChipFromPage(CurrDFPage);
-
- Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD);
- Dataflash_SendAddressBytes(CurrDFPage, 0);
- Dataflash_SendByte(0x00);
- Dataflash_SendByte(0x00);
- Dataflash_SendByte(0x00);
- Dataflash_SendByte(0x00);
- }
-
- for (uint8_t ByteNum = 0; ByteNum < 16; ByteNum++)
- *(BufferPtr++) = Dataflash_ReceiveByte();
-
- CurrDFPageByteDiv16++;
-
- BytesInBlockDiv16++;
- }
-
- TotalBlocks--;
- }
-
- Dataflash_DeselectChip();
- }
- void DataflashManager_ResetDataflashProtections(void)
- {
-
- Dataflash_SelectChip(DATAFLASH_CHIP1);
- Dataflash_SendByte(DF_CMD_GETSTATUS);
-
- if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
- {
- Dataflash_ToggleSelectedChipCS();
-
- Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[0]);
- Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[1]);
- Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[2]);
- Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[3]);
- }
-
- #if (DATAFLASH_TOTALCHIPS == 2)
- Dataflash_SelectChip(DATAFLASH_CHIP2);
- Dataflash_SendByte(DF_CMD_GETSTATUS);
-
- if (Dataflash_ReceiveByte() & DF_STATUS_SECTORPROTECTION_ON)
- {
- Dataflash_ToggleSelectedChipCS();
-
- Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[0]);
- Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[1]);
- Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[2]);
- Dataflash_SendByte(DF_CMD_SECTORPROTECTIONOFF[3]);
- }
- #endif
-
- Dataflash_DeselectChip();
- }
- bool DataflashManager_CheckDataflashOperation(void)
- {
- uint8_t ReturnByte;
-
- Dataflash_SelectChip(DATAFLASH_CHIP1);
- Dataflash_SendByte(DF_CMD_READMANUFACTURERDEVICEINFO);
- ReturnByte = Dataflash_ReceiveByte();
- Dataflash_DeselectChip();
-
- if (ReturnByte != DF_MANUFACTURER_ATMEL)
- return false;
- #if (DATAFLASH_TOTALCHIPS == 2)
-
- Dataflash_SelectChip(DATAFLASH_CHIP2);
- Dataflash_SendByte(DF_CMD_READMANUFACTURERDEVICEINFO);
- ReturnByte = Dataflash_ReceiveByte();
- Dataflash_DeselectChip();
-
- if (ReturnByte != DF_MANUFACTURER_ATMEL)
- return false;
- #endif
- return true;
- }
|