SEGGER emWin Audio Player with MPLAB® Harmony v2 Tutorial

 Objective

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 an audio player application running on the PIC32 Multimedia Expansion Board II (MEB II). The application reads WAV files from the USB flash drive (a.k.a. thumb drive) or SD card (both plugged into the MEB II Board), decodes them, and plays them using the MEB II's audio CODEC. You will also learn how to use SEGGER's emWin graphics library to create a touch-controlled User Interface (UI) to control the audio player.

SEGGER is a third-party provider for Microchip 32-bit microcontrollers. They provide the J-Link Debug Probe, the embOS RTOS, and their popular graphics library called emWin (included with Harmony download). MPLAB Harmony users can use emWin at no cost.

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. Create new application states and variables for the Harmony project.
  4. Identify the proper place to add your own application code to the Harmony project.
  5. Use Harmony peripheral drivers to control PIC32 peripherals (SPI, I2S, I2C, DMA, etc.).
  6. Use Harmony device drivers (SD Card, Graphics, Touch, etc.).
  7. Use the Harmony USB library (USB Mass Storage Class in Host mode).
  8. Use SEGGER's emWin Graphics Library and its utilities (GuiBuilder, BmpCvt, etc.).

 Materials

Hardware Tools

Tool About Purchase
REALICE-50px.png
MPLAB® REAL ICE™
In-Circuit Emulator
PIC32MZ-EF-50px.png
PIC32MZ Embedded Connectivity with FPU
Starter Kit
PIC32-MEB2v5-50px.png
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 can be connected to a host PC through the USB Mini-B connector on the PIC32MZ EF Starter Kit for programming/debugging (refer to the red cable in the Hardware Setup figure).

Apart from the hardware tools listed above, the following items are required for the lab:

  • Wired Stereo headset (Speaker).
  • USB Type-A male to mini-B male cable for programming/debugging with PKOB.
  • Micro SD Card and USB Flash Drive.
pic32mz_ef_sk_meb2.jpg
Figure: Hardware Setup

Software Tools

This project has been verified to work with the following versions of software tools:
MPLAB X IDE v3.45, MPLAB XC32 Compiler v1.42, MPLAB Harmony v2.01b

Note: 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 the Archived Downloads tab)
MPLAB X IDE and XC32 Compiler (see the Downloads Archive tab)
Note that multiple versions of all these tools can co-exist on the same computer.

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
MPLAB® X
Integrated Development Environment
MPLAB® XC32
C/C++ Compiler
MPLAB® Harmony 2.xx
Integrated Software Framework

Overview:

This tutorial shows you how to use the Harmony framework to create an audio player from scratch. It allows you to play WAV audio files from an SD card or USB flash drive connected to the development board. The development board's touch-controlled display provides you with a graphical user interface (GUI) to control the audio player.

The default media type is the SD card. This can be changed to the flash drive from the GUI. The application reads WAV files from the SD card or flash drive and streams the decoded audio data to the onboard CODEC (connected to the PIC32 using I2S).

The GUI provides standard audio player functionalities:

  • Track-list view
  • Play/pause button
  • Previous/next track buttons
  • Volume controls
  • Mute/unmute
  • Progress bar showing elapsed and total track time
  • Seek bar to seek the track in forward or reverse directions
  • Random shuffling of tracks using random number generation
  • Options to loop/un-loop the tracklist and looping of a single track
  • Settings window to allow the selection of media type and to change the background image

Figure 1.1 APP_Tasks()
Fig_1_1.png

Figure 1.1 shows the application tasks. The application has two tasks. These tasks run in a cooperative multitasking environment.
APP_SDCARD_AUDIO_Tasks (Figure 1.2)

  • Reads the WAV files from the media and streams it to the CODEC.

APP_DISPLAY_Tasks (Figure 1.4)

  • Handles the events generated by the GUI by passing them to the APP_SDCARD_AUDIO_Tasks task and forwards the handling of various GUI requests generated by the APP_SDCARD_AUDIO_Tasks to the GUI.

Figure 1.2 APP_SDCARD_AUDIO_Tasks()
Fig_1_2.png

Figure 1.2 shows the APP_SDCARD_AUDIO_Tasks.
This task connects to the SD card or flash drive and plays the WAV files found there. The initial states in this task sequentially progress through the following states:

  1. Enable the USB bus for Host mode of operation.
  2. Wait for the USB bus to be enabled.
  3. Wait until the file system is mounted on the currently selected media device (SD card/flash drive)
  4. Set the current drive path.
  5. Open audio CODEC.
  6. Set audio CODEC buffer completion event handler.
  7. Enter the Running state. This state searches all directories on the current media and saves the list of WAV files into a table. It then runs the Audio Player State Machine (Figure 1.3)

Figure 1.3 Audio Player State Machine
Fig_1_3.png

Figure 1.3 shows the Audio Player State Machine.
The audio player state machine then starts to run. The states perform the task of reading the audio data from the opened WAV audio file, decoding the WAV raw data through the decoder, and then passing the decoded buffer to the audio CODEC for playing. The states also handle track change events and waiting for the current audio buffer completion event from the audio CODEC. You will see these states described in more detail in Step 12.


Figure 1.4 APP_DISPLAY_Tasks()
Fig_1_4.png

Figure 1.4 shows the APP_DISPLAY_Tasks.
The APP_DISPLAY_Tasks registers a callback function with the GUI and the APP_SDCARD_AUDIO_Tasks() task to receive GUI events and requests. These events/requests are pushed into a common queue maintained by the display task. The display task services the events generated from the GUI (widget events generated by user touch) by passing them to the appropriate event handler in the APP_SDCARD_AUDIO_Tasks(). Similarly, the display task services the GUI requests generated by the APP_SDCARD_AUDIO_Tasks() task by calling the appropriate widget APIs in the GUI.

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/v2_01b)

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:

  • emwin_media_player contains the lab folder emwin_media_player_lab.
  • emwin_media_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.

Procedure:

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 in MPLAB X IDE
  • 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 – Verify I/O Pins using the Graphical Pin Manager

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
  • Step 2.4 - Configure I2S driver for CODEC
  • Step 2.5 - Configure CODEC Input Clock
  • Step 2.6 - Configure the DMA System Service for Audio
  • Step 2.7 - Verify/Set the I/O Pins used by I2S module

Step 3: Configure the SD card driver, SPI driver & File System

  • Step 3.1 - Configure the SD Card Driver
  • Step 3.2 - Configure the SPI Driver Used by the SD Card Driver
  • Step 3.3 - Map the SPI peripheral I/Os to I/O Pins
  • Step 3.4 - Configure the SD Card's File System Service

Step 4: Add WAV Decoder Functionality

Step 5: Configure USB Library and File System

  • Step 5.1 - Configure USB Library (Mass Storage Host)
  • Step 5.2 - Configure the File System Service for USB MSD media

Step 6: Configure SEGGER emWin Graphics Library

  • Step 6.1 - Enable the SEGGER emWin Graphics Library
  • Step 6.2 - Enable the SEGGER emWin Touch and GUI Wrappers

Step 7: Configure Graphics Display, Graphics Driver and Touch

  • Step 7.1 - Verify Graphics Display Configuration
  • Step 7.2 - Verify Graphics Display Driver (LCC) Configuration
  • Step 7.3 - Configure the display Touch driver to use I2C Driver Instance 1
  • Step 7.4 - Enable Touchscreen Control using I2C
  • Step 7.5 - Verify Touch System Service is Enabled
  • Step 7.6 - Set the I/O Pins Used by the I2C Module
  • Step 7.7 - Set the Interrupt Pin Used by the Touch Driver

Step 8: Enable Random Number Generator (RNG) System Service

Step 9: Generate Code

Step 10: Design Display GUI using SEGGER emWin Graphics Library

  • Step 10.1 - Design Audio Player GUI using GUI builder utility
  • Step 10.2 - Design Audio Player Settings GUI using GUI builder utility
  • Step 10.3 - Analyze C files generated by GUI Builder utility
  • Step 10.4 - Use Bitmap Converter utility to create image assets
  • Step 10.5 - Customize Volume Slider Widget
  • Step 10.6 - Respond to Widget Events

Step 11: Include Application Specific Source Code and Files

Step 12: Review the Application Code

Step 13: 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.