Step 2: Configure Audio CODEC, I2C and I2S Drivers
Step 2.1: Configure Audio CODEC (AK4953)
1
Click on the Options tab in the MPLAB Harmony Configurator main window.
Expand the Harmony Framework Configuration > Drivers > CODEC selection tree.
Remember, if you ever “lose” the MHC Tool, you can find it here:
Tools > Embedded > MPLAB Harmony Configurator.
2
Check the Use Codec AK 4953? Box
3
A default value for Volume is specified. You can change the value depending on your requirements.
4
Do not check Specify MCLK value. A default value of 256 for MCLK is selected under I2S driver configuration options MCLK Sampling rate Multiplier
5
Only one client of the CODEC driver needs to be opened, therefore retain the Number of AK4953 Driver Clients value to 1.
6
The AK4953 Driver supports only one instance of AK4953 CODEC module. AK4953 driver is a single instance driver. Therefore default instance of Codec AK4953 Driver Instance 0 is selected.
7
Select the I2S driver (used for data interface) instance as DRV_I2S_INDEX_0. The AK4953 driver uses the I2S interface for the Audio data transfer to/from the PIC32 microcontroller. The PIC32MZ2048EFH144 device supports multiple instances of I2S peripheral modules. You need to specify which instance (Instance 0 in this case) of the I2S peripheral is used for this driver.
8
Select the I2C driver (used for control interface) instance as DRV_I2C_INDEX_0 or 0. The AK4953 driver uses I2C interface for the Audio control transfers to/from the PIC32 microcontroller. The control transfers exchanges commands to/from the AK4953 CODEC to configure and/or read the initialized/existing configuration. PIC32MZ2048EFH144 device supports multiple instances of I2C modules. An instance (Instance 0) for Audio control communication is selected.
The CODEC devices need a fine clock source to generate accurate audio sampling rates. The clock source (MCLK from the CODEC device’s perspective) can be generated internally by the CODEC device.
PIC32 devices have a flexible reference clock output. The reference clock output module (REFCLKO) can be used to generate the fractional clock that can be used by audio CODEC/DACs to accommodate various sample rates.
The MCLK value represents the multiplier to the sampling frequency (LRCK) which produces the value for the master/reference clock (MCLK/REFCLOCK) to the CODEC. This value should be one of the fs values supported by the CODEC for various sampling rates.
For example: For 256 fs, The MCLK value of 256 for a sampling rate 48000 Hz would generate a MCLK/REFCLOCK of 12288000 Hz. By default, an MCLK value of 256 is selected. This is also shown in the default I2S driver configuration options MCLK Sampling rate Multiplier. If you intend to select a different MCLK value, check Specify MCLK value. It shows the following selectable options: 128, 192, 256, 384, 512, 768, and 1152.
Step 2.2: Configure I2C driver for CODEC
In AK4953 CODEC driver configurations, I2C is selected as the control interface medium (expand Harmony Framework Configuration > Drivers > CODEC > Use Codec AK4953? selection tree to see this). Next, configure the I2C interface driver.
1
Expand the Harmony Framework Configuration > Drivers > I2C selection tree.
2
The Use I2C Driver? is checked by default. MHC selected this for you because the AK4953 CODEC driver uses the I2C driver.
3
Select Dynamic for the Driver Implementation. A "Dynamic" I2C driver implementation will allow the implementation to be used for another instance of I2C module in this lab (needed for the touch screen).
4
Check the Interrupt mode option. This allows I2C data completion events to be detected asynchronously, without the need for polling for the transfer to complete. This is necessary because by the end of the lab, you will add a number of Harmony drivers, system services and middleware libraries. All these Harmony modules have to cooperatively run without blocking the processor.
5
Keep the Number of I2C Driver Clients and Number of I2C Driver Instances set to "1" since at this time I2C is only used to interface with the AK4953 CODEC. You will increase this number in a following lab to enable this driver to support multiple I2C interfaces.
6
Check Include Force Write I2C Function. This will include an API that sends data to the slave even if the slave NACKs data. This is needed by the AK4953 CODEC since it NACKs I2C data during the initialization sequence.
7
Under I2C Driver Instance 0, check the Use Bit Bang I2C Implementation? option.
8
Select I2C_ID_2 as I2C Module ID. I2C module instance 2 is interfaced to the AK4953 CODEC’s control interface.
"I2C_ID_2" refers to the PIC32 I2C2 peripheral (using pins SCL2 & SDA2). These pins are connected to the AK4953 audio CODEC on the MEB II board.
9
Retain the default value DRV_I2C_MODE_MASTER as Operation Mode. PIC32 I2C module 2 will act as the master, and the AK4953 CODEC module control interface (I2C) will act as the slave.
10
Retain the Bit Bang Timer Source as DRV_TMR_INDEX_9. Timer module 9 will be used to clock the I2C bit-bang state machine. Based on the selected baud rate (I2C CLOCK FREQUENCY (HZ)), the I2C driver will configure the timer to generate interrupts at appropriate intervals. You can select any Timer module as the Bit Bang Timer Source, as long as the timer source is not used by another module.
11
Change the I2C CLOCK FREQUENCY (HZ) to 25 KHz.
12
Change the I2C Interrupt Priority to INT_PRIORITY_LEVEL_5, and the I2C Interrupt Sub-priority to INT_SUBPRIORITY_LEVEL0. For I2C configured in bit bang mode, these interrupt priorities indicate the interrupt priority of the timer module (DRV_TMR_INDEX_9).
Step 2.3: Verify the I/O Pins Used by I2C Module
Step 2.4: Configure I2S driver for CODEC
1
In AK4953 CODEC driver configurations, "I2S" is selected as the Audio data interface medium. Next, configure the I2S interface driver.
Expand the Harmony Framework Configuration > Drivers > I2S selection tree.
2
The Use I2S Driver? is checked by default. This is done since the AK4953 CODEC driver uses the I2S driver.
3
Expand the Use I2S Driver? Option.
4
"Dynamic" for Driver Implementation is selected by default and greyed out. The I2S driver does not support static implementation yet.
5
Check the box beside DMA Mode. Under DMA Mode, make sure the Transmit DMA Support and Enable DMA Channel Interrupts? boxes are checked. Transmit DMA Support is enabled to use DMA channels to transfer Audio Data from memory to I2S buffers. DMA Channel is interrupt enabled to allow DMA transfer completion notification.
6
Retain Stop in Idle Mode as it is.
7
Retain default Sampling Rate as 48000. This is the initial value of sampling rate. This will be changed by the program at run time based on the sampling rate specified in the WAV file.
8
Retain default value of 256 for MCLK Sampling rate Multiplier. The AK4953 CODEC supports 256fs with 48000 Hz sampling rate for generating the REFCLOCK.
9
Retain default value of 4 for Master Clock/Bit Clock ratio.
For sampling rate 48000 Hz and MCLK Sampling rate multiplier value of 256, The Mater Clock (MCLK) or Reference clock (REFCLOCK) = 256 * 48000 = 12,288,000 Hz. The common Bit Clock that can be generated for the given combination of MCLK and Sampling rate would be REFCLOCK/1, REFCLOCK/2, REFCLOCK/4 or REFCLOCK/8.
For Example: AK4953 CODEC supports bit clock (BCLK) value as either 32fs or 64fs. For 48000 Hz sampling rate, the Bit clock (BCLK) would be 64 * 48000 = 3072000 Hz. Therefore the MCLK/BCLK ratio is 12288000/3072000 = 4.
10
Retain default value of 1 set for Number of I2S Driver Clients and Number of I2S Driver Instances . The I2S interface is used only for Audio playback, therefore one instance of I2S driver and its client is sufficient.
11
Under I2S Driver Instance 0, Retain SPI_ID_1 as I2S Module ID. The PIC32's SPI/I2S1 peripheral (PIC32 pins: SCK1, SDI1, SDO1, /SS1) is interfaced to the AK4953 CODEC’s data lines.
12
Retain default value of Usage Mode as DRV_I2S_MODE_MASTER. It indicates whether the I2S instance will act as a Master or Slave. In Master mode PIC32 generates the BCLK to the Slave. In Slave mode PIC32 receives BCLK from the I2S Master. In the interface to AK4953 CODEC. PIC32 I2S will act as a Master and hence generates the BCLK (Bit Clock).
13
Use the Reference Clock (198MHz) to drive the SPI baud rate generator. For the "Baud Clock", select SPI_BAUD_RATE_MCLK_CLOCK. The SPI Baud Rate Generator can be driven by Peripheral Bus Clock #2 (PBCLK) or Reference Clock 1 (MCLK).
14
Retain default value of Clock Mode as
DRV_I2S_CLOCK_MODE_IDLE_HIGH_EDGE_FALL. The default polarity for I2S protocol.
15
Change Audio Communication Width to
SPI_AUDIO_COMMUNICATION_16DATA_16FIFO_32CHANNEL. In the scope of this lab CD quality Audio playback is supported, therefore setting 16 bit Audio data per channel, 32 bit channel width.
16
Retain default value of Audio Mode as SPI_AUDIO_TRANSMIT_STEREO. The lab supports stereo Audio Playback.
17
Retain default value of Input Sample Phase Selection as SPI_INPUT_SAMPLING_PHASE_IN_MIDDLE. The default phase for I2S protocol.
18
Retain default value of Audio Protocol Mode as DRV_I2S_AUDIO_I2S. I2S protocol interface mode is chosen as it is supported by AK4953 CODEC.
19
Retain Queue Size Transmit to 3. This will allow queuing of up to 3 Audio buffer objects at a time.
20
Set Queue Size Receive to 1. Receive path is not utilized and hence a minimal value of 1 buffer object is set.
21
Retain Transmit DMA Channel Instance to 0. DMA Channel Instance 0 is used for Audio data transfer.
Step 2.5: Configure CODEC Input Clock
The PIC32 acts as I2S Master to the AK4953 CODEC. It needs to provide its "Reference Clock Generator Output 1" signal (REFCLKO1) to the CODEC's "External Master Clock Input" (MCKI) in addition to the Bit clock (BCLK).
1
Expand the Harmony Framework Configuration > Options > System Services > Clock selection tree.
2
Expand Use Clock System Service?
3
Click Execute on Launch Clock Configurator
4
Locate the Reference Clock block. By default Reference Clock #1 tab is selected. The Reference Clock #1 enables configuring reference clock to SPI/I2S, PPS.
5
Check ON box. This enables the reference clock circuitry.
7
Click the Auto-Calculate button to generate the Reference Clock #1. It opens up a window Reference Clock Divisor and Trim Auto Calculator
8
Verify that the Reference Clock Divisor and Trim Auto Calculator show the desired Reference Clock and Sampling values to be generated. The values are generated from the MCLK multiplier and sampling rate specified in CODEC/I2S driver configuration.
9
Select the radio button Target I2S Input Frequency and click Apply button. The Target I2S Input Frequency is the reference clock input to the AK4953 CODEC.
If Target I2S Input Frequency is shown as 0, some I2S configuration is not done properly. Deselect and Re-configure the I2S driver as explained above and come back to this step.
10
Click on the OE button to enable the Reference Clock #1 output.
It is important to note that a reference clock of 12288000 Hz cannot be accurately generated from a system clock of 200 MHz. It is for this reason that the system clock is configured to 198 MHz, during the oscillator settings step.
Step 2.6: Configure the DMA System Service for Audio
1
The I2S driver has been configured to use a DMA channel for transferring Audio data to the CODEC.
2
Expand the Harmony Framework Configuration > System Services > DMA selection tree.
3
Expand Use DMA System Service? It is enabled by default as the I2S driver is configured to use DMA for transferring audio data to the CODEC.
4
Retain Select Service Mode as Dynamic. This enables the same driver instance to manage multiple DMA channels (if required).
5
Keep the Number of DMA Channel Instances at 1. One DMA channel instance is used by I2S.
6
Expand the DMA Channel Instance 0. This is the instance set by the I2S driver for transferring audio data.
7
Under DMA Channel, Select DMA_CHANNEL_2. DMA Channel 2 will be used for I2S data transfer.
Step 2.7: Set the I/O Pins Used by the I2S Module
Select the MPLAB Harmony Configurator tab and Pin Diagram sub-tab. In the lower window of MPLAB Harmony Configurator select the “Pin Table” tab.
1
Map the PIC32's "Reference Clock Generator Output 1" signal (REFCLKO1) to pin 70 (RPD15). Click on the blue box to map this signal to this pin. This will change the color of the box from blue (available) to green (locked).
See the board schematics in the User's Guides for reference:
Table of Contents
|