Audio input and output problems are often encountered in embedded product development. How to add "speaker" and "microphone" devices to them? This article will briefly introduce the audio solution in ARM+Linux products.
Let's first get to know the I2S (Inter-IC Sound) bus. I2S is also known as the integrated circuit built-in audio bus. This bus is specifically used for data transmission between audio devices and is widely used in various multimedia systems. It adopts a design that transmits clock and data signals along independent wires. By separating the data and clock signals, distortion caused by time difference can be effectively avoided.
Taking the HDG2L-IoT evaluation kit as an example, the RZ/G2L processor provides at least one I2S bus, which is connected to the audio codec chip (IC model WM8960) to provide high-quality recording and broadcasting functions. The hardware deployment of the audio module is shown below.
Figure 1 HDG2L-IoT audio interface
The audio communication pins between the G2L core board and WM8960 are as follows:
In the audio codec driver of the Linux system, the I2C communication interface communicates with the audio IC, abstracting the various function registers in the audio IC into audio controls, so that users can directly adjust the parameters in the IC in the User space. The I2S audio pin is the channel for sending and receiving digital signals.
When broadcasting, the audio IC converts the received digital signal into an analog signal and sends it out. When recording, the audio IC converts the received audio analog quantity into a digital quantity and sends it to the processor.
ALSA is the mainstream audio architecture of Linux. It includes a set of kernel drivers, API libraries and tools to support Linux sound. Applications can control the underlying audio hardware by calling the API provided by alsa-lib.
Linux manages audio IC controls through the alsa library and alsa tools, and lists the audio controls in the current system through "amixer controls":
The key control information of the audio function is as follows: (recording channel, broadcast channel, headphone volume, speaker volume).
On the development board, run the aplay program to test audio playback. Before testing, plug the earphone into the earphone jack of the development board.
To test the I2S-based audio output interface, execute the following command in the command line:
2.wav is the audio file to be played, and it is assumed here that 2.wav is located in the /home/root/audios directory.
When playing audio, for the I2S-based audio output interface, you can use the amixer program to adjust the volume. The command line format for using the amixer program to adjust the headphone playback volume is:
The volume value ranges from 0 to 127, with 127 indicating the maximum volume. For example, to set the volume to 100, execute the following command in the command line:
Connect the speakers to the left and right channel sockets of the HDG2L-IOT development board. Before playing the audio, you can set the speaker playback volume and total volume, and then play the audio file:
The recording function requires setting the parameters of the recording control, as shown below.
Use the arecord command to record and save it as "/tmp/t.wav", then use the aplay command to play it back for verification.
Since the current microphone output signal is only connected to the Left end of the audio processing as input, only the left channel is output when playing the recording file. If you want to record the microphone output signal in both the left and right channels, you can make the following settings:
Set “ADC Data Output Select” to 1, which means “Left Data = Left ADC; Right Data = Left ADC”.
As can be seen from the above, the audio codec chip exists in the form of a control in the Linux system. The internal parameters are generally only recorded in the memory. When the system loses power, the modified parameters will disappear, so save and restore instructions are needed to save the last parameters in the configuration file.
The configuration file of alsa is: /var/lib/alsa/asound.state
The write operation after the control is modified is shown below.
The operation of restoring the control parameters is as follows (Note: the configuration file will be automatically read after restarting the board).