00001
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 #include "conf_usb.h"
00051
00052
00053 #if USB_DEVICE_FEATURE == ENABLED
00054
00055 #include "board.h"
00056 #include "FreeRTOS.h"
00057 #include "semphr.h"
00058 #include "task.h"
00059 #include "usb_drv.h"
00060 #include "usb_descriptors.h"
00061 #include "usb_standard_request.h"
00062 #include "ctrl_access.h"
00063 #include "scsi_decoder.h"
00064 #include "device_mass_storage_task.h"
00065
00066 #include "supervisor.h"
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 extern volatile Bool ms_multiple_drive;
00078
00080 extern xSemaphoreHandle xUSBMutex;
00081
00082 static U16 sof_cnt;
00083 static U32 dCBWTag;
00084
00085 U8 usb_LUN;
00086 U8 ms_endpoint;
00087 static portBASE_TYPE xGiveUsbMutex = pdFALSE;
00088
00089
00090 static void usb_mass_storage_cbw(void);
00091 static void usb_mass_storage_csw(void);
00092
00093
00098 void device_mass_storage_task_init(void)
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 }
00111
00112
00120 void device_mass_storage_task(void *pvParameters)
00121 {
00122 portTickType xLastWakeTime;
00123
00124 xLastWakeTime = xTaskGetTickCount();
00125 while (TRUE)
00126 {
00127 vTaskDelayUntil(&xLastWakeTime, configTSK_USB_DMS_PERIOD);
00128
00129
00130 if (!Is_device_enumerated()) continue;
00131
00132
00133
00134
00135
00136 if (Is_usb_out_received(EP_MS_OUT))
00137 {
00138 usb_mass_storage_cbw();
00139 usb_mass_storage_csw();
00140 }
00141 }
00142 }
00143
00144
00152 void usb_sof_action(void)
00153 {
00154 sof_cnt++;
00155 }
00156
00157
00163 static void usb_mass_storage_cbw(void)
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
00216
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 }
00223
00224
00230 static void usb_mass_storage_csw(void)
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
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
00267 while( 0 != Usb_nb_busy_bank(EP_MS_IN) )
00268 {
00269 if (Is_usb_setup_received()) usb_process_request();
00270 }
00271
00272
00273
00274 if( TRUE == xGiveUsbMutex )
00275 {
00276 x_supervisor_SemaphoreGive( xUSBMutex );
00277 }
00278 }
00279
00280
00281 #endif // USB_DEVICE_FEATURE == ENABLED