• AVR Freaks

Hot!File System (SYS_FS) stuck after 512 bytes read

Author
Vincent44
Starting Member
  • Total Posts : 47
  • Reward points : 0
  • Joined: 2014/09/19 03:41:47
  • Location: France
  • Status: offline
2020/07/01 05:32:11 (permalink)
0

File System (SYS_FS) stuck after 512 bytes read

Hello,
 
I am working on a project with a custom board, using a PIC32MX470F512L), that can manage both SD card and USB stick.
This main app is pretty advanced, and is working working fine. I know for a fact that there is not hardware issue on this board, and that read and writes works fine on both the SD and the USB MSD.
 
I am now working on the bootloader. I started to develop it so that i take the source file from the SD card, and updates PIC32's FLASH. Everything is working fine !
 
I now need to change the source media, from SD to USB stick. In Harmony v3, I simply swap the (SPI + SD CARD) blocs by the (USB FULL SPEED + HOST LAYER + MSD CLIENT) blocs.
 
 
The parameter "Media Type" from FILE SYSTEM is updated from "SD" to "MSD", and the command USB_HOST_BusEnable(0) is added at the begining of my program, to allow the USB to start.
The rest of my program, remains unchanged.
 
It's all working fine, the USB stick is detected and I start reading the source file ... until 512 bytes have been read.
After 512 byte read, the program will get stuck at the next call of SYS_FS_FileRead(). It's going back and forth in functions generated by Harmony (USB, File System, ...), but never returns.
The problem occurs, no matter the way I read : 512 readings of 1 bytes, 4 readings of 128 bytes, 1 reading of 512 ... All produce the same result.
 
The only clue I have so far is that the "Size of bloc" in the FILE SYSTEM  entity is 512. But it's also the case when using the SD card, so I am not sure it's related.
 
Any hints ?
 
Thanks !

Attached Image(s)

#1

7 Replies Related Threads

    optimus_jack
    Senior Member
    • Total Posts : 100
    • Reward points : 0
    • Joined: 2017/02/16 03:02:47
    • Location: 0
    • Status: offline
    Re: File System (SYS_FS) stuck after 512 bytes read 2020/07/01 05:59:03 (permalink)
    0
    Hi,
    I don't see any issues on the File System Configurations you have done. I have a MSD based bootloader on PIC32MX470 Curiosity board and it works fine for me.
     
    You may need to wait for bus to be enabled using below API before proceeding further if not already done before if (USB_HOST_BusIsEnabled(0) == true)
    {
        //Wait for SYS_FS_EVENT_MOUNT event
    }
     
    You can refer to one of the MSD examples https://github.com/Microchip-MPLAB-Harmony/usb/tree/master/apps/host/msd_basic/firmware/pic32mx470_curiosity.X for any configurations related issues
    #2
    Vincent44
    Starting Member
    • Total Posts : 47
    • Reward points : 0
    • Joined: 2014/09/19 03:41:47
    • Location: France
    • Status: offline
    Re: File System (SYS_FS) stuck after 512 bytes read 2020/07/02 06:16:30 (permalink)
    0
    optimus_jack, thanks for your answer.
    I added the USB_HOST_BusIsEnabled() condition to my state machine, but it did not help.


    After some more investigation, I suspect an incompatibility with the NVM module.
    Here is what my bootloader state machine is doing:
    #1 Wait for USB media to mount.
    #2 Read a 5 char at the begining of the file on the media (to check that the file contains a new firmware).
    #3 Erase the PIC32 flash, by pages of 4kB using NVM_PageErase()
    #4 Read the rest of the file, and program the new firmware in the flash.
     
    Eventhough my step #3 is made is a non blocking manner by letting the SYS_Task() to exectue between each call of NVM_PageErase(), my state machine is waiting ~25ms before exiting because of a while(NVM_IsBusy()).
     
    After that, I am only able to read the remaing 507 char. I am guessing that they have been loaded in an internal buffer during step #2.
     
    If I remove step #3 (for tests), I have no problem reading the entire file.
    It looks like getting stuck for "a long time" in step #3 is "disconnecting" the USB stick, and get it stuck. The FILE SYSTEM does not see it as unmounted.
    However, the 25ms wait is a lot smaller than the 2 adjustable timings provided in the USB Full Speed Driver bloc
     
     
     

    Attached Image(s)

    #3
    optimus_jack
    Senior Member
    • Total Posts : 100
    • Reward points : 0
    • Joined: 2017/02/16 03:02:47
    • Location: 0
    • Status: offline
    Re: File System (SYS_FS) stuck after 512 bytes read 2020/07/02 07:57:10 (permalink)
    0
    Hi,
     
    When you call Erase function the MCU will be stalled as you are running from flash hence you are seeing it is stuck the task routine.
     
    Attaching my application file which i am using to receive a file over thumb drive and program.
    The attached code is placed from 0x9D000000 to 0x9D011000. Hope it helps.
    post edited by optimus_jack - 2020/07/02 07:59:57
    #4
    Vincent44
    Starting Member
    • Total Posts : 47
    • Reward points : 0
    • Joined: 2014/09/19 03:41:47
    • Location: France
    • Status: offline
    Re: File System (SYS_FS) stuck after 512 bytes read 2020/07/02 09:15:54 (permalink)
    0
    Thanks for the file.
    Your example is very similar to my code. The state machine of a bootloader is rather standard anyway.
    Overall, I know that my code is correct, because it's working perfectly when I use an SD card instead of a USB stick.
     
    I am pretty sure it is related to some timing and/or some interrupts.
     
    Did you had to play on the interrupt priorities ? The prefetch and cache ? What clock speed are you using ?
    #5
    Vincent44
    Starting Member
    • Total Posts : 47
    • Reward points : 0
    • Joined: 2014/09/19 03:41:47
    • Location: France
    • Status: offline
    Re: File System (SYS_FS) stuck after 512 bytes read 2020/07/03 04:15:19 (permalink)
    0
    I increased the interrupt priority of the USB, and I now have my bootloader state machine reaching the end : all the data is read from the USB stick, and written in the PIC32 flash (which I double checked by reading the entire flash with my ICD4).

    But I still have a last issue : when jumping to the app, my PIC32 seem to freeze. Again, using the exact same code with the SD card, I have no problem.

    I know that it's not related to my memory map, as the linker script forces the APP to start at 0x1D016000, which allows enough space for my bootloader (0x11500).
    Also, the configuration bits are the same in the APP and the Bootloader.

    I have some ideas in mind, but I can't figure out what's wrong...
    - Do I need to free anything in RAM before jumping to the app ?
    - Should I de-initialize the USB before jumping to the app ? If so, how ?
    - Should the app start by doing something special ?

    I tried adding some code before jumping to the app, but it did not help ...

      
       
        USB_HOST_BusDisable(0);
        while(!USB_HOST_BusIsDisabled(0)){
            SYS_Tasks() ;
        }
     
        // Set default to APP_FLASH_START_ADDRESS
        fptr = (void (*)(void))APP_FLASH_START_ADDRESS;
        // Disable Global Interrupts
        __builtin_disable_interrupts();
           
        // Jump to Application
        fptr();



    #6
    Paul PortSol
    Super Member
    • Total Posts : 630
    • Reward points : 0
    • Joined: 2015/07/03 11:52:03
    • Location: Newfoundland, Canada
    • Status: offline
    Re: File System (SYS_FS) stuck after 512 bytes read 2020/07/03 04:41:08 (permalink)
    0
    Verify: DevCfg in bootloader identical to DevCfg settings in App. Jumping to App doesn't change those settings.
    #7
    Vincent44
    Starting Member
    • Total Posts : 47
    • Reward points : 0
    • Joined: 2014/09/19 03:41:47
    • Location: France
    • Status: offline
    Re: File System (SYS_FS) stuck after 512 bytes read 2020/07/03 07:46:21 (permalink)
    5 (1)
    I finally found the solution !
    Not exactly what Paul PortSol suggested, but quite close.
    When jumpping to the app, the peripheral I was using in my bootloader where not fully disabled. This was probably causing some error in the main app. For example, receiving a USB interrupt before that the USB peripheral was configured ...
     
    Here is the last piece of code which is executed in my bootloader :
     
    static void Enter_Application(void)
    {
        void (*fptr)(void);
        
        // Disable Global Interrupts
        __builtin_disable_interrupts();
            
        //Ensure all interrupts and interrupt-flags are CLEARED before entering the APP.
        IEC0 = 0 ;
        IFS0 = 0 ;
        IEC1 = 0 ;
        IFS1 = 0 ;
        IEC2 = 0 ;
        IFS2 = 0 ;
        
        // Make sure all the peripheral used in the bootloader are DISABLED.
        // Timer 2
        T2CONbits.ON = false ;
        PMD4bits.T2MD = 1 ;
        
        // USB
        U1CONbits.USBEN = false ;
        PMD5bits.USBMD = 1 ;
        
        // Jump to the APPlication
        fptr = (void (*)(void))APP_FLASH_START_ADDRESS;
        fptr();
    }

     
     
     
    For the record, the initial issue (see topic name) was resolved by setting the USB interrupt priority to the highest priority among my peripherals.
    #8
    Jump to:
    © 2020 APG vNext Commercial Version 4.5