Added new USB_Device_GetFrameNumber() and USB_Host_GetFrameNumber() functions to...
[pub/lufa.git] / Demos / Host / LowLevel / MassStorageHost / Lib / MassStoreCommands.c
index 129c50a..c86e51b 100644 (file)
@@ -63,7 +63,8 @@ static uint32_t MassStore_Tag = 1;
  *
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
  */
-static uint8_t MassStore_SendCommand(CommandBlockWrapper_t* SCSICommandBlock, void* BufferPtr)
+static uint8_t MassStore_SendCommand(CommandBlockWrapper_t* const SCSICommandBlock,
+                                     void* BufferPtr)
 {
        uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
 
@@ -109,20 +110,23 @@ static uint8_t MassStore_SendCommand(CommandBlockWrapper_t* SCSICommandBlock, vo
  */
 static uint8_t MassStore_WaitForDataReceived(void)
 {
-       uint16_t TimeoutMSRem = COMMAND_DATA_TIMEOUT_MS;
+       uint16_t TimeoutMSRem        = COMMAND_DATA_TIMEOUT_MS;
+       uint16_t PreviousFrameNumber = USB_Host_GetFrameNumber();
 
        /* Select the IN data pipe for data reception */
        Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);
        Pipe_Unfreeze();
-
+       
        /* Wait until data received in the IN pipe */
        while (!(Pipe_IsINReceived()))
        {
+               uint16_t CurrentFrameNumber = USB_Host_GetFrameNumber();
+               
                /* Check to see if a new frame has been issued (1ms elapsed) */
-               if (USB_INT_HasOccurred(USB_INT_HSOFI))
+               if (CurrentFrameNumber != PreviousFrameNumber)
                {
-                       /* Clear the flag and decrement the timeout period counter */
-                       USB_INT_Clear(USB_INT_HSOFI);
+                       /* Save the new frame number and decrement the timeout period */
+                       PreviousFrameNumber = CurrentFrameNumber;
                        TimeoutMSRem--;
 
                        /* Check to see if the timeout period for the command has elapsed */
@@ -178,7 +182,8 @@ static uint8_t MassStore_WaitForDataReceived(void)
  *
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
  */
-static uint8_t MassStore_SendReceiveData(CommandBlockWrapper_t* SCSICommandBlock, void* BufferPtr)
+static uint8_t MassStore_SendReceiveData(CommandBlockWrapper_t* const SCSICommandBlock,
+                                         void* BufferPtr)
 {
        uint8_t  ErrorCode = PIPE_RWSTREAM_NoError;
        uint16_t BytesRem  = SCSICommandBlock->DataTransferLength;
@@ -233,7 +238,7 @@ static uint8_t MassStore_SendReceiveData(CommandBlockWrapper_t* SCSICommandBlock
  *
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  */
-static uint8_t MassStore_GetReturnedStatus(CommandStatusWrapper_t* SCSICommandStatus)
+static uint8_t MassStore_GetReturnedStatus(CommandStatusWrapper_t* const SCSICommandStatus)
 {
        uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
 
@@ -334,7 +339,8 @@ uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex)
  *
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  */
-uint8_t MassStore_Inquiry(const uint8_t LUNIndex, SCSI_Inquiry_Response_t* const InquiryPtr)
+uint8_t MassStore_Inquiry(const uint8_t LUNIndex,
+                          SCSI_Inquiry_Response_t* const InquiryPtr)
 {
        uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
 
@@ -384,7 +390,8 @@ uint8_t MassStore_Inquiry(const uint8_t LUNIndex, SCSI_Inquiry_Response_t* const
  *
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  */
-uint8_t MassStore_RequestSense(const uint8_t LUNIndex, SCSI_Request_Sense_Response_t* const SensePtr)
+uint8_t MassStore_RequestSense(const uint8_t LUNIndex,
+                               SCSI_Request_Sense_Response_t* const SensePtr)
 {
        uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
 
@@ -437,8 +444,11 @@ uint8_t MassStore_RequestSense(const uint8_t LUNIndex, SCSI_Request_Sense_Respon
  *
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  */
-uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockAddress,
-                                  const uint8_t Blocks, const uint16_t BlockSize, void* BufferPtr)
+uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex,
+                                  const uint32_t BlockAddress,
+                                  const uint8_t Blocks,
+                                  const uint16_t BlockSize,
+                                  void* BufferPtr)
 {
        uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
 
@@ -458,7 +468,7 @@ uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockAd
                                        (BlockAddress >> 16),
                                        (BlockAddress >> 8),
                                        (BlockAddress & 0xFF),  // LSB of Block Address
-                                       0x00,                   // Unused (reserved)
+                                       0x00,                   // Reserved
                                        0x00,                   // MSB of Total Blocks to Read
                                        Blocks,                 // LSB of Total Blocks to Read
                                        0x00                    // Unused (control)
@@ -495,8 +505,11 @@ uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockAd
  *
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  */
-uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockAddress,
-                                   const uint8_t Blocks, const uint16_t BlockSize, void* BufferPtr)
+uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex,
+                                   const uint32_t BlockAddress,
+                                   const uint8_t Blocks,
+                                   const uint16_t BlockSize,
+                                   void* BufferPtr)
 {
        uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
 
@@ -599,7 +612,8 @@ uint8_t MassStore_TestUnitReady(const uint8_t LUNIndex)
  *
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  */
-uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex, SCSI_Capacity_t* const CapacityPtr)
+uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex,
+                               SCSI_Capacity_t* const CapacityPtr)
 {
        uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
 
@@ -658,7 +672,8 @@ uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex, SCSI_Capacity_t* const Ca
  *
  *  \return A value from the Pipe_Stream_RW_ErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
  */
-uint8_t MassStore_PreventAllowMediumRemoval(const uint8_t LUNIndex, const bool PreventRemoval)
+uint8_t MassStore_PreventAllowMediumRemoval(const uint8_t LUNIndex,
+                                            const bool PreventRemoval)
 {
        uint8_t ErrorCode = PIPE_RWSTREAM_NoError;