How to configure CODEC driver using MPLAB Harmony

Explains how to configure codec driver in MPLAB Harmony.

Transferring audio such as voice or music is such a frequent design requirement in embedded system with audio application, that I2S(Inter IC Sound) protocol has been developed by electronics industry. Microchip PIC32 microcontroller supports I2S for audio transfer between the microcontroller and the external peripherals such as DAC(Digital to Analog Converter). Here you will learn how I2S is used with the help of CODEC in MPLAB Harmony. This concept of I2S and CODEC is useful in understanding electrical and electronic engineering technology. No today’s electronic engineering degree or electronics engineering schools will teach these things so this should help you.

Most of DAC or similar components also requires control interface, that is, microcontroller can control the external device using the control interface. For this, usually, SPI(Serial Peripheral Protocol) is used. An example of interfacing Microcontroller with external DAC for audio transfer is shown below.

How to configure CODEC driver using MPLAB Harmony

As shown in the figure above, the microcontroller represents the PIC32 microchip microcontroller.

Most of the modern DAC has support for SPI control interface and I2S data interface. In Microchip development boards, the DAC used are Asahi Kasei Microdevices DACs such as AK4384, AK4642, AK4943 etc. These DAC differ in features they support such as I2S transfer mode, digital filters etc.

In MPLAB Harmony framework, the microcontroller software that handles the data and control is the CODEC driver. There are different codec driver depending on which DAC is used such as AK4384, AK4642, AK4943 etc.

To understand how to use these CODEC driver, one has to understand how the driver interacts with the system and the user or client application. Like most of the drivers, the CODEC driver interaction with the system is the initialization of the CODEC driver, obtaining a handle to the CODEC driver and maintaining its state machine. Thus all the functions are either related to the system or meant for client application.

One has to remember that the CODEC driver uses the I2S driver and can use SPI driver. When using the MHC via the MPLABX IDE, when the CODEC driver is selected to be used, the I2S driver and optionally the SPI driver are automatically selected. Which SPI and I2S driver will be used with which CODEC driver is determined during the CODEC initialization data structure(explained below).

Note: to know how to do the following code via projects and MHC see USART read write tutorial using MPLAB Harmony.

Codec System Interaction

By codec system interaction we mean here creating data initialization structure, using this to open the codec driver, obtaining system handle to the codec driver and finally maintaining its state machine via system tasks.

The first step is to create a codec initialization data structure. This is then later used to register a system handle to the driver which in turn is used to start the state machine of the driver by the system.

Example of codec initialization data structure is below.

The configuration setting for the data structure are the device requested power state, SPI driver module index, I2S driver module index, sampling rate, master clock detection mode, power down pin port initialization and queue size for audio data transfer. Some of the configuration are optional.

After the codec data structure is created, the next step is to obtain system object handle which will initialize the codec. This is done using the DRV_AK4384_Initialize function which returns system object handle of type SYS_MODULE_OBJ.

Once the handle to the driver for the system is obtained, the state machine of driver is configured using the DRV_AK4384_Tasks() as shown below.

CODEC Client Interaction

The codec client interaction involves- opening the CODEC to obtain client handle to the CODEC driver then using it for application functions, callbacks from the driver state machine etc.

But unlike in many drivers in Harmony it is necessary to get first the status of the codec driver. The system object handle is used as parameter to the function DRV_CODEC_Status() to get the status driver.This function returns status of the type SYS_STATUS. An example code is shown below,

If the codecStatus is SYS_STATUS_READY then we can proceed to open the CODEC driver using the function DRV_CODEC_Open(). It returns an object handle of the driver for the client application. The client handle must be of type DRV_HANDLE. An example code for this process for obtaining client handle is below,

Now that we have obtained the client handle to the CODEC driver is obtained, we can setup CODEC specific functionality using the provided functions and then register event handlers for command and data buffer.

By codec specific functionality, we mean that different codec driver like the AK4384 driver or the AK4642 driver can have different configuration functions. For example, for AK4384 codec driver we can use the following provided function to set different functionality of the codec.


After we have set up required function we can then register event handlers. In case of CODEC driver there are two types of event handlers. The first is the command event handler and the second is the buffer event handler for the codec. The command event handler set is for the control related information transfer between the codec driver within the microcontroller and the external DAC. Such command includes setting sampling rate, zero mute, setting volume etc. The buffer event handler set is for transferring the actual audio data.

The command event handler set is registered using the function  DRV_AK4384_CommandEventHandlerSet() and the buffer event handler is registered using the DRV_AK4384_BufferEventHandlerSet() function.

An example of how to register these two codec event handler is as follows.

First a structure for the codec client is created. In it the member includes a codec driver handle, a buffer handle, buffer event handler, command event handler, context, a pointer to transmit buffer object and buffer size. These will be later used to register event handlers.

After checking the driver status, opening the driver, setting configuration for specific codec driver, the event handlers are registered. Example for command type and buffer type event handler for the codec is shown below.

Registration of command event handler

Registration of buffer event handler

Appropriate call back functions to the event handler have to be created to handle the event generated by respective event handler set.

Below is example code for call back functions:

So in this way, you can utilize CODEC implementation for embedded system with audio application. CODEC, I2S, SPI are essential part of today’s electronics engineering technology and should be helpful in your career. No today’s electronic engineering degree or electronics engineering schools will teach these things.

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Leave a Reply

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

USART read write tutorial using MPLAB Harmony

USART read write tutorial using MPLAB Harmony

I2S driver tutorial with MPLAB Harmony

I2S driver tutorial with MPLAB Harmony