PSoC 6 MCU: USB Audio Recorder
Document Number: 002-25786 Rev. **
3
Figure 3. PDM/PCM to USB Flowchart
Task While Loop
Wait for
Recording
Request
Clear
PDM/PCM
FIFO
Enable
PDM/PCM
Sampling
Write to the
Audio IN
Endpoint Buffer
Audio IN Endpoint Callback
Read PDM/PCM
FIFO Level
Is level too
high?
Is level too
low?
Increase
Frame Size
Decrease
Frame Size
Set Original
Frame Size
Enable the DMA
PCM Transfer
DMA PCM Interrupt Handler
Is still
streaming?
Disable
PDM/PCM
Sampling
Write to the
Audio IN
Endpoint Buffer
This callback is
executed after
transferring data to
the USB host
This interrupt service routine is
executed after the DMA PCM transfers
one frame data from the PDM/PCM
FIFO to SRAM
Yes
No
Yes
No
No
Yes
The audio in ‘task while’ loop waits for a recording request from the USB host. When it receives the request, it prepares the
PDM/PCM block to sample a new frame. It initially writes a null frame to the Audio IN Endpoint buffer to initiate the streaming.
The frame size depends on the sample rate (48 ksps), the number of channels (2x) and the time of USB transfers (1 ms). The
overall equation is:
𝐹𝑟𝑎𝑚𝑒 𝑠𝑖𝑧𝑒 = 𝑆𝑎𝑚𝑝𝑙𝑒 𝑅𝑎𝑡𝑒 × 𝑁𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝐶ℎ𝑎𝑛𝑛𝑒𝑙𝑠 × 𝑇𝑟𝑎𝑛𝑠𝑓𝑒𝑟 𝑇𝑖𝑚𝑒
In this example, the frame size is equal to 48000 × 2 x 0.001 = 96 samples. Note that the Audio IN Endpoint Callback also
implements a method to change the frame size depending on the FIFO level in the PDM/PCM block. This is important because
there are clocking differences between the USB host bus and the PSoC 6 MCU audio subsystem.
The DMA PCM is configured to transfer an entire audio frame per trigger. It triggers when there is enough data in the PDM/PCM
FIFO. Once it completes a transfer, it disables itself; it is enabled again only when the Audio IN Endpoint Callback is executed.
On completion, the DMA PCM Interrupt Handler is executed, which might terminate the recording by disabling the PDM/PCM
block or keep the streaming by writing more data to the Audio IN Endpoint buffer.
The firmware uses FreeRTOS to execute the processes required by this application. All tasks run in the Arm® Cortex®-M4 CPU.
The following tasks are created:
1.
AudioInTask:
described in
as the Task While Loop
2.
AudioAppTask:
initiates the application and handles certain USB events. In this application, no USB events are handled
in this task.
3.
DebugTask:
handles messages printed to the terminal over UART (optional).
The example also uses the Event Group. It is used to notify tasks when USB events occur. Only the event that initiates a
recording is used in this application.
The project consists of the following files:
▪
main.c
(CM4) contains the main function, which is the entry point and execution of the firmware application. The main
function sets up user tasks and then starts the RTOS scheduler. The CM0+ equivalent contains the main function that starts
up CM4. CM0+ is not used to run any application code.
▪
audio_app.c/h
files contain the task that initializes the audio-related blocks, such as the USB and the PDM/PCM blocks,
and handles USB events in the task main loop.
▪
audio_in.c/h
files contain the task that handle recording requests to the Audio IN endpoint. These files also implement the
Audio IN Data Endpoint callback and the DMA interrupt service routine.