This file manages the USB device mass-storage task.
Definition in file device_mass_storage_task.c.
#include "conf_usb.h"
#include "board.h"
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.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"
#include "supervisor.h"
Go to the source code of this file.
Functions | |
void | device_mass_storage_task (void *pvParameters) |
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 |
static portBASE_TYPE | xGiveUsbMutex = pdFALSE |
xSemaphoreHandle | xUSBMutex |
void device_mass_storage_task | ( | void * | pvParameters | ) |
Entry point of the device mass-storage task management.
This function links the device mass-storage SCSI commands to the USB bus.
pvParameters | Input. Unused. |
Definition at line 120 of file device_mass_storage_task.c.
References configTSK_USB_DMS_PERIOD, EP_MS_OUT, usb_mass_storage_cbw(), and usb_mass_storage_csw().
Referenced by device_mass_storage_task_init().
00121 { 00122 portTickType xLastWakeTime; 00123 00124 xLastWakeTime = xTaskGetTickCount(); 00125 while (TRUE) 00126 { 00127 vTaskDelayUntil(&xLastWakeTime, configTSK_USB_DMS_PERIOD); 00128 00129 // First, check the device enumeration state 00130 if (!Is_device_enumerated()) continue; 00131 00132 // Display Start-of-Frame counter on LEDs 00133 // LED_Display_Field(0x7E, sof_cnt >> 5); 00134 00135 // If we receive something in the OUT endpoint, parse it 00136 if (Is_usb_out_received(EP_MS_OUT)) 00137 { 00138 usb_mass_storage_cbw(); 00139 usb_mass_storage_csw(); 00140 } 00141 } 00142 }
void device_mass_storage_task_init | ( | void | ) |
This function initializes the hardware/software resources required for device mass-storage task.
Definition at line 98 of file device_mass_storage_task.c.
References configTSK_USB_DMS_NAME, configTSK_USB_DMS_PRIORITY, configTSK_USB_DMS_STACK_SIZE, device_mass_storage_task(), EP_MS_IN, EP_MS_OUT, and sof_cnt.
Referenced by b_usbsys_start().
00099 { 00100 g_scsi_ep_ms_in = EP_MS_IN; 00101 g_scsi_ep_ms_out = EP_MS_OUT; 00102 sof_cnt = 0; 00103 00104 xTaskCreate(device_mass_storage_task, 00105 configTSK_USB_DMS_NAME, 00106 configTSK_USB_DMS_STACK_SIZE, 00107 NULL, 00108 configTSK_USB_DMS_PRIORITY, 00109 NULL); 00110 }
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 163 of file device_mass_storage_task.c.
References dCBWTag, EP_MS_IN, EP_MS_OUT, ms_endpoint, ms_multiple_drive, usb_LUN, x_supervisor_SemaphoreTake(), xGiveUsbMutex, and xUSBMutex.
Referenced by device_mass_storage_task().
00164 { 00165 Bool cbw_error; 00166 00167 Usb_reset_endpoint_fifo_access(EP_MS_OUT); 00168 00170 cbw_error = (Usb_read_endpoint_data(EP_MS_OUT, 32) != *(U32 *)&"USBC"); 00171 00173 dCBWTag = Usb_read_endpoint_data(EP_MS_OUT, 32); 00174 00175 g_scsi_data_remaining = Usb_read_endpoint_data(EP_MS_OUT, 32); 00176 g_scsi_data_remaining = usb_format_usb_to_mcu_data(32, g_scsi_data_remaining); 00177 00179 if (Usb_read_endpoint_data(EP_MS_OUT, 8)) 00180 { 00181 ms_endpoint = EP_MS_IN; 00182 if (cbw_error) 00183 { 00184 Usb_ack_out_received_free(EP_MS_OUT); 00185 Usb_enable_stall_handshake(EP_MS_IN); 00186 return; 00187 } 00188 } 00189 else 00190 { 00191 ms_endpoint = EP_MS_OUT; 00192 if (cbw_error) 00193 { 00194 Usb_enable_stall_handshake(EP_MS_OUT); 00195 Usb_ack_out_received_free(EP_MS_OUT); 00196 return; 00197 } 00198 } 00199 00200 usb_LUN = Usb_read_endpoint_data(EP_MS_OUT, 8); 00201 00202 if (!ms_multiple_drive) 00203 { 00204 usb_LUN = get_cur_lun(); 00205 } 00206 00208 Usb_read_endpoint_data(EP_MS_OUT, 8); 00209 00211 usb_read_ep_rxpacket(EP_MS_OUT, g_scsi_command, sizeof(g_scsi_command), NULL); 00212 00213 Usb_ack_out_received_free(EP_MS_OUT); 00214 00215 // Take the USB Mutex(i.e. we're allowed to perform a ms cmd). 00216 /* if( ( pdFALSE == ( xGiveUsbMutex = x_supervisor_SemaphoreTake( xUSBMutex, 0 ) ) ) || ( !scsi_decode_command() && g_scsi_data_remaining ) ) */ 00217 if( ( pdFALSE == (xGiveUsbMutex = x_supervisor_SemaphoreTake( xUSBMutex, 0 ) ) ) 00218 || ( !scsi_decode_command() && g_scsi_data_remaining ) ) 00219 { 00220 Usb_enable_stall_handshake(ms_endpoint); 00221 } 00222 }
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 230 of file device_mass_storage_task.c.
References dCBWTag, EP_MS_IN, EP_MS_OUT, x_supervisor_SemaphoreGive(), xGiveUsbMutex, and xUSBMutex.
Referenced by device_mass_storage_task().
00231 { 00232 while (Is_usb_endpoint_stall_requested(EP_MS_IN)) 00233 { 00234 if (Is_usb_setup_received()) usb_process_request(); 00235 } 00236 00237 while (Is_usb_endpoint_stall_requested(EP_MS_OUT)) 00238 { 00239 if (Is_usb_setup_received()) usb_process_request(); 00240 } 00241 00242 // MSC Compliance - Free BAD out receiv during SCSI command 00243 while( Is_usb_out_received(EP_MS_OUT) ) { 00244 Usb_ack_out_received_free(EP_MS_OUT); 00245 } 00246 00247 while (!Is_usb_in_ready(EP_MS_IN)); 00248 00249 Usb_reset_endpoint_fifo_access(EP_MS_IN); 00250 00252 Usb_write_endpoint_data(EP_MS_IN, 32, *(U32 *)&"USBS"); 00253 00255 Usb_write_endpoint_data(EP_MS_IN, 32, dCBWTag); 00256 00258 Usb_write_endpoint_data(EP_MS_IN, 32, 00259 usb_format_mcu_to_usb_data(32, g_scsi_data_remaining)); 00260 00262 Usb_write_endpoint_data(EP_MS_IN, 8, g_scsi_status); 00263 00264 Usb_ack_in_ready_send(EP_MS_IN); 00265 00266 // MSC Compliance - Wait end of all transmitions on USB line 00267 while( 0 != Usb_nb_busy_bank(EP_MS_IN) ) 00268 { 00269 if (Is_usb_setup_received()) usb_process_request(); 00270 } 00271 00272 00273 // Release the USB Mutex. 00274 if( TRUE == xGiveUsbMutex ) 00275 { 00276 x_supervisor_SemaphoreGive( xUSBMutex ); 00277 } 00278 }
U32 dCBWTag [static] |
Definition at line 83 of file device_mass_storage_task.c.
Referenced by usb_mass_storage_cbw(), and usb_mass_storage_csw().
U8 ms_endpoint |
volatile Bool ms_multiple_drive |
Definition at line 67 of file usb_specific_request.c.
Referenced by usb_mass_storage_cbw(), usb_user_endpoint_init(), and usb_user_read_request().
U16 sof_cnt [static] |
Definition at line 82 of file device_mass_storage_task.c.
Referenced by device_mass_storage_task_init(), host_mass_storage_task_init(), host_sof_action(), and usb_sof_action().
U8 usb_LUN |
portBASE_TYPE xGiveUsbMutex = pdFALSE [static] |
Definition at line 87 of file device_mass_storage_task.c.
Referenced by usb_mass_storage_cbw(), and usb_mass_storage_csw().
xSemaphoreHandle xUSBMutex |
The USB system mutex.
Definition at line 187 of file supervisor.c.
Referenced by e_supervisor_switch_to_maintenance_mode(), portTASK_FUNCTION(), prv_v_common_leave_UsbDevice_mode(), usb_mass_storage_cbw(), usb_mass_storage_csw(), x_supervisor_SemaphoreGive(), and x_supervisor_SemaphoreTake().