STM32F4 Discovery USB host and MP3 player

I have not written any new post for a while, so here is one more project for the stm32f4 discovery. I prepared this post a long time ago, but for some reason I never posted it, so here goes:

STM32F4_MP3

Based on one of the examples provided by ST, I have written a program for the STM32F4 Discovery board that plays MP3 files from an USB memory stick. You can download it here.

If you use a usb-host cable such as this one you can connect an USB memory stick (with fat32 file system) with mp3-files to the micro-usb port of the stm32f4 discovery. The files should then be played one by one. The user button can be used to skip to the next file.

If you follow this tutorial you should be able to build and upload the provided files right away.

The main.c-file has many comments, so hopefully you can figure out how it works. Using a USB-memorystick for logging is quite easy with this program as a basis. Only the two usb-pins, 5V and gnd are required to connect it to any stm32 with USB support, so hw-wise it’s even easier than using an SD-card.

36 thoughts on “STM32F4 Discovery USB host and MP3 player

  1. I’m using Windows 7. I build the previous project successfully, But I can not build this one. So many error :(

    • I do not use M$ windows, nor do I plan to do so ever, so I can’t help you with this issue.

      If you are willing to use any GNU/Linux distribution (e.g. Ubuntu) and build the ARM toolchain with my script or something similar, I can have a look at the compile output and try to help you.

  2. A big thank you for a very — very,very — great job!
    Run on windoz + CooCo-CoIde+ arm-none-eabi and “little” job for manage files .
    ( STM32F407 and Discovery are magic…)

    Thank you.

    • Hi Daniel,

      I will be very grateful if you could let us know how you arranged the files and maybe make your CooCox project available.
      I also use CooCox, I tried to arrange the many files but get plenty of errors which I am not able to sort out.

      Thanks.

  3. Mp3 with sample rate lower than 44.1k plays really fast like I am fast forwarding it while playing how to rectify this?

    • change this line:

      InitializeAudio(Audio44100HzSettings);

      options are:
      Audio8000HzSettings
      Audio16000HzSettings
      Audio32000HzSettings
      Audio48000HzSettings
      Audio96000HzSettings
      Audio22050HzSettings
      Audio44100HzSettings

      This could also be done in an automatic way based on the settings for each mp3, but this is only a simple example.

    • Hi,
      I got the code to compile under CooCox.
      #define USE_HOST_MODE was commented out in usb_conf.h

      Thanks again for your effort.
      Farren

  4. Hi,

    If got a question, do I need 5V for the usb Support ? Because I’d like to connect only 3,3V to the board. Do you think that would be enough ?

    kind regards

    Danny

    • As far as I know, you need 5V for USB devices to work. If you are lucky your USB-memory might work with 3.3V too, so give it at try.

    • Yes, USB needs 5V bau stm32f4discovery has a charge pump to evelate power from 3.3v to 5v for usb usage

  5. Hi,
    Thanks so much for the CooCox project files.
    But when I comile I get this error:

    C:\CooCox_Proj\stm32_th\stm32_th\lib\USB_Host\Core\src\usbh_hcs.c:104:7: error: ‘USB_OTG_CORE_HANDLE’ has no member named ‘host’

  6. Hi Benjamin,

    I have been busy truing to work through the STM32F4 Discovery example that records and plays back a .wav file on attached USB Thumb Drive.
    The ST examples are like looking at scrambled egg and I have got confused and not progresssed well.
    I would really appreciate it if I could get some help with source code to just create/open a file on a USB thumb drive and then write some text to the file such as “hello USB Drive” and then open/read the same file contents to a buffer.
    The aim is to have a very straight forward example project of file i/o using the stm32F4 USB OTG host functions.
    I dont want any Audio or any other overhead code in the way.
    Ultimately I am hoping to use the STM32F4 on a small data logger that logs ASCII data to USB Thumb Drive.
    At the moment I can log data to SD card using ChanFat but I dont want to use the SD Card.

    I am prepared to pay for the code if this can help me save time.

    Thanks
    Farren

    • Hi Farren,

      Does the MP3 player work for you? In that case it should be no problem to just remove the helix decoder files and some parts of the main program and just do what you intend to. The ST example used here is indeed based in Chan’s FatFS-code, so you should be able to use the functions right away.

      Unfortunately, I’m not able to help you with anything related to CooCox since I only use Linux.

      /Benjamin

  7. Hi Benjamin,
    Faddistr posted a reply with link to the CooCox environment.
    I just managed to get your project to compile 100%, no errors with CooCox a few minutes ago.
    I will load it onto the Discovery board to see if it works and then I will try to take out the unneeded audio parts and work through the example to see if I can get what I need.
    Thank you very much for your website, you are a good man to share your work with the community like this.
    Much appreciated.

    P.S. If I get really stuck can I ask your help, I am willing to pay for your time…
    Farren

    • It is nice that you appreciate my website :-)

      Sure, you can ask me for help if you get stuck. Other people have actually asked me do add a donate button, so I will try to do that some time. Then you can buy me a pizza if I manage to help you :-)

  8. Yes , A donate button will be a good addition !

    I have loaded the .hex onto the Discovery board and plugged in a USB Thumb Drive and hit reset button. The LED’s flicker and the LED on the Thumb drive also flickers for a short while then stops. So it is doing something but not playing the MP3′s, pressing the user button does not advance to the next MP3 and the system seems to stop there. This is no big deal for me as I will ultimately not be using the mp3 part of the code.
    I will add some code to printf to a terminal through USART2 so that I can put some printf’s in the code to see what is going on.
    I have a TTL to RS232 module connected to TX and RX (PA2 and PA3) and have this working on another Stm32F4 project so I will cut and paste this into to your code.
    I will work on this for a bit and let you know how I progress.

    Is there a way to contact you ? email address ?

    Regards
    Farren

    • The flickering LEDs are a good sign, so I’m sure you will manage to make it work.

      My email address is on the contact page: benjamin (at) vedder.se

  9. Hi,
    It is all working , read and write files to the USB pen drive for data logger application on STM32F4 using CooCox CoIde and gcc arm.
    Thanks to Benjamin and everyone that offered help.
    Farren

  10. Hi,

    Thanks so much for the CooCox project files.
    But when I comile I get the same error:

    C:\CooCox_Proj\stm32_th\stm32_th\lib\USB_Host\Core\src\usbh_hcs.c:104:7: error: ‘USB_OTG_CORE_HANDLE’ has no member named ‘host’

    how can I solve it?

    Thanks
    Raphael

  11. Hi!

    I have the same problem like Farren: the LEDs on the boards and the pendrive are flickering for a while, and after that a silent noise comes out from the headphone.

    Can you give me a tip, what should I do? The project is same than the posted CooCox project. Only one modification in the usb_conf.h: uncomment the “#define USE_HOST_MODE”

    I know you are not using the CooCox, but I guess it is not related to the IDE.

    Thanks,
    yoman

    • I’m sorry, but I can’t help you. The code that I have uploaded is working for me after all. It is not possible for me to test the coocox project since I don’t have any machine that runs windows and I don’t plan to get one ever.

    • Thanks for the quick reply Benjamin!

      Okay, I am trying to be specific. My project (downloaded from here) contains two usb_conf.h files. One in the “inc” folder, and one in the “lib/Conf” folder. There is a little difference between these. In the first there is “#define USE_HOST_MODE” and in the second there is “#define USE_DEVICE_MODE”.
      I guess the compiler is little confused due to the same names. Is it ok, that there are two same files?

      Or which mode should I use?

  12. Okay last wish :D

    Can somebody upload only the hex file of this project? To test, the program can work with my usb drive and mp3 files.
    Thanx!

  13. Hi Benjamin,

    Can you explain to me why the size of these:

    static int16_t audio_buffer0[4096];
    static int16_t audio_buffer1[4096];

    I have to reduce the RAM, I was trying to halve this buffer, but if you do this it does not play.

    Can you help me?

    best regards
    Raphael Gonçalves

    • Hi Raphael,

      The mp3 frame length for higher sample rates than 32kHz is 1152 and there are two channels, so the length of the buffers has to be at least 2304. Thus, half the size is slightly too small :-)

      /Benjamin

  14. Hi Benjamin,

    Thank you for reply.
    but I can change the sample rate to reduce the length of the buffer?

    Best regards
    Raphael Gonçalves

    • I haven’t tried different sample rates, but it could work. I’m not an expert on mp3 files…

      Another thing you could do would be do split one mp3 frame into two audio callbacks. For this you need one full size buffer (2304 x int16) to decode the whole frame and one smaller buffer to play from while decoding the frame. Then you have something like this:

      Suppose that the smaller buffer has the size SMALL_BUF_SIZE

      Audio callback 1:
      Decode the next mp3 frame to the large buffer. Provide the decoded buffer with the size of the smaller buffer subtracted:
      ProvideAudioBuffer(large_buffer, mp3FrameInfo.outputSamps – SMALL_BUF_SIZE);

      Audio callback 2:
      Copy the last SMALL_BUF_SIZE samples of the larger buffer to the smaller buffer. Provide it to the audio driver.

      For this approach, the small buffer has to be large enough to last for the time it takes to decode one mp3 frame, otherwise the playback won’t be smooth. You can experiment with that size to see what works.

      Hope this helps.
      /Benjamin

  15. Pingback: STM32F4 Beginner Steps (using free tools) | Tools & Technologies From Hasaranga

  16. how to access left/right samples in audio callback?
    is this correct?
    for(int i = 0; i<mp3FrameInfo.outputSamps;i+=2)
    {
    left= samples[i];
    right=samples[i+1];
    }

  17. I have my STMF4 Discovery also working for USB Audio (as USB Sound Card). Changed to 24bit, 48KHz, works as well.
    And: I use the STMF4 Discovery as USB-to-I2S plus an CmodS6 FPGA for audio DSP and processing.
    More details and project files are here:
    http://www.tjaekel.com

    Now I am thinking to do the same with the STMF4 Disco, with the LCD. But it looks like, almost all ports are used for the LCD. And the same project does not work without proper changes (e.g. different port for USB).

    Has anybody tried to create a Coocox project for STMF4 Disco (new chip, not supported yet) with USB OTG (as CDC or Audio)?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>