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