1.1.1 AK4953 CODEC Driver Library Help

This topic describes the AK4953 Codec Driver Library.

Introduction

This topic describes the basic architecture of the AK4953 Codec Driver Library and provides information and examples on its use.

Interface Header File: drv_AK4953.h

The interface to the AK4953 Codec Driver library is defined in the audio/driver/codec/AK4953/drv_AK4953.h header file. Any C language source (.c) file that uses the AK4953 Codec Driver library should include this header.

Library Source Files:

The AK4953 Codec Driver library source files are provided in the audio/driver/codec/AK4953/src directory. This folder may contain optional files and alternate implementations. Please refer to Configuring the Library for instructions on how to select optional features and to Building the Library for instructions on how to build the library.

Abstraction Model

This library provides a low-level abstraction of the AK4954 Codec Driver Library on the Microchip family microcontrollers with a convenient C language interface. This topic describes how that abstraction is modeled in software and introduces the library's interface.

Description

The abstraction model shown in the following diagram depicts how the AK4953 Codec Driver is positioned in the MPLAB Harmony framework. The AK4953 Codec Driver uses the I2C and I2S drivers for control and audio data transfers to the AK4953 module.

AK4953 Driver Abstraction Model

Library Overview

Refer to the Driver Library Overview section for information on how the driver operates in a system.

The AK4953 Codec Driver Library provides an API interface to transfer control commands and digital audio data to the serially interfaced AK4953 Codec module. The library interface routines are divided into various sub-sections, which address one of the blocks or the overall operation of the AK4953 Codec Driver Library.

Library Interface SectionDescription
System FunctionsProvides system module interfaces, device initialization, deinitialization, reinitialization, tasks and status functions.
Client Setup FunctionsProvides open and close functions.
Data Transfer FunctionsProvides data transfer functions, such as Buffer Read and Write.
Settings FunctionsProvides driver specific functions for settings, such as volume control and sampling rate.
Other FunctionsMiscellaneous functions, such as getting the driver’s version number and syncing to the LRCLK signal.
Data Types and ConstantsThese data types and constants are required while interacting and setting up the AK4953 Codec Driver Library.

Note: All functions and constants in this section are named with the format DRV_ AK4953_xxx, where 'xxx' is a function name or constant. These names are redefined in the appropriate configuration’s configuration.h file to the format DRV_CODEC_xxx using #defines so that code in the application that references the library can be written as generically as possible (e.g., by writing DRV_CODEC_Open instead of DRV_ AK4953_Open etc.). This allows the codec type to be changed in the MHC without having to modify the application’s source code.

How the Library Works

How the Library Works

The library provides interfaces to support:

  • System Functionality

  • Client Functionality

Description

Setup (Initialization)

This topic describes system initialization, implementations, and includes a system access code example.

Description

The system performs the initialization of the device driver with settings that affect only the instance of the device that is being initialized. During system initialization in the system_init.c file, each instance of the AK4953 module would be initialized with the following configuration settings (either passed dynamically at run time using DRV_AK4953_INIT or by using Initialization Overrides) that are supported by the specific AK4953 device hardware:

  • Device requested power state: one of the System Module Power States. For specific details please refer to Data Types and Constants in the Library Interface section.

  • I2C driver module index. The module index should be same as the one used in initializing the I2C Driver

  • I2S driver module index. The module index should be same as the one used in initializing the I2S Driver

  • Sampling rate

  • Volume

  • Audio data format. The audio data format should match with the audio data format settings done in I2S driver initialization

  • Determines whether or not the microphone input is enabled

The DRV_AK4953_Initialize API returns an object handle of the type SYS_MODULE_OBJ. The object handle returned by the Initialize interface would be used by the other system interfaces such as DRV_AK4953_Deinitialize, DRV_AK4953_Status and DRV_I2S_Tasks.

Client Access

This topic describes driver initialization and provides a code example.

Description

For the application to start using an instance of the module, it must call the DRV_AK4953_Open function. The DRV_AK4953_Open function provides a driver handle to the AK4953 Codec Driver instance for operations. If the driver is deinitialized using the function DRV_AK4953_Deinitialize, the application must call the DRV_AK4953_Open function again to set up the instance of the driver.

For the various options available for IO_INTENT, please refer to Data Types and Constants in the Library Interface section.

Note: It is necessary to check the status of driver initialization before opening a driver instance. The status of the AK4953 Codec Driver can be known by calling DRV_AK4953_Status.

Example:

DRV_HANDLE handle; SYS_STATUS ak4953Status;

ak4953Status Status = DRV_AK4953_Status(sysObjects.ak4953Status DevObject); if (SYS_STATUS_READY == ak4953Status)

{

\/\/ The driver can now be opened.

appData.ak4953Client.handle = DRV_AK4953_Open

(DRV_AK4953_INDEX_0, DRV_IO_INTENT_WRITE | DRV_IO_INTENT_EXCLUSIVE); if(appData.ak4953Client.handle != DRV_HANDLE_INVALID)

{

appData.state = APP_STATE_AK4953_SET_BUFFER_HANDLER;

}

else

{

SYS_DEBUG(0, "Find out what's wrong \r\n");

}

}

else

{

\/* AK4953 Driver Is not ready *\/

}

Client Operations

This topic provides information on client operations.

Description

Client operations provide the API interface for control command and audio data transfer to the AK4953 Codec.

The following AK4953 Codec specific control command functions are provided:

  • DRV_AK4953_SamplingRateSet

  • DRV_AK4953_SamplingRateGet

  • DRV_AK4953_VolumeSet

  • DRV_AK4953_VolumeGet

  • DRV_AK4953_MuteOn

  • DRV_AK4953_MuteOff

These functions schedule a non-blocking control command transfer operation. These functions submit the control command request to the AK4953 Codec. These functions submit the control command request to I2C Driver transmit queue, the request is processed immediately if it is the first request, or processed when the previous request is complete.

DRV_AK4953_BufferAddWrite, DRV_AK4953_BufferAddRead, and DRV_AK4953_BufferAddWriteRead are buffered data operation functions. These functions schedule non-blocking audio data transfer operations. These functions add the request to I2S Driver transmit or receive buffer queue depends on the request type, and are executed immediately if it is the first buffer, or executed later when the previous buffer is complete. The driver notifies the client with

DRV_AK4953_BUFFER_EVENT_COMPLETE, DRV_AK4953_BUFFER_EVENT_ERROR, or DRV_AK4953_BUFFER_EVENT_ABORT events.

Note: It is not necessary to close and reopen the client between multiple transfers.

Configuring the Library

The configuration of the I2S Driver Library is based on the file configurations.h, as generated by the MHC.

This header file contains the configuration selection for the I2S Driver Library. Based on the selections made, the I2S Driver Library may support the selected features. These configuration settings will apply to all instances of the I2S Driver Library.

This header can be placed anywhere; however, the path of this header needs to be present in the include search path for a successful build. Refer to the Applications Help section for more details.

System Configuration

Configuring MHC

Provides examples on how to configure the MPLAB Harmony Configurator (MHC) for a specific driver.

Description

When building a new application, start by creating a 32-bit MPLAB Harmony 3 project in MPLAB X IDE by selecting File > New Project. Chose the Configuration name the based on the BSP, and select the appropriate processor.

In the MHC, under Available Components select the appropriate BSP. Under Audio>Templates, double-click on a codec template such as AK4953. Answer Yes to all questions.

You should end up with a project graph that looks like this, after rearranging the boxes:

Click on the AK4953 Driver component (not AK4953 Codec) and the following menu will be displayed in the Configurations Options:

  • I2C Driver Used will display the driver instance used for the I2C interface.

  • I2S Driver Used will display the driver instance used for the I2S interface.

  • Usage Mode indicates whether the AK4953 is a Host (supplies I2S clocks) or a Client (MCU supplies I2S clocks).

  • Number of AK4953 Clients indicates the maximum number of clients that can be connected to the AK4953 Driver.

  • Sampling Rate indicates the number of samples per second per channel, 8000 to 96,000.

  • Volume indicates the volume in a linear scale from 0-255.

• Audio Data Format is either

  • 24-bit Left Justified (ADC), 24-bit Right-justified(DAC)

  • 24-bit Left Justified (ADC), 16-bit Right-justified(DAC)

  • 24-bit Left Justified (ADC), 24-bit Left-justified(DAC)

  • 24/16-bit I2S

It must match the audio protocol and data length set up in either the SSC or I2S PLIB.

  • Microphone/Line Input selects which microphone or line input is selected, either:

  • Internal Mic (mounted on the AK4953 daughterboard)

  • External Mic Input

  • Line Input

If External Mic input or Line Input is selected, then the following option is provided:

• Ext Mic Gain in dB range 0(min) to 31(max)

If External Mic input is selected, then the following option is provided:

Enable Microphone Bias should be checked if using an electret microphone.

You can also bring in the AK4953 Driver by itself, by double clicking AK4953 under Audio_->Driver->Codec_ in the Available Components list. You will then need to add any additional needed components manually and connect them together.

Note that the AK4953 requires the TCx Peripheral Library and TIME System Service in order to perform some of its internal timing sequences.

Bulding the Library

This section lists the files that are available in the AK4953 Codec Driver Library.

Description

This section lists the files that are available in the src folder of the AK4953 Codec Driver. It lists which files need to be included in the build based on either a hardware feature present on the board or configuration option selected by the system.

The following three tables list and describe the header (.h) and source (.c) files that implement this library. The parent folder for these files is audio/driver/codec/AK4953\. Interface File(s)

This table lists and describes the header files that must be included (i.e., using #include) by any code that uses this library.

Source File NameDescription
drv_ak4953.hHeader file that exports the driver API.

Required File(s)

All of the required files listed in the following table are automatically added into the MPLAB X IDE project by the MHC when the library is selected for use.

This table lists and describes the source and header files that must always be included in the MPLAB X IDE project to build this library.

Source File NameDescription
/src/drv_ak4953.cThis file contains implementation of the AK4953 Codec Driver.

Optional File(s)

This table lists and describes the source and header files that may optionally be included if required for the desired implementation.

Source File NameDescription
N/ANo optional files are available for this library.

Module Dependencies

The AK4953 Codec Driver Library depends on the following modules:

  • I2S Driver Library

  • I2C Driver Library

Library Interface

Client Setup Functions

NameDescription
DRV_AK4953_CloseCloses an opened-instance of the AK4953 driver
DRV_AK4953_CommandEventHandlerSetThis function allows a client to identify a command event handling function for the driver to call back when the last submitted command have finished.
DRV_AK4953_OpenOpens the specified AK4953 driver instance and returns a handle to it.
DRV_AK4953_BufferEventHandlerSetThis function allows a client to identify a buffer event handling function for the driver to call back when queued buffer transfers have finished.

Data Transfer Functions

NameDescription
DRV_AK4953_BufferAddReadSchedule a non-blocking driver read operation.
DRV_AK4953_BufferAddWriteSchedule a non-blocking driver write operation.
DRV_AK4953_BufferAddWriteReadSchedule a non-blocking driver write-read operation.
DRV_AK4953_ReadQueuePurgeRemoves all buffer requests from the read queue.
DRV_AK4953_WriteQueuePurgeRemoves all buffer requests from the write queue.

Data Types and Constants

NameDescription
DRV_AK4953_AUDIO_DATA_FORMATIdentifies the Serial Audio data interface format.
DRV_AK4953_BUFFER_EVENTIdentifies the possible events that can result from a buffer add request.
DRV_AK4953_BUFFER_EVENT_HANDLERPointer to a AK4953 Driver Buffer Event handler function
DRV_AK4953_BUFFER_HANDLEHandle identifying a write buffer passed to the driver.
DRV_AK4953_CHANNELIdentifies Left/Right Audio channel
DRV_AK4953_COMMAND_EVENT_HANDLERPointer to a AK4953 Driver Command Event Handler Function
DRV_AK4953_DIGITAL_BLOCK_CONTROLIdentifies Bass-Boost Control function
DRV_AK4953_INITDefines the data required to initialize or reinitialize the AK4953 driver
DRV_AK4953_INT_EXT_MICIdentifies the Mic input source.
DRV_AK4953_MICThis is type DRV_AK4953_MIC.
DRV_AK4953_MONO_STEREO_MICIdentifies the Mic input as Mono / Stereo.

Other Functions

NameDescription
DRV_AK4953_GetI2SDriverGet the handle to the I2S driver for this codec instance.
DRV_AK4953_LRCLK_SyncSynchronize to the start of the I2S LRCLK (left/right clock) signal
DRV_AK4953_VersionGetThis function returns the version of AK4953 driver.
DRV_AK4953_VersionStrGetThis function returns the version of AK4953 driver in string format.

Settings Functions

NameDescription
DRV_AK4953_IntExtMicSetThis function sets up the codec for the X32 DB internal or the external microphone use.
DRV_AK4953_MicGainGetThis function gets the microphone gain for the AK4953 Codec.
DRV_AK4953_MicGainSetThis function sets the microphone gain for the AK4953 CODEC.
DRV_AK4953_MicMuteOffUmutes th AK4953's microphone input.
DRV_AK4953_MicMuteOnMutes the AK4953's microphone input
DRV_AK4953_MicSetThis function sets up the codec for the internal or the AK4953 Mic1 or Mic2 input.
DRV_AK4953_MonoStereoMicSetThis function sets up the codec for the Mono or Stereo microphone mode.
DRV_AK4953_MuteOffThis function disables AK4953 output for soft mute.
DRV_AK4953_MuteOnThis function allows AK4953 output for soft mute on.
DRV_AK4953_SamplingRateGetThis function gets the sampling rate set on the DAC AK4953.
DRV_AK4953_SamplingRateSetThis function sets the sampling rate of the media stream.
DRV_AK4953_VolumeGetThis function gets the volume for AK4953 Codec.
DRV_AK4953_VolumeSetThis function sets the volume for AK4953 Codec.

System Interaction Functions

NameDescription
DRV_AK4953_DeinitializeDeinitializes the specified instance of the AK4953 driver module.
DRV_AK4953_EnableInitializationEnable delayed initialization of the driver.
DRV_AK4953_InitializeInitializes hardware and data for the instance of the AK4953 Codec module.
DRV_AK4953_IsInitializationDelayedChecks if delayed initialization of the driver has been requested.
DRV_AK4953_StatusGets the current status of the AK4953 driver module.
DRV_AK4953_TasksMaintains the driver's control and data interface state machine.