device_mass_storage_task.c File Reference


Detailed Description

Management of the USB device mass-storage task.

This file manages the USB device mass-storage task.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file device_mass_storage_task.c.

#include "conf_usb.h"
#include "board.h"
#include "usb_drv.h"
#include "usb_descriptors.h"
#include "usb_standard_request.h"
#include "ctrl_access.h"
#include "scsi_decoder.h"
#include "device_mass_storage_task.h"

Go to the source code of this file.

Functions

void device_mass_storage_task (void)
 Entry point of the device mass-storage task management.
void device_mass_storage_task_init (void)
 This function initializes the hardware/software resources required for device mass-storage task.
static void usb_mass_storage_cbw (void)
 USB Command Block Wrapper (CBW) management.
static void usb_mass_storage_csw (void)
 USB Command Status Wrapper (CSW) management.
void usb_sof_action (void)
 usb_sof_action

Variables

static U32 dCBWTag
U8 ms_endpoint
volatile Bool ms_multiple_drive
static U16 sof_cnt
U8 usb_LUN


Function Documentation

void device_mass_storage_task ( void   ) 

Entry point of the device mass-storage task management.

This function links the device mass-storage SCSI commands to the USB bus.

Definition at line 127 of file device_mass_storage_task.c.

References EP_MS_OUT, sof_cnt, usb_mass_storage_cbw(), and usb_mass_storage_csw().

Referenced by device_mass_storage_task_init(), and main().

00129 {
00130 #ifdef FREERTOS_USED
00131   portTickType xLastWakeTime;
00132 
00133   xLastWakeTime = xTaskGetTickCount();
00134   while (TRUE)
00135   {
00136     vTaskDelayUntil(&xLastWakeTime, configTSK_USB_DMS_PERIOD);
00137 
00138     // First, check the device enumeration state
00139     if (!Is_device_enumerated()) continue;
00140 #else
00141     // First, check the device enumeration state
00142     if (!Is_device_enumerated()) return;
00143 #endif  // FREERTOS_USED
00144 
00145 #if BOARD == EVK1100
00146     // Display Start-of-Frame counter on LEDs
00147     LED_Display_Field(LED_MONO0_GREEN |
00148                       LED_MONO1_GREEN |
00149                       LED_MONO2_GREEN |
00150                       LED_MONO3_GREEN,
00151                       sof_cnt >> 5);
00152 #elif BOARD == EVK1101 || BOARD == EVK1104 || BOARD == EVK1105
00153     // Display Start-of-Frame counter on LEDs
00154     LED_Display_Field(LED0 |
00155                       LED1,
00156                       sof_cnt >> 5);
00157 #else
00158   #error The display of the SOFs must be defined here.
00159 #endif
00160 
00161     // If we receive something in the OUT endpoint, parse it
00162     if (Is_usb_out_received(EP_MS_OUT))
00163     {
00164       usb_mass_storage_cbw();
00165       usb_mass_storage_csw();
00166     }
00167 #ifdef FREERTOS_USED
00168   }
00169 #endif
00170 }

void device_mass_storage_task_init ( void   ) 

This function initializes the hardware/software resources required for device mass-storage task.

Definition at line 93 of file device_mass_storage_task.c.

References device_mass_storage_task(), EP_MS_IN, EP_MS_OUT, and sof_cnt.

Referenced by main().

00094 {
00095   g_scsi_ep_ms_in = EP_MS_IN;
00096   g_scsi_ep_ms_out = EP_MS_OUT;
00097   sof_cnt = 0;
00098 #ifndef FREERTOS_USED
00099   #if USB_HOST_FEATURE == ENABLED
00100   // If both device and host features are enabled, check if device mode is engaged
00101   // (accessing the USB registers of a non-engaged mode, even with load operations,
00102   // may corrupt USB FIFO data).
00103   if (Is_usb_device())
00104   #endif  // USB_HOST_FEATURE == ENABLED
00105     Usb_enable_sof_interrupt();
00106 #endif  // FREERTOS_USED
00107 
00108 #ifdef FREERTOS_USED
00109   xTaskCreate(device_mass_storage_task,
00110               configTSK_USB_DMS_NAME,
00111               configTSK_USB_DMS_STACK_SIZE,
00112               NULL,
00113               configTSK_USB_DMS_PRIORITY,
00114               NULL);
00115 #endif  // FREERTOS_USED
00116 }

static void usb_mass_storage_cbw ( void   )  [static]

USB Command Block Wrapper (CBW) management.

This function decodes the CBW command and stores the SCSI command.

Check if dCBWSignature is correct

Store CBW Tag to be repeated in CSW

if (bmCBWFlags.bit7 == 1) {direction = IN;}

Dummy CBWCBLength read

Store scsi_command

Definition at line 191 of file device_mass_storage_task.c.

References dCBWTag, EP_MS_IN, EP_MS_OUT, ms_endpoint, ms_multiple_drive, and usb_LUN.

Referenced by device_mass_storage_task().

00192 {
00193   Bool cbw_error;
00194 
00195   Usb_reset_endpoint_fifo_access(EP_MS_OUT);
00196 
00198   cbw_error = (Usb_read_endpoint_data(EP_MS_OUT, 32) != *(U32 *)&"USBC");
00199 
00201   dCBWTag = Usb_read_endpoint_data(EP_MS_OUT, 32);
00202 
00203   g_scsi_data_remaining = Usb_read_endpoint_data(EP_MS_OUT, 32);
00204   g_scsi_data_remaining = usb_format_usb_to_mcu_data(32, g_scsi_data_remaining);
00205 
00207   if (Usb_read_endpoint_data(EP_MS_OUT, 8))
00208   {
00209     ms_endpoint = EP_MS_IN;
00210     if (cbw_error)
00211     {
00212       Usb_ack_out_received_free(EP_MS_OUT);
00213       Usb_enable_stall_handshake(EP_MS_IN);
00214       return;
00215     }
00216   }
00217   else
00218   {
00219     ms_endpoint = EP_MS_OUT;
00220     if (cbw_error)
00221     {
00222       Usb_enable_stall_handshake(EP_MS_OUT);
00223       Usb_ack_out_received_free(EP_MS_OUT);
00224       return;
00225     }
00226   }
00227 
00228   usb_LUN = Usb_read_endpoint_data(EP_MS_OUT, 8);
00229 
00230   if (!ms_multiple_drive)
00231   {
00232     usb_LUN = get_cur_lun();
00233   }
00234 
00236   Usb_read_endpoint_data(EP_MS_OUT, 8);
00237 
00239   usb_read_ep_rxpacket(EP_MS_OUT, g_scsi_command, sizeof(g_scsi_command), NULL);
00240 
00241   Usb_ack_out_received_free(EP_MS_OUT);
00242 
00243   if (!scsi_decode_command())
00244   {
00245     Usb_enable_stall_handshake(ms_endpoint);
00246   }
00247 }

static void usb_mass_storage_csw ( void   )  [static]

USB Command Status Wrapper (CSW) management.

This function sends the status in relation with the last CBW.

Write CSW Signature

Write stored CBW Tag

Write data residual value

Write command status

Definition at line 255 of file device_mass_storage_task.c.

References dCBWTag, EP_MS_IN, and EP_MS_OUT.

Referenced by device_mass_storage_task().

00256 {
00257   while (Is_usb_endpoint_stall_requested(EP_MS_IN))
00258   {
00259     if (Is_usb_setup_received()) usb_process_request();
00260   }
00261 
00262   while (Is_usb_endpoint_stall_requested(EP_MS_OUT))
00263   {
00264     if (Is_usb_setup_received()) usb_process_request();
00265   }
00266 
00267   // MSC Compliance - Free BAD out receiv during SCSI command
00268   while( Is_usb_out_received(EP_MS_OUT) ) {
00269     Usb_ack_out_received_free(EP_MS_OUT);
00270   }
00271   
00272   while (!Is_usb_in_ready(EP_MS_IN));
00273 
00274   Usb_reset_endpoint_fifo_access(EP_MS_IN);
00275 
00277   Usb_write_endpoint_data(EP_MS_IN, 32, *(U32 *)&"USBS");
00278 
00280   Usb_write_endpoint_data(EP_MS_IN, 32, dCBWTag);
00281 
00283   Usb_write_endpoint_data(EP_MS_IN, 32,
00284                           usb_format_mcu_to_usb_data(32, g_scsi_data_remaining));
00285 
00287   Usb_write_endpoint_data(EP_MS_IN, 8, g_scsi_status);
00288 
00289   Usb_ack_in_ready_send(EP_MS_IN);
00290   
00291   // MSC Compliance - Wait end of all transmitions on USB line
00292   while( 0 != Usb_nb_busy_bank(EP_MS_IN) )
00293   {
00294     if (Is_usb_setup_received()) usb_process_request();
00295   }
00296 }


Variable Documentation

U32 dCBWTag [static]

Definition at line 79 of file device_mass_storage_task.c.

Referenced by usb_mass_storage_cbw(), and usb_mass_storage_csw().

Definition at line 82 of file device_mass_storage_task.c.

Referenced by usb_mass_storage_cbw().

volatile Bool ms_multiple_drive

U16 sof_cnt [static]

U8 usb_LUN

Definition at line 81 of file device_mass_storage_task.c.

Referenced by usb_mass_storage_cbw().


Generated on Fri Feb 19 02:22:54 2010 for AVR32 - EVK1104 Demo by  doxygen 1.5.5