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.

Library

Features

  • 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

Dependencies

  • CMSIS
    • 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.

tilz0R

Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...