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_HOST_FEATURE == ENABLED
00054
00055 #include "board.h"
00056 #include "FreeRTOS.h"
00057 #include "task.h"
00058 #include "conf_usb.h"
00059 #include "usb_drv.h"
00060 #include "usb_host_enum.h"
00061 #include "usb_host_task.h"
00062 #include "host_mem.h"
00063 #include "ctrl_access.h"
00064 #include "navigation.h"
00065 #include "fsaccess.h"
00066 #include "host_mass_storage_task.h"
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 #if configCTRLPANEL_TRACE == 1
00078 static const char log_ms_dev_connected[] = "Mass-storage device connected\n";
00079 #endif
00080
00081 static char ms_str[MAX_FILE_PATH_LENGTH];
00082 static U16 sof_cnt;
00083
00084 volatile Bool ms_new_device_connected;
00085 volatile Bool ms_connected;
00086
00087
00091 void host_mass_storage_task_init(void)
00092 {
00093 sof_cnt = 0;
00094 ms_new_device_connected = FALSE;
00095 ms_connected = FALSE;
00096
00097 xTaskCreate(host_mass_storage_task,
00098 configTSK_USB_HMS_NAME,
00099 configTSK_USB_HMS_STACK_SIZE,
00100 NULL,
00101 configTSK_USB_HMS_PRIORITY,
00102 NULL);
00103 }
00104
00105
00111 void host_mass_storage_task(void *pvParameters)
00112 {
00113 U8 i;
00114 U8 max_lun;
00115 U32 capacity;
00116
00117 portTickType xLastWakeTime;
00118
00119 xLastWakeTime = xTaskGetTickCount();
00120 while (TRUE)
00121 {
00122 vTaskDelayUntil(&xLastWakeTime, configTSK_USB_HMS_PERIOD);
00123
00124
00125
00126 if (Is_host_ready())
00127 {
00128
00129
00130
00131
00132 if (ms_new_device_connected)
00133 {
00134 ms_new_device_connected = FALSE;
00135
00136
00137 for (i = 0; i < Get_nb_supported_interface(); i++)
00138 {
00139
00140 if (Get_class(i) == MS_CLASS)
00141 {
00142 ms_connected = TRUE;
00143 LOG_STR(log_ms_dev_connected);
00144
00145
00146 if (Is_ep_in(i, 0))
00147 {
00148 g_pipe_ms_in = Get_ep_pipe(i, 0);
00149 g_pipe_ms_out = Get_ep_pipe(i, 1);
00150 }
00151 else
00152 {
00153 g_pipe_ms_in = Get_ep_pipe(i, 1);
00154 g_pipe_ms_out = Get_ep_pipe(i, 0);
00155 }
00156
00157
00158 max_lun = host_get_lun();
00159
00160
00161 for (host_selected_lun = 0; host_selected_lun < max_lun; host_selected_lun++)
00162 {
00163 host_ms_inquiry();
00164 host_read_capacity(host_selected_lun, &capacity);
00165 host_ms_request_sense();
00166
00167 for( i=0; i<3; i++)
00168 {
00169 if( host_test_unit_ready(host_selected_lun) == CTRL_GOOD )
00170 {
00171 host_read_capacity(host_selected_lun, &capacity);
00172 break;
00173 }
00174 }
00175 }
00176 break;
00177 }
00178 }
00179 }
00180 }
00181 }
00182 }
00183
00184
00192 void host_sof_action(void)
00193 {
00194 sof_cnt++;
00195 }
00196
00197
00208 Bool host_mass_storage_task_copy_tree( const char *pcdir_name, U8 sync_direction,
00209 Bool bDeleteSrc )
00210 {
00211 U8 u8_folder_level = 0;
00212 long nav_src = 0;
00213 long nav_dst = 0;
00214
00215 U8 u8SrcDrv = LUN_ID_MEM_USB;
00216 U8 u8DstDrv = LUN_ID_AT45DBX_MEM;
00217 U8 u8Status;
00218
00219
00220
00221 if( sync_direction & HOST_TO_DEVICE )
00222 {
00223 u8SrcDrv = LUN_ID_AT45DBX_MEM;
00224 u8DstDrv = LUN_ID_MEM_USB;
00225 }
00226
00227
00228
00229 nav_src = FS_NAV_ID_COPYFILE_TREE_SRC;
00230 nav_dst = FS_NAV_ID_COPYFILE_TREE_DEST;
00231
00232
00233 nav_select( nav_src );
00234 if( !nav_drive_set( u8SrcDrv ))
00235 return( FALSE );
00236 if( !nav_partition_mount() )
00237 return( FALSE );
00238 if( !nav_filelist_findname( (FS_STRING )pcdir_name , FALSE) )
00239 return( FALSE );
00240 if( !nav_dir_cd())
00241 return( FALSE );
00242
00243
00244 nav_select( nav_dst );
00245 if( !nav_drive_set( u8DstDrv ))
00246 return( FALSE );
00247 if( !nav_partition_mount() )
00248 return( FALSE );
00249
00250 if( !nav_dir_make( (FS_STRING )pcdir_name ))
00251 {
00252 if( FS_ERR_FILE_EXIST != fs_g_status )
00253 return( FALSE );
00254
00255 }
00256
00257 if( !nav_dir_cd())
00258 return( FALSE );
00259
00260
00261
00262 while(1)
00263 {
00264
00265 while(1)
00266 {
00267
00268 nav_select( nav_src );
00269 if( nav_filelist_set( 0 , FS_FIND_NEXT ) )
00270 break;
00271
00272
00273 if( 0 == u8_folder_level )
00274 return TRUE;
00275
00276
00277
00278
00279 u8_folder_level--;
00280 if( !nav_dir_gotoparent() )
00281 return( FALSE );
00282
00283 nav_select( nav_dst );
00284 if( !nav_dir_gotoparent() )
00285 return( FALSE );
00286 }
00287
00288 if( nav_file_isdir())
00289 {
00290
00291
00292 if( !nav_file_name( (FS_STRING )ms_str , MAX_FILE_PATH_LENGTH , FS_NAME_GET, FALSE ))
00293 return( FALSE );
00294
00295 if( !nav_dir_cd())
00296 return( FALSE );
00297 u8_folder_level++;
00298
00299 nav_select( nav_dst );
00300
00301 if( !nav_dir_make( (FS_STRING )ms_str ))
00302 {
00303 if( FS_ERR_FILE_EXIST != fs_g_status )
00304 return( FALSE );
00305
00306 }
00307
00308 if( !nav_dir_cd())
00309 {
00310 if( FS_ERR_NO_DIR == fs_g_status )
00311 {
00312
00313 }
00314 return( FALSE );
00315 }
00316
00317 }
00318 else
00319 {
00320
00321
00322 if( !nav_file_name( (FS_STRING )ms_str , MAX_FILE_PATH_LENGTH , FS_NAME_GET , FALSE ))
00323 return( FALSE );
00324 if( !nav_file_copy())
00325 return( FALSE );
00326
00327
00328 nav_select( nav_dst );
00329 while( !nav_file_paste_start( (FS_STRING)ms_str ) )
00330 {
00331
00332 if( fs_g_status != FS_ERR_FILE_EXIST )
00333 return( FALSE );
00334
00335
00336 if( !nav_file_del( TRUE ) )
00337 return( FALSE );
00338
00339 }
00340
00341 do{
00342 u8Status = nav_file_paste_state(FALSE);
00343 }while( COPY_BUSY == u8Status );
00344
00345 if( COPY_FINISH != u8Status )
00346 return( FALSE );
00347
00348 if( TRUE == bDeleteSrc )
00349 {
00350 nav_select( nav_src );
00351 nav_file_del( FALSE );
00352 }
00353 }
00354 }
00355 }
00356
00357
00370 Bool host_mass_storage_task_sync_drives( signed short FsNavId, U8 sync_direction,
00371 const char *pcdir_name, Bool bDeleteSrc )
00372 {
00373
00374
00375 if (!Is_host_ready()) return FALSE;
00376
00377 fsaccess_take_mutex();
00378
00379
00380 if (sync_direction & DEVICE_TO_HOST)
00381 {
00382 if( FALSE == host_mass_storage_task_copy_tree( pcdir_name, DEVICE_TO_HOST, bDeleteSrc ) )
00383 {
00384
00385 fsaccess_give_mutex();
00386 return FALSE;
00387 }
00388 }
00389
00390
00391 if (sync_direction & HOST_TO_DEVICE)
00392 {
00393 if( FALSE == host_mass_storage_task_copy_tree( pcdir_name, HOST_TO_DEVICE, bDeleteSrc ) )
00394 {
00395 fsaccess_give_mutex();
00396 return FALSE;
00397 }
00398 }
00399
00400 fsaccess_give_mutex();
00401 return TRUE;
00402 }
00403
00404
00405 #endif // USB_HOST_FEATURE == ENABLED
00406