@ -67,93 +67,41 @@ int main(void)
for ( ; ; )
{
switch ( USB_HostState )
{
case HOST_STATE_Addressed :
LEDs_SetAllLEDs ( LEDMASK_USB_ENUMERATING ) ;
uint16_t ConfigDescriptorSize ;
uint8_t ConfigDescriptorData [ 512 ] ;
MassStorageHost_Task ( ) ;
if ( USB_Host_GetDeviceConfigDescriptor ( 1 , & ConfigDescriptorSize , ConfigDescriptorData ,
sizeof ( ConfigDescriptorData ) ) ! = HOST_GETCONFIG_Successful )
{
puts_P ( PSTR ( " Error Retrieving Configuration Descriptor. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
USB_HostState = HOST_STATE_WaitForDeviceRemoval ;
break ;
MS_Host_USBTask ( & FlashDisk_MS_Interface ) ;
USB_USBTask ( ) ;
}
if ( MS_Host_ConfigurePipes ( & FlashDisk_MS_Interface ,
ConfigDescriptorSize , ConfigDescriptorData ) ! = MS_ENUMERROR_NoError )
{
puts_P ( PSTR ( " Attached Device Not a Valid Mass Storage Device. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
USB_HostState = HOST_STATE_WaitForDeviceRemoval ;
break ;
}
if ( USB_Host_SetDeviceConfiguration ( 1 ) ! = HOST_SENDCONTROL_Successful )
{
puts_P ( PSTR ( " Error Setting Device Configuration. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
USB_HostState = HOST_STATE_WaitForDeviceRemoval ;
break ;
}
puts_P ( PSTR ( " Mass Storage Device Enumerated. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_READY ) ;
USB_HostState = HOST_STATE_Configured ;
break ;
case HOST_STATE_Configured :
LEDs_SetAllLEDs ( LEDMASK_USB_BUSY ) ;
uint8_t MaxLUNIndex ;
if ( MS_Host_GetMaxLUN ( & FlashDisk_MS_Interface , & MaxLUNIndex ) )
/** Configures the board hardware and chip peripherals for the demo's functionality. */
void SetupHardware ( void )
{
puts_P ( PSTR ( " Error retrieving max LUN index. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
USB_HostState = HOST_STATE_WaitForDeviceRemoval ;
break ;
}
printf_P ( PSTR ( " Total LUNs: %d - Using first LUN in device. \r \n " ) , ( MaxLUNIndex + 1 ) ) ;
/* Disable watchdog if enabled by bootloader/fuses */
MCUSR & = ~ ( 1 < < WDRF ) ;
wdt_disable ( ) ;
if ( MS_Host_ResetMSInterface ( & FlashDisk_MS_Interface ) )
{
puts_P ( PSTR ( " Error resetting Mass Storage interface. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
USB_HostState = HOST_STATE_WaitForDeviceRemoval ;
break ;
}
/* Disable clock division */
clock_prescale_set ( clock_div_1 ) ;
SCSI_Request_Sense_Response_t SenseData ;
if ( MS_Host_RequestSense ( & FlashDisk_MS_Interface , 0 , & SenseData ) ! = 0 )
{
puts_P ( PSTR ( " Error retrieving device sense. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
USB_HostState = HOST_STATE_WaitForDeviceRemoval ;
break ;
}
/* Hardware Initialization */
Serial_Init ( 9600 , false ) ;
LEDs_Init ( ) ;
USB_Init ( ) ;
if ( MS_Host_PreventAllowMediumRemoval ( & FlashDisk_MS_Interface , 0 , true ) )
{
puts_P ( PSTR ( " Error setting Prevent Device Removal bit. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
USB_HostState = HOST_STATE_WaitForDeviceRemoval ;
break ;
/* Create a stdio stream for the serial port for stdin and stdout */
Serial_CreateStream ( NULL ) ;
}
SCSI_Inquiry_Response_t InquiryData ;
if ( MS_Host_GetInquiryData ( & FlashDisk_MS_Interface , 0 , & InquiryData ) )
/** Task to manage an enumerated USB Mass Storage device once connected, to print out
* data from the device .
*/
void MassStorageHost_Task ( void )
{
puts_P ( PSTR ( " Error retrieving device Inquiry data. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
USB_HostState = HOST_STATE_WaitForDeviceRemoval ;
break ;
}
if ( USB_HostState ! = HOST_STATE_Configured )
return ;
printf_P ( PSTR ( " Vendor \" %.8s \" , Product \" %.16s \" \r \n " ) , InquiryData . VendorID , InquiryData . ProductID ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_BUSY ) ;
puts_P ( PSTR ( " Waiting until ready... \r \n " ) ) ;
@ -169,8 +117,8 @@ int main(void)
{
puts_P ( PSTR ( " Error waiting for device to be ready. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
USB_HostState = HOST_STATE_WaitForDeviceRemoval ;
break ;
USB_Host_SetDeviceConfiguration ( 0 ) ;
return ;
}
}
@ -181,8 +129,8 @@ int main(void)
{
puts_P ( PSTR ( " Error retrieving device capacity. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
USB_HostState = HOST_STATE_WaitForDeviceRemoval ;
break ;
USB_Host_SetDeviceConfiguration ( 0 ) ;
return ;
}
printf_P ( PSTR ( " %lu blocks of %lu bytes. \r \n " ) , DiskCapacity . Blocks , DiskCapacity . BlockSize ) ;
@ -193,8 +141,8 @@ int main(void)
{
puts_P ( PSTR ( " Error reading device block. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
USB_HostState = HOST_STATE_WaitForDeviceRemoval ;
break ;
USB_Host_SetDeviceConfiguration ( 0 ) ;
return ;
}
puts_P ( PSTR ( " \r \n Contents of first block: \r \n " ) ) ;
@ -223,32 +171,7 @@ int main(void)
}
LEDs_SetAllLEDs ( LEDMASK_USB_READY ) ;
USB_HostState = HOST_STATE_WaitForDeviceRemoval ;
break ;
}
MS_Host_USBTask ( & FlashDisk_MS_Interface ) ;
USB_USBTask ( ) ;
}
}
/** Configures the board hardware and chip peripherals for the demo's functionality. */
void SetupHardware ( void )
{
/* Disable watchdog if enabled by bootloader/fuses */
MCUSR & = ~ ( 1 < < WDRF ) ;
wdt_disable ( ) ;
/* Disable clock division */
clock_prescale_set ( clock_div_1 ) ;
/* Hardware Initialization */
Serial_Init ( 9600 , false ) ;
LEDs_Init ( ) ;
USB_Init ( ) ;
/* Create a stdio stream for the serial port for stdin and stdout */
Serial_CreateStream ( NULL ) ;
USB_Host_SetDeviceConfiguration ( 0 ) ;
}
/** Event handler for the USB_DeviceAttached event. This indicates that a device has been attached to the host, and
@ -274,6 +197,82 @@ void EVENT_USB_Host_DeviceUnattached(void)
*/
void EVENT_USB_Host_DeviceEnumerationComplete ( void )
{
LEDs_SetAllLEDs ( LEDMASK_USB_ENUMERATING ) ;
uint16_t ConfigDescriptorSize ;
uint8_t ConfigDescriptorData [ 512 ] ;
if ( USB_Host_GetDeviceConfigDescriptor ( 1 , & ConfigDescriptorSize , ConfigDescriptorData ,
sizeof ( ConfigDescriptorData ) ) ! = HOST_GETCONFIG_Successful )
{
puts_P ( PSTR ( " Error Retrieving Configuration Descriptor. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
return ;
}
if ( MS_Host_ConfigurePipes ( & FlashDisk_MS_Interface ,
ConfigDescriptorSize , ConfigDescriptorData ) ! = MS_ENUMERROR_NoError )
{
puts_P ( PSTR ( " Attached Device Not a Valid Mass Storage Device. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
return ;
}
if ( USB_Host_SetDeviceConfiguration ( 1 ) ! = HOST_SENDCONTROL_Successful )
{
puts_P ( PSTR ( " Error Setting Device Configuration. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
return ;
}
uint8_t MaxLUNIndex ;
if ( MS_Host_GetMaxLUN ( & FlashDisk_MS_Interface , & MaxLUNIndex ) )
{
puts_P ( PSTR ( " Error retrieving max LUN index. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
USB_Host_SetDeviceConfiguration ( 0 ) ;
return ;
}
printf_P ( PSTR ( " Total LUNs: %d - Using first LUN in device. \r \n " ) , ( MaxLUNIndex + 1 ) ) ;
if ( MS_Host_ResetMSInterface ( & FlashDisk_MS_Interface ) )
{
puts_P ( PSTR ( " Error resetting Mass Storage interface. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
USB_Host_SetDeviceConfiguration ( 0 ) ;
return ;
}
SCSI_Request_Sense_Response_t SenseData ;
if ( MS_Host_RequestSense ( & FlashDisk_MS_Interface , 0 , & SenseData ) ! = 0 )
{
puts_P ( PSTR ( " Error retrieving device sense. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
USB_Host_SetDeviceConfiguration ( 0 ) ;
return ;
}
if ( MS_Host_PreventAllowMediumRemoval ( & FlashDisk_MS_Interface , 0 , true ) )
{
puts_P ( PSTR ( " Error setting Prevent Device Removal bit. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
USB_Host_SetDeviceConfiguration ( 0 ) ;
return ;
}
SCSI_Inquiry_Response_t InquiryData ;
if ( MS_Host_GetInquiryData ( & FlashDisk_MS_Interface , 0 , & InquiryData ) )
{
puts_P ( PSTR ( " Error retrieving device Inquiry data. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_ERROR ) ;
USB_Host_SetDeviceConfiguration ( 0 ) ;
return ;
}
printf_P ( PSTR ( " Vendor \" %.8s \" , Product \" %.16s \" \r \n " ) , InquiryData . VendorID , InquiryData . ProductID ) ;
puts_P ( PSTR ( " Mass Storage Device Enumerated. \r \n " ) ) ;
LEDs_SetAllLEDs ( LEDMASK_USB_READY ) ;
}