Squashed 'tmk_core/' changes from caca2c0..dc0e46e

dc0e46e Rename LUFA to LUFA-git
3bfa7fa Remove LUFA-120730
215b764 Merge commit 'afa0f22a9299686fd88f58ce09c5b521ac917e8f' as 'protocol/lufa/LUFA'
afa0f22 Squashed 'protocol/lufa/LUFA/' content from commit def7fca
c0c42fa Remove submodule of LUFA
30f897d Merge commit '87ced33feb74e79c3281dda36eb6d6d153399b41' as 'protocol/usb_hid/USB_Host_Shield_2.0'
87ced33 Squashed 'protocol/usb_hid/USB_Host_Shield_2.0/' content from commit aab4a69
14f6d49 Remove submodule of USB_Host_Shield_2.0

git-subtree-dir: tmk_core
git-subtree-split: dc0e46eaa4367d4e218f8816e3c117895820f07c
This commit is contained in:
tmk
2015-05-13 11:13:10 +09:00
parent 4d116a04e9
commit f6d56675f9
1575 changed files with 421901 additions and 63190 deletions

View File

@ -0,0 +1,93 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2014.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2014 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 disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
* \brief LUFA Library Configuration Header File
*
* This header file is used to configure LUFA's compile time options,
* as an alternative to the compile time constants supplied through
* a makefile.
*
* For information on what each token does, refer to the LUFA
* manual section "Summary of Compile Tokens".
*/
#ifndef _LUFA_CONFIG_H_
#define _LUFA_CONFIG_H_
#if (ARCH == ARCH_AVR8)
/* Non-USB Related Configuration Tokens: */
// #define DISABLE_TERMINAL_CODES
/* USB Class Driver Related Tokens: */
// #define HID_HOST_BOOT_PROTOCOL_ONLY
// #define HID_STATETABLE_STACK_DEPTH {Insert Value Here}
// #define HID_USAGE_STACK_DEPTH {Insert Value Here}
// #define HID_MAX_COLLECTIONS {Insert Value Here}
// #define HID_MAX_REPORTITEMS {Insert Value Here}
// #define HID_MAX_REPORT_IDS {Insert Value Here}
// #define NO_CLASS_DRIVER_AUTOFLUSH
/* General USB Driver Related Tokens: */
// #define ORDERED_EP_CONFIG
#define USE_STATIC_OPTIONS (USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)
// #define USB_DEVICE_ONLY
#define USB_HOST_ONLY
#define USB_STREAM_TIMEOUT_MS 5000
// #define NO_LIMITED_CONTROLLER_CONNECT
// #define NO_SOF_EVENTS
/* USB Device Mode Driver Related Tokens: */
// #define USE_RAM_DESCRIPTORS
// #define USE_FLASH_DESCRIPTORS
// #define USE_EEPROM_DESCRIPTORS
// #define NO_INTERNAL_SERIAL
// #define FIXED_CONTROL_ENDPOINT_SIZE {Insert Value Here}
// #define DEVICE_STATE_AS_GPIOR {Insert Value Here}
// #define FIXED_NUM_CONFIGURATIONS {Insert Value Here}
// #define CONTROL_ONLY_DEVICE
// #define INTERRUPT_CONTROL_ENDPOINT
// #define NO_DEVICE_REMOTE_WAKEUP
// #define NO_DEVICE_SELF_POWER
/* USB Host Mode Driver Related Tokens: */
// #define HOST_STATE_AS_GPIOR {Insert Value Here}
// #define USB_HOST_TIMEOUT_MS {Insert Value Here}
// #define HOST_DEVICE_SETTLE_DELAY_MS {Insert Value Here}
// #define NO_AUTO_VBUS_MANAGEMENT
// #define INVERTED_VBUS_ENABLE_LINE
#else
#error Unsupported architecture for this LUFA configuration file.
#endif
#endif

View File

@ -0,0 +1,173 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2014.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2014 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 disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
*
* USB Device Configuration Descriptor processing routines, to determine the correct pipe configurations
* needed to communication with an attached USB device. Descriptors are special computer-readable structures
* which the host requests upon device enumeration, to determine the device's capabilities and functions.
*/
#include "ConfigDescriptor.h"
/** Reads and processes an attached device's descriptors, to determine compatibility and pipe configurations. This
* routine will read in the entire configuration descriptor, and configure the hosts pipes to correctly communicate
* with compatible devices.
*
* This routine searches for a MSD interface descriptor containing bulk IN and OUT data endpoints.
*
* \return An error code from the \ref MassStorageHost_GetConfigDescriptorDataCodes_t enum.
*/
uint8_t ProcessConfigurationDescriptor(void)
{
uint8_t ConfigDescriptorData[512];
void* CurrConfigLocation = ConfigDescriptorData;
uint16_t CurrConfigBytesRem;
USB_Descriptor_Interface_t* MSInterface = NULL;
USB_Descriptor_Endpoint_t* DataINEndpoint = NULL;
USB_Descriptor_Endpoint_t* DataOUTEndpoint = NULL;
/* Retrieve the entire configuration descriptor into the allocated buffer */
switch (USB_Host_GetDeviceConfigDescriptor(1, &CurrConfigBytesRem, ConfigDescriptorData, sizeof(ConfigDescriptorData)))
{
case HOST_GETCONFIG_Successful:
break;
case HOST_GETCONFIG_InvalidData:
return InvalidConfigDataReturned;
case HOST_GETCONFIG_BuffOverflow:
return DescriptorTooLarge;
default:
return ControlError;
}
while (!(DataINEndpoint) || !(DataOUTEndpoint))
{
/* See if we've found a likely compatible interface, and if there is an endpoint within that interface */
if (!(MSInterface) ||
USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
DComp_NextMSInterfaceBulkDataEndpoint) != DESCRIPTOR_SEARCH_COMP_Found)
{
/* Get the next Mass Storage interface from the configuration descriptor */
if (USB_GetNextDescriptorComp(&CurrConfigBytesRem, &CurrConfigLocation,
DComp_NextMSInterface) != DESCRIPTOR_SEARCH_COMP_Found)
{
/* Descriptor not found, error out */
return NoCompatibleInterfaceFound;
}
/* Save the interface in case we need to refer back to it later */
MSInterface = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Interface_t);
/* Clear any found endpoints */
DataINEndpoint = NULL;
DataOUTEndpoint = NULL;
/* Skip the remainder of the loop as we have not found an endpoint yet */
continue;
}
/* Retrieve the endpoint address from the endpoint descriptor */
USB_Descriptor_Endpoint_t* EndpointData = DESCRIPTOR_PCAST(CurrConfigLocation, USB_Descriptor_Endpoint_t);
/* If the endpoint is a IN type endpoint */
if ((EndpointData->EndpointAddress & ENDPOINT_DIR_MASK) == ENDPOINT_DIR_IN)
DataINEndpoint = EndpointData;
else
DataOUTEndpoint = EndpointData;
}
/* Configure the Mass Storage data IN pipe */
Pipe_ConfigurePipe(MASS_STORE_DATA_IN_PIPE, EP_TYPE_BULK, DataINEndpoint->EndpointAddress, DataINEndpoint->EndpointSize, 1);
/* Configure the Mass Storage data OUT pipe */
Pipe_ConfigurePipe(MASS_STORE_DATA_OUT_PIPE, EP_TYPE_BULK, DataOUTEndpoint->EndpointAddress, DataOUTEndpoint->EndpointSize, 1);
/* Valid data found, return success */
return SuccessfulConfigRead;
}
/** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
* configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
* descriptor processing if an incompatible descriptor configuration is found.
*
* This comparator searches for the next Interface descriptor of the correct Mass Storage Class, Subclass and Protocol values.
*
* \return A value from the DSEARCH_Return_ErrorCodes_t enum
*/
uint8_t DComp_NextMSInterface(void* CurrentDescriptor)
{
USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
if (Header->Type == DTYPE_Interface)
{
USB_Descriptor_Interface_t* Interface = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Interface_t);
/* Check the descriptor class and protocol, break out if correct class/protocol interface found */
if ((Interface->Class == MASS_STORE_CLASS) &&
(Interface->SubClass == MASS_STORE_SUBCLASS) &&
(Interface->Protocol == MASS_STORE_PROTOCOL))
{
return DESCRIPTOR_SEARCH_Found;
}
}
return DESCRIPTOR_SEARCH_NotFound;
}
/** Descriptor comparator function. This comparator function is can be called while processing an attached USB device's
* configuration descriptor, to search for a specific sub descriptor. It can also be used to abort the configuration
* descriptor processing if an incompatible descriptor configuration is found.
*
* This comparator searches for the next Bulk Endpoint descriptor of the correct MSD interface, aborting the search if
* another interface descriptor is found before the next endpoint.
*
* \return A value from the DSEARCH_Return_ErrorCodes_t enum
*/
uint8_t DComp_NextMSInterfaceBulkDataEndpoint(void* CurrentDescriptor)
{
USB_Descriptor_Header_t* Header = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Header_t);
if (Header->Type == DTYPE_Endpoint)
{
USB_Descriptor_Endpoint_t* Endpoint = DESCRIPTOR_PCAST(CurrentDescriptor, USB_Descriptor_Endpoint_t);
/* Check the endpoint type, break out if correct BULK type endpoint found */
if ((Endpoint->Attributes & EP_TYPE_MASK) == EP_TYPE_BULK)
return DESCRIPTOR_SEARCH_Found;
}
else if (Header->Type == DTYPE_Interface)
{
return DESCRIPTOR_SEARCH_Fail;
}
return DESCRIPTOR_SEARCH_NotFound;
}

View File

@ -0,0 +1,78 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2014.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2014 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 disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
*
* Header file for ConfigDescriptor.c.
*/
#ifndef _CONFIGDESCRIPTOR_H_
#define _CONFIGDESCRIPTOR_H_
/* Includes: */
#include <LUFA/Drivers/USB/USB.h>
#include "MassStorageHost.h"
/* Macros: */
/** Interface Class value for the Mass Storage Device class. */
#define MASS_STORE_CLASS 0x08
/** Interface Class value for the Mass Storage Device subclass. */
#define MASS_STORE_SUBCLASS 0x06
/** Interface Protocol value for the Bulk Only transport protocol. */
#define MASS_STORE_PROTOCOL 0x50
/** Pipe address of the Mass Storage data IN pipe. */
#define MASS_STORE_DATA_IN_PIPE (PIPE_DIR_IN | 1)
/** Pipe address of the Mass Storage data OUT pipe. */
#define MASS_STORE_DATA_OUT_PIPE (PIPE_DIR_OUT | 2)
/* Enums: */
/** Enum for the possible return codes of the \ref ProcessConfigurationDescriptor() function. */
enum MassStorageHost_GetConfigDescriptorDataCodes_t
{
SuccessfulConfigRead = 0, /**< Configuration Descriptor was processed successfully */
ControlError = 1, /**< A control request to the device failed to complete successfully */
DescriptorTooLarge = 2, /**< The device's Configuration Descriptor is too large to process */
InvalidConfigDataReturned = 3, /**< The device returned an invalid Configuration Descriptor */
NoCompatibleInterfaceFound = 4, /**< A compatible interface with the required endpoints was not found */
};
/* Function Prototypes: */
uint8_t ProcessConfigurationDescriptor(void);
uint8_t DComp_NextMSInterface(void* CurrentDescriptor);
uint8_t DComp_NextMSInterfaceBulkDataEndpoint(void* CurrentDescriptor);
#endif

View File

@ -0,0 +1,635 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2014.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2014 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 disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
*
* Mass Storage Device commands, to issue MSD commands to the device for
* reading device status, capacity, and other characteristics. This file
* also contains block read and write functions, so that device blocks
* can be read and written. In general, these functions would be chained
* to a FAT library to give file-level access to an attached device's contents.
*
* \note Many Mass Storage devices on the market are non-compliant to the
* specifications and thus can prove difficult to interface with. It
* may be necessary to retry the functions in the module several times
* after they have returned and error to successfully send the command
* to the device. Some devices may also need to have the stream function
* timeout period extended beyond 100ms (some badly designed devices exceeding
* 1.5 seconds occasionally) by defining USB_STREAM_TIMEOUT_MS to a
* larger value in the project makefile and passing it to the compiler
* via the -D switch.
*/
#define INCLUDE_FROM_MASSSTORE_COMMANDS_C
#include "MassStoreCommands.h"
/** Current Tag value used in issued CBWs to the device. This is automatically incremented
* each time a command is sent, and is not externally accessible.
*/
static uint32_t MassStore_Tag = 1;
/** Routine to send the current CBW to the device, and increment the Tag value as needed.
*
* \param[in] SCSICommandBlock Pointer to a SCSI command block structure to send to the attached device
* \param[in,out] BufferPtr Pointer to a buffer for the data to send or receive to/from the device, or NULL if no data
*
* \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
*/
static uint8_t MassStore_SendCommand(MS_CommandBlockWrapper_t* const SCSICommandBlock,
void* BufferPtr)
{
uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
/* Wrap Tag value when invalid - MS class defines tag values of 0 and 0xFFFFFFFF to be invalid */
if (++MassStore_Tag == 0xFFFFFFFF)
MassStore_Tag = 1;
/* Each transmission should have a unique tag value, increment before use */
SCSICommandBlock->Tag = MassStore_Tag;
/* Select the OUT data pipe for CBW transmission */
Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE);
Pipe_Unfreeze();
/* Write the CBW command to the OUT pipe */
if ((ErrorCode = Pipe_Write_Stream_LE(SCSICommandBlock, sizeof(MS_CommandBlockWrapper_t), NULL)) !=
PIPE_RWSTREAM_NoError)
{
Pipe_Freeze();
return ErrorCode;
}
/* Send the data in the OUT pipe to the attached device */
Pipe_ClearOUT();
/* Wait until command has been sent */
Pipe_WaitUntilReady();
/* Freeze pipe after use */
Pipe_Freeze();
if (BufferPtr != NULL)
{
/* Transfer the requested data (if any) to or from the device */
ErrorCode = MassStore_SendReceiveData(SCSICommandBlock, (void*)BufferPtr);
/* Only fail completely if the transfer fails without a STALL, as a logical STALL can be recovered from */
if ((ErrorCode != PIPE_RWSTREAM_NoError) && (ErrorCode != PIPE_RWSTREAM_PipeStalled))
{
Pipe_Freeze();
return ErrorCode;
}
}
/* Retrieve the returned SCSI status from the device */
MS_CommandStatusWrapper_t SCSIStatusBlock;
return MassStore_GetReturnedStatus(&SCSIStatusBlock);
}
/** Waits until the attached device is ready to accept data following a CBW, checking
* to ensure that the device has not stalled the transaction.
*
* \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
*/
static uint8_t MassStore_WaitForDataReceived(void)
{
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 (CurrentFrameNumber != PreviousFrameNumber)
{
/* 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 */
if (!(TimeoutMSRem))
return PIPE_RWSTREAM_Timeout;
}
Pipe_Freeze();
Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE);
Pipe_Unfreeze();
/* Check if pipe stalled (command failed by device) */
if (Pipe_IsStalled())
{
/* Clear the stall condition on the OUT pipe */
USB_Host_ClearEndpointStall(Pipe_GetBoundEndpointAddress());
return PIPE_RWSTREAM_PipeStalled;
}
Pipe_Freeze();
Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);
Pipe_Unfreeze();
/* Check if pipe stalled (command failed by device) */
if (Pipe_IsStalled())
{
/* Clear the stall condition on the IN pipe */
USB_Host_ClearEndpointStall(Pipe_GetBoundEndpointAddress());
return PIPE_RWSTREAM_PipeStalled;
}
/* Check to see if the device was disconnected, if so exit function */
if (USB_HostState == HOST_STATE_Unattached)
return PIPE_RWSTREAM_DeviceDisconnected;
};
Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);
Pipe_Freeze();
Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE);
Pipe_Freeze();
return PIPE_RWSTREAM_NoError;
}
/** Sends or receives the transaction's data stage to or from the attached device, reading or
* writing to the nominated buffer.
*
* \param[in] SCSICommandBlock Pointer to a SCSI command block structure being sent to the attached device
* \param[in,out] BufferPtr Pointer to the data buffer to read from or write to
*
* \return A value from the Pipe_Stream_RW_ErrorCodes_t enum
*/
static uint8_t MassStore_SendReceiveData(MS_CommandBlockWrapper_t* const SCSICommandBlock,
void* BufferPtr)
{
uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
uint16_t BytesRem = SCSICommandBlock->DataTransferLength;
/* Check the direction of the SCSI command data stage */
if (SCSICommandBlock->Flags & MS_COMMAND_DIR_DATA_IN)
{
/* Wait until the device has replied with some data */
if ((ErrorCode = MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_NoError)
return ErrorCode;
/* Select the IN data pipe for data reception */
Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);
Pipe_Unfreeze();
/* Read in the block data from the pipe */
if ((ErrorCode = Pipe_Read_Stream_LE(BufferPtr, BytesRem, NULL)) != PIPE_RWSTREAM_NoError)
return ErrorCode;
/* Acknowledge the packet */
Pipe_ClearIN();
}
else
{
/* Select the OUT data pipe for data transmission */
Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE);
Pipe_Unfreeze();
/* Write the block data to the pipe */
if ((ErrorCode = Pipe_Write_Stream_LE(BufferPtr, BytesRem, NULL)) != PIPE_RWSTREAM_NoError)
return ErrorCode;
/* Acknowledge the packet */
Pipe_ClearOUT();
while (!(Pipe_IsOUTReady()))
{
if (USB_HostState == HOST_STATE_Unattached)
return PIPE_RWSTREAM_DeviceDisconnected;
}
}
/* Freeze used pipe after use */
Pipe_Freeze();
return PIPE_RWSTREAM_NoError;
}
/** Routine to receive the current CSW from the device.
*
* \param[out] SCSICommandStatus Pointer to a destination where the returned status data should be stored
*
* \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(MS_CommandStatusWrapper_t* const SCSICommandStatus)
{
uint8_t ErrorCode = PIPE_RWSTREAM_NoError;
/* If an error in the command occurred, abort */
if ((ErrorCode = MassStore_WaitForDataReceived()) != PIPE_RWSTREAM_NoError)
return ErrorCode;
/* Select the IN data pipe for data reception */
Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);
Pipe_Unfreeze();
/* Load in the CSW from the attached device */
if ((ErrorCode = Pipe_Read_Stream_LE(SCSICommandStatus, sizeof(MS_CommandStatusWrapper_t), NULL)) !=
PIPE_RWSTREAM_NoError)
{
return ErrorCode;
}
/* Clear the data ready for next reception */
Pipe_ClearIN();
/* Freeze the IN pipe after use */
Pipe_Freeze();
/* Check to see if command failed */
if (SCSICommandStatus->Status != MS_SCSI_COMMAND_Pass)
ErrorCode = MASS_STORE_SCSI_COMMAND_FAILED;
return ErrorCode;
}
/** Issues a Mass Storage class specific request to reset the attached device's Mass Storage interface,
* readying the device for the next CBW. The Data endpoints are cleared of any STALL condition once this
* command completes successfully.
*
* \return A value from the USB_Host_SendControlErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
*/
uint8_t MassStore_MassStorageReset(void)
{
uint8_t ErrorCode;
USB_ControlRequest = (USB_Request_Header_t)
{
.bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
.bRequest = MS_REQ_MassStorageReset,
.wValue = 0,
.wIndex = 0,
.wLength = 0,
};
/* Select the control pipe for the request transfer */
Pipe_SelectPipe(PIPE_CONTROLPIPE);
if ((ErrorCode = USB_Host_SendControlRequest(NULL)) != HOST_SENDCONTROL_Successful)
return ErrorCode;
/* Select first data pipe to clear STALL condition if one exists */
Pipe_SelectPipe(MASS_STORE_DATA_IN_PIPE);
if ((ErrorCode = USB_Host_ClearEndpointStall(Pipe_GetBoundEndpointAddress())) != HOST_SENDCONTROL_Successful)
return ErrorCode;
/* Select second data pipe to clear STALL condition if one exists */
Pipe_SelectPipe(MASS_STORE_DATA_OUT_PIPE);
if ((ErrorCode = USB_Host_ClearEndpointStall(Pipe_GetBoundEndpointAddress())) != HOST_SENDCONTROL_Successful)
return ErrorCode;
return HOST_SENDCONTROL_Successful;
}
/** Issues a Mass Storage class specific request to determine the index of the highest numbered Logical
* Unit in the attached device.
*
* \note Some devices do not support this request, and will STALL it when issued. To get around this,
* on unsupported devices the max LUN index will be reported as zero and no error will be returned
* if the device STALLs the request.
*
* \param[out] MaxLUNIndex Pointer to the location that the maximum LUN index value should be stored
*
* \return A value from the USB_Host_SendControlErrorCodes_t enum, or MASS_STORE_SCSI_COMMAND_FAILED if the SCSI command fails
*/
uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex)
{
uint8_t ErrorCode;
USB_ControlRequest = (USB_Request_Header_t)
{
.bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
.bRequest = MS_REQ_GetMaxLUN,
.wValue = 0,
.wIndex = 0,
.wLength = 1,
};
/* Select the control pipe for the request transfer */
Pipe_SelectPipe(PIPE_CONTROLPIPE);
if ((ErrorCode = USB_Host_SendControlRequest(MaxLUNIndex)) == HOST_SENDCONTROL_SetupStalled)
{
/* Clear the pipe stall */
Pipe_ClearStall();
/* Some faulty Mass Storage devices don't implement the GET_MAX_LUN request, so assume a single LUN */
*MaxLUNIndex = 0;
/* Clear the error, and pretend the request executed correctly if the device STALLed it */
ErrorCode = HOST_SENDCONTROL_Successful;
}
return ErrorCode;
}
/** Issues a SCSI Inquiry command to the attached device, to determine the device's information. This
* gives information on the device's capabilities.
*
* \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
* \param[out] InquiryPtr Pointer to the inquiry data structure where the inquiry data from the device is to be stored
*
* \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)
{
/* Create a CBW with a SCSI command to issue INQUIRY command */
MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
{
.Signature = MS_CBW_SIGNATURE,
.DataTransferLength = sizeof(SCSI_Inquiry_Response_t),
.Flags = MS_COMMAND_DIR_DATA_IN,
.LUN = LUNIndex,
.SCSICommandLength = 6,
.SCSICommandData =
{
SCSI_CMD_INQUIRY,
0x00, // Reserved
0x00, // Reserved
0x00, // Reserved
sizeof(SCSI_Inquiry_Response_t), // Allocation Length
0x00 // Unused (control)
}
};
/* Send the command and any data to the attached device */
return MassStore_SendCommand(&SCSICommandBlock, InquiryPtr);
}
/** Issues a SCSI Request Sense command to the attached device, to determine the current SCSI sense information. This
* gives error codes for the last issued SCSI command to the device.
*
* \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
* \param[out] SensePtr Pointer to the sense data structure where the sense data from the device is to be stored
*
* \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)
{
/* Create a CBW with a SCSI command to issue REQUEST SENSE command */
MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
{
.Signature = MS_CBW_SIGNATURE,
.DataTransferLength = sizeof(SCSI_Request_Sense_Response_t),
.Flags = MS_COMMAND_DIR_DATA_IN,
.LUN = LUNIndex,
.SCSICommandLength = 6,
.SCSICommandData =
{
SCSI_CMD_REQUEST_SENSE,
0x00, // Reserved
0x00, // Reserved
0x00, // Reserved
sizeof(SCSI_Request_Sense_Response_t), // Allocation Length
0x00 // Unused (control)
}
};
/* Send the command and any data to the attached device */
return MassStore_SendCommand(&SCSICommandBlock, SensePtr);
}
/** Issues a SCSI Device Block Read command to the attached device, to read in one or more data blocks from the
* storage medium into a buffer.
*
* \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
* \param[in] BlockAddress Start block address to read from
* \param[in] Blocks Number of blocks to read from the device
* \param[in] BlockSize Size in bytes of each block to read
* \param[out] BufferPtr Pointer to the buffer where the read data is to be written to
*
* \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)
{
/* Create a CBW with a SCSI command to read in the given blocks from the device */
MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
{
.Signature = MS_CBW_SIGNATURE,
.DataTransferLength = ((uint32_t)Blocks * BlockSize),
.Flags = MS_COMMAND_DIR_DATA_IN,
.LUN = LUNIndex,
.SCSICommandLength = 10,
.SCSICommandData =
{
SCSI_CMD_READ_10,
0x00, // Unused (control bits, all off)
(BlockAddress >> 24), // MSB of Block Address
(BlockAddress >> 16),
(BlockAddress >> 8),
(BlockAddress & 0xFF), // LSB of Block Address
0x00, // Reserved
0x00, // MSB of Total Blocks to Read
Blocks, // LSB of Total Blocks to Read
0x00 // Unused (control)
}
};
/* Send the command and any data to the attached device */
return MassStore_SendCommand(&SCSICommandBlock, BufferPtr);
}
/** Issues a SCSI Device Block Write command to the attached device, to write one or more data blocks to the
* storage medium from a buffer.
*
* \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
* \param[in] BlockAddress Start block address to write to
* \param[in] Blocks Number of blocks to write to in the device
* \param[in] BlockSize Size in bytes of each block to write
* \param[in] BufferPtr Pointer to the buffer where the write data is to be sourced from
*
* \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)
{
/* Create a CBW with a SCSI command to write the given blocks to the device */
MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
{
.Signature = MS_CBW_SIGNATURE,
.DataTransferLength = ((uint32_t)Blocks * BlockSize),
.Flags = MS_COMMAND_DIR_DATA_OUT,
.LUN = LUNIndex,
.SCSICommandLength = 10,
.SCSICommandData =
{
SCSI_CMD_WRITE_10,
0x00, // Unused (control bits, all off)
(BlockAddress >> 24), // MSB of Block Address
(BlockAddress >> 16),
(BlockAddress >> 8),
(BlockAddress & 0xFF), // LSB of Block Address
0x00, // Unused (reserved)
0x00, // MSB of Total Blocks to Write
Blocks, // LSB of Total Blocks to Write
0x00 // Unused (control)
}
};
/* Send the command and any data to the attached device */
return MassStore_SendCommand(&SCSICommandBlock, BufferPtr);
}
/** Issues a SCSI Device Test Unit Ready command to the attached device, to determine if the device is ready to accept
* other commands.
*
* \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
*
* \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_TestUnitReady(const uint8_t LUNIndex)
{
/* Create a CBW with a SCSI command to issue TEST UNIT READY command */
MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
{
.Signature = MS_CBW_SIGNATURE,
.DataTransferLength = 0,
.Flags = MS_COMMAND_DIR_DATA_IN,
.LUN = LUNIndex,
.SCSICommandLength = 6,
.SCSICommandData =
{
SCSI_CMD_TEST_UNIT_READY,
0x00, // Reserved
0x00, // Reserved
0x00, // Reserved
0x00, // Reserved
0x00 // Unused (control)
}
};
/* Send the command and any data to the attached device */
return MassStore_SendCommand(&SCSICommandBlock, NULL);
}
/** Issues a SCSI Device Read Capacity command to the attached device, to determine the capacity of the
* given Logical Unit within the device.
*
* \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
* \param[out] CapacityPtr Device capacity structure where the capacity data is to be stored
*
* \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 ErrorCode = PIPE_RWSTREAM_NoError;
/* Create a CBW with a SCSI command to issue READ CAPACITY command */
MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
{
.Signature = MS_CBW_SIGNATURE,
.DataTransferLength = sizeof(SCSI_Capacity_t),
.Flags = MS_COMMAND_DIR_DATA_IN,
.LUN = LUNIndex,
.SCSICommandLength = 10,
.SCSICommandData =
{
SCSI_CMD_READ_CAPACITY_10,
0x00, // Reserved
0x00, // MSB of Logical block address
0x00,
0x00,
0x00, // LSB of Logical block address
0x00, // Reserved
0x00, // Reserved
0x00, // Partial Medium Indicator
0x00 // Unused (control)
}
};
/* Send the command and any data to the attached device */
if ((ErrorCode = MassStore_SendCommand(&SCSICommandBlock, CapacityPtr)) != PIPE_RWSTREAM_NoError)
return ErrorCode;
/* Endian-correct the read data */
CapacityPtr->Blocks = SwapEndian_32(CapacityPtr->Blocks);
CapacityPtr->BlockSize = SwapEndian_32(CapacityPtr->BlockSize);
return ErrorCode;
}
/** Issues a SCSI Device Prevent/Allow Medium Removal command to the attached device, to lock the physical media from
* being removed. This is a legacy command for SCSI disks with removable storage (such as ZIP disks), but should still
* be issued before the first read or write command is sent.
*
* \param[in] LUNIndex Index of the LUN inside the device the command is being addressed to
* \param[in] PreventRemoval Whether or not the LUN media should be locked to prevent removal or not
*
* \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)
{
/* Create a CBW with a SCSI command to issue PREVENT ALLOW MEDIUM REMOVAL command */
MS_CommandBlockWrapper_t SCSICommandBlock = (MS_CommandBlockWrapper_t)
{
.Signature = MS_CBW_SIGNATURE,
.DataTransferLength = 0,
.Flags = MS_COMMAND_DIR_DATA_OUT,
.LUN = LUNIndex,
.SCSICommandLength = 6,
.SCSICommandData =
{
SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL,
0x00, // Reserved
0x00, // Reserved
PreventRemoval, // Prevent flag
0x00, // Reserved
0x00 // Unused (control)
}
};
/* Send the command and any data to the attached device */
return MassStore_SendCommand(&SCSICommandBlock, NULL);
}

View File

@ -0,0 +1,86 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2014.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2014 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 disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
*
* Header file for MassStoreCommands.c.
*/
#ifndef _MASS_STORE_COMMANDS_H_
#define _MASS_STORE_COMMANDS_H_
/* Includes: */
#include <avr/io.h>
#include "../MassStorageHost.h"
#include <LUFA/Drivers/USB/USB.h>
/* Macros: */
/** Timeout period between the issuing of a CBW to a device, and the reception of the first packet. */
#define COMMAND_DATA_TIMEOUT_MS 10000
/** Additional error code for Mass Storage functions when a device returns a logical command failure. */
#define MASS_STORE_SCSI_COMMAND_FAILED 0xC0
/* Function Prototypes: */
#if defined(INCLUDE_FROM_MASSSTORE_COMMANDS_C)
static uint8_t MassStore_SendCommand(MS_CommandBlockWrapper_t* const SCSICommandBlock,
void* BufferPtr);
static uint8_t MassStore_WaitForDataReceived(void);
static uint8_t MassStore_SendReceiveData(MS_CommandBlockWrapper_t* const SCSICommandBlock,
void* BufferPtr) ATTR_NON_NULL_PTR_ARG(1);
static uint8_t MassStore_GetReturnedStatus(MS_CommandStatusWrapper_t* const SCSICommandStatus) ATTR_NON_NULL_PTR_ARG(1);
#endif
uint8_t MassStore_MassStorageReset(void);
uint8_t MassStore_GetMaxLUN(uint8_t* const MaxLUNIndex);
uint8_t MassStore_RequestSense(const uint8_t LUNIndex,
SCSI_Request_Sense_Response_t* const SensePtr) ATTR_NON_NULL_PTR_ARG(2);
uint8_t MassStore_Inquiry(const uint8_t LUNIndex,
SCSI_Inquiry_Response_t* const InquiryPtr) ATTR_NON_NULL_PTR_ARG(2);
uint8_t MassStore_ReadDeviceBlock(const uint8_t LUNIndex,
const uint32_t BlockAddress,
const uint8_t Blocks,
const uint16_t BlockSize,
void* BufferPtr) ATTR_NON_NULL_PTR_ARG(5);
uint8_t MassStore_WriteDeviceBlock(const uint8_t LUNIndex,
const uint32_t BlockAddress,
const uint8_t Blocks,
const uint16_t BlockSize,
void* BufferPtr) ATTR_NON_NULL_PTR_ARG(5);
uint8_t MassStore_ReadCapacity(const uint8_t LUNIndex,
SCSI_Capacity_t* const CapacityPtr) ATTR_NON_NULL_PTR_ARG(2);
uint8_t MassStore_TestUnitReady(const uint8_t LUNIndex);
uint8_t MassStore_PreventAllowMediumRemoval(const uint8_t LUNIndex,
const bool PreventRemoval);
#endif

View File

@ -0,0 +1,373 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2014.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2014 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 disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
*
* Main source file for the MassStorageHost demo. This file contains the main tasks of
* the demo and is responsible for the initial application hardware configuration.
*/
#include "MassStorageHost.h"
/** Index of the highest available LUN (Logical Unit) in the attached Mass Storage Device */
uint8_t MassStore_MaxLUNIndex;
/** Main program entry point. This routine configures the hardware required by the application, then
* enters a loop to run the application tasks in sequence.
*/
int main(void)
{
SetupHardware();
puts_P(PSTR(ESC_FG_CYAN "Mass Storage Host Demo running.\r\n" ESC_FG_WHITE));
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
GlobalInterruptEnable();
for (;;)
{
MassStorageHost_Task();
USB_USBTask();
}
}
/** Configures the board hardware and chip peripherals for the demo's functionality. */
void SetupHardware(void)
{
#if (ARCH == ARCH_AVR8)
/* Disable watchdog if enabled by bootloader/fuses */
MCUSR &= ~(1 << WDRF);
wdt_disable();
/* Disable clock division */
clock_prescale_set(clock_div_1);
#endif
/* Hardware Initialization */
Serial_Init(9600, false);
LEDs_Init();
Buttons_Init();
USB_Init();
/* Create a stdio stream for the serial port for stdin and stdout */
Serial_CreateStream(NULL);
}
/** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
* starts the library USB task to begin the enumeration and USB management process.
*/
void EVENT_USB_Host_DeviceAttached(void)
{
puts_P(PSTR(ESC_FG_GREEN "Device Attached.\r\n" ESC_FG_WHITE));
LEDs_SetAllLEDs(LEDMASK_USB_ENUMERATING);
}
/** Event handler for the USB_DeviceUnattached event. This indicates that a device has been removed from the host, and
* stops the library USB task management process.
*/
void EVENT_USB_Host_DeviceUnattached(void)
{
puts_P(PSTR(ESC_FG_GREEN "\r\nDevice Unattached.\r\n" ESC_FG_WHITE));
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
}
/** Event handler for the USB_DeviceEnumerationComplete event. This indicates that a device has been successfully
* enumerated by the host and is now ready to be used by the application.
*/
void EVENT_USB_Host_DeviceEnumerationComplete(void)
{
puts_P(PSTR("Getting Config Data.\r\n"));
uint8_t ErrorCode;
/* Get and process the configuration descriptor data */
if ((ErrorCode = ProcessConfigurationDescriptor()) != SuccessfulConfigRead)
{
if (ErrorCode == ControlError)
puts_P(PSTR(ESC_FG_RED "Control Error (Get Configuration).\r\n"));
else
puts_P(PSTR(ESC_FG_RED "Invalid Device.\r\n"));
printf_P(PSTR(" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
return;
}
/* Set the device configuration to the first configuration (rarely do devices use multiple configurations) */
if ((ErrorCode = USB_Host_SetDeviceConfiguration(1)) != HOST_SENDCONTROL_Successful)
{
printf_P(PSTR(ESC_FG_RED "Control Error (Set Configuration).\r\n"
" -- Error Code: %d\r\n" ESC_FG_WHITE), ErrorCode);
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
return;
}
puts_P(PSTR("Mass Storage Disk Enumerated.\r\n"));
LEDs_SetAllLEDs(LEDMASK_USB_READY);
}
/** Event handler for the USB_HostError event. This indicates that a hardware error occurred while in host mode. */
void EVENT_USB_Host_HostError(const uint8_t ErrorCode)
{
USB_Disable();
printf_P(PSTR(ESC_FG_RED "Host Mode Error\r\n"
" -- Error Code %d\r\n" ESC_FG_WHITE), ErrorCode);
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
for(;;);
}
/** Event handler for the USB_DeviceEnumerationFailed event. This indicates that a problem occurred while
* enumerating an attached USB device.
*/
void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
const uint8_t SubErrorCode)
{
printf_P(PSTR(ESC_FG_RED "Dev Enum Error\r\n"
" -- Error Code %d\r\n"
" -- Sub Error Code %d\r\n"
" -- In State %d\r\n" ESC_FG_WHITE), ErrorCode, SubErrorCode, USB_HostState);
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
}
/** Task to set the configuration of the attached device after it has been enumerated, and to read in blocks from
* the device and print them to the serial port.
*/
void MassStorageHost_Task(void)
{
if (USB_HostState != HOST_STATE_Configured)
return;
/* Indicate device busy via the status LEDs */
LEDs_SetAllLEDs(LEDMASK_USB_BUSY);
uint8_t ErrorCode;
/* Send the request, display error and wait for device detach if request fails */
if ((ErrorCode = MassStore_GetMaxLUN(&MassStore_MaxLUNIndex)) != HOST_SENDCONTROL_Successful)
{
ShowDiskReadError(PSTR("Get Max LUN"), ErrorCode);
USB_Host_SetDeviceConfiguration(0);
return;
}
/* Print number of LUNs detected in the attached device */
printf_P(PSTR("Total LUNs: %d - Using first LUN in device.\r\n"), (MassStore_MaxLUNIndex + 1));
/* Reset the Mass Storage device interface, ready for use */
if ((ErrorCode = MassStore_MassStorageReset()) != HOST_SENDCONTROL_Successful)
{
ShowDiskReadError(PSTR("Mass Storage Reset"), ErrorCode);
USB_Host_SetDeviceConfiguration(0);
return;
}
/* Get sense data from the device - many devices will not accept any other commands until the sense data
* is read - both on start-up and after a failed command */
SCSI_Request_Sense_Response_t SenseData;
if ((ErrorCode = MassStore_RequestSense(0, &SenseData)) != 0)
{
ShowDiskReadError(PSTR("Request Sense"), ErrorCode);
USB_Host_SetDeviceConfiguration(0);
return;
}
/* Set the prevent removal flag for the device, allowing it to be accessed */
if ((ErrorCode = MassStore_PreventAllowMediumRemoval(0, true)) != 0)
{
ShowDiskReadError(PSTR("Prevent/Allow Medium Removal"), ErrorCode);
USB_Host_SetDeviceConfiguration(0);
return;
}
/* Get inquiry data from the device */
SCSI_Inquiry_Response_t InquiryData;
if ((ErrorCode = MassStore_Inquiry(0, &InquiryData)) != 0)
{
ShowDiskReadError(PSTR("Inquiry"), ErrorCode);
USB_Host_SetDeviceConfiguration(0);
return;
}
/* Print vendor and product names of attached device */
printf_P(PSTR("Vendor \"%.8s\", Product \"%.16s\"\r\n"), InquiryData.VendorID, InquiryData.ProductID);
/* Wait until disk ready */
puts_P(PSTR("Waiting until ready.."));
for (;;)
{
Serial_SendByte('.');
/* Abort if device removed */
if (USB_HostState == HOST_STATE_Unattached)
break;
/* Check to see if the attached device is ready for new commands */
ErrorCode = MassStore_TestUnitReady(0);
/* If attached device is ready, abort the loop */
if (!(ErrorCode))
break;
/* If an error other than a logical command failure (indicating device busy) returned, abort */
if (ErrorCode != MASS_STORE_SCSI_COMMAND_FAILED)
{
ShowDiskReadError(PSTR("Test Unit Ready"), ErrorCode);
USB_Host_SetDeviceConfiguration(0);
return;
}
}
puts_P(PSTR("\r\nRetrieving Capacity... "));
/* Create new structure for the disk's capacity in blocks and block size */
SCSI_Capacity_t DiskCapacity;
/* Retrieve disk capacity */
if ((ErrorCode = MassStore_ReadCapacity(0, &DiskCapacity)) != 0)
{
ShowDiskReadError(PSTR("Read Capacity"), ErrorCode);
USB_Host_SetDeviceConfiguration(0);
return;
}
/* Display the disk capacity in blocks * block size bytes */
printf_P(PSTR("%lu blocks of %lu bytes.\r\n"), DiskCapacity.Blocks, DiskCapacity.BlockSize);
/* Create a new buffer capable of holding a single block from the device */
uint8_t BlockBuffer[DiskCapacity.BlockSize];
/* Read in the first 512 byte block from the device */
if ((ErrorCode = MassStore_ReadDeviceBlock(0, 0x00000000, 1, DiskCapacity.BlockSize, BlockBuffer)) != 0)
{
ShowDiskReadError(PSTR("Read Device Block"), ErrorCode);
USB_Host_SetDeviceConfiguration(0);
return;
}
puts_P(PSTR("\r\nContents of first block:\r\n"));
/* Print out the first block in both HEX and ASCII, 16 bytes per line */
for (uint16_t Chunk = 0; Chunk < (DiskCapacity.BlockSize >> 4); Chunk++)
{
/* Pointer to the start of the current 16-byte chunk in the read block of data */
uint8_t* ChunkPtr = &BlockBuffer[Chunk << 4];
/* Print out the 16 bytes of the chunk in HEX format */
for (uint8_t ByteOffset = 0; ByteOffset < (1 << 4); ByteOffset++)
{
char CurrByte = *(ChunkPtr + ByteOffset);
printf_P(PSTR("%.2X "), CurrByte);
}
puts_P(PSTR(" "));
/* Print out the 16 bytes of the chunk in ASCII format */
for (uint8_t ByteOffset = 0; ByteOffset < (1 << 4); ByteOffset++)
{
char CurrByte = *(ChunkPtr + ByteOffset);
putchar(isprint(CurrByte) ? CurrByte : '.');
}
puts_P(PSTR("\r\n"));
}
puts_P(PSTR("\r\n\r\nPress board button to read entire ASCII contents of disk...\r\n\r\n"));
/* Wait for the board button to be pressed */
while (!(Buttons_GetStatus() & BUTTONS_BUTTON1))
{
/* Abort if device removed */
if (USB_HostState == HOST_STATE_Unattached)
return;
}
/* Print out the entire disk contents in ASCII format */
for (uint32_t CurrBlockAddress = 0; CurrBlockAddress < DiskCapacity.Blocks; CurrBlockAddress++)
{
/* Read in the next block of data from the device */
if ((ErrorCode = MassStore_ReadDeviceBlock(0, CurrBlockAddress, 1, DiskCapacity.BlockSize, BlockBuffer)) != 0)
{
ShowDiskReadError(PSTR("Read Device Block"), ErrorCode);
USB_Host_SetDeviceConfiguration(0);
return;
}
/* Send the ASCII data in the read in block to the serial port */
for (uint16_t Byte = 0; Byte < DiskCapacity.BlockSize; Byte++)
{
char CurrByte = BlockBuffer[Byte];
putchar(isprint(CurrByte) ? CurrByte : '.');
}
}
/* Indicate device no longer busy */
LEDs_SetAllLEDs(LEDMASK_USB_READY);
USB_Host_SetDeviceConfiguration(0);
}
/** Indicates that a communication error has occurred with the attached Mass Storage Device,
* printing error codes to the serial port and waiting until the device is removed before
* continuing.
*
* \param[in] CommandString ASCII string located in PROGMEM space indicating what operation failed
* \param[in] ErrorCode Error code of the function which failed to complete successfully
*/
void ShowDiskReadError(const char* CommandString,
const uint8_t ErrorCode)
{
if (ErrorCode == MASS_STORE_SCSI_COMMAND_FAILED)
{
/* Display the error code */
printf_P(PSTR(ESC_FG_RED "SCSI command error (%S).\r\n"), CommandString);
}
else
{
/* Display the error code */
printf_P(PSTR(ESC_FG_RED "Command error (%S).\r\n"), CommandString);
printf_P(PSTR(" -- Error Code: %d" ESC_FG_WHITE), ErrorCode);
}
Pipe_Freeze();
/* Indicate device error via the status LEDs */
LEDs_SetAllLEDs(LEDMASK_USB_ERROR);
}

View File

@ -0,0 +1,91 @@
/*
LUFA Library
Copyright (C) Dean Camera, 2014.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
*/
/*
Copyright 2014 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 disclaims all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
/** \file
*
* Header file for MassStoreHost.c.
*/
#ifndef _MASS_STORE_HOST_H_
#define _MASS_STORE_HOST_H_
/* Includes: */
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#include <avr/power.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "ConfigDescriptor.h"
#include "Lib/MassStoreCommands.h"
#include <LUFA/Drivers/Misc/TerminalCodes.h>
#include <LUFA/Drivers/USB/USB.h>
#include <LUFA/Drivers/Peripheral/Serial.h>
#include <LUFA/Drivers/Board/LEDs.h>
#include <LUFA/Drivers/Board/Buttons.h>
#include <LUFA/Platform/Platform.h>
/* Macros: */
/** LED mask for the library LED driver, to indicate that the USB interface is not ready. */
#define LEDMASK_USB_NOTREADY LEDS_LED1
/** LED mask for the library LED driver, to indicate that the USB interface is enumerating. */
#define LEDMASK_USB_ENUMERATING (LEDS_LED2 | LEDS_LED3)
/** LED mask for the library LED driver, to indicate that the USB interface is ready. */
#define LEDMASK_USB_READY (LEDS_LED2 | LEDS_LED4)
/** LED mask for the library LED driver, to indicate that an error has occurred in the USB interface. */
#define LEDMASK_USB_ERROR (LEDS_LED1 | LEDS_LED3)
/** LED mask for the library LED driver, to indicate that the USB interface is busy. */
#define LEDMASK_USB_BUSY LEDS_LED2
/* Function Prototypes: */
void SetupHardware(void);
void MassStorageHost_Task(void);
void EVENT_USB_Host_HostError(const uint8_t ErrorCode);
void EVENT_USB_Host_DeviceAttached(void);
void EVENT_USB_Host_DeviceUnattached(void);
void EVENT_USB_Host_DeviceEnumerationFailed(const uint8_t ErrorCode,
const uint8_t SubErrorCode);
void EVENT_USB_Host_DeviceEnumerationComplete(void);
void ShowDiskReadError(const char* CommandString,
const uint8_t ErrorCode);
#endif

View File

@ -0,0 +1,68 @@
/** \file
*
* This file contains special DoxyGen information for the generation of the main page and other special
* documentation pages. It is not a project source file.
*/
/** \mainpage Mass Storage Host Demo
*
* \section Sec_Compat Demo Compatibility:
*
* The following list indicates what microcontrollers are compatible with this demo.
*
* \li Series 7 USB AVRs (AT90USBxxx7)
*
* \section Sec_Info USB Information:
*
* The following table gives a rundown of the USB utilization of this demo.
*
* <table>
* <tr>
* <td><b>USB Mode:</b></td>
* <td>Host</td>
* </tr>
* <tr>
* <td><b>USB Class:</b></td>
* <td>Mass Storage Device</td>
* </tr>
* <tr>
* <td><b>USB Subclass:</b></td>
* <td>Bulk Only</td>
* </tr>
* <tr>
* <td><b>Relevant Standards:</b></td>
* <td>USBIF Mass Storage Standard \n
* USB Bulk-Only Transport Standard \n
* SCSI Primary Commands Specification \n
* SCSI Block Commands Specification</td>
* </tr>
* <tr>
* <td><b>Supported USB Speeds:</b></td>
* <td>Full Speed Mode</td>
* </tr>
* </table>
*
* \section Sec_Description Project Description:
*
* Mass Storage host demonstration application. This gives a simple reference
* application for implementing a USB Mass Storage host, for USB storage devices
* using the standard Mass Storage USB profile.
*
* The first 512 bytes (boot sector) of an attached disk's memory will be dumped
* out of the serial port in HEX and ASCII form when it is attached to the AT90USB1287
* AVR. The device will then wait for HWB to be pressed, whereupon the entire ASCII contents
* of the disk will be dumped to the serial port.
*
* \section Sec_Options Project Options
*
* The following defines can be found in this demo, which can control the demo behaviour when defined, or changed in value.
*
* <table>
* <tr>
* <td>
* None
* </td>
* </tr>
* </table>
*/

View File

@ -0,0 +1,53 @@
<asf xmlversion="1.0">
<project caption="Mass Storage Host Demo (Low Level APIs)" id="lufa.demos.host.lowlevel.ms.example.avr8">
<require idref="lufa.demos.host.lowlevel.ms"/>
<require idref="lufa.boards.dummy.avr8"/>
<generator value="as5_8"/>
<device-support value="at90usb1287"/>
<config name="lufa.drivers.board.name" value="none"/>
<build type="define" name="F_CPU" value="16000000UL"/>
<build type="define" name="F_USB" value="16000000UL"/>
</project>
<module type="application" id="lufa.demos.host.lowlevel.ms" caption="Mass Storage Host Demo (Low Level APIs)">
<info type="description" value="summary">
Mass Storage Host demo, capable of reading and writing raw 512 byte segments to the device's serial port. This demo uses the Low Level LUFA APIs to manually implement a USB Class for demonstration purposes without using the simpler in-built LUFA Class Driver APIs.
</info>
<info type="gui-flag" value="move-to-root"/>
<info type="keyword" value="Technology">
<keyword value="Low Level APIs"/>
<keyword value="USB Host"/>
<keyword value="Mass Storage Class"/>
</info>
<device-support-alias value="lufa_avr8"/>
<device-support-alias value="lufa_xmega"/>
<device-support-alias value="lufa_uc3"/>
<build type="distribute" subtype="user-file" value="doxyfile"/>
<build type="distribute" subtype="user-file" value="MassStorageHost.txt"/>
<build type="c-source" value="MassStorageHost.c"/>
<build type="c-source" value="ConfigDescriptor.c"/>
<build type="header-file" value="MassStorageHost.h"/>
<build type="header-file" value="ConfigDescriptor.h"/>
<build type="c-source" value="Lib/MassStoreCommands.c"/>
<build type="header-file" value="Lib/MassStoreCommands.h"/>
<build type="module-config" subtype="path" value="Config"/>
<build type="header-file" value="Config/LUFAConfig.h"/>
<require idref="lufa.common"/>
<require idref="lufa.platform"/>
<require idref="lufa.drivers.usb"/>
<require idref="lufa.drivers.peripheral.usart"/>
<require idref="lufa.drivers.misc.ansi"/>
<require idref="lufa.drivers.board"/>
<require idref="lufa.drivers.board.leds"/>
</module>
</asf>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,38 @@
#
# LUFA Library
# Copyright (C) Dean Camera, 2014.
#
# dean [at] fourwalledcubicle [dot] com
# www.lufa-lib.org
#
# --------------------------------------
# LUFA Project Makefile.
# --------------------------------------
# Run "make help" for target help.
MCU = at90usb1287
ARCH = AVR8
BOARD = USBKEY
F_CPU = 8000000
F_USB = $(F_CPU)
OPTIMIZATION = s
TARGET = MassStorageHost
SRC = $(TARGET).c ConfigDescriptor.c Lib/MassStoreCommands.c $(LUFA_SRC_USB) $(LUFA_SRC_SERIAL)
LUFA_PATH = ../../../../LUFA
CC_FLAGS = -DUSE_LUFA_CONFIG_HEADER -IConfig/
LD_FLAGS =
# Default target
all:
# Include LUFA build script makefiles
include $(LUFA_PATH)/Build/lufa_core.mk
include $(LUFA_PATH)/Build/lufa_sources.mk
include $(LUFA_PATH)/Build/lufa_build.mk
include $(LUFA_PATH)/Build/lufa_cppcheck.mk
include $(LUFA_PATH)/Build/lufa_doxygen.mk
include $(LUFA_PATH)/Build/lufa_dfu.mk
include $(LUFA_PATH)/Build/lufa_hid.mk
include $(LUFA_PATH)/Build/lufa_avrdude.mk
include $(LUFA_PATH)/Build/lufa_atprogram.mk