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 U16 sof_cnt;
00079 static U32 dCBWTag;
00080
00081 U8 usb_LUN;
00082 U8 ms_endpoint;
00083
00084
00085 static void usb_mass_storage_cbw(void);
00086 static void usb_mass_storage_csw(void);
00087
00088
00093 void device_mass_storage_task_init(void)
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
00101
00102
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 }
00117
00118
00124 #ifdef FREERTOS_USED
00125 void device_mass_storage_task(void *pvParameters)
00126 #else
00127 void device_mass_storage_task(void)
00128 #endif
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
00139 if (!Is_device_enumerated()) continue;
00140 #else
00141
00142 if (!Is_device_enumerated()) return;
00143 #endif // FREERTOS_USED
00144
00145 #if BOARD == EVK1100
00146
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
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
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 }
00171
00172
00180 void usb_sof_action(void)
00181 {
00182 sof_cnt++;
00183 }
00184
00185
00191 static void usb_mass_storage_cbw(void)
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 }
00248
00249
00255 static void usb_mass_storage_csw(void)
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
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
00292 while( 0 != Usb_nb_busy_bank(EP_MS_IN) )
00293 {
00294 if (Is_usb_setup_received()) usb_process_request();
00295 }
00296 }
00297
00298
00299 #endif // USB_DEVICE_FEATURE == ENABLED