Audio-Tone Generation Using a Lookup Table With MPLAB® Harmony v3: Step 9

Last modified by Microchip on 2023/11/09 09:07

Review the Application Code

C LANGUAGE REFRESHER: enumerated types and structures
In case you need it, here is a refresher on enum and struct types.

C Refresher

MPLAB Tips

Application File: app_tone_lookup_table.h

Open file app_tone_lookup_table.h. This file defines application states, data, and APIs.

Application states corresponding to the state machine, described in the "Overview" section above, are shown in the following image:Application States​​

Observe the application data structure shown in the following image. Notice the variable state. The variable codec is for the data type corresponding to the AK4953 codec.

application Data Structure​​

Observe the CODEC data structure in the following image.

  • handle refers to the instance of the opened AK4953 codec driver.
  • Buffered Harmony driver APIs (e.g., DRV_I2S_BufferAddWrite) returns a handle for each data buffer submitted for processing (transmission/reception). writeBufHandle holds the buffer handle for the audio data buffers submitted for transmission using the codec API DRV_CODEC_BufferAddWrite.
  • The application can register an event handler to notify when the submitted transmission/reception request is completed. bufferHandler holds the address of the function which is called when the audio data buffer has been transferred by the codec driver.
  • context holds an application-specific value passed to the driver.
  • txbufferObject holds the address of the audio data that needs to be transferred to the codec. In this example, it will hold the address of a sine tone lookup table.
  • bufferSize holds the size of the audio data buffer.

CODEC Data Structure​​


Application APIs for initialization, task state machine, and buffer completion events are declared.

Application File: app_tone_lookup_table.c


Open the app_tone_lookup_table.c file. This file defines the tone lookup table and implements the APIs.

The implementation of function APP_TONE_LOOKUP_TABLE_Initialize sets up the default state of tone lookup table task and initializes the variables for the codec driver.

The APP_TONE_LOOKUP_TABLE_Tasks function implements the task’s state machine as shown in the figure in section Overview above. It starts in the APP_TONE_LOOKUP_TABLE_STATE_CODEC_OPEN state and remains in it until it opens the codec driver and gets a valid driver handle.

In state APP_TONE_LOOKUP_TABLE_STATE_CODEC_ADD_BUFFER, the application submits audio data buffers to the driver queue and enters a waiting state (APP_TONE_LOOKUP_TABLE_STATE_CODEC_WAIT_FOR_BUFFER_COMPLETE) for the last submitted buffer to complete. The number of buffers queued is equal to queue size - 1 (i.e., 2 buffers).

The DMA transfers the last buffer and the codec calls the event handler when the buffer transmission is completed. In the event handler APP_TONE_LOOKUP_TABLE_BufferEventHandler, the state of the task is changed from waiting to complete.

A new buffer is submitted from the state APP_TONE_LOOKUP_TABLE_STATE_CODEC_BUFFER_COMPLETE and the state machine goes to the waiting state again.
The cycle of waiting / complete state continues.