X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/8f6b4ddf764c3a54e42d00a7502c82c5c3e71b1c..ea743970951cbcd6d5206b3136de8c9eb90298a5:/Demos/Host/MassStorageHost/MassStoreCommands.c diff --git a/Demos/Host/MassStorageHost/MassStoreCommands.c b/Demos/Host/MassStorageHost/MassStoreCommands.c index 41c59dc43..95aafca8c 100644 --- a/Demos/Host/MassStorageHost/MassStoreCommands.c +++ b/Demos/Host/MassStorageHost/MassStoreCommands.c @@ -73,9 +73,9 @@ static uint32_t MassStore_Tag = 1; */ static uint8_t MassStore_SendCommand(void) { - uint8_t ErrorCode = PIPE_RWSTREAM_ERROR_NoError; + uint8_t ErrorCode = PIPE_RWSTREAM_NoError; - /* Each transmission should have a unique tag value, excluding valued 0 and 0xFFFFFFFF */ + /* Each transmission should have a unique tag value, excluding values 0 and 0xFFFFFFFF */ if (++MassStore_Tag == 0xFFFFFFFF) MassStore_Tag = 1; @@ -84,19 +84,18 @@ static uint8_t MassStore_SendCommand(void) Pipe_Unfreeze(); /* Write the CBW command to the OUT pipe */ - if ((ErrorCode = Pipe_Write_Stream_LE(&SCSICommandBlock, sizeof(CommandBlockWrapper_t))) != PIPE_RWSTREAM_ERROR_NoError) + if ((ErrorCode = Pipe_Write_Stream_LE(&SCSICommandBlock, sizeof(CommandBlockWrapper_t))) != PIPE_RWSTREAM_NoError) return ErrorCode; /* Send the data in the OUT pipe to the attached device */ Pipe_ClearOUT(); - - /* Some buggy devices require a delay here before the pipe freezing or they will lock up */ - USB_Host_WaitMS(1); + while(!(Pipe_IsOUTReady())); + /* Freeze pipe after use */ Pipe_Freeze(); - return PIPE_RWSTREAM_ERROR_NoError; + return PIPE_RWSTREAM_NoError; } /** Waits until the attached device is ready to accept data following a CBW, checking @@ -128,7 +127,7 @@ static uint8_t MassStore_WaitForDataReceived(void) /* Check to see if the timeout period for the command has elapsed */ if (!(TimeoutMSRem)) - return PIPE_RWSTREAM_ERROR_Timeout; + return PIPE_RWSTREAM_Timeout; } Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE); @@ -139,7 +138,7 @@ static uint8_t MassStore_WaitForDataReceived(void) /* Clear the stall condition on the OUT pipe */ MassStore_ClearPipeStall(MASS_STORE_DATA_OUT_PIPE); - return PIPE_RWSTREAM_ERROR_PipeStalled; + return PIPE_RWSTREAM_PipeStalled; } Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE); @@ -150,15 +149,15 @@ static uint8_t MassStore_WaitForDataReceived(void) /* Clear the stall condition on the IN pipe */ MassStore_ClearPipeStall(MASS_STORE_DATA_IN_PIPE); - return PIPE_RWSTREAM_ERROR_PipeStalled; + return PIPE_RWSTREAM_PipeStalled; } /* Check to see if the device was disconnected, if so exit function */ if (!(USB_IsConnected)) - return PIPE_RWSTREAM_ERROR_DeviceDisconnected; + return PIPE_RWSTREAM_DeviceDisconnected; }; - return PIPE_RWSTREAM_ERROR_NoError; + return PIPE_RWSTREAM_NoError; } /** Sends or receives the transaction's data stage to or from the attached device, reading or @@ -170,7 +169,7 @@ static uint8_t MassStore_WaitForDataReceived(void) */ static uint8_t MassStore_SendReceiveData(void* BufferPtr) { - uint8_t ErrorCode = PIPE_RWSTREAM_ERROR_NoError; + uint8_t ErrorCode = PIPE_RWSTREAM_NoError; uint16_t BytesRem = SCSICommandBlock.Header.DataTransferLength; /* Check the direction of the SCSI command data stage */ @@ -181,7 +180,7 @@ static uint8_t MassStore_SendReceiveData(void* BufferPtr) Pipe_Unfreeze(); /* Read in the block data from the pipe */ - if ((ErrorCode = Pipe_Read_Stream_LE(BufferPtr, BytesRem)) != PIPE_RWSTREAM_ERROR_NoError) + if ((ErrorCode = Pipe_Read_Stream_LE(BufferPtr, BytesRem)) != PIPE_RWSTREAM_NoError) return ErrorCode; /* Acknowledge the packet */ @@ -194,20 +193,19 @@ static uint8_t MassStore_SendReceiveData(void* BufferPtr) Pipe_Unfreeze(); /* Write the block data to the pipe */ - if ((ErrorCode = Pipe_Write_Stream_LE(BufferPtr, BytesRem)) != PIPE_RWSTREAM_ERROR_NoError) + if ((ErrorCode = Pipe_Write_Stream_LE(BufferPtr, BytesRem)) != PIPE_RWSTREAM_NoError) return ErrorCode; /* Acknowledge the packet */ Pipe_ClearOUT(); + + while (!(Pipe_IsOUTReady())); } - - /* Some buggy devices require a delay here before the pipe freezing or they will lock up */ - USB_Host_WaitMS(1); /* Freeze used pipe after use */ Pipe_Freeze(); - return PIPE_RWSTREAM_ERROR_NoError; + return PIPE_RWSTREAM_NoError; } /** Routine to receive the current CSW from the device. @@ -216,10 +214,10 @@ static uint8_t MassStore_SendReceiveData(void* BufferPtr) */ static uint8_t MassStore_GetReturnedStatus(void) { - uint8_t ErrorCode = PIPE_RWSTREAM_ERROR_NoError; + uint8_t ErrorCode = PIPE_RWSTREAM_NoError; /* If an error in the command ocurred, abort */ - if ((ErrorCode == MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_ERROR_NoError) + if ((ErrorCode = MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_NoError) return ErrorCode; /* Select the IN data pipe for data reception */ @@ -227,19 +225,16 @@ static uint8_t MassStore_GetReturnedStatus(void) Pipe_Unfreeze(); /* Load in the CSW from the attached device */ - if ((ErrorCode = Pipe_Read_Stream_LE(&SCSICommandStatus, sizeof(CommandStatusWrapper_t))) != PIPE_RWSTREAM_ERROR_NoError) + if ((ErrorCode = Pipe_Read_Stream_LE(&SCSICommandStatus, sizeof(CommandStatusWrapper_t))) != PIPE_RWSTREAM_NoError) return ErrorCode; /* Clear the data ready for next reception */ Pipe_ClearIN(); - - /* Some buggy devices require a delay here before the pipe freezing or they will lock up */ - USB_Host_WaitMS(1); /* Freeze the IN pipe after use */ Pipe_Freeze(); - return PIPE_RWSTREAM_ERROR_NoError; + return PIPE_RWSTREAM_NoError; } /** Clears the stall condition in the attached device on the nominated endpoint number. @@ -250,13 +245,13 @@ static uint8_t MassStore_GetReturnedStatus(void) */ uint8_t MassStore_ClearPipeStall(const uint8_t EndpointNum) { - USB_HostRequest = (USB_Host_Request_Header_t) + USB_ControlRequest = (USB_Request_Header_t) { - bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT), - bRequest: REQ_ClearFeature, - wValue: FEATURE_ENDPOINT_HALT, - wIndex: EndpointNum, - wLength: 0, + .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_STANDARD | REQREC_ENDPOINT), + .bRequest = REQ_ClearFeature, + .wValue = FEATURE_ENDPOINT_HALT, + .wIndex = EndpointNum, + .wLength = 0, }; /* Select the control pipe for the request transfer */ @@ -272,13 +267,13 @@ uint8_t MassStore_ClearPipeStall(const uint8_t EndpointNum) */ uint8_t MassStore_MassStorageReset(void) { - USB_HostRequest = (USB_Host_Request_Header_t) + USB_ControlRequest = (USB_Request_Header_t) { - bmRequestType: (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE), - bRequest: REQ_MassStorageReset, - wValue: 0, - wIndex: 0, - wLength: 0, + .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE), + .bRequest = REQ_MassStorageReset, + .wValue = 0, + .wIndex = 0, + .wLength = 0, }; /* Select the control pipe for the request transfer */ @@ -298,13 +293,13 @@ uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex) { uint8_t ErrorCode; - USB_HostRequest = (USB_Host_Request_Header_t) + USB_ControlRequest = (USB_Request_Header_t) { - bmRequestType: (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE), - bRequest: REQ_GetMaxLUN, - wValue: 0, - wIndex: 0, - wLength: 1, + .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE), + .bRequest = REQ_GetMaxLUN, + .wValue = 0, + .wIndex = 0, + .wLength = 1, }; /* Select the control pipe for the request transfer */ @@ -332,22 +327,22 @@ uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex) */ uint8_t MassStore_RequestSense(const uint8_t LUNIndex, const SCSI_Request_Sense_Response_t* const SensePtr) { - uint8_t ReturnCode = PIPE_RWSTREAM_ERROR_NoError; + uint8_t ReturnCode = PIPE_RWSTREAM_NoError; /* Create a CBW with a SCSI command to issue REQUEST SENSE command */ SCSICommandBlock = (CommandBlockWrapper_t) { - Header: + .Header = { - Signature: CBW_SIGNATURE, - Tag: MassStore_Tag, - DataTransferLength: sizeof(SCSI_Request_Sense_Response_t), - Flags: COMMAND_DIRECTION_DATA_IN, - LUN: LUNIndex, - SCSICommandLength: 6 + .Signature = CBW_SIGNATURE, + .Tag = MassStore_Tag, + .DataTransferLength = sizeof(SCSI_Request_Sense_Response_t), + .Flags = COMMAND_DIRECTION_DATA_IN, + .LUN = LUNIndex, + .SCSICommandLength = 6 }, - SCSICommandData: + .SCSICommandData = { SCSI_CMD_REQUEST_SENSE, 0x00, // Reserved @@ -362,27 +357,27 @@ uint8_t MassStore_RequestSense(const uint8_t LUNIndex, const SCSI_Request_Sense_ MassStore_SendCommand(); /* Wait until data received from the device */ - if ((ReturnCode = MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_ERROR_NoError) + if ((ReturnCode = MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_NoError) { Pipe_Freeze(); return ReturnCode; } /* Read the returned sense data into the buffer */ - if ((ReturnCode = MassStore_SendReceiveData((uint8_t*)SensePtr)) != PIPE_RWSTREAM_ERROR_NoError) + if ((ReturnCode = MassStore_SendReceiveData((uint8_t*)SensePtr)) != PIPE_RWSTREAM_NoError) { Pipe_Freeze(); return ReturnCode; } /* Read in the returned CSW from the device */ - if ((ReturnCode = MassStore_GetReturnedStatus()) != PIPE_RWSTREAM_ERROR_NoError) + if ((ReturnCode = MassStore_GetReturnedStatus()) != PIPE_RWSTREAM_NoError) { Pipe_Freeze(); return ReturnCode; } - return PIPE_RWSTREAM_ERROR_NoError; + return PIPE_RWSTREAM_NoError; } /** Issues a SCSI Device Block Read command to the attached device, to read in one or more data blocks from the @@ -399,22 +394,22 @@ uint8_t MassStore_RequestSense(const uint8_t LUNIndex, const SCSI_Request_Sense_ uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockAddress, const uint8_t Blocks, const uint16_t BlockSize, void* BufferPtr) { - uint8_t ReturnCode = PIPE_RWSTREAM_ERROR_NoError; + uint8_t ReturnCode = PIPE_RWSTREAM_NoError; /* Create a CBW with a SCSI command to read in the given blocks from the device */ SCSICommandBlock = (CommandBlockWrapper_t) { - Header: + .Header = { - Signature: CBW_SIGNATURE, - Tag: MassStore_Tag, - DataTransferLength: ((uint32_t)Blocks * BlockSize), - Flags: COMMAND_DIRECTION_DATA_IN, - LUN: LUNIndex, - SCSICommandLength: 10 + .Signature = CBW_SIGNATURE, + .Tag = MassStore_Tag, + .DataTransferLength = ((uint32_t)Blocks * BlockSize), + .Flags = COMMAND_DIRECTION_DATA_IN, + .LUN = LUNIndex, + .SCSICommandLength = 10 }, - SCSICommandData: + .SCSICommandData = { SCSI_CMD_READ_10, 0x00, // Unused (control bits, all off) @@ -433,27 +428,27 @@ uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockAd MassStore_SendCommand(); /* Wait until data received from the device */ - if ((ReturnCode = MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_ERROR_NoError) + if ((ReturnCode = MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_NoError) { Pipe_Freeze(); return ReturnCode; } /* Read the returned block data into the buffer */ - if ((ReturnCode = MassStore_SendReceiveData(BufferPtr)) != PIPE_RWSTREAM_ERROR_NoError) + if ((ReturnCode = MassStore_SendReceiveData(BufferPtr)) != PIPE_RWSTREAM_NoError) { Pipe_Freeze(); return ReturnCode; } /* Read in the returned CSW from the device */ - if ((ReturnCode = MassStore_GetReturnedStatus()) != PIPE_RWSTREAM_ERROR_NoError) + if ((ReturnCode = MassStore_GetReturnedStatus()) != PIPE_RWSTREAM_NoError) { Pipe_Freeze(); return ReturnCode; } - return PIPE_RWSTREAM_ERROR_NoError; + return PIPE_RWSTREAM_NoError; } /** Issues a SCSI Device Block Write command to the attached device, to write one or more data blocks to the @@ -470,22 +465,22 @@ uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockAd uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockAddress, const uint8_t Blocks, const uint16_t BlockSize, void* BufferPtr) { - uint8_t ReturnCode = PIPE_RWSTREAM_ERROR_NoError; + uint8_t ReturnCode = PIPE_RWSTREAM_NoError; /* Create a CBW with a SCSI command to write the given blocks to the device */ SCSICommandBlock = (CommandBlockWrapper_t) { - Header: + .Header = { - Signature: CBW_SIGNATURE, - Tag: MassStore_Tag, - DataTransferLength: ((uint32_t)Blocks * BlockSize), - Flags: COMMAND_DIRECTION_DATA_OUT, - LUN: LUNIndex, - SCSICommandLength: 10 + .Signature = CBW_SIGNATURE, + .Tag = MassStore_Tag, + .DataTransferLength = ((uint32_t)Blocks * BlockSize), + .Flags = COMMAND_DIRECTION_DATA_OUT, + .LUN = LUNIndex, + .SCSICommandLength = 10 }, - SCSICommandData: + .SCSICommandData = { SCSI_CMD_WRITE_10, 0x00, // Unused (control bits, all off) @@ -504,20 +499,20 @@ uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockA MassStore_SendCommand(); /* Write the data to the device from the buffer */ - if ((ReturnCode = MassStore_SendReceiveData(BufferPtr)) != PIPE_RWSTREAM_ERROR_NoError) + if ((ReturnCode = MassStore_SendReceiveData(BufferPtr)) != PIPE_RWSTREAM_NoError) { Pipe_Freeze(); return ReturnCode; } /* Read in the returned CSW from the device */ - if ((ReturnCode = MassStore_GetReturnedStatus()) != PIPE_RWSTREAM_ERROR_NoError) + if ((ReturnCode = MassStore_GetReturnedStatus()) != PIPE_RWSTREAM_NoError) { Pipe_Freeze(); return ReturnCode; } - return PIPE_RWSTREAM_ERROR_NoError; + return PIPE_RWSTREAM_NoError; } /** Issues a SCSI Device Test Unit Ready command to the attached device, to determine if the device is ready to accept @@ -529,22 +524,22 @@ uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex, const uint32_t BlockA */ uint8_t MassStore_TestUnitReady(const uint8_t LUNIndex) { - uint8_t ReturnCode = PIPE_RWSTREAM_ERROR_NoError; + uint8_t ReturnCode = PIPE_RWSTREAM_NoError; /* Create a CBW with a SCSI command to issue TEST UNIT READY command */ SCSICommandBlock = (CommandBlockWrapper_t) { - Header: + .Header = { - Signature: CBW_SIGNATURE, - Tag: MassStore_Tag, - DataTransferLength: 0, - Flags: COMMAND_DIRECTION_DATA_IN, - LUN: LUNIndex, - SCSICommandLength: 6 + .Signature = CBW_SIGNATURE, + .Tag = MassStore_Tag, + .DataTransferLength = 0, + .Flags = COMMAND_DIRECTION_DATA_IN, + .LUN = LUNIndex, + .SCSICommandLength = 6 }, - SCSICommandData: + .SCSICommandData = { SCSI_CMD_TEST_UNIT_READY, 0x00, // Reserved @@ -559,13 +554,13 @@ uint8_t MassStore_TestUnitReady(const uint8_t LUNIndex) MassStore_SendCommand(); /* Read in the returned CSW from the device */ - if ((ReturnCode = MassStore_GetReturnedStatus()) != PIPE_RWSTREAM_ERROR_NoError) + if ((ReturnCode = MassStore_GetReturnedStatus()) != PIPE_RWSTREAM_NoError) { Pipe_Freeze(); return ReturnCode; } - return PIPE_RWSTREAM_ERROR_NoError; + return PIPE_RWSTREAM_NoError; } /** Issues a SCSI Device Read Capacity command to the attached device, to determine the capacity of the @@ -578,22 +573,22 @@ uint8_t MassStore_TestUnitReady(const uint8_t LUNIndex) */ uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex, SCSI_Capacity_t* const CapacityPtr) { - uint8_t ReturnCode = PIPE_RWSTREAM_ERROR_NoError; + uint8_t ReturnCode = PIPE_RWSTREAM_NoError; /* Create a CBW with a SCSI command to issue READ CAPACITY command */ SCSICommandBlock = (CommandBlockWrapper_t) { - Header: + .Header = { - Signature: CBW_SIGNATURE, - Tag: MassStore_Tag, - DataTransferLength: 8, - Flags: COMMAND_DIRECTION_DATA_IN, - LUN: LUNIndex, - SCSICommandLength: 10 + .Signature = CBW_SIGNATURE, + .Tag = MassStore_Tag, + .DataTransferLength = sizeof(SCSI_Capacity_t), + .Flags = COMMAND_DIRECTION_DATA_IN, + .LUN = LUNIndex, + .SCSICommandLength = 10 }, - SCSICommandData: + .SCSICommandData = { SCSI_CMD_READ_CAPACITY_10, 0x00, // Reserved @@ -612,14 +607,14 @@ uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex, SCSI_Capacity_t* const Ca MassStore_SendCommand(); /* Wait until data received from the device */ - if ((ReturnCode = MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_ERROR_NoError) + if ((ReturnCode = MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_NoError) { Pipe_Freeze(); return ReturnCode; } /* Read the returned capacity data into the buffer */ - if ((ReturnCode = MassStore_SendReceiveData(CapacityPtr)) != PIPE_RWSTREAM_ERROR_NoError) + if ((ReturnCode = MassStore_SendReceiveData(CapacityPtr)) != PIPE_RWSTREAM_NoError) { Pipe_Freeze(); return ReturnCode; @@ -630,13 +625,13 @@ uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex, SCSI_Capacity_t* const Ca CapacityPtr->BlockSize = SwapEndian_32(CapacityPtr->BlockSize); /* Read in the returned CSW from the device */ - if ((ReturnCode = MassStore_GetReturnedStatus()) != PIPE_RWSTREAM_ERROR_NoError) + if ((ReturnCode = MassStore_GetReturnedStatus()) != PIPE_RWSTREAM_NoError) { Pipe_Freeze(); return ReturnCode; } - return PIPE_RWSTREAM_ERROR_NoError; + return PIPE_RWSTREAM_NoError; } /** Issues a SCSI Device Prevent/Allow Medium Removal command to the attached device, to lock the physical media from @@ -650,22 +645,22 @@ uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex, SCSI_Capacity_t* const Ca */ uint8_t MassStore_PreventAllowMediumRemoval(const uint8_t LUNIndex, const bool PreventRemoval) { - uint8_t ReturnCode = PIPE_RWSTREAM_ERROR_NoError; + uint8_t ReturnCode = PIPE_RWSTREAM_NoError; /* Create a CBW with a SCSI command to issue PREVENT ALLOW MEDIUM REMOVAL command */ SCSICommandBlock = (CommandBlockWrapper_t) { - Header: + .Header = { - Signature: CBW_SIGNATURE, - Tag: MassStore_Tag, - DataTransferLength: 0, - Flags: COMMAND_DIRECTION_DATA_OUT, - LUN: LUNIndex, - SCSICommandLength: 6 + .Signature = CBW_SIGNATURE, + .Tag = MassStore_Tag, + .DataTransferLength = 0, + .Flags = COMMAND_DIRECTION_DATA_OUT, + .LUN = LUNIndex, + .SCSICommandLength = 6 }, - SCSICommandData: + .SCSICommandData = { SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL, 0x00, // Reserved @@ -680,11 +675,11 @@ uint8_t MassStore_PreventAllowMediumRemoval(const uint8_t LUNIndex, const bool P MassStore_SendCommand(); /* Read in the returned CSW from the device */ - if ((ReturnCode = MassStore_GetReturnedStatus()) != PIPE_RWSTREAM_ERROR_NoError) + if ((ReturnCode = MassStore_GetReturnedStatus())) { Pipe_Freeze(); return ReturnCode; } - return PIPE_RWSTREAM_ERROR_NoError; + return PIPE_RWSTREAM_NoError; }