X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/86e6ed7f31d29fa3d2aea1f6c40aa4647fce2690..03ee87b35abdb8b92e8b55ec040fa943f9a6786c:/Demos/Device/LowLevel/MassStorage/Lib/DataflashManager.c?ds=sidebyside diff --git a/Demos/Device/LowLevel/MassStorage/Lib/DataflashManager.c b/Demos/Device/LowLevel/MassStorage/Lib/DataflashManager.c index 6dc549599..4bfe7573a 100644 --- a/Demos/Device/LowLevel/MassStorage/Lib/DataflashManager.c +++ b/Demos/Device/LowLevel/MassStorage/Lib/DataflashManager.c @@ -1,21 +1,21 @@ /* LUFA Library - Copyright (C) Dean Camera, 2009. + Copyright (C) Dean Camera, 2010. dean [at] fourwalledcubicle [dot] com www.fourwalledcubicle.com */ /* - Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com) - - Permission to use, copy, modify, and distribute this software - and its documentation for any purpose and without fee is hereby - granted, provided that the above copyright notice appear in all - copies and that both that the copyright notice and this - permission notice and warranty disclaimer appear in supporting - documentation, and that the name of the author not be used in - advertising or publicity pertaining to distribution of the + Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com) + + Permission to use, copy, modify, distribute, and sell this + software and its documentation for any purpose is hereby granted + without fee, provided that the above copyright notice appear in + all copies and that both that the copyright notice and this + permission notice and warranty disclaimer appear in supporting + documentation, and that the name of the author not be used in + advertising or publicity pertaining to distribution of the software without specific, written prior permission. The author disclaim all warranties with regard to this @@ -53,22 +53,24 @@ void DataflashManager_WriteBlocks(const uint32_t BlockAddress, uint16_t TotalBlo 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); -#endif 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) { uint8_t BytesInBlockDiv16 = 0; @@ -83,7 +85,8 @@ void DataflashManager_WriteBlocks(const uint32_t BlockAddress, uint16_t TotalBlo Endpoint_ClearOUT(); /* Wait until the host has sent another packet */ - while (!(Endpoint_IsReadWriteAllowed())); + if (Endpoint_WaitUntilReady()) + return; } /* Check if end of dataflash page reached */ @@ -182,8 +185,10 @@ void DataflashManager_ReadBlocks(const uint32_t BlockAddress, uint16_t TotalBloc 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); @@ -192,7 +197,8 @@ void DataflashManager_ReadBlocks(const uint32_t BlockAddress, uint16_t TotalBloc Dataflash_SendByte(0x00); /* Wait until endpoint is ready before continuing */ - while (!(Endpoint_IsReadWriteAllowed())); + if (Endpoint_WaitUntilReady()) + return; while (TotalBlocks) { @@ -208,7 +214,8 @@ void DataflashManager_ReadBlocks(const uint32_t BlockAddress, uint16_t TotalBloc 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 */ @@ -287,16 +294,17 @@ void DataflashManager_WriteBlocks_RAM(const uint32_t BlockAddress, uint16_t Tota 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); -#endif Dataflash_WaitWhileBusy(); +#endif /* Send the dataflash buffer write command */ - Dataflash_ToggleSelectedChipCS(); Dataflash_SendByte(DF_CMD_BUFF1WRITE); Dataflash_SendAddressBytes(0, CurrDFPageByte); @@ -384,8 +392,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); @@ -478,3 +488,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; +}