This code example shows how to play the sound of WAV format via I2S functionality of AUDIO Subsystem using P-DMA
The device used in this code example (CE) is:
The board used for testing is:
- TRAVEO™ T2G evaluation kit (KIT_T2G-B-H_EVK, KIT_T2G-B-H_LITE)
TRAVEO™ T2G MCUs have an Audio Subsystem that can establish a serial bus interface used to connect digital audio device together. This CE transmits a WAV sound source prepared in Code Flash via standard I2S bus. In addition, the sound can be played without any CPU load since the CPU only used to input a trigger to start playing the sound and the sound data transmission is done automatically by P-DMA.
AUDIO Subsystem
- Supports standard I2S, LJ, and eight-channel TDM digital audio interface formats
- Supports both master and slave mode operation in all the digital audio formats
- Supports independent operation of Receive (RX) and Transmit (TX) directions
- Supports operating from an external master clock provided through an external IC such as audio codec
- Provides configurable clock divider registers to generate the required sample rates
- Supports data word length of 8-bit, 16-bit, 18-bit, 20-bit, 24-bit, and 32-bit per channel
- Supports channel length of 8-bit, 16-bit, 18-bit, 20-bit, 24-bit, and 32-bit per channel (channel length fixed at 32-bit in TDM format)
- Provides two hardware FIFO buffers, one each for the TX block and RX block, respectively
- Supports both DMA- and CPU-based data transfers
Peripheral DMA (P-DMA)
The MCU has two types of DMA, P-DMA and M-DMA. P-DMA is used to transfer data between memory and peripherals without CPU involvement: the CPU configures/programs the P-DMA but the actual transfer is done by the P-DMA controller. The primary design target is P-DMA functionality at limited
area overhead to the platform. Functionally, the P-DMA controller is similar to a general-purpose DMA controller.
Feature | P-DMA | M-DMA |
---|---|---|
Focus | Low latency | High memory bandwidth |
Used for | Transfer between peripheral and memory | Transfer between memories |
Transfer engine | Shared between all channels | Dedicated for each channel |
Transfer size | 8-bit/16-bit/32-bit | |
Channel priority | Four levels, Preemptable | Four levels |
Transfer mode | Single 1D/2D CRC transfer | Single 1D/2D Memory copy Scatter |
Descriptor | Source and destination address Transfer size Channel action Data transfer mode Activation trigger type (4 types) Output trigger type (4 types) Interrupt type (4 types) Descriptor chaining | |
Access-control attributes The access attributes of the bus transfer that programmed the channel will be inherited | Privileged/Unprivileged Secure/Non-secure Protection contexts |
More details can be found in Technical Reference Manual (TRM), Registers TRM and Data Sheet.
This CE has been developed for:
-
TRAVEO™ T2G evaluation kit (KIT_T2G-B-H_EVK)
No changes are required from the board's default settings.
Output signals are output from the following pins respectively:Tx Serial Clock (SCK) Tx Word Select (WS) Tx Serial Data (SD) P11[1] P11[2] P12[0] -
TRAVEO™ T2G Body High Lite evaluation kit (KIT_T2G-B-H_LITE)
No changes are required from the board's default settings.
Output signals are output from the following pins respectively:Tx Serial Clock (SCK) Tx Word Select (WS) Tx Serial Data (SD) P13[1] P13[2] P13[3]
I2S waveform
The figure below shows the timing diagrams for the different word
length on 32-bit channel length in the standard I2S digital audio format:
Channel length and word length can be selected according to the requirements of the target module. This CE uses 16-bit word length on 32-bit channel length. The configuration is done in setup_Sound() function by calling Cy_I2S_Init() with the configuration structure generated by Device Configurator:
P-DMA configuration
In this CE, P-DMA is used to transferring WAV data onto TX_FIFO of I2S function. The P-DMA transfer must be configured by descriptor(s) that P-DMA can interpret. The figure below outlines the structure of the P-DMA descriptor group provided for WAV transfer to I2S:
In the prepare_Dw_Descriptor_Stereo() function called by setup_Sound(), multiple descriptors will be dynamically created based on the size of the WAV to be transferred. The maximum transferable count of word by single descriptor is 65536 by using 2D transfer, where x and y value are both 256. So this function configures descriptors as this maximum length until the rest size of WAV data becomes less than it. After that the remaining data configured to be transferred by descriptor(s) which x and y is specified as the rest size. These descriptors configured as to chain to next one sequentially, and the last descriptor chained to top descriptor. This descriptor chain realizes endless playback of WAV data without any CPU load. These configured descriptors are set to P-DMA hardware by using Cy_DMA_Descriptor_Init() function. Note that the function SCB_CleanDCache() should be called after these descriptors preparation to clean up the D-cache since the P-DMA controller also read the descriptors.
Then the setup_Sound() function initializes P-DMA by calling Cy_DMA_Channel_Init(), then enables the P-DMA by calling Cy_DMA_Channel_Enable() and Cy_DMA_Enable(). Next it call Cy_TrigMux_Select() to configure the P-DMA channel to be triggered by the I2S channel.
Miscellaneous settings
-
STDIN / STDOUT setting
-
Calling cy_retarget_io_init() function to use UART as STDIN / STDOUT
- Initialize the port defined as CYBSP_DEBUG_UART_TX as UART TX, defined as CYBSP_DEBUG_UART_RX as UART RX (these pins are connected to KitProg3 COM port)
- The serial port parameters become to 8N1 and 115200 baud
-
The UART receiving interrupt is configured in the cyhal_uart_enable_event() function
-
When the command is received through the terminal, interrupt occur and it controls the sound playback by process_Key_Press().
-
The callback routine is prepared as handle_UART_Event() and is registered by the cyhal_uart_register_callback() function, it will be call backed from the driver by calling cyhal_uart_read_async() if some data is received and available.
-
-
-
Sound playback control
- When the main() function detects that
p
is input on terminal, it pauses and also restarts the sound playback. These operations are done in functions stop_Sound() and start_Sound(), they realize their functionality by disabling / enabling the I2S channel by calling Cy_I2S_DisableTx() and Cy_I2S_EnableTx().
- When the main() function detects that
For this CE, a terminal emulator is required for displaying outputs and get inputs. Install a terminal emulator if you do not have one. Instructions in this document use Tera Term.
After code compilation, perform the following steps to flashing the device:
-
Connect the board to your PC using the provided USB cable through the KitProg3 USB connector.
-
Open a terminal program and select the KitProg3 COM port. Set the serial port parameters to 8N1 and 115200 baud.
-
Program the board using one of the following:
- Select the CE project in the Project Explorer.
- In the Quick Panel, scroll down, and click [Project Name] Program (KitProg3_MiniProg4).
-
After programming, the CE starts automatically. Confirm that the messages are displayed on the UART terminal.
-
You can debug the example to step through the code. In the IDE, use the [Project Name] Debug (KitProg3_MiniProg4) configuration in the Quick Panel. For details, see the "Program and debug" section in the Eclipse IDE for ModusToolbox™ software user guide.
Note: (Only while debugging) On the CM7 CPU, some code in main() may execute before the debugger halts at the beginning of main(). This means that some code executes twice: once before the debugger stops execution, and again after the debugger resets the program counter to the beginning of main(). See KBA231071 to learn about this and for the workaround.
Relevant Application notes are:
- AN235305 - GETTING STARTED WITH TRAVEO™ T2G FAMILY MCUS IN MODUSTOOLBOX™
- AN226043 - How to Use Sound Subsystem in Traveo II Family
- AN220191 - How to use direct memory access (DMA) controller in TRAVEO™ II family
ModusToolbox™ is available online:
Associated TRAVEO™ T2G MCUs can be found on:
More code examples can be found on the GIT repository:
For additional trainings, visit our webpage:
For questions and support, use the TRAVEO™ T2G Forum: