Harmony v2 USB Flash Drive Audio Player Tutorial


MPLAB® Harmony is a flexible firmware development platform for PIC32 microcontrollers. This integrated software framework provides driver libraries and APIs that make it easier for you to use PIC32 peripherals (Timers, GPIO, UART, I2C, SPI, etc.). It also includes middleware libraries for USB (Host and Device), TCP/IP (Ethernet and Wi-Fi®), Graphics, Bluetooth®, and others.

This tutorial shows you how to use the MPLAB Harmony Configurator (MHC) to create a USB flash drive (a.k.a. thumb drive) audio player application running on the PIC32 Multimedia Expansion Board II (MEB II). The application will read WAV files from the flash drive (plugged into the MEB II Board), decode them, and play them using the MEB II's audio CODEC. You will also learn how to use the MPLAB Harmony Graphics Composer (MHGC) to create a touch-controlled display to control the USB flash drive player.

Two Ways to Use This Tutorial

  1. Create the project from scratch.
    • Use the provided source files and step-by-step instructions below.
  2. Use the solution project as an example.
    • Just build the solution project and download it to the MEB II Board to observe the expected behavior.

Lab Objectives

  1. Create an MPLAB X IDE Harmony project for a PIC32 microcontroller from scratch.
  2. Use the MPLAB Harmony Configurator (MHC) to configure and generate Harmony code.
  3. Use (MHC) to configure the audio CODEC on the PIC32 Multimedia Expansion Board II.
  4. Create new application states and variables for Harmony projects.
  5. Identify the proper place to add your own application code to the Harmony project.
  6. Demonstrate the use of Harmony peripheral libraries and drivers to control PIC32 peripherals (I2S, I2C, Timers etc.).
  7. Demonstrate the use of Harmony's USB and Graphics (middleware) libraries.


Hardware Tools

Tool About Purchase
In-Circuit Emulator
PIC32MZ Embedded Connectivity with FPU
Starter Kit
PIC32 Multimedia Expansion Board II (v5)
For PIC32 Starter Kits

It is not necessary to have an MPLAB Real ICE™ to program/debug the code. The PIC32MZ EF Starter Kit includes a PICkit™ on-board (PKOB) debugger. The PKOB debugger is connected to the host PC through the USB Mini-B connector on the PIC32MZ EF Starter Kit for programming/debugging (See the red cable in the Hardware Setup figure).

Apart from the hardware tools listed above, the following items are required to play audio from a PC over the USB interface:

  • Wired Stereo headphone.
  • USB Type-A male to micro-B male cable for USB audio speaker functionality.
  • USB Type-A male to mini-B male cable for programming/debugging.
Figure: Hardware Setup

Development Board User's Guide Files


Software Tools

This project has been verified to work with the following versions of software tools:
MPLAB X IDE v3.26, MPLAB XC32 Compiler v1.40, MPLAB Harmony v1.08.

Because we regularly update our tools, occasionally you may discover an issue while using newer versions. If you suspect that to be the case, we recommend that you double-check using the same versions that the project was tested with.

Archived versions of our tools can be found on the following Microchip websites:
MPLAB Harmony (see "Archived Downloads" tab)
MPLAB X IDE and XC32 Compiler (see "Downloads Archive" tab)
Note that multiple versions of all these tools can co-exist on the same computer.

Tool About Installers
Windows Linux Mac OSX
Integrated Development Environment
C/C++ Compiler
MPLAB® Harmony 1.xx
Integrated Software Framework


This lab application plays WAV audio files (sampling rate: 48 kHz, 16-bit data) stored in a flash drive. This is done by adding a CODEC driver and the USB Host Mass Storage Class (MSC) stack functionality to the project. The implementation is demonstrated by connecting a flash drive to the development board and then playing the WAV audio files saved on the flash drive.

Figure 1.1 APP_Tasks()

This figure shows the application tasks. The APP_THUMB_DRIVE_AUDIO_Tasks handles USB flash drive and CODEC related states and events. The APP_DISPLAY_Tasks handles graphics related events.

Click image to enlarge.
Click image to enlarge.

Figure 1.2 APP_THUMB_DRIVE_AUDIO_Tasks()

The Thumb drive Audio state machine first registers event handlers with the File System and USB Host layer and then enables the USB bus. Once the USB bus is enabled, the state machine waits for the device (thumb drive) to get attached. Once the thumb drive is attached (which is indicated by the File System - Mount event in the File System event handler) the state machine opens the audio CODEC driver and registers a buffer event handler with it.

Click image to enlarge.
Click image to enlarge.

Figure 1.3 Audio Player State Machine

The state machine then enters the audio running state. All folders on the flash drive are searched to create a table of existing WAV files. The audio player state machine then starts to run. The audio player states perform the task of reading the audio data from the opened WAV audio file, decoding the WAV raw data through the audio decoder, and then passing the decoded buffer to the audio CODEC for playing. The audio player states also handle track change events and waiting for the current audio buffer completion event from the audio CODEC. This will be discussed in more detail in step Step 9: Review the application code.

Click image to enlarge.
Click image to enlarge.

Lab Source Files and Solutions:

This ZIP file contains the completed solution project for this lab. It also contains the source files needed to perform the labs per the following step-by-step instructions (see the "Procedure" section on this page).

The contents of this ZIP file need to be placed in this folder:
<Harmony install path>/apps/training/middleware/
(example Harmony install path = c:/microchip/harmony/v1_08)

If this is not done, the MPLAB X IDE will not be able to find all source files for the lab solution; the solution project will not build successfully.

Extracting the ZIP file will create the following folders:

  • thumb_drive_player contains the lab folder thumb_drive_player_lab
  • thumb_drive_player_lab contains the lab solution (in the firmware folder) and source files (in the dev_files folder).
  • dev_files contains application source files and other support files (if any) required to perform the lab (see "Procedure" section below).
  • firmware contains the completed lab solution project. It can be directly built and downloaded on the hardware to observe expected behavior.


All steps must be completed before you will be ready to build, download, and run the application.

Lab Index

Step 1: Create project and configure the PIC32

  • Step 1.1 – Create an MPLAB Harmony Project
  • Step 1.2 – Select the Board Support Package (BSP)
  • Step 1.3 – Verify Configuration Bits are Correct
  • Step 1.4 – Verify and Change Oscillator Settings
  • Step 1.5 – Use the Graphical Pin Manager to Verify I/O Pins

Step 2: Configure Audio CODEC, I2C and I2S Drivers

  • Step 2.1 - Configure CODEC AK4953
  • Step 2.2 - Configure I2C driver for CODEC
  • Step 2.3 - Verify/Set the I/O Pins used by I2C module using the Graphical Pin Manager
  • Step 2.4 - Configure I2S driver for CODEC
  • Step 2.5 - Configure External Master Clock Input (MCKI) to the CODEC
  • Step 2.6 - Configure/Verify DMA system service for allocating I2S transmit DMA channel
  • Step 2.7 - Verify/Set the I/O Pins used by I2S module using the Graphical Pin Manager

Step 3: Configure USB Library (Mass Storage Host)
Step 4: Configure File System
Step 5: Add WAV Decoder Functionality
Step 6: Design Display GUI, and Configure the Touch and I2C Driver

  • Step 6.1 - Rename the Screen Name
  • Step 6.2 - Import Images and Fonts (Assets)
  • Step 6.3 - Create Schemes (assign standard colors and fonts)
  • Step 6.4 - Add Primitive Objects (rectangle, image, text)
  • Step 6.5 - Add Widget Objects (list-box, buttons, slider)
  • Step 6.6 - Configure the Graphics display and Graphics Touch Driver
  • Step 6.7 - Enable Touchscreen Control using I2C
  • Step 6.8 - Enable the Graphics Touch System Service

Step 7: Generate Harmony code
Step 8: Include Application Specific Source Code and Files
Step 9: Review the Application Code
Step 10: Debug Your Application

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.