STM32F4 Discovery MP3 player working - with source code

This forum is about you. Feel free to discuss anything is related to embedded and electronics, your awesome projects, your ideas, your announcements, not necessarily related to ChibiOS but to embedded in general. This forum is NOT for support.
watz
Posts: 8
Joined: Mon Mar 12, 2012 9:19 pm

STM32F4 Discovery MP3 player working - with source code

Postby watz » Thu Mar 15, 2012 3:26 pm

Hi,

I've enabled my STM32F4 Discovery Board to play MP3 files (using ChibiOS and I2S support from ST libs). There is no display support yet. However, I think the code may already be a good start for people trying to do something similar. Some code for reading ID3v2 tags has been added, too.

The boards DAC is driven through I2S and a circular DMA buffer, which makes playback perfectly smooth.

Only an SD card has to be attached to the board get the code working. Optionally, you can watch player messages on the PC through the UART TX on PA2. More details can be found in the comments at the top of main.c. The SD card currently has to be in the slot/attached when the board is turned on. All MP3 files in the cards root are then played in a loop in the order they are recorded in the FAT. You can press the blue user button to skip the currently playing title.

The sound quality is, IMHO, very impressing.

To build the source I used Code Sourcery Lite + Eclipse Indigo + CDT/GNU ARM plugins on Windows as well as on Linux. You can find the according eclipse project with all sources and a prebuilt binary here:

http://watz.at/F4D_MP3_1.zip

Gruß,
Watz

P.S.: The UART output looks like this:

Code: Select all

SD: Connected to card
SD: File system mounted
Mp3PlayAllFiles: Playing all files in "/"
Mp3PlayAllFiles: FILE "/10-THE~1.MP3"
Mp3Decode: Start decoding "/10-THE~1.MP3"
Mp3Decode: Skipping 2266 bytes of ID3v2 tag
Mp3Decode: Now playing (ID3v2): Within Temptation - The Truth Beneath The Rose
Mp3Decode: 44100 Hz 16 Bit 2 Channels
Mp3Decode: frame 0, bitrate=128000
Mp3Decode: frame 100, bitrate=160000
Mp3Decode: frame 200, bitrate=160000
Mp3Decode: frame 300, bitrate=160000
Mp3Decode: frame 400, bitrate=192000
Mp3Decode: frame 500, bitrate=192000
Mp3Decode: frame 600, bitrate=192000
Mp3Decode: frame 700, bitrate=224000
Mp3Decode: frame 800, bitrate=224000
Mp3Decode: frame 900, bitrate=192000
Mp3Decode: Stop requested
Mp3Decode: Finished decoding
Mp3PlayAllFiles: FILE "/11-FOR~1.MP3"
Mp3Decode: Start decoding "/11-FOR~1.MP3"
Mp3Decode: Skipping 2248 bytes of ID3v2 tag
Mp3Decode: Now playing (ID3v2): Within Temptation - Forgiven
Mp3Decode: 44100 Hz 16 Bit 2 Channels
Mp3Decode: frame 0, bitrate=128000
Mp3Decode: frame 100, bitrate=160000
Mp3Decode: frame 200, bitrate=160000
Mp3Decode: frame 300, bitrate=160000
Mp3Decode: frame 400, bitrate=192000
Mp3Decode: frame 500, bitrate=256000
Mp3Decode: frame 600, bitrate=160000
Mp3Decode: frame 700, bitrate=160000
Mp3Decode: frame 800, bitrate=128000
Mp3Decode: frame 900, bitrate=128000
Last edited by watz on Thu Mar 15, 2012 3:30 pm, edited 1 time in total.

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: STM32F4 Discovery MP3 player working - with source code

Postby Giovanni » Thu Mar 15, 2012 3:29 pm

Cool, thank you :-)

Giovanni

DrunkenDonkey
Posts: 124
Joined: Sun Aug 28, 2011 5:16 pm

Re: STM32F4 Discovery MP3 player working - with source code

Postby DrunkenDonkey » Thu Mar 15, 2012 7:00 pm

Great! :D
I also totally agree with the choice of music to test with :D

User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Location: Minsk, Belarus
Been thanked: 7 times

Re: STM32F4 Discovery MP3 player working - with source code

Postby barthess » Fri Mar 16, 2012 3:13 pm

Hi watz,
Nice work.
What version of ChibiOS you are using for you mp3-player? If you observe some bugs with SDIO please let us know in this topic viewtopic.php?f=3&t=256

chrisjensen
Posts: 10
Joined: Tue Feb 28, 2012 9:53 pm

Re: STM32F4 Discovery MP3 player working - with source code

Postby chrisjensen » Sat Mar 24, 2012 7:47 pm

Hello all,

I am a self described noob, but I am doing a project that will be using an MP3 player and wanted to try to build the code, and I wasn't able to. My set up is this:

Mac OSX > YAGARTO > CDT Indigo > Upload with qstlink (no debugging ability yet)

Anyway I have tried to compile from the command line as well as from with eclipse. I can successfully build the ChibiOS demo application for the STM32F4DISCOVERY that comes with chibiOS 2.4.0, but I am having trouble building the MP3 demo as published above.

Is it a compatibility issue with the Cross compiler/YAGARTO?
Do I need to do something special when adding the project to Eclipse?

Any help would be greatly appreciated, even if it was just links on where to learn, and what to learn.

thanks
Christopher

User avatar
barthess
Posts: 861
Joined: Wed Dec 08, 2010 7:55 pm
Location: Minsk, Belarus
Been thanked: 7 times

Re: STM32F4 Discovery MP3 player working - with source code

Postby barthess » Sat Mar 24, 2012 9:10 pm

hi Christopher,
chrisjensen wrote:even if it was just links on where to learn, and what to learn.

http://www.catb.org/~esr/faqs/smart-que ... anslations
Provide additional information. What you exactly do when try to compile. What version of chibios you use. What error message you got.

pito
Posts: 199
Joined: Sun Nov 06, 2011 3:54 pm

Re: STM32F4 Discovery MP3 player working - with source code

Postby pito » Fri Mar 30, 2012 12:45 pm

Hi, the player works nice! I am trying to compile the source as I need 115k uart :roll: , but still coping with an error.
I'm using Eclipse Indigo + Codesourcery Lite (arm-2011.03-42-arm-none-eabi.exe) + plugin(org.eclipse.cdt.cross.arm.gnu_0.5.4.201202210114) and I've imported the project (all under xp). The compiler shows 31 errors all related to palSetPad, palClearPad:
ie:
Field 'palSetPad(GPIOD, 12)' could not be resolved main.c /F4D_MP3/src line 218 Semantic Error
..or
Field 'palClearPad(...)..

I've added ${ProjDirPath}/src/ChibiOS_2.4.0/os/hal/include to the include directories, but no success yet.

PS: the same with arm-2011.09-69-arm-none-eabi.exe
P.

pito
Posts: 199
Joined: Sun Nov 06, 2011 3:54 pm

Re: STM32F4 Discovery MP3 player working - with source code

Postby pito » Fri Mar 30, 2012 5:35 pm

:oops: I've found out the eclipse destroys the makefile imported from the project source..
After trying to build I got something like this:

Code: Select all

# All Target
all: F4D_MP3.elf
# Tool invocations
   @echo 'No tool found that can build the extension specified with the build artifact name $@'
USER_OBJS: $(OBJS) $(USER_OBJS)
   @echo 'Invoking: ARM Sourcery Windows GCC C Linker'
   arm-none-eabi-gcc -nostartfiles -Wl,-Map,F4D_MP3.map -mcpu=cortex-m4 -mthumb -g -gdwarf-2 -o "USER_OBJS" $(OBJS) $(USER_OBJS) $(LIBS)
   @echo 'Finished building: $@'
   @echo ' '

and it shall be something like this:

Code: Select all

# All Target
all: F4D_MP3.elf secondary-outputs
# Tool invocations
F4D_MP3.elf: $(OBJS) $(USER_OBJS)
   @echo 'Building target: $@'
   @echo 'Invoking: ARM Sourcery Linux GCC C Linker'
   arm-none-eabi-gcc -T"C:/MyCode/ARMEC/ARM_F4/F4D_MP3/src/ChibiOS_2.4.0/os/ports/GCC/ARMCMx/STM32F4xx/ld/STM32F407xG.ld" -nostartfiles -Wl,-Map,F4D_MP3.map -mcpu=cortex-m4 -mthumb -mfloat-abi=softfp -mfpu=fpv4-sp-d16 -o "F4D_MP3.elf" $(OBJS) $(USER_OBJS) $(LIBS)
   @echo 'Finished building target: $@'
   @echo ' '
   $(MAKE) --no-print-directory post-build


Mind the -T"C:/...shall be your path to STM32F407xG.ld"

So I took simply that part from the original make file, edited the above path and at the windos command prompt I did "make" (while in the Release folder).
And it does compile with my new 115k baud uart settings :shock: !

Mp3Decode: Start decoding "/08_BAR~1.MP3"
Mp3Decode: Skipping 1054 bytes of ID3v2 tag
Mp3Decode: Now playing (ID3v2): Diverse Klassisk - Henricus Aloysius Bruckner - Sonata a 4
Mp3Decode: 44100 Hz 16 Bit 2 Channels
Mp3Decode: frame 0, bitrate=96000
Mp3Decode: frame 100, bitrate=96000
Mp3Decode: frame 200, bitrate=96000
Mp3Decode: frame 300, bitrate=96000
p.

PS: with latest yagarto 20111119 it compiles (as described above, still those makefile issues with eclipse) 170kB .hex size (orig .hex is 183kB).
PS1: Unchecking "Generate Makefiles automatically" in C/C++Build properties avoids the makefile corruption and it compiles from Eclipse then..

Abhishek
Posts: 266
Joined: Wed May 23, 2012 3:15 pm
Location: India

Re: STM32F4 Discovery MP3 player working - with source code

Postby Abhishek » Mon Jun 11, 2012 7:48 am

I tested your code a week ago. Works great, however 44.1 kHz doesn't seem to be working right, I get lower pitch and slow playback speed, and about 80% of my mp3 collection is in 44.1kHz!

With some quick calculations I figured out that with the default settings for PLLI2S (PLLI2S VCO=86 MHz=258/3), error on 48kHz sample rate is:0.0186 % (I2S prescale value:6.998697916666667 ~= 7) while error on 44.1kHz sample rate is ~5%! (I2S prescale:7.617630385487528 ~= 8)

I found out that if in mcuconf.h we replace:

Code: Select all

#define STM32_PLLI2SN_VALUE                 258
#define STM32_PLLI2SR_VALUE                 3


with: (courtesy STM32F4 Reference manual)

Code: Select all

#define STM32_PLLI2SN_VALUE                 271
#define STM32_PLLI2SR_VALUE                 2


Then it works great for both 44.1kHz and 48kHz

Error on 44.1kHz (12.00219671201814 ~= 12) = 0.0183% and on 48kHz (11.02701822916667 ~= 11) = 0.245% (Hardly Noticeable).

I'm currently using these values for the wave player my project (forum post link here: viewtopic.php?f=8&t=468).

I might implement the mp3 playback functionality with GUI. Thank you for demonstrating an Mp3 player! Your post provided me direction for my project.

User avatar
Tectu
Posts: 1226
Joined: Thu May 10, 2012 9:50 am
Location: Switzerland
Contact:

Re: STM32F4 Discovery MP3 player working - with source code

Postby Tectu » Mon Jun 11, 2012 8:48 am

Very nice work!

If you are interested, we are currently working on a GLCD and TouchPad library for ChibiOS. Maybe you are interested?

Take a look here: http://chibios.org/dokuwiki/doku.php?id=chibios:community:lcddriver_by_tectu&#todo
and here: http://forum.chibios.org/phpbb/viewtopic.php?f=3&t=458


~ Tectu


Return to “User Projects”

Who is online

Users browsing this forum: No registered users and 3 guests