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