• AVR Freaks

Hot!How do you re-Open SD card file after repetition of attaching and detaching of SD card ?

Author
ysaitoysaito
Super Member
  • Total Posts : 405
  • Reward points : 0
  • Joined: 2010/08/13 08:31:35
  • Location: 0
  • Status: offline
2015/12/06 06:05:39 (permalink)
0

How do you re-Open SD card file after repetition of attaching and detaching of SD card ?


 I am studying SD card system by using ver.1.06_01 Harmony, ver.3.15 MPLABX, ver.1.40 XC32 and PIC32MZ2048EFH100.
I would like to open and read file in SD card after repetition of attaching and detaching of SD card, namely  I boot PIC32MZ , insert SD card in SD slot ,open file ,read file ,close file remove SD card, insert SD card ,open file, read file, close file, remove SD card, insert SD card, open file,.......      But I cannot open file, after  I boot PIC32MZ , insert SD card in SD slot ,open file ,read file ,close file remove SD card and insert SD card.  Because the command branches into the mode that SD card is not Mounted , namely "SYS_FS_Mount("/dev/mmcblka1", "/mnt/myDrive", FAT, 0, NULL) != SYS_FS_RES_SUCCESS" is true.


 I show my code in app.c as follows.


SYS_FS_HANDLE fileHandle;
SYS_FS_HANDLE dirHandle;
SYS_FS_FSTA stat;
char longFileName[300];



....
....
....

void APP_Tasks ( void )
{
    switch(appData.state)
    {
  case APP_STATE_INIT:
         if(SYS_FS_Mount("/dev/mmcblka1", "/mnt/myDrive", FAT, 0, NULL) != SYS_FS_RES_SUCCESS)
   {
             //SD card is not Mounted
   }
         else
   {
    //SD card is Mounted
    dirHandle = SYS_FS_DirOpen("/mnt/myDrive"); //Directory Open
    stat.lfname = longFileName;
    stat.lfsize = 300; //Set memory for LFN

    if(SYS_FS_DirSearch(dirHandle, "*.*", SYS_FS_ATTR_ARC, &stat) == SYS_FS_RES_FAILURE)
    {
                    // Specified file is not found
    }
    else
    {
     //Specified file is found

     fileHandle = SYS_FS_FileOpen(stat.lfname, SYS_FS_FILE_OPEN_READ); // File Open
     if(fileHandle == SYS_FS_HANDLE_INVALID)
     {
      // File Open is failure

     }
     else
     {
      // File Open is succesful
      SYS_FS_FileRead(fileHandle, Buf, 1);
      eof = SYS_FS_FileEOF(fileHandle);
      ....
      ....
      ....
  
                        SYS_FS_FileClose(fileHandle);
                        SYS_FS_DirClose(dirHandle);
     
      appData.state = APP_STATE_RUN;
     }
    }
         }
             break;
        
        case APP_STATE_RUN:
            
            if(PORTFbits.RF12 == 1) //1: Removement of SD card is Detected // RF12 is SD card detect port
            {
       appData.state = APP_STATE_INIT;
            }
           break;
        
        default:
            break;
    }
    
}
....
....
....

 

        


Should I add more code ?
Or Are there any mistake in my codes ?

Any advice make me pleased.

ysaito


 
#1

11 Replies Related Threads

    RISC
    Super Member
    • Total Posts : 5379
    • Reward points : 0
    • Status: offline
    Re: How do you re-Open SD card file after repetition of attaching and detaching of SD card 2015/12/06 08:25:28 (permalink)
    0
    Hi,
     
    Did you check the TCP/IP webserver example which uses SD card with FAT to store webpages :
    C:\microchip\harmony\v1_06_01\apps\tcpip\web_server_sdcard_fatfs\firmware
     
    You should find the code for accessing/reading files
     
    Regards
    #2
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re: How do you re-Open SD card file after repetition of attaching and detaching of SD card 2015/12/06 09:06:49 (permalink)
    0
    You may want to close file sorry, already done.
     
    Maybe wait until the sensor reverts back? and/or add some delay?

    GENOVA :D :D ! GODO
    #3
    ysaitoysaito
    Super Member
    • Total Posts : 405
    • Reward points : 0
    • Joined: 2010/08/13 08:31:35
    • Location: 0
    • Status: offline
    Re: How do you re-Open SD card file after repetition of attaching and detaching of SD card 2015/12/07 06:45:30 (permalink)
    4 (1)
    Dear RISC and DarioG

     Thank you very much for advice.
    Below works as what I want, but sometime (about 20-30%) it become fail. 

    ....
    ....
    ....


    void APP_Tasks ( void )
    {
       switch(appData.state)
        {
           case APP_STATE_INIT:
               if(PORTFbits.RF12 == 0) //Removement of SD card is Detected // RF12 is SD card detect port
               {
                  
         SYS_FS_Mount("/dev/mmcblka1", "/mnt/myDrive", FAT, 0, NULL); //Mount File system
                   
                 
        dirHandle = SYS_FS_DirOpen("/mnt/myDrive"); //Open directory

        stat.lfname = longFileName;
        stat.lfsize = 300;
        SYS_FS_DirSearch(dirHandle, "*.*", SYS_FS_ATTR_ARC, &stat); //Search file
                      
        fileHandle = SYS_FS_FileOpen(stat.lfname, SYS_FS_FILE_OPEN_READ); //File Open
        SYS_FS_FileRead(fileHandle, Buf, 1);
             eof = SYS_FS_FileEOF(fileHandle);
        ....
        ....
        ....
        
        SYS_FS_DirClose(dirHandle); //Close directry
        SYS_FS_FileClose(fileHandle); //Close File
                   
               }
               
               else
               {
                    
                   SYS_FS_FileClose(fileHandle);
                   SYS_FS_DirClose(dirHandle);

                   SYS_FS_Unmount("/mnt/myDrive"); //UnMount file system
                
               }
               
               
               
               break;

      default:
               break;
     }
    }

    ....
    ....
    ....
         


    I will try more trials.
     

    Best regards

    ysaito


    #4
    bugz
    Junior Member
    • Total Posts : 82
    • Reward points : 0
    • Joined: 2014/11/10 19:08:02
    • Location: 0
    • Status: offline
    Re: How do you re-Open SD card file after repetition of attaching and detaching of SD card 2015/12/07 20:35:58 (permalink)
    0
    Are you unmounting the file system?
    #5
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re: How do you re-Open SD card file after repetition of attaching and detaching of SD card 2015/12/08 04:17:38 (permalink)
    0
    So it seems:
                   SYS_FS_Unmount("/mnt/myDrive"); //UnMount file system

    GENOVA :D :D ! GODO
    #6
    tixmcprods
    New Member
    • Total Posts : 12
    • Reward points : 0
    • Joined: 2019/06/12 15:33:51
    • Location: 0
    • Status: offline
    Re: How do you re-Open SD card file after repetition of attaching and detaching of SD card 2019/06/14 05:14:17 (permalink)
    0
    Hello,
    Also have that problem around same ratio. Do you found a solution ?
    Happen with SYS_FS_FileOpen, not always.
    More details : https://www.microchip.com/forums/m1051951.aspx
    Thanks for your help !
    #7
    moser
    Super Member
    • Total Posts : 511
    • Reward points : 0
    • Joined: 2015/06/16 02:53:47
    • Location: Germany
    • Status: offline
    Re: How do you re-Open SD card file after repetition of attaching and detaching of SD card 2019/06/14 10:02:22 (permalink)
    0
    A long time ago I also had this problem, although I closed my files and unmounted. The problem was:
     
    SD card is attached --> PIC power up --> works --> detach --> attach --> doesn't work
    SD card is detached --> PIC power up --> attach --> works --> detach --> attach --> doesn't work
     
    and it always stayed like this.
     
    However, somebody made me aware that there is an auto mount feature in Harmony. It is not really in the documentation, but you can activate it with MHC and you need to define the mount points. Your application can register a handler of type 
    void APP_SYSFSEventHandler(SYS_FS_EVENT event, void * eventData, uintptr_t context)

    with function
    SYS_FS_EventHandlerSet(APP_SYSFSEventHandler, (uintptr_t)NULL);

     
    The handler is called for mount and unmount events. It can check the mounting point (by string comparison), and you could for example set or reset a "sd card is available" flag. The app code could then start using the FS after the flag is set, and immediately stop if the flag is reset.
     
     
    #8
    tixmcprods
    New Member
    • Total Posts : 12
    • Reward points : 0
    • Joined: 2019/06/12 15:33:51
    • Location: 0
    • Status: offline
    Re: How do you re-Open SD card file after repetition of attaching and detaching of SD card 2019/06/14 10:34:33 (permalink)
    0
    Hello,
    Thanks for your reply.
    The automount feature is already checked, and my SD Card accesses are OK, from SYS_FS_FileOpen and also from TCP/IP File Wrapper. Both are working well. Added a subfolder "web" for wrapper, so have two root folder for web and other files.
    But, assuming I never detach SD Card, just want to know why sometimes it detach. Not always at the same time. A same file opened (and closed at each launch of course) often work, sometimes (around 20%) it fails and put a 1 second died time before restore and work again (during this dead time, all modules are idle (default states mode for app)).
    Is the automount detaching automatically ? Even if I'm not calling TCP/IP wrapper, it fail...
    I can assume to have a little to for auto-remounting but not 1 second...
    Of course too, have tested several SD Cards from different manufacturers, different classes and also on another copy of my custom board (fully manufactured).
    Tested different timings, sample points, etc... no change.
    Thanks for your help.
    Best regards.
    #9
    moser
    Super Member
    • Total Posts : 511
    • Reward points : 0
    • Joined: 2015/06/16 02:53:47
    • Location: Germany
    • Status: offline
    Re: How do you re-Open SD card file after repetition of attaching and detaching of SD card 2019/06/17 01:46:50 (permalink)
    4 (1)
    With automount you never need to call mount or unmount on your own. Your code above is not using automount. And it could be that the automount interferes with your own mounting. I believe you should not do both.
     
     
    When you card is detached, the driver unmounts it, and then APP_SYSFSEventHandler() gets called with event = SYS_FS_EVENT_UNMOUNT. If you do not remove your SD card then it should never happen. I don't know what could be the reason for this.
     
    How are you detecting hat the SD card fails? Is it by a network connection? If yes, are you sure it is the SD card and not something network related (e.g. running out of sockets / buffer space / file handles ... ) )?
    #10
    tixmcprods
    New Member
    • Total Posts : 12
    • Reward points : 0
    • Joined: 2019/06/12 15:33:51
    • Location: 0
    • Status: offline
    Re: How do you re-Open SD card file after repetition of attaching and detaching of SD card 2019/06/26 00:07:52 (permalink)
    0
    Hello,
     
    Of course, I use automount and don't mount in APP.
    Not tried yet FS event.
     
    Finally I have found something : I'm also using touch driver at the same time, sharing the same SPI bus, and had a CS confusion because used PLIB directly for touch and SD driver seems to lose the handle and try to reinitialize.
    It take a long time, that is the main purpose of the problem...
    I'm trying to make a DRV based driver for my touch driver, and stay a little problem with external source interrupt, that put low at middle of the read (after release high by the touch driver).
    See this post for details : https://www.microchip.com/forums/m1102853.aspx
     
    To be sure of SD driver, I disabled touch driver and SD card seems OK at 99.9% : it freeze after a lot of calls. I'll see if I have overflow somewhere...
     
    Thanks !
     
    Best regards.
    #11
    tixmcprods
    New Member
    • Total Posts : 12
    • Reward points : 0
    • Joined: 2019/06/12 15:33:51
    • Location: 0
    • Status: offline
    Re: How do you re-Open SD card file after repetition of attaching and detaching of SD card 2019/08/01 04:07:11 (permalink)
    0
    Hello,
    Answered to my own question. IRQ also happen when reading data.
    I just have to find how to disabled interrupt while reading.
    Thanks !
    #12
    Jump to:
    © 2019 APG vNext Commercial Version 4.5