• Main Page
  • Related Pages
  • Data Structures
  • Files
  • File List
  • Globals

fs.c

Go to the documentation of this file.
00001 /****************** COPYRIGHT (C) 2007-2010 RAISONANCE S.A.S. *****************/
00014 /*******************************************************************************/
00015 
00016 /* Includes ------------------------------------------------------------------*/
00017 #include "circle.h"
00018 
00019 #if SDCARD_SDIO
00020 #include "sdio_sd.h"
00021 #endif
00022 #include "fs.h"
00023 #include <string.h>
00024 
00026 
00027 /* Private typedef -----------------------------------------------------------*/
00028 
00029 /* Private define ------------------------------------------------------------*/
00030 #define MAX_DIRS 30
00031 #define MAX_PATH_LENGTH 200
00032 
00033 /* Private macro -------------------------------------------------------------*/
00034 
00035 /* Private variables ---------------------------------------------------------*/
00036 //static u8  sector[SECTOR_SIZE];
00037 static union array
00038 {
00039     u8 sector[SECTOR_SIZE];    // the char array
00040     unsigned int  i;           // force 32-bit int alignment
00041 } u;
00042 static u32 StartMBR;
00043 static u8  SDCardfault = 0;
00044 
00045 /* Public variables ----------------------------------------------------------*/
00046 VOLINFO volume_info;
00047 DIRINFO directory_info;
00048 DIRENT directory_entry;
00049 u8* FS_PathFilter = 0;
00050 
00051 struct DirItem
00052 {
00053     u8 Name[14];
00054     u8 flags;
00055 };
00056 
00057 struct DirItem DirItems[MAX_DIRS];
00058 tList DirMenu =    {    1,    "SDCard",    30,    0, 0, 0, 0, 0,    6,    0,    0,    };
00059 
00060 u8 level = 0;
00061 u8 CurrentPath[MAX_PATH_LENGTH];
00062 
00063 
00064 /* Private functions ----------------------------------------------------------*/
00065 void NVIC_Config_SDIO( void );
00066 
00067 /*******************************************************************************
00068 *
00069 *                            FS_Explorer_UpdateList
00070 *
00071 *******************************************************************************/
00079 NODEBUG2 void FS_Explorer_UpdateList( void )
00080 {
00081     u8 i = 0;
00082     u32 res;
00083 
00084     DFS_memset( ( void* ) DirItems, 0, sizeof( DirItems ) );
00085 
00086     /* Loop through root dir and display files / dirs*/
00087     while ( !( res = FS_GetNextEntry( &volume_info, &directory_info, &directory_entry ) ) )
00088     {
00089         if ( directory_entry.name[0] )
00090         {
00091             /* Directory*/
00092             if ( directory_entry.attr & FS_ATTR_DIRECTORY )
00093             {
00094                 if ( strncmp( directory_entry.name, ". ", 2 ) ) /* don't display current "." directory*/
00095                 {
00096                     strcat( DirItems[i].Name, "[" );
00097                     strncat( DirItems[i].Name, directory_entry.name, 11 );
00098                     strcat( DirItems[i].Name, "]" );
00099 
00100                     DirMenu.Items[i].Text = DirItems[i].Name;
00101                     DirItems[i].flags |= FS_ATTR_DIRECTORY; /* set flag in local list*/
00102                     i++;
00103                 }
00104             }
00105             else /* File*/
00106             {
00107                 /* Check if matches the type filter*/
00108                 if ( ( FS_PathFilter == 0 ) || ( strncmp( directory_entry.name + 8, FS_PathFilter, 3 ) == 0 ) )
00109                 {
00110                     DFS_strncpy( DirItems[i].Name, directory_entry.name, 11 );
00111                     DirMenu.Items[i].Text = DirItems[i].Name;
00112                     i++;
00113                 }
00114             }
00115 
00116             if ( i >= MAX_DIRS )
00117                 break;
00118         }
00119 
00120 
00121     }
00122 
00123     if ( i == 0 ) /* empty dir ?*/
00124     {
00125         DFS_memset( ( void* )DirItems[0].Name, 0, 11 );
00126         DFS_strcpy( DirItems[i].Name, "[..]" );
00127         DirMenu.Items[i].Text = DirItems[i].Name;
00128         DirItems[i].flags |= FS_ATTR_DIRECTORY; /* set flag in local list*/
00129         i++;
00130     }
00131 
00132     /* Set number of items in list (i + cde "Quit")*/
00133     DirMenu.Items[i].Text = "Quit";
00134     i++;
00135     DirMenu.NbItems = i;
00136     DirMenu.NbDisp = i > 7 ? 7 : i;
00137 
00138     DirMenu.XPos  = 0;
00139     DirMenu.YPos  = 150;
00140     DirMenu.XSize = 128;
00141     DirMenu.YSize = 140;
00142 }
00143 
00144 
00146 
00147 /* Public functions ----------------------------------------------------------*/
00148 
00149 /*******************************************************************************
00150 *
00151 *                            FS_Mount
00152 *
00153 *******************************************************************************/
00163 /*******************************************************************************/
00164 u32 FS_Mount( enum STORAGE_device device )
00165 {
00166 #if SDCARD_SDIO
00167     NVIC_Config_SDIO();
00168 #endif
00169     return DFS_Mount( device );
00170 }
00171 
00172 
00173 /*******************************************************************************
00174 *
00175 *                            FS_Unmount
00176 *
00177 *******************************************************************************/
00187 /*******************************************************************************/
00188 u32 FS_Unmount( enum STORAGE_device device )
00189 {
00190     return DFS_UnMount( device );
00191 }
00192 
00193 
00194 /*******************************************************************************
00195 *
00196 *                            FS_OpenFile
00197 *
00198 *******************************************************************************/
00227 /*******************************************************************************/
00228 u32 FS_OpenFile( PVOLINFO volinfo, u8* path, u8 mode, PFILEINFO fileinfo )
00229 {
00230     return DFS_OpenFile( volinfo, path, mode, u.sector, fileinfo );
00231 }
00232 
00233 
00234 
00235 /*******************************************************************************
00236 *
00237 *                            FS_ReadFile
00238 *
00239 *******************************************************************************/
00251 /*******************************************************************************/
00252 u32 FS_ReadFile( PFILEINFO fileinfo,  u8* buffer, u32* successcount, u32 len )
00253 {
00254     return DFS_ReadFile( fileinfo, u.sector, buffer, successcount, len );
00255 }
00256 
00257 
00258 /*******************************************************************************
00259 *
00260 *                            FS_WriteFile
00261 *
00262 *******************************************************************************/
00294 /*******************************************************************************/
00295 u32 FS_WriteFile( PFILEINFO fileinfo,  u8* buffer, u32* successcount, u32 len )
00296 {
00297     return DFS_WriteFile( fileinfo, u.sector, buffer, successcount, len );
00298 }
00299 
00300 
00301 /*******************************************************************************
00302 *
00303 *                            FS_Close
00304 *
00305 *******************************************************************************/
00315 /*******************************************************************************/
00316 u32 FS_Close( PFILEINFO fileinfo )
00317 {
00318     /* not implemented ;*/
00319 }
00320 
00321 
00322 /*******************************************************************************
00323 *
00324 *                            FS_Seek
00325 *
00326 *******************************************************************************/
00336 /*******************************************************************************/
00337 void FS_Seek( PFILEINFO fileinfo, u32 offset )
00338 {
00339     DFS_Seek( fileinfo, offset, u.sector );
00340 }
00341 
00342 
00343 /*******************************************************************************
00344 *
00345 *                            FS_Delete
00346 *
00347 *******************************************************************************/
00361 /*******************************************************************************/
00362 u32 FS_Delete( PVOLINFO volinfo, u8* path )
00363 {
00364     return DFS_UnlinkFile( volinfo, path, u.sector );
00365 }
00366 
00367 
00368 /*******************************************************************************
00369 *
00370 *                            FS_GetNextEntry
00371 *
00372 *******************************************************************************/
00395 /*******************************************************************************/
00396 u32 FS_GetNextEntry( PVOLINFO volinfo, PDIRINFO dirinfo, PDIRENT dirent )
00397 {
00398     return DFS_GetNext( volinfo, dirinfo, dirent );
00399 }
00400 
00401 
00402 /*******************************************************************************
00403 *
00404 *                            FS_OpenDirectory
00405 *
00406 *******************************************************************************/
00422 /*******************************************************************************/
00423 u32 FS_OpenDirectory( PVOLINFO volinfo, u8* dirname, PDIRINFO dirinfo )
00424 {
00425     dirinfo->scratch = u.sector;  /* DFS Note - you must PREPOPULATE*/
00426     /* the DIRINFO.scratch field with a pointer to a sector scratch buffer.*/
00427     return DFS_OpenDir( volinfo, dirname, dirinfo );
00428 }
00429 
00430 
00431 /*******************************************************************************
00432 *
00433 *                            FS_GetVolumeInfo
00434 *
00435 *******************************************************************************/
00451 /*******************************************************************************/
00452 u32 FS_GetVolumeInfo( u8 unit, u32 startsector, PVOLINFO volinfo )
00453 {
00454     return DFS_GetVolInfo( unit, u.sector, startsector, volinfo );
00455 }
00456 
00457 /*******************************************************************************
00458 *
00459 *                            FS_Explorer_Ini
00460 *
00461 *******************************************************************************/
00470 enum MENU_code FS_Explorer_Ini( void )
00471 {
00472 
00473     DFS_memset( ( void* ) CurrentPath, 0, sizeof( CurrentPath ) );
00474 
00475     SDCardfault = 0;
00476 
00477     SD_DeInit();
00478 
00479     /* Mount MMCSD */
00480     StartMBR = FS_Mount( MMCSD_SDIO );
00481 
00482     if ( StartMBR == 0xFFFFFFFF )
00483     {
00484         // Try another time (pb in SPI mode) 
00485         StartMBR = FS_Mount( MMCSD_SDIO );
00486         if ( StartMBR == 0xFFFFFFFF )
00487         {
00488             DirMenu.Items[0].Text = "No SDCARD";
00489             SDCardfault = 1;
00490         }
00491     }
00492 
00493     if ( !SDCardfault )
00494     {
00495         /* Open volume on first partition (0)*/
00496         if ( FS_GetVolumeInfo( 0, StartMBR, &volume_info ) )
00497         {
00498             DirMenu.Items[0].Text = "Err: GetVolInfo";
00499             SDCardfault = 1;
00500         }
00501     }
00502 
00503     if ( !SDCardfault )
00504     {
00505         /* Open root directory*/
00506         if ( FS_OpenDirectory( &volume_info, "", &directory_info ) )
00507         {
00508             DirMenu.Items[0].Text = "Err: Open Root";
00509             SDCardfault = 1;
00510         }
00511     }
00512 
00513     if ( SDCardfault )
00514     {
00515         /* 2 items : error msg + quit*/
00516         DirMenu.NbItems = 2;
00517         DirMenu.Items[1].Text = "Quit";
00518 
00519         /* Show list*/
00520         LIST_Set( &DirMenu, 0, 0, 1 );
00521     }
00522     else
00523     {
00524 
00525         /* Populate the list*/
00526         FS_Explorer_UpdateList();
00527 
00528         MENU_SetAppliDivider( 10 );
00529 
00530         /* Show list*/
00531         LIST_Set( &DirMenu, 0, 0, 1 );
00532 
00533     }
00534 
00535     return MENU_CONTINUE_COMMAND;
00536 
00537 }
00538 
00539 
00540 /*******************************************************************************
00541 *
00542 *                            FS_Explorer
00543 *
00544 *******************************************************************************/
00559 s32 FS_Explorer( void )
00560 {
00561     s32 i;
00562     u32 retval;
00563     u8 EOP = 0;
00564 
00565     /* Call the List manager of the current list*/
00566     i = LIST_Manager();
00567 
00568     /* If item selected*/
00569     if ( i != -1 )
00570     {
00571 
00572         /* If choice different than quit and card OK*/
00573         if ( ( i != ( DirMenu.NbItems - 1 ) ) && !SDCardfault )
00574         {
00575             u8 dir[MAX_PATH_LENGTH];
00576             DFS_memset( dir, 0, sizeof( dir ) );
00577 
00578             /* Directory selected from list*/
00579             if ( DirItems[i].flags & FS_ATTR_DIRECTORY )
00580             {
00581                 /* Check if we want to go up a level*/
00582                 if ( !( strncmp( DirItems[i].Name + 1, "..", 2 ) ) )
00583                 {
00584                     u8* ptr = CurrentPath + ( sizeof( CurrentPath ) - 1 );
00585 
00586 
00587                     /* Remove directory name from CurrentPath*/
00588                     for ( i = MAX_PATH_LENGTH; i > 0; i-- )
00589                     {
00590                         if ( ( *ptr == '/' ) && ( !EOP ) )
00591                         {
00592                             /* Found end of current path    */
00593                             EOP = 1;
00594                         }
00595                         else if ( ( *ptr == '/' ) && ( EOP ) )
00596                         {
00597                             break; /* erased subdir and found //*/
00598                         }
00599                         *ptr-- = 0;
00600                     }
00601                     DFS_strcpy( dir, CurrentPath );
00602                 }
00603                 else
00604                 {
00605                     DFS_strcpy( dir, CurrentPath );
00606                     strncat( dir, DirItems[i].Name + 1, 11 ); /* strip '[ ]'*/
00607                 }
00608 
00609                 /* Open directory*/
00610                 if ( retval = FS_OpenDirectory( &volume_info, dir, &directory_info ) )
00611                 {
00612                     /* Cannot open dir*/
00613                     /* add error handling. Go back to root for now*/
00614                     if ( retval == 3 )
00615                     {
00616                         FS_OpenDirectory( &volume_info, "", &directory_info );
00617                     }
00618                     /* Reset CurrentPath                */
00619                     DFS_memset( ( void* ) CurrentPath, 0, sizeof( CurrentPath ) );
00620                 }
00621                 else
00622                 {
00623                     /* Check if we didn't go up*/
00624                     if ( !EOP )
00625                     {
00626                         u8 len = my_strlen( CurrentPath );
00627                         u8 j;
00628 
00629 
00630                         /* Remove trailing spaces*/
00631                         for ( j = len; j > 0; j-- )
00632                         {
00633                             if ( CurrentPath[j] == 0x20 )
00634                                 CurrentPath[j] = 0;
00635                         }
00636 
00637                         /* Add new dir to current path*/
00638                         strncat( CurrentPath, DirItems[i].Name + 1, 11 ); /* strip '[ ]'*/
00639 
00640                         len = my_strlen( CurrentPath );
00641                         /* Remove trailing spaces again*/
00642                         for ( j = len; j > 0; j-- )
00643                         {
00644                             if ( CurrentPath[j] == 0x20 )
00645                                 CurrentPath[j] = 0;
00646                         }
00647 
00648                         /* Add dir seperator*/
00649                         strcat( CurrentPath, "/" );
00650                     }
00651                 }
00652 
00653                 /* Update directory list*/
00654                 FS_Explorer_UpdateList();
00655 
00656                 /* Clear display*/
00657                 DRAW_Clear();
00658 
00659                 /* Show list*/
00660                 LIST_Set( &DirMenu, 0, 0, 1 );
00661 
00662             } /* end (DirItems[i].flags & FS_ATTR_DIRECTORY)*/
00663             else
00664                 /* File selected from list*/
00665             {
00666                 /* Add file name to the path*/
00667                 strncat( CurrentPath, DirItems[i].Name, 11 );
00668                 FS_Unmount( MMCSD_SDIO );
00669                 return 1;
00670             }
00671 
00672         } /* end (i != (DirMenu.NbItems - 1) )  && !SDCardfault)*/
00673         else
00674         {
00675             /* Quit requested*/
00676             FS_Unmount( MMCSD_SDIO );
00677             return 0;
00678         }
00679 
00680     } /* end (i != -1)*/
00681 
00682     return -1;
00683 }
00684 
00685 /*******************************************************************************
00686 *
00687 *                            FS_GetSDCardCurrentPath
00688 *
00689 *******************************************************************************/
00699 u8* FS_GetSDCardCurrentPath( void )
00700 {
00701     return ( u8* ) &CurrentPath;
00702 }
00703 
00704 
00705 /*******************************************************************************
00706 *
00707 *                            FS_GetSDCardVolInfo
00708 *
00709 *******************************************************************************/
00721 VOLINFO* FS_GetSDCardVolInfo( void )
00722 {
00723     return &volume_info;
00724 }
00725 
00726 /*******************************************************************************
00727 *
00728 *                            FS_GetPathFilter
00729 *
00730 *******************************************************************************/
00738 u8* FS_GetPathFilter( void )
00739 {
00740     return FS_PathFilter;
00741 }
00742 
00743 /*******************************************************************************
00744 *
00745 *                            FS_SetPathFilter
00746 *
00747 *******************************************************************************/
00758 void FS_SetPathFilter( u8* filter )
00759 {
00760     if ( filter )
00761         FS_PathFilter = strupr( filter );
00762     else
00763         FS_PathFilter = 0;
00764 }
00765 
00767 
00768 #if SDCARD_SDIO
00769 /*******************************************************************************
00770 * Function Name  : NVIC_Config_SDIO
00771 * Description    : Configures SDIO IRQ channel.
00772 * Input          : None
00773 * Output         : None
00774 * Return         : None
00775 *******************************************************************************/
00776 NODEBUG2 void NVIC_Config_SDIO( void )
00777 {
00778     NVIC_InitTypeDef NVIC_InitStructure;
00779 
00780     /* Configure the NVIC Preemption Priority Bits */
00781     NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1 );
00782 
00783     NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
00784     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
00785     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
00786     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00787     NVIC_Init( &NVIC_InitStructure );
00788 }
00789 #endif
00790