1. The process of calling the application when setting the volume
(1)strace分析: amixer cset numid=1 30 (设置音量)
/dev/snd/controlC0
open
SNDRV_CTL_IOCTL_CARD_INFO
SNDRV_CTL_IOCTL_PVERSION
SNDRV_CEL_CTL_CTL_CTL_CTL_V
READ
SNDRV_CTL_IOCTL_ELEM_WRITE
: snd_ctl_elem_write_user
(2) When the application calls SNDRV_CTL_IOCTL_ELEM_WRITE, the driver calls the snd_ctl_elem_write_user function, which copies some parameters from user space and then calls the snd_ctl_elem_write function.
(3) Function snd_ctl_elem_write
Find a snd_kcontrol structure and then call the put function of the snd_control structure.
(4) Who provides this snd_kcontrol structure?
ASOC drivers are divided into three parts (machine, codec, platform). The codec part should be provided because it is closely related to the sound card. When adjusting the volume, you must adjust it.
2. Write the program (uda1341.c (codec))
(1)Structure snd_kcontrol_new
static const struct snd_kcontrol_new uda1341_vol_control =
{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, //Indicates which type of device the snd_kcontrol structure is used for (indicates parameter setting)
.name = "Master Playback Volume", //Volume control. The snd_kcontrol of each sound card driver is different. Why can applications adjust its volume? For some commonly used properties, they all have fixed names. The application finds its snd_kcontrol item according to the name and calls the put function inside.
.info = uda1341_info_vol, //Get some information, such as the volume range.get
= uda1341_get_vol, //Get the current volume value.put
= uda1341_put_vol, //Set the volume
};
(2) Obtain volume information, such as minimum and maximum values
/*
* Get volume information, such as minimum and maximum values
*/
int uda1341_info_vol(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_info *uinfo)
{
uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; //The type of volume value is integer
uinfo->count = 2; //The number of channels is dual-channel
uinfo->value.integer.min = 0; //Minimum integer,
uinfo->value.integer.max = 63; //Maximum integer,
return 0;
}
Because the volume control of uda1341 is 6 bits (0 means maximum volume, 63 means minimum volume), and in the application, 0 means minimum volume, and the larger the value, the higher the volume.
(3) Get the current volume value
/*
* Get the current volume value
*/
int uda1341_get_vol(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
ucontrol->value.integer.value[1] = //Here it means ucontrol->value.integer.value[0] is equal to ucontrol->value.integer.value[1], because it is dual channel
ucontrol->value.integer.value[0] = 63 - snd_soc_read(codec, UDA1341_DATA00); //Read the value of register DAT00, because the value of the driver and the value of the application are opposite in size, uda1341 does not support register read operation, to get the value of a register, it is to read a cache (this cache saves the value of the setting register)
return 0;
}
(4) Set the current volume value
/*
* Set the current volume value
*/
int uda1341_put_vol(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
unsigned int val;
val = 63 - ucontrol->value.integer.value[0];//The value passed by the application needs to be reversed when written to the register
snd_soc_write(codec, UDA1341_DATA00, val);//Write the value val to register DATA00 return
0;
}
3. Connection between structure snd_kcontrol_new and kernel part
(1) probe function
static int uda1341_soc_probe(struct snd_soc_codec *codec)
{
int ret;
uda1341_init_regs(codec);
ret = snd_soc_add_codec_controls(codec, &uda1341_vol_control, 1);
return ret;
}
4. Testing
amixer controlsView controls
amixer cget numid=1 means check the current volume
amixer cset numid=1 30 Set the volume
5、Input Mux
Because different boards have different microphone channels (in uda1341), with the same driver, the application should set the input mux option when recording.
Indicates which microphone channel it can select, which microphone channel is currently, and which microphone channel is set
The value uda134x_mixer_enum[2] is the second item in the array
6、
Let the board use the kernel's built-in driver
View the device nodes and control items (input mux is the second to last item)
View the value of control item 11 (the current value is 0)
If you select the first channel (analog channel), the last parameter indicates which channel
Previous article:ALSA sound card 09_parameter setting written from scratch_study notes
Next article:ALSA Sound Card 16_Writing ALSA Sound Card Application_Study Notes
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- I want to design a sine wave generator.
- [RT-Thread reading notes] Part 1 Simple principles of the kernel
- I worked hard to drive the CC2640 Hanshuo electronic tag.
- Solution to the error empty character constant
- Newbie wants to use PLC to control air conditioner
- Looking for a microcontroller model
- Measurement of radar power, spectrum and related parameters
- [ESP32-Audio-Kit Audio Development Board Review]——(2): Questions about play_mp3_control
- Is it popular to reduce the size of laptop keyboards now?
- (Bonus 5) GD32L233 Review - CRC (with the clearest article explaining CRC in history)