X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/f1076ac4d6e56bff7fb6d2126746af1108211370..21cc9c9e19ce6bd757410cc6da29b5dc2ee8041c:/Demos/Device/ClassDriver/MassStorage/Lib/DataflashManager.c diff --git a/Demos/Device/ClassDriver/MassStorage/Lib/DataflashManager.c b/Demos/Device/ClassDriver/MassStorage/Lib/DataflashManager.c index 326f66024..6646278ee 100644 --- a/Demos/Device/ClassDriver/MassStorage/Lib/DataflashManager.c +++ b/Demos/Device/ClassDriver/MassStorage/Lib/DataflashManager.c @@ -43,7 +43,7 @@ * the pre-selected data OUT endpoint. This routine reads in OS sized blocks from the endpoint and writes * them to the dataflash in Dataflash page sized blocks. * - * \param[in] MSInterfaceInfo Pointer to a Mass Storage class state structure for the Mass Storage interface being used + * \param[in] MSInterfaceInfo Pointer to a structure containing a Mass Storage Class configuration and state * \param[in] BlockAddress Data block starting address for the write sequence * \param[in] TotalBlocks Number of blocks of data to write */ @@ -52,20 +52,25 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, co 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; - /* Copy selected dataflash's current page contents to the dataflash buffer */ + /* Select the correct starting Dataflash IC for the block requested */ Dataflash_SelectChipFromPage(CurrDFPage); + +#if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) + /* Copy selected dataflash's current page contents to the dataflash buffer */ Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1); Dataflash_SendAddressBytes(CurrDFPage, 0); Dataflash_WaitWhileBusy(); +#endif /* Send the dataflash buffer write command */ - Dataflash_ToggleSelectedChipCS(); Dataflash_SendByte(DF_CMD_BUFF1WRITE); Dataflash_SendAddressBytes(0, CurrDFPageByte); /* Wait until endpoint is ready before continuing */ - while (!(Endpoint_IsReadWriteAllowed())); + if (Endpoint_WaitUntilReady()) + return; while (TotalBlocks) { @@ -81,41 +86,44 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, co Endpoint_ClearOUT(); /* Wait until the host has sent another packet */ - while (!(Endpoint_IsReadWriteAllowed())); + if (Endpoint_WaitUntilReady()) + return; } /* Check if end of dataflash page reached */ if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4)) { /* Write the dataflash buffer contents back to the dataflash page */ - Dataflash_ToggleSelectedChipCS(); - Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE); + Dataflash_WaitWhileBusy(); + Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE : DF_CMD_BUFF1TOMAINMEMWITHERASE); Dataflash_SendAddressBytes(CurrDFPage, 0); /* Reset the dataflash buffer counter, increment the page counter */ CurrDFPageByteDiv16 = 0; CurrDFPage++; + /* Once all the dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */ + if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS)) + UsingSecondBuffer = !(UsingSecondBuffer); + /* Select the next dataflash chip based on the new dataflash page index */ Dataflash_SelectChipFromPage(CurrDFPage); - Dataflash_WaitWhileBusy(); #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */ if ((TotalBlocks * (VIRTUAL_MEMORY_BLOCK_SIZE >> 4)) < (DATAFLASH_PAGE_SIZE >> 4)) { /* Copy selected dataflash's current page contents to the dataflash buffer */ - Dataflash_ToggleSelectedChipCS(); - Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1); + Dataflash_WaitWhileBusy(); + Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2 : DF_CMD_MAINMEMTOBUFF1); Dataflash_SendAddressBytes(CurrDFPage, 0); Dataflash_WaitWhileBusy(); } #endif /* Send the dataflash buffer write command */ - Dataflash_ToggleSelectedChipCS(); - Dataflash_SendByte(DF_CMD_BUFF1WRITE); - Dataflash_SendAddressBytes(0, 0); + Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2WRITE : DF_CMD_BUFF1WRITE); + Dataflash_SendAddressBytes(0, 0); } /* Write one 16-byte chunk of data to the dataflash */ @@ -152,8 +160,8 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, co } /* Write the dataflash buffer contents back to the dataflash page */ - Dataflash_ToggleSelectedChipCS(); - Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE); + Dataflash_WaitWhileBusy(); + Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE : DF_CMD_BUFF1TOMAINMEMWITHERASE); Dataflash_SendAddressBytes(CurrDFPage, 0x00); Dataflash_WaitWhileBusy(); @@ -169,7 +177,7 @@ void DataflashManager_WriteBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, co * the pre-selected data IN endpoint. This routine reads in Dataflash page sized blocks from the Dataflash * and writes them in OS sized blocks to the endpoint. * - * \param[in] MSInterfaceInfo Pointer to a Mass Storage class state structure for the Mass Storage interface being used + * \param[in] MSInterfaceInfo Pointer to a structure containing a Mass Storage Class configuration and state * \param[in] BlockAddress Data block starting address for the read sequence * \param[in] TotalBlocks Number of blocks of data to read */ @@ -179,8 +187,10 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, con uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE); uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4); - /* Send the dataflash main memory page read command */ + /* Select the correct starting Dataflash IC for the block requested */ Dataflash_SelectChipFromPage(CurrDFPage); + + /* Send the dataflash main memory page read command */ Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD); Dataflash_SendAddressBytes(CurrDFPage, CurrDFPageByte); Dataflash_SendByte(0x00); @@ -189,7 +199,8 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, con Dataflash_SendByte(0x00); /* Wait until endpoint is ready before continuing */ - while (!(Endpoint_IsReadWriteAllowed())); + if (Endpoint_WaitUntilReady()) + return; while (TotalBlocks) { @@ -205,7 +216,8 @@ void DataflashManager_ReadBlocks(USB_ClassInfo_MS_Device_t* MSInterfaceInfo, con Endpoint_ClearIN(); /* Wait until the endpoint is ready for more data */ - while (!(Endpoint_IsReadWriteAllowed())); + if (Endpoint_WaitUntilReady()) + return; } /* Check if end of dataflash page reached */ @@ -282,18 +294,22 @@ void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress, uint16_t Tota 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; - /* Copy selected dataflash's current page contents to the dataflash buffer */ + /* Select the correct starting Dataflash IC for the block requested */ Dataflash_SelectChipFromPage(CurrDFPage); + +#if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) + /* Copy selected dataflash's current page contents to the dataflash buffer */ Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1); Dataflash_SendAddressBytes(CurrDFPage, 0); Dataflash_WaitWhileBusy(); +#endif /* Send the dataflash buffer write command */ - Dataflash_ToggleSelectedChipCS(); Dataflash_SendByte(DF_CMD_BUFF1WRITE); Dataflash_SendAddressBytes(0, CurrDFPageByte); - + while (TotalBlocks) { uint8_t BytesInBlockDiv16 = 0; @@ -305,25 +321,28 @@ void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress, uint16_t Tota if (CurrDFPageByteDiv16 == (DATAFLASH_PAGE_SIZE >> 4)) { /* Write the dataflash buffer contents back to the dataflash page */ - Dataflash_ToggleSelectedChipCS(); - Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE); + Dataflash_WaitWhileBusy(); + Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE : DF_CMD_BUFF1TOMAINMEMWITHERASE); Dataflash_SendAddressBytes(CurrDFPage, 0); /* Reset the dataflash buffer counter, increment the page counter */ CurrDFPageByteDiv16 = 0; CurrDFPage++; + /* Once all the dataflash ICs have had their first buffers filled, switch buffers to maintain throughput */ + if (Dataflash_GetSelectedChip() == DATAFLASH_CHIP_MASK(DATAFLASH_TOTALCHIPS)) + UsingSecondBuffer = !(UsingSecondBuffer); + /* Select the next dataflash chip based on the new dataflash page index */ Dataflash_SelectChipFromPage(CurrDFPage); - Dataflash_WaitWhileBusy(); #if (DATAFLASH_PAGE_SIZE > VIRTUAL_MEMORY_BLOCK_SIZE) /* If less than one dataflash page remaining, copy over the existing page to preserve trailing data */ if ((TotalBlocks * (VIRTUAL_MEMORY_BLOCK_SIZE >> 4)) < (DATAFLASH_PAGE_SIZE >> 4)) { /* Copy selected dataflash's current page contents to the dataflash buffer */ - Dataflash_ToggleSelectedChipCS(); - Dataflash_SendByte(DF_CMD_MAINMEMTOBUFF1); + Dataflash_WaitWhileBusy(); + Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_MAINMEMTOBUFF2 : DF_CMD_MAINMEMTOBUFF1); Dataflash_SendAddressBytes(CurrDFPage, 0); Dataflash_WaitWhileBusy(); } @@ -351,8 +370,8 @@ void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress, uint16_t Tota } /* Write the dataflash buffer contents back to the dataflash page */ - Dataflash_ToggleSelectedChipCS(); - Dataflash_SendByte(DF_CMD_BUFF1TOMAINMEMWITHERASE); + Dataflash_WaitWhileBusy(); + Dataflash_SendByte(UsingSecondBuffer ? DF_CMD_BUFF2TOMAINMEMWITHERASE : DF_CMD_BUFF1TOMAINMEMWITHERASE); Dataflash_SendAddressBytes(CurrDFPage, 0x00); Dataflash_WaitWhileBusy(); @@ -367,7 +386,7 @@ void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress, uint16_t Tota * * \param[in] BlockAddress Data block starting address for the read sequence * \param[in] TotalBlocks Number of blocks of data to read - * \param[out] BufferPtr Pointer to the data destination RAM buffer + * \param[out] BufferPtr Pointer to the data destination RAM buffer */ void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress, uint16_t TotalBlocks, uint8_t* BufferPtr) { @@ -375,8 +394,10 @@ void DataflashManager_ReadBlocks_RAM(const uint32_t BlockAddress, uint16_t Total uint16_t CurrDFPageByte = ((BlockAddress * VIRTUAL_MEMORY_BLOCK_SIZE) % DATAFLASH_PAGE_SIZE); uint8_t CurrDFPageByteDiv16 = (CurrDFPageByte >> 4); - /* Send the dataflash main memory page read command */ + /* Select the correct starting Dataflash IC for the block requested */ Dataflash_SelectChipFromPage(CurrDFPage); + + /* Send the dataflash main memory page read command */ Dataflash_SendByte(DF_CMD_MAINMEMPAGEREAD); Dataflash_SendAddressBytes(CurrDFPage, CurrDFPageByte); Dataflash_SendByte(0x00); @@ -469,3 +490,36 @@ void DataflashManager_ResetDataflashProtections(void) /* Deselect current dataflash chip */ Dataflash_DeselectChip(); } + +/** Performs a simple test on the attached Dataflash IC(s) to ensure that they are working. + * + * \return Boolean true if all media chips are working, false otherwise + */ +bool DataflashManager_CheckDataflashOperation(void) +{ + uint8_t ReturnByte; + + /* Test first Dataflash IC is present and responding to commands */ + Dataflash_SelectChip(DATAFLASH_CHIP1); + Dataflash_SendByte(DF_CMD_READMANUFACTURERDEVICEINFO); + ReturnByte = Dataflash_ReceiveByte(); + Dataflash_DeselectChip(); + + /* If returned data is invalid, fail the command */ + if (ReturnByte != DF_MANUFACTURER_ATMEL) + return false; + + #if (DATAFLASH_TOTALCHIPS == 2) + /* Test second Dataflash IC is present and responding to commands */ + Dataflash_SelectChip(DATAFLASH_CHIP2); + Dataflash_SendByte(DF_CMD_READMANUFACTURERDEVICEINFO); + ReturnByte = Dataflash_ReceiveByte(); + Dataflash_DeselectChip(); + + /* If returned data is invalid, fail the command */ + if (ReturnByte != DF_MANUFACTURER_ATMEL) + return false; + #endif + + return true; +}