MPLAB® Harmony v2 Voice Recorder/Player 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 a voice recorder and player application running on the PIC32 Multimedia Expansion Board II (MEB II). The application reads WAV files from the SD card (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 the MPLAB Harmony Graphics Composer (MHGC) to create a touch-controlled display to control the voice recorder/player application.

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 Framework libraries to use the Graphics Library.

 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 (see the red cable in the Hardware Setup).

Apart from the hardware tools listed above, the following items are required for voice recorder and player:

  • Wired Stereo headset (Speaker + Microphone).
  • USB Type-A male to mini-B male cable for programming/debugging.

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
Installation
Instructions
Windows Linux Mac OSX
MPLAB® X
Integrated Development Environment
MPLAB® XC32
C/C++ Compiler
MPLAB® Harmony 1.xx
Integrated Software Framework

Overview:

This lab enables the MEB II Board to act as a voice recorder and player. The voice recording (mono) uses 16-bit data with a 16 kHz sampling rate. It supports three modes of operation:

  1. Loopback: The audio spoken on the microphone is looped back so that it can be heard on the headphone line of the headset.
  2. Record: The audio spoken on the microphone is recorded and stored as a WAV file on the SD card mounted on the development board.
  3. Playback: The voice recordings are played from the SD card over the I2S interface. This mode essentially implements the functionality of an audio player (for WAV files only). The implementation switches to the next track when the current track playing is completed (assuming that there is more than one track on the SD card).

Figure 1.1 APP_Tasks()

This figure shows the four application tasks:

  • APP_VOICE_RECORD_LOOP_Tasks: Handles the Loopback mode
  • APP_VOICE_RECORD_STORE_Tasks: Handles the Record mode
  • APP_SDCARD_AUDIO_Tasks: Handles the Playback mode
  • APP_DISPLAY_Tasks: Handles the display updates on the screen based on the mode selected
Fig_1_1.PNG
Click image to enlarge.
Click image to enlarge.

Figure 1.2 APP_VOICE_RECORD_LOOP_Tasks()

The APP_VOICE_RECORD_LOOP_Tasks task handles the Loopback mode application task related states and events.

Initially, the Loopback mode task state machine opens the CODEC driver in write_read mode and sets the buffer completion event handler. After that, it submits the first CODEC write_read request by writing dummy bytes to the CODEC. The state machine is moved to a buffer completion wait state. An event occurs when the last submitted write and read request is completed. In the event handler, ping-pong buffer logic manages the buffer completion and submits the next requests. The state is moved back to waiting for the buffer completion.

Fig_1_2.PNG
Click image to enlarge.
Click image to enlarge.

Figure 1.3 APP_VOICE_RECORD_STORE_Tasks()

The APP_VOICE_RECORD_STORE_Tasks task handles the Record mode application task-related states and events. The tasks sequentially progress through the following steps:

  1. Mount the file system for the SD card.
  2. Set the current drive path for the SD card.
  3. Open the CODEC to obtain a handle to the CODEC buffer.
  4. Register a CODEC buffer event handler function (called when the buffer read completes).
  5. Create a WAV audio file.
  6. Write header information to the WAV audio file.
  7. Submit Initial audio data read requests and enter the Idle/Wait state.
  8. When the audio data is read, the CODEC buffer read event is triggered. Write the audio data received to the WAV file in SD card and check if the data size is less than 30 seconds of audio.
  9. If the data size is less than 30 seconds of audio, enter the idle/wait state to receive further audio recording data from the user.
  10. If the data size is greater than or equal to 30 seconds, update the WAV file header and close the WAV file.
  11. Prompt the user for any further recording. If a further recording is needed move to step 5.
Fig_1_3.PNG
Click image to enlarge.
Click image to enlarge.

Figure 1.4 APP_SDCARD_AUDIO_Tasks()

The APP_SDCARD_AUDIO_Tasks task handles the Playback mode application task related states and events. The initial states in this task sequentially progress through the following states:

  1. Open the CODEC to obtain a handle to the CODEC buffer.
  2. Register a CODEC buffer event handler function (called when the buffer transfer completes).
  3. Mount the file system for the SD card.
  4. Set the current drive path for the SD card.
  5. After this, a new state is added: STATE RUNNING (See Figure 1.5 below). This state searches all directories on the SD card, and saves a list of WAV audio files into a table.
Fig_1_4.png
Click image to enlarge.
Click image to enlarge.

Figure 1.5 Audio Player State Machine

The audio player state machine then starts to run. The audio player states perform the following tasks:

  • Read the audio data from the opened WAV audio file
  • Decode the WAV raw data through the decoder
  • Pass the decoded buffer to the audio CODEC for playing
  • Handle track change events
  • Wait for the current audio buffer completion event from the audio CODEC
Fig_1_5.png
Click image to enlarge.
Click image to enlarge.

Figure 1.6 APP_DISPLAY_Tasks()

The APP_DISPLAY_Tasks task handles the display task functionality. Depending on the mode selected (Loopback, Record, or Playback), the task updates the graphics display with the relevant display controls. Additionally it enables/disables the mode, Start/Stop button, depending on the action performed on the display controls.

Fig_1_6.PNG
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, MPLAB X IDE will not be able to find all source files for the lab solution and the solution project will not build successfully.


Extracting the .zip file will create the following folders:

  • voice_recorder contains the lab folder voice_recorder_lab.
  • voice_recorder_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
  • 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 the SD card driver, SPI driver and File System

  • Step 3.1 - Configure SD card driver
  • Step 3.2 - Configure the SPI driver used by the SD card driver
  • Step 3.3 - Verify/Set the I/O Pins used by I2S module using the Graphical Pin Manager
  • Step 3.4 - Configure the File System Service used by the SD card

Step 4: Add WAV Decoder Functionality
Step 5: Design Display GUI, and Configure the Touch and I2C Driver

  • Step 5.1 - Rename the Screen Name
  • Step 5.2 - Import Images and Fonts (Assets)
  • Step 5.3 - Create Schemes (assign standard colors and fonts)
  • Step 5.4 - Add Primitive Objects (rectangle, image, text)
  • Step 5.5 - Add Widget Objects (list-box, buttons, slider)
  • Step 5.6 - Configure the Graphics display and Graphics Touch Driver
  • Step 5.7 - Enable Touchscreen Control using I2C
  • Step 5.8 - Enable the Graphics Touch System Service

Step 6: Generate Harmony Code
Step 7: Include Application Specific Source Code and Files
Step 8: Review the Application Code
Step 9: 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.