Library 29- USB MSC HOST for USB flash drive on STM32F4

Recently I made a library to read SD cards with FatFs by Chan. Today, I made a new library for USB MSC Host and connect both libraries together, so you can operate with USB flash drives and SD cards simultaneously with STM32F4xx device. USB MSC Host library is a separate library, to handle and do stuff with USB stack. You will also need my FatFS library for SD cards.

I will go here step by step, how to configure this library and FatFs together and show example, how to work with USB and SD card at the same time.



  • Operate with USB flash drives
  • Supported USB FS and USB HS in FS mode
    • USB OTG is connected to USB FS mode on STM32F4-Discovery board
    • USB OTG is connected to USB HS in FS mode on STM32F429-Discovery board
  • FatFS by Chan supported
  • FAT16 and FAT32 file formats supported


    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • MISC
  • TM
    • FatFs
    • defines.h
  • USB
    • USB MSC HOST stack provided by STMicroelectronics (included in library)
USB FS Mode HS in FS Mode Description
Data + PA12 PB15 USB Data+ line
Data – PA11 PB14 USB Data- line
ID PA10 PB12 USB ID pin
VBUS PA9 PB13 USB activate

Technically, Data+ and Data- are enough for USB communication in any way. But, STM32F4- and STM32F429- Discovery boards uses ID and VBUS pins for activate USB communication (STMPS2151 chip for USB). If you are using STM32F4 or STM32F429 Discovery boards, you need at least VBUS pin. If you are working on custom application, you can disable ID and VBUS pins. To disable pins, open project’s defines.h file and add lines below:

By default, USB FS mode is used, also used on STM32F4-Discovery board. If you want to enable USB HS in FS mode for STM32F429 Discovery board, open project’s defines.h file and add lines below:

We are done now with settings for USB Host communication. We have now to add my FatFs library and configure it.

Enable FatFs for USB MSC Host

In dependencies section is link where you can download my FatFs library. I have updated it to set it up with USB MSC Host. To enable FatFs library with USB Host, you will have to add these lines in your defines.h file:

When you activate line above, you automatically disable SDcard settings. If you want to enable SDcard communication too, you will have to also add these lines in your defines.h file:

Look at my SDcard FatFs library to properly set up SDcard communication.

FatFs files for USB MSC Host

You will need these files included for USB communication:

  • tm_stm32f4_fatfs.h
  • tm_stm32f4_fatfs.c
  • fatfs/diskio.h
  • fatfs/diskio.c
  • fatfs/ff.h
  • fatfs/ff.c
  • fatfs/ffconf.h
  • fatfs/integer.h
  • fatfs/option/syscall.c
  • fatfs/option/unicode.c
  • fatfs/drivers/fatfs_usb.h
  • fatfs/drivers/fatfs_usb.c
  • USB MSC HOST Stack
  • tm_stm32f4_usb_msc_host.h
  • tm_stm32f4_usb_msc_host.c

USB FatFs settings

I’m using the same FatFs library files for USB and SD card. For that purpose, SDcard has partition 0: and USB device has partition 1:.

If you need real time clock for FatFs, check my SDcard library with FatFs how to configure it.

USB Host important notes

  • Clock was set down to 168MHz for STM32F429 because you can not get 48MHz for USB from 180MHz core clock.
  • When you format your USB flash drive, it can take some time to be recognized by STM32F4 USB MSC Host library.
  • Some USB flash drives will not work with library. I got some of them that “device not supported”. This can be handled by library too. Check functions below.
  • FAT16 and FAT32 devices supported.
  • Tested with 4GB, 8GB and 2x 16GB USB flash drives.

USB Host functions

Almost everything is configured in FatFs communication library.

You have to use some functions to handle USB device:

Example 1

Write first data to USB flash. This library was tested on STM32F429-Discovery board. But if you configure defines.h file to work in USB FS mode, you will be able to run this on STM32F4-Discovery too.

Possible leds:

  • Only RED on: device is not recognized
  • Only GREEN: Everything OK
  • Both on: mounted OK, but file was not opened

Example 2

USB Flash and SD card write

USB Flash and SD card write

This example works with SDcard and USB MSC Host flash drive.

USB Host in configured in USB HS in FS mode and SD card has SDIO communication.

Leds status:

  • Led RED on: SDcard written OK
  • Led GREEN on: flash drive written OK

Both projects are available on my Github account, where you can download it, download library below.



Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • Arif Ahmet Balık

    I use library with this library together?

    How should I do?

    • It’s hard to. My libraries currently not support more than one mode.
      I will update libraries maybe in future to support more at same time.

      So, lets say on USB FS mode you will use MSC HOST
      and on USB HS in FS mode you will be able to use HID.

      But now it’s not possible.

      • Arif Ahmet Balık

        I got it. Thank you.

        • dongdong gu

          nice,it,s help me,谢谢

  • rahim

    hi i want to driving the usb in stm32f407.can you help me? it is my mail ” ” thanks.

    • You need my help,
      so you will contact me.

      And, everything you have provided. Did you even read the content??

      • rahim

        i am want starting now. i am Beginner to usb.

        • So, where is the problem? What is not working? Did you even read the content?

          And write grammatically!

  • bobby

    I want to make a project based on your USB library. I want to use STM-32 ADC collect data at rate about 1M Hz. Then I will transmit to PC by USB. So PC is the USB host and STM32 is the USB deivce. I use software Bus Hound to watch it.

  • bobby

    First, I develop base on your library of VCP. And used Python lib Pyusb for windows7. It can work. Pyusb can receive the packet from the STM32. By it can only send 32 packet per 100 ms. If I want to increase the packet number or decrese the interval to 10 ms. It cannot work well.

    So I want to develop base on the library of MSC. But I have not find suitable MSC library of USB devcie. Do you have such library example? Thanks a lot.

    • if (i < 100000000000)…

      You will send 100000000000 times the same thing and 10ms is delay between sending.

      Why? This will take 100years

  • bobby

    Even if you do not have such library example. If you can give some advice based on your MSC HOST USB project. I will be very appreciated.

  • Cahya

    In your usb example, the read/write access to the file is written in the main loop. But, where should I put this file access normally if I use it to read wav/mp3 file continuously for music player (since as you mentioned, the function TM_USB_MSCHOST_Process has to be called periodically as fast as possible). I read the example audio playback and recording project from ST, they put the file access in the function USBH_USR_MSC_Application.

    • If you know how fast you must call and read your block of data for conversion, you can do own function which will do that and make interrupt or something like that for operation.

  • Peter Mather


    Is is possible to post a working hex file for the 407 discovery? I’m really struggling with all USB host libraries. The VCP one works fine. I’m beginning to suspect some hardware issue on my board or with the USB/microUSB adapters I’m using



  • Peter Mather


    Thanks to your help I’m making progress integrating the USB code into my program. I’m hitting a problem though in doing more than one thing to the USB drive. I’m using TM_USB_MSCHOST_Init(); in the main program and FATFS USB_Fs and FIL USB_Fil are defined as global variables.

    I’ve written the simple routine below to return the free space. This works perfectly the first time it is run but times out on subsequent occasions. Help would be greatly appreciated

    Thanks Peter

    uint32_t func_free(void){
    uint8_t usb_done = 0;
    uint32_t free, total;
    while (!usb_done && TM_Time2) {
    /* Host Task handler */
    /* This have to be called periodically as fast as possible */
    /* USB Device is connected and ready to use */
    if (TM_USB_MSCHOST_Device() == TM_USB_MSCHOST_Result_Connected) {
    if (!usb_done) {
    /* Try to mount USB device */
    /* USB is at 1: */
    if (f_mount(&USB_Fs, “1:”, 1) == FR_OK) {
    /* Mounted ok */
    usb_done = 1;
    /* Get total and free space on USB */
    TM_FATFS_USBDriveSize(&total, &free);
    /* Unmount USB */
    f_mount(0, “1:”, 1);
    return free;

    • Peter Mather

      I’ve done more work on this but still can’t solve it. The second time called the device thinks it is connected but the mount fails. I assume something is not being properly cleared in the unmount? I’ve looked at the code but can’t find anything obvious.

      • Hum, well I’m not sure I will be correct person for FATFS related things.
        Maybe, you can post this on FATFS forums.

        • Peter Mather

          I don’t think it is a FAT issue as all works well using SPI SD card with your libraries. Can you explain the use of TM_USB_MSCHOST_Process();
          Perhaps I’m not calling it enough or at the wrong time

  • Jack

    I try your library 29 which is for USB MSC HOST. At First, I load example 1 which looks like mount the USB drive and write a text file. But When I load the firmware in the STM32 board and run the program with USB drive, the USB drive really write a file. In Windows 8.1, the USB drive’s filename shows garbage, looks like wrong encoding. The specific filename’s content does not show anything. Do you have any suggestion for me?

    • Format usb

      • Jack

        I already format my USB drive many times. Can you give me some format parameters ? e.g. FAT32 or FAT16 ? Blocks ? Quick Format or not ?

        • Just quick, can be fat16,others default.

          Download my gps project and run it. I use usb there. Some file header data will be written at start, so dont worry if you dont have gps.

          • Jack

            I try your GPS Project (Project 04), and it writes in my USB drive successfully. After that, I use f_open, f_puts and f_close three functions to write a text file, it success too. May I ask what’s different between Library 29 and Project 04 about USB HAL ?

          • Hello,

            I’m glad that it works. Now we know what to test.
            I assume that you downloaded my GPS project and you used FATFS library from download there.
            If that is so, open ffconf.h file in downloaded project and library you have in your project.
            ffconf.h file is located under “fatfs” folder.

            Compare these 2 files. I’ve enabled LFN on the heap.
            Use to compare files or tortoise SVN.

            Also, you can copy ffconf.h from GPS project to your project if it will work then.

          • Ok, I’ve made some research.
            The problem was that there was not enough HEAP memory.
            If you use Keil uVision, then just open .s file in your project and increase “Heap_Size” value in that file.
            By default, 0x200 is set. I’ve set it to 0x400 for test and it worked then great!

  • Gary

    Hi Tilen,

    Can you think of any reason that this library would not work on an STM32F0xx (specifically, STM32F07) processor?
    I only need the USB mass storage, not SD card.
    For functionality, I want to detect when a USB drive is plugged in, and then write data to it. Is it possible to detect when a USB dive is plugged in with only USB_DM and USB_DP pins?

    Thanks! (great site by the way)

    • As far I know F0x7 series have USB inside. But maybe USB registers are on different locations then these on F1,2,4. I don’t know here exactly what is going wrong to you.

      Using only these 2 pins you will hard detect connected device with hardware.

      • Gary

        Yes, you are correct. It has even clocking inside for USB, but because of some unstable devices that may be just outside compliance, I will still use HSE oscillator.
        The ST description says it supports file system, so I’m hoping it isn’t so difficult to implement, as you have made it easy for all of us with your STM32F4 examples.


  • Charles

    Hi, there miss some init functions for a SDIO – pin, clock and SDIO registr setting. Or in main.c module, there isnt some calling this function in init area. Is right? Is init SDIO/SD card in some other module – by FatFS functions maybe? Thanks for a reply.

    • Charles

      Im sorry, this example is only for an USB HOST FATFS. But there is example on GITHUB for a SD card and USB Host with one fatfs system as “Library 29”,and there miss some init function for a SD CARD. USB Host
      is right.

      • Everything is done behind in library in fatfs/drivers folder. Just add driver you need and that’s all.
        Everything is there, just don’t worry too much.

  • Gary


    For my project, I used the same physical implementation as on the STM32F407 Discovery board. To protect DM and DP lines, I used an STM USBUF06. Otherwise, it’s connected to all the USB OTG lines into the STM32F4. I plan to use it to plug in a USB stick and write data to it from a small NRAM chip.

    My question is this – when the USB is plugged in, is it detectable by the STM32F4? What line, or pin, allows this to happen? I just want to make sure nothing’s missing for my PCB design.


    • Hello, device is detected by software.
      I suggest you to use my lib 24 and connect board with PC. If it will work, then you are ok.

      Dont forget for drivers for VCP from ST, and setting PLL correct :))

  • Gary

    Thanks, it’s a great help!

  • George

    Hi, I tried using your DS18B20 with the USB library and since then the USB can’t mount. I managed to figure out it was this instruction, while (!TM_DS18B20_AllDone(&OneWire1)), which was stalling the programming. Is there any way around this?

    • Using while loop is for example. In production, use if statement to check if done.

      • George

        Hi thanks for the reply. I tried the if statement and it didnt work because the usb starts working but not the temp sensor. Could you please be a bit more specific because i tried a few variations of the if statement. Thanks in advance

  • ArmstrongLee

    Hi. Thank you for support your library files. I am beginer of STM32f4-Discovery board(MCU may be stm32f407). I downloaded your library files for testing the USB Communication using USB stick. when I try to compile it, there was some errors. especially there is not declared extern variable in usb_bsp.c file which name is USB_OTG_Core;

    I thought if I change USB_OTG_CORE_HANDLE USB_OTG_MSC_Core to USB_OTG_CORE_HANDLE USB_OTG_Core in tm_stm32f4_usb_msc_host.c, it will work. but after compile it, it can’t find USB stick.

    Am I wrong to change it? If I am wrong how can I change the USB_OTG_Core? and If I am right is there any other problem to detect the USB stick?
    Thank you for reading.

    • Jan Slavotínek

      Hi! I just came to this site with intention of setting up USB fat host (for storing data on USB under FAT32). I have downloaded libs, but i can find tm_stm32f4_usb_msc_host.c anywhere! Where should i obtain that lib? Author of this page states, taht this application is dependant on that lib, but where should i get it?
      Thanks for advice

  • Gary

    Hi TM:

    I do not want to use any of the SD card functionality, only the USB stuff. Which files to exclude? For example, based on your list above (FatFs files for USB MSC Host), I excluded “fatfs_sd_sdio.h”. But it appears it’s required in the c file “diskio.c” when compiling.

    Can you please tell me which files can be excluded safely without breaking the USB required files?


    • Hi,

      it is explained in post above. Read carefully about setting some defines.

  • Gary

    Thanks for responding. I did follow that….and now it is asking for the file “”tm_stm32f4_sdram.h”, which has nothing to do with USB.

    • You can also check which file includes sdram.h file and remove that file from project if exists!

  • Gary


    I have only this in the defines.h file: #define FATFS_USE_USB 1
    The compiler breaks in file “usb_bsp.c”, on this line at bottom: USBH_OTG_ISR_Handler(&USB_OTG_Core);
    It says: “undefined reference to USB_OTG_CORE”


  • Gary

    Hi TM,

    I should mention my implementation is not on the Discovery board, but my own. However, the physical connections to the USB are implemented the same as the Discovery board. All lines including ID are connected. The only difference is that my VBUS power is on PA.8 (not PC.0), so I changed that in the file “usb_bsp.h”

    Okay, so it’s now compiled OK with no errors. When I run it, the init seems to be good, the USB VBUS LED switches on. However, it returns only “USB Not Connected”. I have tried at least 2 different, USB 16Gb data sticks.

    What would cause it to return this message? Is there a hardware line or register to check that would help determine this?

    Thanks again…

    • Many things, probably clock settings.
      For start, use lib 24 for vcp and test if communication works with PC.

      If it does, then you are ready to go further.

  • Gary


    USB is now working. However, I want to know how to *disable* it, or stop it. The USB stick is sensed on boot-up, data is written to it, and then after the user removes the stick, I want to disable it. I’ve tried disable the interrrupt:
    NVIC_DisableIRQ( OTG_FS_IRQn);

    but after the code exits the while(1) loop, it gets to the WDT initialization, and then reboots.

    Any suggestions on how to disable it?


  • Gary

    Thanks TM,

    Very helpful and it works, but not with the result I expected. Does the enumerated result of “TM_USB_MSCHOST_Result_Disconnected” imply physically disconnected? If so, it returns this result even with the device still connected. In my section above that writes the code, I set a flag if “writeOK”. I then “unmount” the drive.After this code section, I then run the call to TM_USB_MSCHOST_DeInit(), which returns the result “TM_USB_MSCHOST_Result_Disconnected”, even with the device physically plugged in.

    Should this be the case?

    Thanks again,

    • The point is, that when you unmount usb with f_unmount, you can unplug it. When you call deinit function, as you should see, function returns disconnected hardcoded.

      If you disable usb host, then you cant detect if connected or not!

  • Gary

    Ok, thanks.
    I’m trying to figure out why my system crashes after running the code. Just for testing, I call this: TM_USB_MSCHOST_Init(), and immediately after, I call: TM_USB_MSCHOST_DeInit(). Even with just that, the system crashes (causes WDT to keep resetting)

    After I run those 2 functions, I do a typical system setup – as I would do, with or without the USB code. When if comment out those 2 function calls, the system boots OK.

    Do you have any idea what might cause this? Why would initializing the USB OTG interface cause subsequent code to crash? Why would the DeInit not reverse what the initialization did?

    By the way, I realize your code is good, and based on the standard USB interface calls, etc. Just that something weird going on and I cannot imagine why the initialization would cause this.

  • Gary

    The USB code functions and so does FreeRTOS, after the USB code. How did I get FreeRTOS to stop crashing? Using this call the line before the USB initialization:


    Whatever that call does, – looks like it just reads a register – it does something that allows FreeRTOS to operate normally after the USB initializes, runs,and deinitializes. And I confirmed it over quite a few tests so it’s not a one-off behavior.
    Is this is a core bug of some kind.???

    My code is well written and behaves perfectly normal before inserting the USB interface. I am using FreeRTOS tasks, message queues, semaphores, and in interrupts as well.

    In any case I’m glad it’s working….weirdness I have seen in embedded…none like this…..

    • __get_PRIMASK functions just returns interrupts status if they are enabled or not.

  • Gary

    Then it has something to do with running that call in the GCC compiler. The subsequent code of FreeRTOS works *only*

    if that call is in place.

    Some quirky behavior I have never seen. Now you know why I was pulling my hair out? Only by a fluke I decided to try this, as I knew it had something to do with interrupts. Good thing you posted on one of your articles, which is how I found it. 8–)

  • Chien-Hung Lai

    This is what I want now! I want to access my SD card by USB MSC on PC, meantime, I want to access my SD card by STM32F103ZET6 for the other applications at same time.

    But my project is based on STM32F103ZET6 compiled on Keil C 4.74. How can I import your achievement?

    • You need DEVICE mode first for MSC. This is HOST mode which ready USB memory.
      You want to be read from other devices.

      • Chien-Hung Lai

        Pardon? You mean this is not what I need?

        I just want to read write SD card by FatFs after it has been initialized by USB on PC.

        • This is correct. Ouu cant use this stack for your needs.

          • Chien-Hung Lai

            So, could you tell me what I need, or what I got to do? Sorry! It’s my first time to program STM32F103ZET6.

  • Tim Nicholson


    I have built your library for the STM32F205VE which has been fitted in place of the STM32F407VG on the STM32407 Discovery board (all the pins are exactly the same and the device was professionally changed). I am using the relevant CMSIS files for the F2 device. I am building the project using GCC and Eclipse. ALL I have changed in your library is the include files from ST32F4xx_yyy.h to ST32F2xx_yyy.h and the cmsis C files from F4 to F2. Everything builds fine as I would expect.

    When nothing is plugged into the OTG socket, the main loop runs normally and I get a RED LED. The GREEN VBUS LED also comes on. However when I plug in the USB pen drive (2GB FAT32), the main loop completely stops and processor spends all its time processing the interrupts. when I trace the ISR, I find it is entering USB_OTG_USBH_handle_sof_ISR() and USB_OTG_USBH_handle_nptxfempty_ISR().

    I have checked the clock settings using the ST XL Spreadsheet and every looks correct. The processor is configured to run at 120 Mhz. It is running from a 8Mhz HSE and PLL with the USB OTG FS set at 48Mhz.

    Do you have any ideas why the Interrupts are not clearing? I am guessing a status register is not being cleared correctly but I have not changed anything in your code other than the names of the header files. I have implemented your example 1 exactly. The only change I made was to turn on the Orange LED at the top of the loop and to turn it off again at the bottom so I could monitor the loop activity on an Oscilloscope. I can confirm it completely stops when a device is plugged in!

    Any tips would be greatly appreciated


    • Tim Nicholson

      Just an update, I tried setting USE_USB_OTG_HS in defines.h just in case I had misunderstood the Blog. When I do that the GREEN VBus light does not come on and there are no interrupts generated at all. That does not really surprise me, but I just wanted to rule it out.

      It seems the library does not work with this F2 processor, but I don’t understand why.

      I will debug the ISR to find the problem and report back.

    • Tim Nicholson

      I have fixed the original problem, I defined my own instance of the USB_OTG_CORE_HANDLE called USB_OTG_Core rather than use the global version USB_OTG_MSC_Core that is set up by TM_USB_MSCHOST_Init(). The confusion came because the variable refereed to in the OTG_FS_IRQHandle was USB_OTG_Core (for some reason) and not USB_OTG_MSC_Core. That led to a linker error which made we think that I needed to provide the variable! I should I thought about it a little harder – It is always the silly problems that take the most time to fix!

      Everything seems to work very well now apart from one thing, Sometimes my drive will not reconnect again having been removed.

      It always connects if the drive is present at initialisation, but if the drive is unplugged and then plugged back in, the library does not always see it. I get the Disconnect event every time I remove it, but I do not always get the connect event when I plug it back in. However, If I plug in another device (like a mouse), I always get an Unknown Device event (every time) so it has something to do with plugging in the drive having already removed it. What is the best solution for this? I did try resetting the OTG controller after each Disconnect event but that did not solve the problem, it is not really obvious how to do a proper hardware reset on the OTG without resetting the whole MCU.

      • Janusz Pawlacz

        Hello, I had similar problem to yours, but your comment has helped me! thank you very much

  • Ahmed Emad el-deen

    Hi TM,
    I work on stm32f407 board and i just used your library to work with SD card using SDIO interface, i wanna use the board as a flash memory device. So when i implement SD card into board and connect the board to the PC by USB i wanna access SD card from PC as a normal flash memory. Any help ?

  • Majid Barz

    Hi TM, again thanks for your work..
    My question is I did everything as you said here, step by step, to configure this library but it keeps giving these errors. do you have any idea what is the problem?

    • You did not add everything to project then.

      • Majid Barz

        I added everything you mentioned in this post
        at first it did not recognize where “ff.h” and … were, so I added “TMfatfs” to “included paths” in c/c++ options.
        but after resolving that I got errors above.
        I am trying to solve this for 2 days and got so frustrated. can you help me?
        also I uploaded my project that gives these errors here:

    • Manuel

      have you fixed the problem? I have exactly the same errors and can’t solve it.
      Thanks in advance for your help

      Reguards, Manuel

      • You have to add USB host core files to projects. I propose you start with HAL USB library.

  • Faisal Rana

    Hello Tilen!!
    i m realy impressed with you are work.
    i am using you are library#29 its compiled good but when i load the hex file to stm32f4discovery board407vgt and also connected USB but both green and Red LEDs are ON at time(ERROR!! File is not opened) i have also written a text file with same name as u given in code but again ERROR is same. my questions are..
    1) Is we need special mode of Format for USB?
    2) Is there any hardware Error?

    Thanx in advance

  • nanna

    I work on stm32f429 custom board with your library. But I have some problem.
    It’s generated WKUPINT and OTGINT interrupt when connect USB memory.
    It’s not generated HPRTINT interrupt. Then It’s not work USB Connect operation.
    please, give me some advice.

    • How sure are you about your layout for USB?

      • nanna

        I used STM3221G-EVAL. USB OTG Part is almost same. But OTG_FS_PowerSwitchOn and OTG_FS_OverCurrent Port is different.
        I don’t understand exactly this part.
        I changed OTG_FS_PowerSwitchOn pin. But I can’t find OTG_FS_OverCurrent pin in the source code. Is this a problem?

  • AhMeD EMaD El-deen

    Hi TM,
    i worked on a similar project which copies file from SD card to flash memory and every thing seems good, but when i call this copy function in an interrupt service routine it doesn’t work and can’t open files in both sides ?

  • joristip

    Hello Majerle,

    Yesterday you helped me out perfect with the USB VCP lib. Now I know that all my settings are okay, I want to use this library to read and write to a USB flash disk. I included the nescesary files to CoIDE and change the main.c file to example 1. To compile this library with the gcc compiler, I had to use these two options to my linker: -lc; -lrdimon (don’t know what these do).
    The code is running, but every time I connect an USB flash disk, the LED on the stick flickers, but the red LED on the STM32F407VG remains red. In other words, the USB flash disk is not detected propperly. I tried 3 different flash disks.
    Any idea what’s wrong?
    Could you maybe send me a precompiled .hexfile with the Keil uVision to make sure there is nothing wrong with my flash disks?

      • joristip

        I loaded this .hex file to my STM32F407G-DICS1 board and tried the 3 USB flash disk, same behavior as with my GCC compiled .hex file, the red LED stays red. also after waiting more than a while.
        To check my USB host cable, I loaded library 31 (USB HID host) and connected a mouse. The LED’s behaved as described and after connecting a USB flash disc, the red and green LED’s started to blink like described.
        I flashed the board back to USB MSC host an tried 2 more flash drives (now 5 different in total) and I tried to format the USB flash disks as different file systems (FAT, FAT32, exFAT), all without succes.

        • joristip

          I got the solution :).

          The LED remaind red because I was unable to mount the USB flash disk. To mount the flash disk propperly, you have to use one of the path names defined in ffconf.h at the define _VOLUME_STRS. You can define your own path and change for example the path “USER1” to “USB” or you can change all the paths in main.c from “USB:” to “USBFS:”.

          The next problem I had was that the program crashed at the alloc() function. To make this work with the GCC compiler, you have to add lib C and lib NOSYS to the linker options (-lc;-lnosys;). So don’t use the lib RDIMON as I described before.
          Now i’m able to use the filesystems FAT and FAT32. The exFAT doesn’t work, but that’s no problem for me.
          Now it’s time for a beer 🙂 Cheers!

          • This could be a problem with disk names.
            Im glad it works and I have to change that in example.

            I dont maintain this code anymore because of new HAL libraries.

  • Phạm Minh Quân

    Hi Majerle,

    I’m using your example with STM32F429-Discovery. I don’t know why you change the value of PLL_M to 336 instead of 360. Is there any way to run USB MSC host example with PLL_M 360?


    • With 360 you can’t get 48MHz for USB.

      With 336 you can, because 336 / 7 = 48.
      PLL_N / PLL_Q

      It might work with 360 too but this is not guaranteed.

      • Phạm Minh Quân

        Got it, thanks

  • vladimir

    Hi Majerle,
    i’m looking for way to read from flash drive one file and write it into mmc card. Can i make that with your code? Thanks

  • James

    Hi Tilen,

    I am storing digital sound waves on the USB and I want to feed them to DAC channel 1, specifically not the codec. How could I do this?

  • S.S. Jain

    Hello Tilen,
    Need some help!

    Board: STM32F4 Discovery Kit
    How to use GPIO Interrupts and USB together?

    I am trying to do the following using STM32Cube HAL Library:
    I want to press the User Switch. The number of switch presses will be counted. Each time the switch is pressed, the text file in the USB pen-drive should be updated. The USB Drive is connected to the board using USB Host Mode OTG cable.

    Following data in the file: No of switch presses is __ . I want blank __ to be updated dynamically as the User Switch on board is pressed.

    Please give me an idea as to how should I go about accomplishing the above task.


  • Ala Eddine Telli

    hi can u send me th project example with keil to mu e-mail

    • I’m afraid I can’t do that.

      • vm

        Afraid of what sir ?

  • aissa93

    hello, I need to play .wav file stored in a USB device, what should I add to this project to play differently .wav file

    • You need USB msc host stack. This lib can work but is not hal compatible.

      • aissa93

        i don’t need audio codec for example ?

  • vm

    Hello sir,
    i compiled the code and when i burn the code initially red led ON mystm32f429 board means device flash drive not connected but after that when i re plug my flash drive then it get connected but flash drive not mounting.
    help needed what to do further!

  • vm

    hello sir,
    after writing all the strings and data in a file created in flash drive the control going to HardFault_Handler();
    and i am not able to perform any operation bcoz it is continuing in that while loop inside HardFault_Handler();
    but file is created and data written in flash drive.

    what i have to check to solve this issue sir

  • issa

    hello sir, I used the project Audio_playback_and_record to play audio file stored in USB, it’s working correctly with only one file (if I want to play another file I should change the WAVE_NAME defined in main. h) what should I do to play different audio without return to main.h ?