1 /******************************************************************************
2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3 * All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************/
18
19 /********************************************************************************************************
20 * @file audio.h
21 *
22 * @brief This is the header file for B91
23 *
24 * @author Driver Group
25 *
26 *******************************************************************************************************/
27 /** @page AUDIO
28 *
29 * Introduction
30 * ===============
31 * audio module consists of 3 parts: audio code, audio in path , and audio out path.
32 *
33 * API Reference
34 * ===============
35 * Header File: audio.h
36 */
37 #ifndef audio_H
38 #define audio_H
39
40 #include "reg_include/register_b91.h"
41 #include "i2c.h"
42 #include "pwm.h"
43 #include "compiler.h"
44
45 typedef enum{
46 I2S_BCK_PC3 = GPIO_PC3,
47 I2S_ADC_LR_PC4 = GPIO_PC4,
48 I2S_ADC_DAT_PC5 = GPIO_PC5,
49 I2S_DAC_LR_PC6 = GPIO_PC6,
50 I2S_DAC_DAT_PC7 = GPIO_PC7,
51 }i2s_pin_e;
52
53
54 typedef enum{
55 DMIC_B2_DAT_B3_CLK,//mono B3 clk1
56 DMIC_C1_DAT_C2_CLK,//mono C2 clk1
57 DMIC_D4_DAT_D5_CLK,//mono D5 clk1
58 DMIC_GROUPB_B2_DAT_B3_B4_CLK,
59 DMIC_GROUPC_C1_DAT_C2_C3_CLK,
60 DMIC_GROUPD_D4_DAT_D5_D6_CLK,
61 }dmic_pin_group_e;
62
63
64 typedef enum{
65 MIC_MODE,
66 SPK_MODE,
67 LOOP_MODE,
68 }audio_loop_mode_e;
69
70
71 typedef enum{
72 CODEC_I2S ,
73 IO_CODEC ,
74 IO_I2S,
75 }audio_flow_e;
76
77
78 typedef enum{
79 I2S_RJ_MODE ,
80 I2S_LJ_MODE ,
81 I2S_I2S_MODE,
82 I2S_DSP_MODE,
83 }i2s_mode_select_e;
84
85 typedef enum{
86 CODEC_PARALLEL_MODE ,
87 CODEC_LJ_MODE ,
88 CODEC_DSP_MODE,
89 CODEC_I2S_MODE,
90 }codec_mode_select_e;
91
92
93 typedef enum{
94 CODEC_ITF_AC,
95 CODEC_ITF_PD,
96 }codec_interface_select_e;
97
98 typedef enum{
99 BIT_16_MONO,
100 BIT_20_OR_24_MONO ,
101 BIT_16_STEREO,
102 BIT_20_OR_24_STEREO ,
103 }audio_in_mode_e;
104
105 typedef enum{
106 BIT_16_MONO_FIFO0 ,
107 BIT_20_OR_24_MONO_FIFO0 ,
108 BIT_16_STEREO_FIFO0 ,
109 BIT_20_OR_24_STEREO_FIFO0 ,
110 BIT_16_MONO_FIFO1 ,
111 BIT_20_OR_24_MONO_FIFO1 ,
112 BIT_16_STEREO_FIFO0_AND_FIFO1 ,
113 BIT_20_OR_24STEREO_FIFO0_AND_FIFO1 ,
114
115 }audio_out_mode_e;
116
117 typedef enum{
118 I2S_BIT_16_DATA,
119 I2S_BIT_20_DATA ,
120 I2S_BIT_24_DATA,
121 }i2s_data_select_e;
122
123 typedef enum{
124 CODEC_BIT_16_DATA,
125 CODEC_BIT_20_DATA=2,
126 CODEC_BIT_24_DATA,
127 }codec_data_select_e;
128
129
130 typedef enum{
131 MONO_BIT_16,
132 MONO_BIT_20,
133 MONO_BIT_24,
134 STEREO_BIT_16,
135 STEREO_BIT_20,
136 STEREO_BIT_24,
137 }audio_channel_wl_mode_e;
138
139
140 typedef enum{
141 AUDIO_BIT_16_DATA,
142 AUDIO_BIT_20_DATA,
143 AUDIO_BIT_24_DATA,
144 }audio_wl_select_e;
145
146 typedef enum{
147 AUDIO_MONO,
148 AUDIO_STEREO,
149 }audio_channel_select_e;
150
151
152 typedef enum{
153 I2S_S_CODEC_M,
154 I2S_M_CODEC_S,
155 }i2s_codec_m_s_mode_e;
156
157 typedef enum{
158 MCU_WREG,
159 I2C_WREG ,
160 }codec_wreg_mode_e;
161
162 typedef enum{
163 I2S_DATA_INVERT_DIS,
164 I2S_DATA_INVERT_EN ,
165 }audio_data_invert_e;
166
167 typedef enum{
168 I2S_LR_CLK_INVERT_DIS,
169 I2S_LR_CLK_INVERT_EN,
170 }audio_i2s_lr_clk_invert_e;
171
172 typedef enum
173 {
174 DAC_OUTPUT_L_R_CHN,
175 DAC_OUTPUT_L_CHN,
176 }audio_output_chn_e;
177
178 typedef enum
179 {
180 SINGLE_ENDED_INPUT,
181 DIFF_ENDED_INPUT,
182 }audio_input_mode_select_e;
183
184 typedef struct {
185 unsigned char audio_in_mode;
186 unsigned char audio_out_mode;
187 unsigned char i2s_data_select;
188 unsigned char codec_data_select;
189 unsigned char i2s_codec_m_s_mode;
190 unsigned char in_digital_gain;
191 unsigned char in_analog_gain;
192 unsigned char out_digital_gain;
193 unsigned char out_analog_gain;
194 unsigned char mic_input_mode_select;
195 unsigned char dac_output_chn_select;
196 unsigned char adc_wnf_mode_select;
197 }aduio_i2s_codec_config_t;
198
199
200 typedef struct {
201 unsigned char i2s_lr_clk_invert_select;
202 unsigned char i2s_data_invert_select;
203 }aduio_i2s_invert_config_t;
204
205
206
207 typedef enum{
208 I2S_DATA_IN_FIFO ,
209 USB_DATA_IN_FIFO ,
210 SAR_ADC_DATA_IN_FIFO,
211 IN_NO_USE,
212 }audio_mux_ain_e;
213
214 typedef enum{
215 I2S_OUT ,
216 USB_IOS_OUT ,
217 OUT_NO_USE=3,
218 }audio_mux_aout_e;
219
220 /**
221 * define audio rate value.
222 */
223 typedef enum{
224 AUDIO_8K,
225 AUDIO_16K=3,
226 AUDIO_32K=6,
227 AUDIO_44EP1K,
228 AUDIO_48K,
229 AUDIO_ADC_16K_DAC_48K,
230 }audio_sample_rate_e;
231
232 typedef enum{
233 AMIC_INPUT,
234 DMIC_INPUT,
235 LINE_INPUT,
236 }audio_input_mode_e;
237
238 typedef enum{
239 AMIC_IN_TO_BUF,
240 DMIC_IN_TO_BUF,
241 LINE_IN_TO_BUF,
242 AMIC_IN_TO_BUF_TO_LINE_OUT,
243 DMIC_IN_TO_BUF_TO_LINE_OUT,
244 LINE_IN_TO_BUF_TO_LINE_OUT,
245 BUF_TO_LINE_OUT,
246 }audio_flow_mode_e;
247
248
249
250 typedef enum{
251 AUDIO_RATE_EQUAL,
252 AUDIO_RATE_GT_L0,
253 AUDIO_RATE_GT_L1,
254 AUDIO_RATE_LT_L0,
255 AUDIO_RATE_LT_L1,
256 }audio_rate_match_e;
257
258
259 /*[0,+43], 1 dB steps*/
260 typedef enum{
261 CODEC_IN_D_GAIN_0_DB=0,
262 CODEC_IN_D_GAIN_4_DB=4,
263 CODEC_IN_D_GAIN_8_DB=8,
264 CODEC_IN_D_GAIN_12_DB=12,
265 CODEC_IN_D_GAIN_16_DB=16,
266 CODEC_IN_D_GAIN_20_DB=20,
267 CODEC_IN_D_GAIN_43_DB=43,
268 }codec_in_path_digital_gain_e;
269
270
271 typedef enum{
272 CODEC_IN_A_GAIN_0_DB,
273 CODEC_IN_A_GAIN_4_DB,
274 CODEC_IN_A_GAIN_8_DB,
275 CODEC_IN_A_GAIN_12_DB,
276 CODEC_IN_A_GAIN_16_DB,
277 CODEC_IN_A_GAIN_20_DB,
278 }codec_in_path_analog_gain_e;
279
280
281
282 typedef enum{
283 CODEC_OUT_D_GAIN_0_DB=0,
284 CODEC_OUT_D_GAIN_m1_DB,
285 CODEC_OUT_D_GAIN_m2_DB,
286 CODEC_OUT_D_GAIN_m3_DB,
287 CODEC_OUT_D_GAIN_m4_DB,
288 CODEC_OUT_D_GAIN_m5_DB,
289 CODEC_OUT_D_GAIN_m6_DB,
290 CODEC_OUT_D_GAIN_m7_DB,
291 CODEC_OUT_D_GAIN_m8_DB,
292 CODEC_OUT_D_GAIN_m9_DB,
293 CODEC_OUT_D_GAIN_m10_DB,
294 CODEC_OUT_D_GAIN_m11_DB,
295 CODEC_OUT_D_GAIN_m12_DB,
296 CODEC_OUT_D_GAIN_m13_DB,
297 CODEC_OUT_D_GAIN_m14_DB,
298 CODEC_OUT_D_GAIN_m15_DB,
299 CODEC_OUT_D_GAIN_m16_DB,
300 CODEC_OUT_D_GAIN_m17_DB,
301 CODEC_OUT_D_GAIN_m18_DB,
302 CODEC_OUT_D_GAIN_m19_DB,
303 CODEC_OUT_D_GAIN_m20_DB,
304 CODEC_OUT_D_GAIN_m21_DB,
305 CODEC_OUT_D_GAIN_m22_DB,
306 CODEC_OUT_D_GAIN_m23_DB,
307 CODEC_OUT_D_GAIN_m24_DB,
308 CODEC_OUT_D_GAIN_m25_DB,
309 CODEC_OUT_D_GAIN_m26_DB,
310 CODEC_OUT_D_GAIN_m27_DB,
311 CODEC_OUT_D_GAIN_m28_DB,
312 CODEC_OUT_D_GAIN_m29_DB,
313 CODEC_OUT_D_GAIN_m30_DB,
314 CODEC_OUT_D_GAIN_m31_DB=31,
315
316 CODEC_OUT_D_GAIN_32_DB,
317 CODEC_OUT_D_GAIN_31_DB,
318 CODEC_OUT_D_GAIN_30_DB,
319 CODEC_OUT_D_GAIN_29_DB,
320 CODEC_OUT_D_GAIN_28_DB,
321 CODEC_OUT_D_GAIN_27_DB,
322 CODEC_OUT_D_GAIN_26_DB,
323 CODEC_OUT_D_GAIN_25_DB,
324 CODEC_OUT_D_GAIN_24_DB,
325 CODEC_OUT_D_GAIN_23_DB,
326 CODEC_OUT_D_GAIN_22_DB,
327 CODEC_OUT_D_GAIN_21_DB,
328 CODEC_OUT_D_GAIN_20_DB,
329 CODEC_OUT_D_GAIN_19_DB,
330 CODEC_OUT_D_GAIN_18_DB,
331 CODEC_OUT_D_GAIN_17_DB,
332 CODEC_OUT_D_GAIN_16_DB,
333 CODEC_OUT_D_GAIN_15_DB,
334 CODEC_OUT_D_GAIN_14_DB,
335 CODEC_OUT_D_GAIN_13_DB,
336 CODEC_OUT_D_GAIN_12_DB,
337 CODEC_OUT_D_GAIN_11_DB,
338 CODEC_OUT_D_GAIN_10_DB,
339 CODEC_OUT_D_GAIN_9_DB,
340 CODEC_OUT_D_GAIN_8_DB,
341 CODEC_OUT_D_GAIN_7_DB,
342 CODEC_OUT_D_GAIN_6_DB,
343 CODEC_OUT_D_GAIN_5_DB,
344 CODEC_OUT_D_GAIN_4_DB,
345 CODEC_OUT_D_GAIN_3_DB,
346 CODEC_OUT_D_GAIN_2_DB,
347 CODEC_OUT_D_GAIN_1_DB,
348 }codec_out_path_digital_gain_e;
349
350
351
352 typedef enum{
353
354 CODEC_OUT_A_GAIN_12_DB,
355 CODEC_OUT_A_GAIN_11_DB,
356 CODEC_OUT_A_GAIN_10_DB,
357 CODEC_OUT_A_GAIN_9_DB,
358 CODEC_OUT_A_GAIN_8_DB,
359 CODEC_OUT_A_GAIN_7_DB,
360 CODEC_OUT_A_GAIN_6_DB,
361 CODEC_OUT_A_GAIN_5_DB,
362 CODEC_OUT_A_GAIN_4_DB,
363 CODEC_OUT_A_GAIN_3_DB,
364 CODEC_OUT_A_GAIN_2_DB,
365 CODEC_OUT_A_GAIN_1_DB,
366 CODEC_OUT_A_GAIN_0_DB,
367 CODEC_OUT_A_GAIN_m1_DB,
368 CODEC_OUT_A_GAIN_m2_DB,
369 CODEC_OUT_A_GAIN_m3_DB,
370 CODEC_OUT_A_GAIN_m4_DB,
371 CODEC_OUT_A_GAIN_m5_DB,
372 CODEC_OUT_A_GAIN_m6_DB,
373 CODEC_OUT_A_GAIN_m7_DB,
374 CODEC_OUT_A_GAIN_m8_DB,
375 CODEC_OUT_A_GAIN_m9_DB,
376 CODEC_OUT_A_GAIN_m10_DB,
377 CODEC_OUT_A_GAIN_m11_DB,
378 CODEC_OUT_A_GAIN_m12_DB,
379 CODEC_OUT_A_GAIN_m13_DB,
380 CODEC_OUT_A_GAIN_m14_DB,
381 CODEC_OUT_A_GAIN_m15_DB,
382 CODEC_OUT_A_GAIN_m16_DB,
383 CODEC_OUT_A_GAIN_m17_DB,
384 CODEC_OUT_A_GAIN_m18_DB,
385 CODEC_OUT_A_GAIN_m19_DB,
386 }codec_out_path_analog_gain_e;
387
388
389 /*The Wind Noise filter (WNF) is a programmable high pass filter feature enabling to reduce wind noise .
390 The wind noise filter is a 1st order filter.
391 Mode1 -3dB 59Hz
392 corner frequency Mode2 -3dB 117Hz
393 Mode3 -3dB 235Hz
394 */
395 typedef enum{
396 CODEC_ADC_WNF_INACTIVE,
397 CODEC_ADC_WNF_MODE1,
398 CODEC_ADC_WNF_MODE2,
399 CODEC_ADC_WNF_MODE3,
400 }adc_wnf_mode_sel_e;
401
402
403
404 typedef enum
405 {
406 INNER_CODEC,
407 EXT_CODEC,//wm8731
408 }codec_type_e;
409
410
411
412
413 typedef enum
414 {
415 CODEC_1P8V,
416 CODEC_2P8V,
417 }codec_volt_supply_e;
418
419
420 /**
421 * @brief This function serves to set the clock of i2s
422 * @param[in] step - the dividing factor of step.
423 * @param[in] mod - the dividing factor of mod.
424 * @return none
425 */
audio_set_i2s_clk(unsigned char step,unsigned char mod)426 static inline void audio_set_i2s_clk(unsigned char step,unsigned char mod)
427 {
428 reg_i2s_step = (step&FLD_I2S_STEP)|FLD_I2S_CLK_EN;
429 reg_i2s_mod = mod;
430 }
431
432 /**
433 * @brief This function serves to set the clock of codec
434 * @param[in] step - the dividing factor of step.
435 * @param[in] mod - the dividing factor of mod.
436 * @return none
437 * @attention codec clock divider from pll,so pll must be 192M
438 */
audio_set_codec_clk(unsigned char step,unsigned char mod)439 static inline void audio_set_codec_clk(unsigned char step,unsigned char mod)
440 {
441 BM_CLR(reg_dmic_clk_set,BIT(0));//set dmic_div
442 reg_dmic_step = (step&FLD_DMIC_STEP)|FLD_DMIC_SEL;
443 reg_dmic_mod = mod;
444 }
445
446
447 /**
448 * @brief This function serves to set enable i2s clk
449 * @return none
450 */
audio_i2s_clk_en(void)451 static inline void audio_i2s_clk_en(void)
452 {
453 BM_SET(reg_i2s_step,FLD_I2S_CLK_EN);
454 }
455
456 /**
457 * @brief This function serves to set disable i2s clk.
458 * @return none
459 */
audio_i2s_clk_dis(void)460 static inline void audio_i2s_clk_dis(void)
461 {
462 BM_CLR(reg_i2s_step,FLD_I2S_CLK_EN);
463 }
464
465
466 /**
467 * @brief This function serves to set disable codec clk
468 * @return none
469 */
audio_codec_clk_en(void)470 static inline void audio_codec_clk_en(void)
471 {
472 BM_SET(reg_dmic_step,FLD_DMIC_SEL);
473 }
474
475
476 /**
477 * @brief This function serves to set disable codec clk
478 * @return none
479 */
audio_codec_clk_dis(void)480 static inline void audio_codec_clk_dis(void)
481 {
482 BM_CLR(reg_dmic_step,FLD_DMIC_SEL);
483 }
484
485
486 /**
487 * @brief This function serves to set the bclk
488 * @param[in] div - bclk=i2s_clk/(div*2),if div=0,i2s_clk=bclk.
489 * @return none
490 */
audio_set_i2s_bclk(unsigned char div)491 static inline void audio_set_i2s_bclk(unsigned char div)
492 {
493 reg_pcm_clk_num=div&0x0f;
494 }
495
496 /**
497 * @brief This function serves to set the lrclk divider.
498 * @param[in] adc_div - adc_lrclk=bclk/(adc_div)
499 * @param[in] dac_div - dac_lrclk=bclk/(dac_div)
500 * @return none
501 */
audio_set_lrclk(unsigned short adc_div,unsigned short dac_div)502 static inline void audio_set_lrclk(unsigned short adc_div,unsigned short dac_div)
503 {
504 reg_int_pcm_num=(adc_div-1);
505 reg_dec_pcm_num=(dac_div-1);
506 }
507
508
509
510 /**
511 * @brief This function serves to set tx buff length,
512 * @param[in] len - the length of tx buff
513 * @return none
514 */
audio_set_tx_buff_len(unsigned short len)515 static inline void audio_set_tx_buff_len(unsigned short len)
516 {
517 reg_tx_max=((len)>>2)-1;
518 }
519
520 /**
521 * @brief This function serves to set rx buff length,
522 * @param[in] len - the length of rx buff
523 * @return none
524 */
audio_set_rx_buff_len(unsigned short len)525 static inline void audio_set_rx_buff_len(unsigned short len)
526 {
527 reg_rx_max=((len)>>2)-1;
528 }
529
530
531 /**
532 * @brief This function serves to write codec register by mc.
533 * @param[in] addr - the address of codec register
534 * @param[in] data - Data to be written
535 * @return none
536 */
audio_write_codec_reg(unsigned char addr,unsigned char data)537 static inline void audio_write_codec_reg(unsigned char addr,unsigned char data)
538 {
539 write_reg8(REG_AUDIO_AHB_BASE+((0x80+addr)<<2), data);
540
541 }
542
543 /**
544 * @brief This function serves to read codec register by mc.
545 * @param[in] addr - the address of codec register
546 * @return data
547 */
audio_read_codec_reg(unsigned char addr)548 static inline unsigned char audio_read_codec_reg(unsigned char addr)
549 {
550 return read_reg8(REG_AUDIO_AHB_BASE+((0x80+addr)<<2));
551
552 }
553
554 /**
555 * @brief This function serves to tx fifo trigger number.
556 * @param[in] number - the number of dma trigger
557 * @return none
558 */
audio_set_fifo_tx_trig_num(unsigned char number)559 static inline void audio_set_fifo_tx_trig_num(unsigned char number)
560 {
561 reg_fifo_trig0 &=~FLD_AUDIO_FIFO_AOUT0_TRIG_NUM;
562 reg_fifo_trig0 |=(number&FLD_AUDIO_FIFO_AOUT0_TRIG_NUM);
563 }
564
565 /**
566 * @brief This function serves to rx fifo trigger number.
567 * @param[in] number - the number of dma trigger
568 * @return none
569 */
audio_set_fifo_rx_trig_num(unsigned char number)570 static inline void audio_set_fifo_rx_trig_num(unsigned char number)
571 {
572 reg_fifo_trig0 &= ~FLD_AUDIO_FIFO_AIN0_TRIG_NUM;
573 reg_fifo_trig0 |=((number&0x0f)<<4);
574 }
575
576 /**
577 * @brief This function serves to enable or disable i2s clk and codec mc clk .
578 * @param[in] i2s_clk_en - 1 enable : 0 disable
579 * @param[in] mc_clk_en - 1 enable : 0 disable
580 * @return none
581 */
audio_clk_en(unsigned char i2s_clk_en,unsigned char mc_clk_en)582 static inline void audio_clk_en(unsigned char i2s_clk_en,unsigned char mc_clk_en)
583 {
584 reg_audio_en=MASK_VAL( FLD_AUDIO_I2S_CLK_EN, i2s_clk_en,\
585 FLD_AUDIO_MC_CLK_EN, mc_clk_en);
586 }
587
588
589 /**
590 * @brief This function serves to get dma rx buff pointer.
591 * @param[in] chn - dma channel
592 * @return none
593 */
audio_get_rx_dma_wptr(dma_chn_e chn)594 static inline unsigned int audio_get_rx_dma_wptr (dma_chn_e chn)
595 {
596 return convert_ram_addr_bus2cpu(reg_dma_dst_addr(chn));
597 }
598
599 /**
600 * @brief This function serves to get dma tx buff pointer.
601 * @param[in] chn - dma channel
602 * @return none
603 */
audio_get_tx_dma_rptr(dma_chn_e chn)604 static inline unsigned int audio_get_tx_dma_rptr (dma_chn_e chn)
605 {
606 return convert_ram_addr_bus2cpu(reg_dma_src_addr(chn));
607 }
608
609
610 /**
611 * @brief This function serves to invert data between R channel and L channel.
612 * @return none
613 */
audio_invert_data_en(void)614 static inline void audio_invert_data_en(void)
615 {
616 BM_SET(reg_i2s_cfg,FLD_AUDIO_I2S_LRSWAP);
617 }
618
619 /**
620 * @brief This function serves to set fifo0 data path .
621 * @param[in] ain0_sel :fifo0 input source select
622 * @param[in] aout0_sel:fifo0 output source select
623 * @return none
624 */
audio_data_fifo0_path_sel(audio_mux_ain_e ain0_sel,audio_mux_aout_e aout0_sel)625 static inline void audio_data_fifo0_path_sel (audio_mux_ain_e ain0_sel, audio_mux_aout_e aout0_sel)
626 {
627 reg_audio_sel = MASK_VAL( FLD_AUDIO_AIN0_SEL, ain0_sel,FLD_AUDIO_AOUT0_SEL, aout0_sel);
628 }
629
630
631 /**
632 * @brief This function serves to set fifo1 data path .
633 * @param[in] ain1_sel :fifo1 input source select
634 * @param[in] aout1_sel:fifo1 output source select
635 * @return none
636 */
audio_data_fifo1_path_sel(audio_mux_ain_e ain1_sel,audio_mux_aout_e aout1_sel)637 static inline void audio_data_fifo1_path_sel ( audio_mux_ain_e ain1_sel, audio_mux_aout_e aout1_sel)
638 {
639 reg_audio_sel = MASK_VAL( FLD_AUDIO_AIN1_SEL, ain1_sel, FLD_AUDIO_AOUT1_SEL, aout1_sel);
640 }
641
642
643
644 /**
645 * @brief This function serves to invert data between R channel and L channel.
646 * @return none
647 */
audio_invert_data_dis(void)648 static inline void audio_invert_data_dis(void)
649 {
650 BM_CLR(reg_i2s_cfg,FLD_AUDIO_I2S_LRSWAP);
651 }
652
653 /**
654 * @brief This function serves to set codec supply voltage
655 * @param[in] volt - the voltage of codec supply.A1 2.8V default,A0 1.8V default.
656 * @return none
657 *
658 */
659 void audio_set_codec_supply (codec_volt_supply_e volt);
660
661 /**
662 * @brief This function configures dmic pin.
663 * @param[in] pin_gp - the group of dmic pin
664 * @return none
665 */
666 void audio_set_dmic_pin(dmic_pin_group_e pin_gp);
667
668 /**
669 * @brief This function serves to set data path.
670 * @param[in] audio_flow - audio flow select
671 * @param[in] ain0_mode - fifo0 input mode select
672 * @param[in] ain1_mode - fifo1 input mode select
673 * @param[in] i2s_aout_mode - fifo output source select
674 * @return none
675 */
676 void audio_mux_config(audio_flow_e audio_flow, audio_in_mode_e ain0_mode , audio_in_mode_e ain1_mode,audio_out_mode_e i2s_aout_mode);
677
678
679 /**
680 * @brief This function serves to config codec for dac.
681 * @param[in] mode - select i2s as master or slave
682 * @param[in] rate - audio sampling rate
683 * @param[in] data_select - codec dac word length
684 * @param[in] wreg_mode - mcu or i2c config codec
685 * @return none
686 */
687 void audio_codec_dac_config(i2s_codec_m_s_mode_e mode,audio_sample_rate_e rate,codec_data_select_e data_select,codec_wreg_mode_e wreg_mode);
688
689 /**
690 * @brief This function serves to config codec for adc.
691 * @param[in] mode - select i2s as master or slave
692 * @param[in] in_mode - line_in/amic/dmic input mode select
693 * @param[in] rate - audio sampling rate
694 * @param[in] data_select - codec adc word length
695 * @param[in] wreg_mode - mcu or i2c config codec
696 * @return none
697 */
698 void audio_codec_adc_config(i2s_codec_m_s_mode_e mode,audio_input_mode_e in_mode,audio_sample_rate_e rate,codec_data_select_e data_select,codec_wreg_mode_e wreg_mode);
699
700
701
702 /**
703 * @brief This function serves to config interface, word length, and m/s .
704 * @param[in] i2s_format - interface protocol
705 * @param[in] wl - audio data word length
706 * @param[in] m_s - select i2s as master or slave
707 * @param[in] i2s_config_t - the prt of i2s_config_t that configure i2s lr_clk phase and lr_clk swap.
708 * i2s_config_t->i2s_lr_clk_invert_select-lr_clk phase control(in RJ,LJ or i2s modes),in i2s mode(opposite phasing in RJ,LJ mode), 0=right channel data when lr_clk high ,1=right channel data when lr_clk low.
709 * in DSP mode(in DSP mode only), DSP mode A/B select,0=DSP mode A ,1=DSP mode B.
710 * i2s_config_t->i2s_data_invert_select - 0=left channel data left,1=right channel data left.
711 * @attention:If the left and right channels are both active in i2s mode,there will be a phase difference(about 1 sample) between the left and right channels,you can set i2s_lr_clk_invert_select=1 to eliminate the phase difference,
712 * but data output channel will be inverted,you can also set i2s_config_t->i2s_data_invert_select=1 to recovery it.
713 * @return none
714 */
715 void audio_i2s_config(i2s_mode_select_e i2s_format,i2s_data_select_e wl, i2s_codec_m_s_mode_e m_s , aduio_i2s_invert_config_t * i2s_config_t);
716
717 /**
718 * @brief This function serves to set i2s clock and audio sampling rate when i2s as master.
719 * @param[in] audio_rate - audio sampling rate
720 * @param[in] match - the match of audio rate.
721 * @param[in] match_en - initial must 0, then change rate must 1
722 * @return none
723 * @attention i2s clock divider from pll,sampling rate calculation is based on pll=192M,so pll must be 192M
724 */
725 _attribute_ram_code_sec_noinline_ void audio_set_i2s_clock (audio_sample_rate_e audio_rate,audio_rate_match_e match, unsigned char match_en);
726
727 /**
728 * @brief This function serves to config rx_dma channel.
729 * @param[in] chn - dma channel
730 * @param[in] dst_addr - the dma address of destination
731 * @param[in] data_len - the length of dma rx size by byte
732 * @param[in] head_of_list - the head address of dma llp.
733 * @return none
734 */
735 void audio_rx_dma_config(dma_chn_e chn,unsigned short * dst_addr,unsigned int data_len,dma_chain_config_t *head_of_list);
736
737 /**
738 * @brief This function serves to set rx dma chain transfer
739 * @param[in] rx_config - the head of list of llp_pointer.
740 * @param[in] llpointer - the next element of llp_pointer.
741 * @param[in] dst_addr -the dma address of destination.
742 * @param[in] data_len -the length of dma size by byte.
743 * @return none
744 */
745 void audio_rx_dma_add_list_element(dma_chain_config_t * rx_config,dma_chain_config_t *llpointer ,unsigned short * dst_addr,unsigned int data_len);
746
747 /**
748 * @brief This function serves to config tx_dma channel.
749 * @param[in] chn - dma channel
750 * @param[in] src_addr - the address of source
751 * @param[in] data_len - the length of dma rx size by byte
752 * @param[in] head_of_list - the head address of dma llp.
753 * @return none
754 */
755 void audio_tx_dma_config(dma_chn_e chn,unsigned short * src_addr, unsigned int data_len,dma_chain_config_t * head_of_list);
756
757 /**
758 * @brief This function serves to set tx dma chain transfer
759 * @param[in] config_addr - the head of list of llp_pointer.
760 * @param[in] llpointer - the next element of llp_pointer.
761 * @param[in] src_addr - the address of source
762 * @param[in] data_len - the length of dma size by byte.
763 * @return none
764 */
765 void audio_tx_dma_add_list_element(dma_chain_config_t *config_addr,dma_chain_config_t *llpointer ,unsigned short * src_addr,unsigned int data_len);
766
767 /**
768 * @brief This function serves to enable rx_dma channel.
769 * @return none
770 */
771 void audio_rx_dma_en(void);
772
773 /**
774 * @brief This function serves to disable rx_dma channel.
775 * @return none
776 */
777 void audio_rx_dma_dis(void);
778
779 /**
780 * @brief This function serves to enable tx_dma channel.
781 * @return none
782 */
783 void audio_tx_dma_en(void);
784
785
786 /**
787 * @brief This function serves to disable tx_dma channel.
788 * @return none
789 */
790 void audio_tx_dma_dis(void);
791
792
793 /**
794 * @brief This function serves to initialize audio by mc
795 * @param[in] flow_mode - select input out flow mode
796 * @param[in] rate - audio sampling rate.
797 * @param[in] channel_wl - word length and channel number.
798 * @return none
799 */
800 void audio_init(audio_flow_mode_e flow_mode,audio_sample_rate_e rate,audio_channel_wl_mode_e channel_wl);
801
802
803 /**
804 * @brief This function serves to initialize audio by i2c
805 * @param[in] flow_mode - select input out flow mode
806 * @param[in] rate - audio sampling rate.
807 * @param[in] channel_wl - word length and channel number.
808 * @return none
809 */
810 void audio_init_i2c(audio_flow_mode_e flow_mode,audio_sample_rate_e rate,audio_channel_wl_mode_e channel_wl);
811
812
813 /**
814 * @brief This function serves to initialize audio(external codec WM8731) by i2c.
815 * @param[in] pwm0_pin - the pin of pwm0
816 * @param[in] sda_pin - the pin port selected as I2C sda pin port.
817 * @param[in] scl_pin - the pin port selected as I2C scl pin port.
818 * @return none
819 */
820 void audio_i2s_init(pwm_pin_e pwm0_pin, i2c_sda_pin_e sda_pin,i2c_scl_pin_e scl_pin);
821
822 /**
823 * @brief This function serves to set audio rx dma chain transfer.
824 * @param[in] chn - dma channel
825 * @param[in] in_buff - the pointer of rx_buff.
826 * @param[in] buff_size - the size of rx_buff.
827 * @return none
828 */
829 void audio_rx_dma_chain_init (dma_chn_e chn,unsigned short * in_buff,unsigned int buff_size );
830
831 /**
832 * @brief This function serves to initialize audio tx dma chain transfer.
833 * @param[in] chn - dma channel
834 * @param[in] out_buff - the pointer of tx_buff.
835 * @param[in] buff_size - the size of tx_buff.
836 * @return none
837 */
838 void audio_tx_dma_chain_init (dma_chn_e chn,unsigned short * out_buff,unsigned int buff_size);
839
840 /**
841 * @brief This function serves to invert data between R channel and L channel.
842 * @param[in] en - I2S_DATA_INVERT_DIS: L channel ( left channel data left); I2S_DATA_INVERT_EN(right channel data left)
843 * @attention must be set before audio_init().
844 * @return none
845 */
846 void audio_set_mono_chn(audio_data_invert_e en);
847
848 /**
849 * @brief This function serves to invert LR-clk.
850 * @param[in] en -lr_clk phase control(in RJ,LJ or i2s modes),in i2s mode(opposite phasing in RJ,LJ mode), 0=right channel data when lr_clk high ,1=right channel data when lr_clk low.
851 * in DSP mode(in DSP mode only), DSP mode A/B select,0=DSP mode A ,1=DSP mode B
852 * @attention If the left and right channels are both active,there will be a phase difference(about 1 sample) between the left and right channels,invert lr_clk can eliminate the phase difference,but data output channel will invert.
853 * @attention must be set before audio_init().
854 * @return none
855 */
856 void audio_invert_i2s_lr_clk(audio_i2s_lr_clk_invert_e en);
857
858
859 /**
860 * @brief This function serves to set mic input mode.
861 * @param[in] input_mode - 0 single-ended input, 1 differential input.
862 * @attention must be set before audio_init().
863 * @return none
864 */
865 void audio_set_codec_mic_input_mode (audio_input_mode_select_e input_mode);
866
867 /**
868 * @brief This function serves to set in path digital and analog gain .
869 * @param[in] d_gain - digital gain value
870 * @param[in] a_gain - analog gain value
871 * @attention must be set before audio_init().
872 * @return none
873 */
874 void audio_set_codec_in_path_a_d_gain (codec_in_path_digital_gain_e d_gain,codec_in_path_analog_gain_e a_gain );
875
876 /**
877 * @brief This function serves to set out path digital and analog gain .
878 * @param[in] d_gain - digital gain value
879 * @param[in] a_gain - analog gain value
880 * @return none
881 */
882 void audio_set_codec_out_path_a_d_gain (codec_out_path_digital_gain_e d_gain,codec_out_path_analog_gain_e a_gain);
883
884 /**
885 * @brief This function serves to choose which is master to provide clock.
886 * @param[in] m_s - I2S_S_CODEC_M: i2s as slave ,codec as master; I2S_M_CODEC_S: i2s as master, codec as slave.
887 * @attention must be set before audio_init().
888 * @return none
889 */
890 void audio_set_i2s_codec_m_s (i2s_codec_m_s_mode_e m_s);
891
892
893 /**
894 * @brief This function serves to set wind noise filter(WNF),it is a programmable high pass filter feature enabling to reduce wind noise.
895 * @param[in] mode - the wind noise filter mode,the wind noise filter is a 1st order filter.
896 * Mode1 -3dB 59Hz
897 * Wind Noise Filter corner frequency Mode2 -3dB 117Hz
898 Mode3 -3dB 235Hz
899 * @attention must be set before audio_init().
900 * @return none
901 */
902 void audio_set_codec_wnf(adc_wnf_mode_sel_e mode);
903
904 /**
905 * @brief This function serves to set dac output channel.
906 * @param[in] chn -DAC_OUTPUT_L_R_CHN - right and left channel both active ; DAC_OUTPUT_L_CHN, only left channel active.
907 * @return none
908 * @attention must be set before audio_init().
909 */
910 void audio_set_output_chn(audio_output_chn_e chn);
911
912 /**
913 * @brief This function serves to active soft mute dac and disable dma.
914 * @return none
915 */
916 void audio_pause_out_path(void);
917
918
919 /**
920 * @brief This function serves to inactive soft mute dac and enable dma .
921 * @return none
922 */
923 void audio_resume_out_path(void);
924
925 /**
926 * @brief This function serves to change sample rate for dac.
927 * @param[in] rate - the sample rate of dac
928 * @return none
929 */
930 _attribute_ram_code_sec_noinline_ void audio_change_sample_rate (audio_sample_rate_e rate);
931
932
933 /**
934 * @brief This function serves to power down codec_dac.
935 * @return none
936 */
937 void audio_codec_dac_power_down(void);
938
939 /**
940 * @brief This function serves to power on codec_dac.
941 * @return none
942 */
943 void audio_codec_dac_power_on(void);
944
945 /**
946 * @brief This function serves to power down codec_adc.
947 * @return none
948 */
949 void audio_codec_adc_power_down(void);
950
951 /**
952 * @brief This function serves to power on codec_adc.
953 * @return none
954 */
955 void audio_codec_adc_power_on(void);
956 #endif
957