X-Git-Url: http://git.linex4red.de/pub/USBasp.git/blobdiff_plain/071e02c6b6b4837fa9cf0b6d4c749994e02638d7..a8871c7fba73307226bd13e2cad4c840c850e6f1:/Demos/Device/LowLevel/MassStorage/MassStorage.c?ds=sidebyside diff --git a/Demos/Device/LowLevel/MassStorage/MassStorage.c b/Demos/Device/LowLevel/MassStorage/MassStorage.c index 9f13ff927..3bf2af16c 100644 --- a/Demos/Device/LowLevel/MassStorage/MassStorage.c +++ b/Demos/Device/LowLevel/MassStorage/MassStorage.c @@ -76,7 +76,7 @@ void SetupHardware(void) /* Hardware Initialization */ LEDs_Init(); - SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_SCK_LEAD_FALLING | SPI_SAMPLE_TRAILING | SPI_MODE_MASTER); + SPI_Init(SPI_SPEED_FCPU_DIV_2 | SPI_ORDER_MSB_FIRST | SPI_SCK_LEAD_FALLING | SPI_SAMPLE_TRAILING | SPI_MODE_MASTER); Dataflash_Init(); USB_Init(); @@ -108,23 +108,16 @@ void EVENT_USB_Device_Disconnect(void) */ void EVENT_USB_Device_ConfigurationChanged(void) { - /* Indicate USB connected and ready */ - LEDs_SetAllLEDs(LEDMASK_USB_READY); + bool ConfigSuccess = true; - /* Setup Mass Storage In and Out Endpoints */ - if (!(Endpoint_ConfigureEndpoint(MASS_STORAGE_IN_EPNUM, EP_TYPE_BULK, - ENDPOINT_DIR_IN, MASS_STORAGE_IO_EPSIZE, - ENDPOINT_BANK_SINGLE))) - { - LEDs_SetAllLEDs(LEDMASK_USB_ERROR); - } - - if (!(Endpoint_ConfigureEndpoint(MASS_STORAGE_OUT_EPNUM, EP_TYPE_BULK, - ENDPOINT_DIR_OUT, MASS_STORAGE_IO_EPSIZE, - ENDPOINT_BANK_SINGLE))) - { - LEDs_SetAllLEDs(LEDMASK_USB_ERROR); - } + /* Setup Mass Storage Data Endpoints */ + ConfigSuccess &= Endpoint_ConfigureEndpoint(MASS_STORAGE_IN_EPNUM, EP_TYPE_BULK, ENDPOINT_DIR_IN, + MASS_STORAGE_IO_EPSIZE, ENDPOINT_BANK_SINGLE); + ConfigSuccess &= Endpoint_ConfigureEndpoint(MASS_STORAGE_OUT_EPNUM, EP_TYPE_BULK, ENDPOINT_DIR_OUT, + MASS_STORAGE_IO_EPSIZE, ENDPOINT_BANK_SINGLE); + + /* Indicate endpoint configuration success or failure */ + LEDs_SetAllLEDs(ConfigSuccess ? LEDMASK_USB_READY : LEDMASK_USB_ERROR); } /** Event handler for the USB_UnhandledControlPacket event. This is used to catch standard and class specific @@ -140,11 +133,10 @@ void EVENT_USB_Device_UnhandledControlRequest(void) if (USB_ControlRequest.bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE)) { Endpoint_ClearSETUP(); + Endpoint_ClearStatusStage(); /* Indicate that the current transfer should be aborted */ IsMassStoreReset = true; - - Endpoint_ClearStatusStage(); } break; @@ -156,8 +148,7 @@ void EVENT_USB_Device_UnhandledControlRequest(void) /* Indicate to the host the number of supported LUNs (virtual disks) on the device */ Endpoint_Write_Byte(TOTAL_LUNS - 1); - Endpoint_ClearIN(); - + Endpoint_ClearIN(); Endpoint_ClearStatusStage(); } @@ -173,47 +164,35 @@ void MassStorage_Task(void) /* Device must be connected and configured for the task to run */ if (USB_DeviceState != DEVICE_STATE_Configured) return; - - /* Select the Data Out Endpoint */ - Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM); - - /* Check to see if a command from the host has been issued */ - if (Endpoint_IsReadWriteAllowed()) + + /* Process sent command block from the host if one has been sent */ + if (ReadInCommandBlock()) { /* Indicate busy */ LEDs_SetAllLEDs(LEDMASK_USB_BUSY); - /* Process sent command block from the host */ - if (ReadInCommandBlock()) - { - /* Check direction of command, select Data IN endpoint if data is from the device */ - if (CommandBlock.Flags & COMMAND_DIRECTION_DATA_IN) - Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM); + /* Check direction of command, select Data IN endpoint if data is from the device */ + if (CommandBlock.Flags & COMMAND_DIRECTION_DATA_IN) + Endpoint_SelectEndpoint(MASS_STORAGE_IN_EPNUM); - /* Decode the received SCSI command, set returned status code */ - CommandStatus.Status = SCSI_DecodeSCSICommand() ? Command_Pass : Command_Fail; + /* Decode the received SCSI command, set returned status code */ + CommandStatus.Status = SCSI_DecodeSCSICommand() ? Command_Pass : Command_Fail; - /* Load in the CBW tag into the CSW to link them together */ - CommandStatus.Tag = CommandBlock.Tag; + /* Load in the CBW tag into the CSW to link them together */ + CommandStatus.Tag = CommandBlock.Tag; - /* Load in the data residue counter into the CSW */ - CommandStatus.DataTransferResidue = CommandBlock.DataTransferLength; - - /* Stall the selected data pipe if command failed (if data is still to be transferred) */ - if ((CommandStatus.Status == Command_Fail) && (CommandStatus.DataTransferResidue)) - Endpoint_StallTransaction(); - - /* Return command status block to the host */ - ReturnCommandStatus(); - - /* Indicate ready */ - LEDs_SetAllLEDs(LEDMASK_USB_READY); - } - else - { - /* Indicate error reading in the command block from the host */ - LEDs_SetAllLEDs(LEDMASK_USB_ERROR); - } + /* Load in the data residue counter into the CSW */ + CommandStatus.DataTransferResidue = CommandBlock.DataTransferLength; + + /* Stall the selected data pipe if command failed (if data is still to be transferred) */ + if ((CommandStatus.Status == Command_Fail) && (CommandStatus.DataTransferResidue)) + Endpoint_StallTransaction(); + + /* Return command status block to the host */ + ReturnCommandStatus(); + + /* Indicate ready */ + LEDs_SetAllLEDs(LEDMASK_USB_READY); } /* Check if a Mass Storage Reset occurred */ @@ -244,6 +223,10 @@ static bool ReadInCommandBlock(void) { /* Select the Data Out endpoint */ Endpoint_SelectEndpoint(MASS_STORAGE_OUT_EPNUM); + + /* Abort if no command has been sent from the host */ + if (!(Endpoint_IsOUTReceived())) + return false; /* Read in command block header */ Endpoint_Read_Stream_LE(&CommandBlock, (sizeof(CommandBlock) - sizeof(CommandBlock.SCSICommandData)),