【i2s】Milk-V Duo使用analog mic

前言

本章介绍如何使用duo板子上的模拟mic进行录制音频。
需要单独购买mic4015,配置驱动,更改配置,安装tinyalsa工具。

一、硬件


i2s0 —— adc
i2s1 —— pdm
i2s3 —— dace ==spk_en存疑==

i2s2 —— 外接解码器

pdm可以不用,因为i2s1的di/do用来连接led了。
注意:这里i2s1的信号和内部codec的speaker输出复用了一个引脚,结果给led了。

mic的型号是4015,淘宝上可以直接搜到,插入的时候对准板子上的圆环,对其的话正负就对了。

二、dts

duo_buildroot_sdk\duo-buildroot-sdk\build\boards\default\dts\cv180x\cv180x_base.dtsi


	adc: adc@0300A100 {
		compatible = "cvitek,cv182xaadc";
		reg = <0x0 0x0300A100 0x0 0x100>;
		clocks = <&i2s_mclk 0>;
		clock-names = "i2sclk";
		clk_source = <0x04130000>; /* MCLK source is I2S3 */
	};

	dac: dac@0300A000 {
		compatible = "cvitek,cv182xadac";
		reg = <0x0 0x0300A000 0x0 0x100>;
		clocks = <&i2s_mclk 0>;
		clock-names = "i2sclk";
	};

	pdm: pdm@0x041D0C00 {
		compatible = "cvitek,cv1835pdm";
		reg = <0x0 0x041D0C00 0x0 0x100>;
		clocks = <&i2s_mclk 0>;
		clock-names = "i2sclk";
	};

	sound_adc {
		compatible = "cvitek,cv182xa-adc";
		cvi,model = "CV182XA";
		cvi,card_name = "cv182xa_adc";
	};

	sound_dac {
		compatible = "cvitek,cv182xa-dac";
		cvi,model = "CV182XA";
		cvi,card_name = "cv182xa_dac";
	};

	sound_PDM {
		compatible = "cvitek,cv182x-pdm";
		cvi,model = "CV182X";
		cvi,card_name = "cv182x_internal_PDM";
	};

cvi-pdm用Cv1835_cv1835pdm.c
182x的dai_link还是旧的接口,编译不过

//cv182x_cv182xpdm.c
static struct snd_soc_dai_link cv182x_pdm_dai = {
	.name = "cv182x-i2s-pdm",
	.stream_name = "cv182x-pdm",
	.cpu_dai_name = "4110000.i2s",
	.codec_dai_name = "cv1835pdm",
	.platform_name = "4110000.i2s",
	.codec_name = "41d0c00.pdm",
	.ops = &cv182x_pdm_ops,
	.init = cv182x_pdm_codec_init,
	.dai_fmt = SND_SOC_DAIFMT_I2S
	| SND_SOC_DAIFMT_IB_NF
	| SND_SOC_DAIFMT_CBM_CFM,
};

默认i2s是关闭的,需要在如下文件中打开。

duo_buildroot_sdk\duo-buildroot-sdk\build\boards\default\dts\cv180x\cv180x_asic_qfn.dtsi

	/* 
		/delete-node/ i2s@04100000;
		/delete-node/ i2s@04130000;
		/delete-node/ i2s@04110000;
		/delete-node/ i2s@04120000; 
	*/

三、配置引脚board_init

duo_buildroot_sdk\duo-buildroot-sdk\build\boards\cv180x\cv1800b_milkv_duo_sd\u-boot\cvi_board_init.c

 PINMUX_CONFIG(PAD_AUD_AINL_MIC, PAD_AUD_AINL_MIC);

存疑,如果spk_en只是开关,那out应该是哪里?难道是led吗?

pin

duo_buildroot_sdk\duo-buildroot-sdk\u-boot-2021.10\board\cvitek\cv180x
这里有board的寄存器信息,以及board_init中的接口。

#define SPK_EN__XGPIOA_15 3

#define PAD_AUD_AINL_MIC__XGPIOC_23 3
#define PAD_AUD_AINL_MIC__IIS1_BCLK 4
#define PAD_AUD_AINL_MIC__IIS2_BCLK 5
#define PAD_AUD_AINR_MIC__XGPIOC_22 3
#define PAD_AUD_AINR_MIC__IIS1_DO 4
#define PAD_AUD_AINR_MIC__IIS2_DI 5
#define PAD_AUD_AINR_MIC__IIS1_DI 6
#define PAD_AUD_AOUTL__XGPIOC_25 3
#define PAD_AUD_AOUTL__IIS1_LRCK 4
#define PAD_AUD_AOUTL__IIS2_LRCK 5
#define PAD_AUD_AOUTR__XGPIOC_24 3
#define PAD_AUD_AOUTR__IIS1_DI 4
#define PAD_AUD_AOUTR__IIS2_DO 5
#define PAD_AUD_AOUTR__IIS1_DO 6

四、makefile

结合上述dts中的compatible,去代码中匹配对应的驱动。

obj-$(CONFIG_SND_SOC_CV182XA_CV182XAADC) += cv181x_cv181xadc.o
obj-$(CONFIG_SND_SOC_CV182XA_CV182XADAC) += cv181x_cv181xdac.o

ifeq ($(CONFIG_SND_SOC_CV182XAADC),m)
obj-$(CONFIG_SND_SOC_CV182XAADC) += cv182xa_ai.o
cv182xa_ai-objs += cv181xadc.o
else
obj-$(CONFIG_SND_SOC_CV182XAADC) += cv181xadc.o
endif

ifeq ($(CONFIG_SND_SOC_CV182XADAC),m)
obj-$(CONFIG_SND_SOC_CV182XADAC) += cv182xa_ao.o
cv182xa_ao-objs += cv181xdac.o
else
obj-$(CONFIG_SND_SOC_CV182XADAC) += cv181xdac.o
endif

五、config

打开config
duo_buildroot_sdk\duo-buildroot-sdk\build\boards\cv180x\cv1800b_milkv_duo_sd\linux\cvitek_cv1800b_milkv_duo_sd_defconfig

#PDM
CONFIG_SND_SOC_CV1835PDM=y
CONFIG_SND_SOC_CV1835_CV1835PDM=y

#ADC
CONFIG_SND_SOC_CV182XAADC=y
CONFIG_SND_SOC_CV182XA_CV182XAADC=y

#DAC
CONFIG_SND_SOC_CV182XADAC=y
CONFIG_SND_SOC_CV182XA_CV182XADAC=y

打开i2s0/1/3

六、添加tinyalsa

这个参考我另一个帖子,这里就不赘述了。

七、测试结果

7.1 查看录制device信息

[root@milkv]~# cat /proc/asound/devices
  0: [ 0]   : control
 16: [ 0- 0]: digital audio playback
 32: [ 1]   : control
 33:        : timer
 56: [ 1- 0]: digital audio capture
 64: [ 2]   : control
 80: [ 2- 0]: digital audio playback

如图,当前支持三个pcm设备,其中card1 device0是录制设备,也就是自带mic。

7.2 录制音频

执行下述命令
tinycap:tinyalsa的工具,用于录制音频
-D:card1,具体见/proc/asound/cards的声卡信息
-d:声卡下属的device,查看/proc/asound/device。
-t:录制时长5秒
-c:录制单声道,默认是双声道

[root@milkv]~# tinycap a.wav -D 1 -d 0 -t 5 -c 1
Capturing sample: 1 ch, 48000 hz, 16 bit
Captured 241664 frames

image

7.3 pc端获取录制好的设备

在windows电脑的cmd窗口,输入命令

/root/a.wav:录制存放的音频路径
./:当前cmd窗口所在的文件夹路径

scp root@192.168.42.1:/root/a.wav ./

7.4 使用audacity查看波形

如图,其中高幅值的是我敲打桌子的声音。

如下图,这是我说话的声音,但是很明显,收音很小,虽然是贴在设备上说的,这个可以通过tinymix改动gain值变大。

底噪非常大,如果想要听清人声,底噪也同步放大,如果需要识别,那需要加上降噪处理。

至此,duo自带开发板的录制功能ok了。

mnt/system/bin里有个用例,就可以录音和播放。
spk 被拿来LED用了,所以大概只能随视频一起录音。

好的,我去看看,之前还没注意到有示例。
spk复用给led,我在board_init.c里面没办法去复用回spk了,提示缺少定义。

This code results in an error:

PINMUX_CONFIG(PAD_AUD_AINL_MIC, PAD_AUD_AINL_MIC);

/home/ericb/Engineering/github/duo-buildroot-sdk/u-boot-2021.10/board/cvitek/cv180x/…/cvi_board_init.c:49:16: error: ‘PAD_AUD_AINL_MIC__PAD_AUD_AINL_MIC’ undeclared (first use in this function)

There doesn’t seem to be an appropriate #define in the header file for that mux configuration.

1 Like

Just comment out this line. The pin is set to mic input by default.

1 Like

Yeah - that seems to be the case. Unfortunately trying the rest of the changes noted above doesn’t seem to enable the audio device driver.

couldn’t find your adventures with building tinyalsa ;-(
and one more thing: will it be different for milkv duo256? and i2s mic?

Hi emeb

You can test to add the following code.

Sound node is used to add sound card.

build\boards\cv180x\cv1800b_milkv_duo_sd\dts_riscv\cv1800b_milkv_duo_sd.dts

/ {
	/* codec */
	max98357a: max98357a {
		#sound-dai-cells = <0>;
		compatible = "maxim,max98357a";
		status = "okay";
		/*sdmode-gpios = <&gpio1 14 0>;*/ 
		/* max98357a has gain & sd_mode gpio. but codec driver just has sdmode */
	};

	/* sound_ext3 use external codec */
	sound {
		compatible = "cvitek,cv1835-max98357a";
		cvi,card_name = "cv1835_max98357a";
	};

};

Hi LangZhao,

Thanks for the suggestion of the additional entry in the device tree file. I have tried that and it builds correctly but I still get the following error during boot:

[ 0.989043] cv1835-max98357a sound: snd_soc_register_card failed (-517)

I’ll take a closer look at all the changes I’ve made to verify there are no typos.