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 #ifdef FREERTOS_USED
00057 #include "FreeRTOS.h"
00058 #include "task.h"
00059 #endif
00060 #include "usb_drv.h"
00061 #include "usb_descriptors.h"
00062 #include "usb_standard_request.h"
00063 #include "ctrl_access.h"
00064 #include "scsi_decoder.h"
00065 #include "device_mass_storage_task.h"
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 extern volatile Bool ms_multiple_drive;
00077
00078 static U32 dCBWTag;
00079
00080 U8 usb_LUN;
00081
00082
00083 static void usb_mass_storage_cbw(void);
00084 static void usb_mass_storage_csw(void);
00085
00086
00091 void device_mass_storage_task_init(void)
00092 {
00093 g_scsi_ep_ms_in = EP_MS_IN;
00094 g_scsi_ep_ms_out = EP_MS_OUT;
00095 #ifndef FREERTOS_USED
00096 #if USB_HOST_FEATURE == ENABLED
00097
00098
00099
00100 if (Is_usb_device())
00101 #endif // USB_HOST_FEATURE == ENABLED
00102 Usb_enable_sof_interrupt();
00103 #endif // FREERTOS_USED
00104
00105 #ifdef FREERTOS_USED
00106 xTaskCreate(device_mass_storage_task,
00107 configTSK_USB_DMS_NAME,
00108 configTSK_USB_DMS_STACK_SIZE,
00109 NULL,
00110 configTSK_USB_DMS_PRIORITY,
00111 NULL);
00112 #endif // FREERTOS_USED
00113 }
00114
00115
00121 #ifdef FREERTOS_USED
00122 void device_mass_storage_task(void *pvParameters)
00123 #else
00124 void device_mass_storage_task(void)
00125 #endif
00126 {
00127 #ifdef FREERTOS_USED
00128 portTickType xLastWakeTime;
00129
00130 xLastWakeTime = xTaskGetTickCount();
00131 while (TRUE)
00132 {
00133 vTaskDelayUntil(&xLastWakeTime, configTSK_USB_DMS_PERIOD);
00134
00135
00136 if (!Is_device_enumerated()) continue;
00137 #else
00138
00139 if (!Is_device_enumerated()) return;
00140 #endif // FREERTOS_USED
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 if (Is_usb_out_received(EP_MS_OUT))
00151 {
00152 usb_mass_storage_cbw();
00153 usb_mass_storage_csw();
00154 }
00155 #ifdef FREERTOS_USED
00156 }
00157 #endif
00158 }
00159
00160
00166 static void usb_mass_storage_cbw(void)
00167 {
00168 Bool cbw_error;
00169 U8 ms_endpoint;
00170
00171 Usb_reset_endpoint_fifo_access(EP_MS_OUT);
00172
00174 cbw_error = (Usb_read_endpoint_data(EP_MS_OUT, 32) != *(U32 *)&"USBC");
00175
00177 dCBWTag = Usb_read_endpoint_data(EP_MS_OUT, 32);
00178
00179 g_scsi_data_remaining = Usb_read_endpoint_data(EP_MS_OUT, 32);
00180 g_scsi_data_remaining = usb_format_usb_to_mcu_data(32, g_scsi_data_remaining);
00181
00183 if (Usb_read_endpoint_data(EP_MS_OUT, 8))
00184 {
00185 ms_endpoint = EP_MS_IN;
00186 if (cbw_error)
00187 {
00188 Usb_ack_out_received_free(EP_MS_OUT);
00189 Usb_enable_stall_handshake(EP_MS_IN);
00190 return;
00191 }
00192 }
00193 else
00194 {
00195 ms_endpoint = EP_MS_OUT;
00196 if (cbw_error)
00197 {
00198 Usb_enable_stall_handshake(EP_MS_OUT);
00199 Usb_ack_out_received_free(EP_MS_OUT);
00200 return;
00201 }
00202 }
00203
00204 usb_LUN = Usb_read_endpoint_data(EP_MS_OUT, 8);
00205
00206 if (!ms_multiple_drive)
00207 {
00208 usb_LUN = get_cur_lun();
00209 }
00210
00212 Usb_read_endpoint_data(EP_MS_OUT, 8);
00213
00215 usb_read_ep_rxpacket(EP_MS_OUT, g_scsi_command, sizeof(g_scsi_command), NULL);
00216
00217 Usb_ack_out_received_free(EP_MS_OUT);
00218
00219 if (!scsi_decode_command() && g_scsi_data_remaining)
00220 {
00221 Usb_enable_stall_handshake(ms_endpoint);
00222 }
00223 }
00224
00225
00231 static void usb_mass_storage_csw(void)
00232 {
00233 while (Is_usb_endpoint_stall_requested(EP_MS_IN))
00234 {
00235 if (Is_usb_setup_received()) usb_process_request();
00236 }
00237
00238 while (Is_usb_endpoint_stall_requested(EP_MS_OUT))
00239 {
00240 if (Is_usb_setup_received()) usb_process_request();
00241 }
00242
00243 while (!Is_usb_in_ready(EP_MS_IN));
00244
00245 Usb_reset_endpoint_fifo_access(EP_MS_IN);
00246
00248 Usb_write_endpoint_data(EP_MS_IN, 32, *(U32 *)&"USBS");
00249
00251 Usb_write_endpoint_data(EP_MS_IN, 32, dCBWTag);
00252
00254 Usb_write_endpoint_data(EP_MS_IN, 32,
00255 usb_format_mcu_to_usb_data(32, g_scsi_data_remaining));
00256
00258 Usb_write_endpoint_data(EP_MS_IN, 8, g_scsi_status);
00259
00260 Usb_ack_in_ready_send(EP_MS_IN);
00261 }
00262
00263
00264 #endif // USB_DEVICE_FEATURE == ENABLED