1 // Copyright (C) 2022 Beken Corporation 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 #pragma once 16 #include <common/bk_include.h> 17 #include <driver/aud_types.h> 18 19 #ifdef __cplusplus 20 extern "C" { 21 #endif 22 23 /* @brief Overview about this API header 24 * 25 */ 26 27 /** 28 * @brief AUD API 29 * @defgroup bk_api_aud AUD API group 30 * @{ 31 */ 32 33 /** 34 * @brief Init the AUD driver 35 * 36 * This API init the resoure common: 37 * - Init AUD driver control memory 38 * - Configure clock and power 39 * - Configure mic enable 40 * - Register AUD isr interrupt 41 * 42 * This API should be called before any other AUD APIs. 43 * 44 * @return 45 * - BK_OK: succeed 46 * - others: other errors. 47 */ 48 bk_err_t bk_aud_driver_init(void); 49 50 /** 51 * @brief Deinit the AUD driver 52 * 53 * This API free all resource related to AUD, power down AUD and mic. 54 * 55 * @return 56 * - BK_OK: succeed 57 * - others: other errors. 58 */ 59 bk_err_t bk_aud_driver_deinit(void); 60 61 /** 62 * @brief Init the adc module of audio 63 * 64 * This API init the adc module: 65 * - Set adc work mode: adc/dtmf 66 * - Configure the adc/dtmf parameters 67 * - disable adc/dtmf 68 * - disable adc/dtmf interrupts 69 * 70 * @param adc_work_mode adc work mode adc/dtmf 71 * @param adc_config adc configure of adc work mode 72 * @param dtmf_config dtmf configure of dtmf work mode 73 * 74 * @return 75 * - BK_OK: succeed 76 * - BK_ERR_AUD_ADC_MODE: adc work mode is error 77 * - BK_ERR_NULL_PARAM: config is NULL 78 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 79 * - others: other errors. 80 */ 81 bk_err_t bk_aud_adc_init(aud_adc_work_mode_t adc_work_mode, const aud_adc_config_t *adc_config, const aud_dtmf_config_t *dtmf_config); 82 83 /** 84 * @brief Deinit adc module 85 * 86 * This API deinit the adc module of audio: 87 * - Disable adc and dtmf 88 * 89 * @return 90 * - BK_OK: succeed 91 * - others: other errors. 92 */ 93 bk_err_t bk_aud_adc_deinit(void); 94 95 /** 96 * @brief Set the sample rate in adc work mode 97 * 98 * @param samp_rate adc sample rate of adc work mode 99 * 100 * @return 101 * - BK_OK: succeed 102 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 103 * - others: other errors. 104 */ 105 bk_err_t bk_aud_set_adc_samp_rate(aud_adc_samp_rate_t samp_rate); 106 107 /** 108 * @brief Set the adc gain in adc work mode 109 * 110 * @param value the gain value of adc work mode 111 * 112 * @return 113 * - BK_OK: succeed 114 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 115 * - others: other errors. 116 */ 117 bk_err_t bk_aud_set_adc_gain(uint32_t value); 118 119 /** 120 * @brief Set the adc gain in adc work mode 121 * 122 * @param mic_chl the mic channel value of adc work mode 123 * 124 * @return 125 * - BK_OK: succeed 126 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 127 * - others: other errors. 128 */ 129 bk_err_t bk_aud_set_mic_chl(aud_mic_enable_t mic_chl); 130 131 /** 132 * @brief Get the adc fifo address in adc work mode 133 * 134 * @param adc_fifo_addr adc fifo address of adc work mode 135 * 136 * @return 137 * - BK_OK: succeed 138 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 139 * - others: other errors. 140 */ 141 bk_err_t bk_aud_get_adc_fifo_addr(uint32_t *adc_fifo_addr); 142 143 /** 144 * @brief Get the dtmf fifo address in adc work mode 145 * 146 * @param dtmf_fifo_addr dtmf fifo address of dtmf work mode 147 * 148 * @return 149 * - BK_OK: succeed 150 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 151 * - others: other errors. 152 */ 153 bk_err_t bk_aud_get_dtmf_fifo_addr(uint32_t *dtmf_fifo_addr); 154 155 /** 156 * @brief Get the dac fifo address 157 * 158 * @param 159 * - dac_fifo_addr: dac fifo address 160 * 161 * @return 162 * - BK_OK: succeed 163 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 164 * - others: other errors. 165 */ 166 bk_err_t bk_aud_get_dac_fifo_addr(uint32_t *dac_fifo_addr); 167 168 /** 169 * @brief Get the adc status information in adc work mode 170 * 171 * This API get the adc status of adc work mode: 172 * - Get fifo status 173 * 174 * @param adc_status adc fifo status and agc status 175 * 176 * @return 177 * - BK_OK: succeed 178 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 179 * - others: other errors. 180 */ 181 bk_err_t bk_aud_get_adc_status(uint32_t *adc_status); 182 183 /** 184 * @brief Get the dtmf status information in dtmf work mode 185 * 186 * This API get the adc status of dtmf work mode: 187 * - Get fifo status 188 * 189 * @param dtmf_status dtmf fifo status 190 * 191 * @return 192 * - BK_OK: succeed 193 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 194 * - others: other errors. 195 */ 196 bk_err_t bk_aud_get_dtmf_status(uint32_t *dtmf_status); 197 198 /** 199 * @brief Enable adc interrupt 200 * 201 * This API enable adc interrupt: 202 * - Enable adc interrupt if work mode is adc work mode 203 * - Enable dtmf interrupt if work mode is dtmf work mode 204 * 205 * @return 206 * - BK_OK: succeed 207 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 208 * - others: other errors. 209 */ 210 bk_err_t bk_aud_enable_adc_int(void); 211 212 /** 213 * @brief Disable adc interrupt 214 * 215 * This API disable adc interrupt: 216 * - Disable adc interrupt if work mode is adc work mode 217 * - Disable dtmf interrupt if work mode is dtmf work mode 218 * 219 * @return 220 * - BK_OK: succeed 221 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 222 * - others: other errors. 223 */ 224 bk_err_t bk_aud_disable_adc_int(void); 225 226 /** 227 * @brief Start adc 228 * 229 * This API start adc: 230 * - Enable adc if work mode is adc work mode 231 * - Enable dtmf if work mode is dtmf work mode 232 * 233 * Usage example: 234 * 235 * //init audio driver 236 * bk_aud_driver_init(); 237 * 238 * //init adc configuration 239 * aud_adc_config_t adc_config; 240 * adc_config.samp_rate = AUD_ADC_SAMP_RATE_8K; 241 * adc_config.adc_enable = AUD_ADC_DISABLE; 242 * adc_config.line_enable = AUD_ADC_LINE_DISABLE; 243 * adc_config.dtmf_enable = AUD_DTMF_DISABLE; 244 * adc_config.adc_hpf2_coef_B2 = 0; 245 * adc_config.adc_hpf2_bypass_enable = AUD_ADC_HPF_BYPASS_ENABLE; 246 * adc_config.adc_hpf1_bypass_enable = AUD_ADC_HPF_BYPASS_ENABLE; 247 * adc_config.adc_set_gain = 0x2d; 248 * adc_config.adc_samp_edge = AUD_ADC_SAMP_EDGE_RISING; 249 * adc_config.adc_hpf2_coef_B0 = 0; 250 * adc_config.adc_hpf2_coef_B1 = 0; 251 * adc_config.adc_hpf2_coef_A0 = 0; 252 * adc_config.adc_hpf2_coef_A1 = 0; 253 * adc_config.dtmf_wr_threshold = 8; 254 * adc_config.adcl_wr_threshold = 8; 255 * adc_config.dtmf_int_enable = AUD_DTMF_INT_DISABLE; 256 * adc_config.adcl_int_enable = AUD_ADCL_INT_DISABLE; 257 * adc_config.loop_adc2dac = AUD_LOOP_ADC2DAC_DISABLE; 258 * adc_config.agc_noise_thrd = 101; 259 * adc_config.agc_noise_high = 101; 260 * adc_config.agc_noise_low = 160; 261 * adc_config.agc_noise_min = 1; 262 * adc_config.agc_noise_tout = 0; 263 * adc_config.agc_high_dur = 3; 264 * adc_config.agc_low_dur = 3; 265 * adc_config.agc_min = 1; 266 * adc_config.agc_max = 4; 267 * adc_config.agc_ng_method = AUD_AGC_NG_METHOD_MUTE; 268 * adc_config.agc_ng_enable = AUD_AGC_NG_DISABLE; 269 * adc_config.agc_decay_time = AUD_AGC_DECAY_TIME_128; 270 * adc_config.agc_attack_time = AUD_AGC_ATTACK_TIME_128; 271 * adc_config.agc_high_thrd = 18; 272 * adc_config.agc_low_thrd = 0; 273 * adc_config.agc_iir_coef = AUD_AGC_IIR_COEF_1_1024; 274 * adc_config.agc_enable = AUD_AGC_DISABLE; 275 * adc_config.manual_pga_value = 0; 276 * adc_config.manual_pga_enable = AUD_GAC_MANUAL_PGA_DISABLE; 277 * adc_config.adc_fracmod_manual = AUD_ADC_TRACMOD_MANUAL_DISABLE; 278 * adc_config.adc_fracmod = 0; 279 * bk_aud_adc_init(AUD_ADC_WORK_MODE_ADC, &adc_config, NULL); 280 * CLI_LOGI("init adc successful\n"); 281 * 282 * //start adc and dac 283 * bk_aud_start_adc(); 284 * 285 * @return 286 * - BK_OK: succeed 287 * - BK_ERR_AUD_ADC_MODE: adc work mode is NULL 288 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 289 * - others: other errors. 290 */ 291 bk_err_t bk_aud_start_adc(void); 292 293 /** 294 * @brief Stop adc 295 * 296 * This API stop adc: 297 * - Disable adc if work mode is adc work mode 298 * - Disable dtmf if work mode is dtmf work mode 299 * 300 * @return 301 * - BK_OK: succeed 302 * - BK_ERR_AUD_ADC_MODE: adc work mode is NULL 303 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 304 * - others: other errors. 305 */ 306 bk_err_t bk_aud_stop_adc(void); 307 308 /** 309 * @brief Get adc data 310 * 311 * This API get adc fifo data 312 * 313 * @return 314 * - BK_OK: succeed 315 * - BK_ERR_AUD_ADC_MODE: adc work mode is NULL 316 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 317 * - others: other errors. 318 */ 319 bk_err_t bk_aud_get_adc_fifo_data(uint32_t *adc_data); 320 321 /** 322 * @brief Get dtmf data 323 * 324 * This API get dtmf fifo data 325 * 326 * @param 327 * - dtmf_data: save dtmf data 328 * 329 * @return 330 * - BK_OK: succeed 331 * - BK_ERR_AUD_ADC_MODE: dtmf work mode is NULL 332 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 333 * - others: other errors. 334 */ 335 bk_err_t bk_aud_get_dtmf_fifo_data(uint32_t *dtmf_data); 336 337 /** 338 * @brief Init the dac module of audio 339 * 340 * This API init the dac module: 341 * - Configure the dac parameters to enable dac function. 342 * 343 * @param 344 * - dac_config: dac parameters configure 345 * 346 * @return 347 * - BK_OK: succeed 348 * - BK_ERR_NULL_PARAM: config is NULL 349 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 350 */ 351 bk_err_t bk_aud_dac_init(const aud_dac_config_t *dac_config); 352 353 /** 354 * @brief Deinit dac module of audio 355 * 356 * This API deinit the dac module: 357 * - Configure the dac parameters to default value. 358 * 359 * @param 360 * - None 361 * 362 * @return 363 * - BK_OK: succeed 364 */ 365 bk_err_t bk_aud_dac_deinit(void); 366 367 /** 368 * @brief Set the dac sample rate 369 * 370 * This API set the dac sample rate value. 371 * 372 * @param 373 * - samp_rate: dac sample rate 374 * 375 * @return 376 * - BK_OK: succeed 377 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 378 */ 379 bk_err_t bk_aud_set_dac_samp_rate(aud_dac_samp_rate_t samp_rate); 380 381 /** 382 * @brief Set the dac gain 383 * 384 * @param value the gain value, range:0x00 ~ 0x3f 385 * 386 * @return 387 * - BK_OK: succeed 388 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 389 * - others: other errors. 390 */ 391 bk_err_t bk_aud_set_dac_gain(uint32_t value); 392 393 /** 394 * @brief Set the dac channel 395 * 396 * @param dac_chl the channel value 397 * 398 * @return 399 * - BK_OK: succeed 400 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 401 * - others: other errors. 402 */ 403 bk_err_t bk_aud_set_dac_chl(aud_dac_chl_enable_t dac_chl); 404 405 /** 406 * @brief Enable dac interrupt 407 * 408 * This API enable dac interrupt: 409 * 410 * @param 411 * - None 412 * 413 * @return 414 * - BK_OK: succeed 415 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 416 */ 417 bk_err_t bk_aud_enable_dac_int(void); 418 419 /** 420 * @brief Disable dac interrupt 421 * 422 * This API disable dac interrupt: 423 * 424 * @param 425 * - None 426 * 427 * @return 428 * - BK_OK: succeed 429 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 430 */ 431 bk_err_t bk_aud_disable_dac_int(void); 432 433 /** 434 * @brief Get the dac status information 435 * 436 * This API get the dac fifo status. 437 * 438 * @param 439 * - dac_status: dac fifo status 440 * 441 * @return 442 * - BK_OK: succeed 443 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 444 * 445 * Usage example: 446 * 447 * uint32_t dac_fifo_status = 0; 448 * bk_aud_get_dac_status(&dac_fifo_status); 449 * if (dac_fifo_status & AUD_DACL_NEAR_EMPTY_MASK) { 450 * } 451 * 452 */ 453 bk_err_t bk_aud_get_dac_status(uint32_t *dac_status); 454 455 /** 456 * @brief Start dac 457 * 458 * This API start dac function. 459 * 460 * @param 461 * - None 462 * 463 * @return 464 * - BK_OK: succeed 465 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 466 */ 467 bk_err_t bk_aud_start_dac(void); 468 469 /** 470 * @brief Stop dac 471 * 472 * This API stop dac function. 473 * 474 * @param 475 * - None 476 * 477 * @return 478 * - BK_OK: succeed 479 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 480 */ 481 bk_err_t bk_aud_stop_dac(void); 482 483 /** 484 * @brief Init the eq module of audio 485 * 486 * This API init the eq module: 487 * - Configure the eq parameters to enable the eq function. 488 * 489 * @param 490 * - eq_config: eq parameter configure 491 * 492 * @return 493 * - BK_OK: succeed 494 * - BK_ERR_NULL_PARAM: config is NULL 495 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 496 */ 497 bk_err_t bk_aud_eq_init(const aud_eq_config_t *eq_config); 498 499 /** 500 * @brief Deinit the eq module of audio 501 * 502 * This API deinit the eq module: 503 * - Configure the eq parameters to default value. 504 * 505 * @param 506 * - eq_config: eq parameter configure 507 * 508 * @return 509 * - BK_OK: succeed 510 * - BK_ERR_NULL_PARAM: config is NULL 511 */ 512 bk_err_t bk_aud_eq_deinit(aud_eq_config_t *eq_config); 513 514 /** 515 * @brief Register audio isr 516 * 517 * This API register audio isr: 518 * - Disable adc if work mode is adc work mode 519 * - Disable dtmf if work mode is dtmf work mode 520 * 521 * Usage example: 522 * 523 * void cli_aud_adcl_isr(void *param) 524 * { 525 * uint32_t adc_data; 526 * uint32_t adc_status; 527 * 528 * bk_aud_get_adc_status(&adc_status); 529 * if (adc_status & AUD_ADCL_NEAR_FULL_MASK) { 530 * bk_aud_get_adc_fifo_data(&adc_data); 531 * bk_aud_dac_write(adc_data); 532 * } 533 * } 534 * 535 * //register isr 536 * ret = bk_aud_register_aud_isr(AUD_ISR_ADCL, cli_aud_adcl_isr, NULL); 537 * CLI_LOGI("register adc isr successful\n"); 538 * 539 * //enable audio interrupt 540 * bk_aud_enable_adc_int(); 541 * CLI_LOGI("enable adc interrupt successful\n"); 542 * 543 * @param isr_id adc work mode adc/dtmf 544 * @param isr audio isr callback 545 * @param param audio isr callback parameter 546 * 547 * @return 548 * - BK_OK: succeed 549 * - BK_ERR_AUD_ADC_MODE: adc work mode is NULL 550 * - BK_ERR_AUD_NOT_INIT: audio driver is not init 551 * - others: other errors. 552 */ 553 bk_err_t bk_aud_register_aud_isr(aud_isr_id_t isr_id, aud_isr_t isr, void *param); 554 555 556 /** 557 * @} 558 */ 559 560 #ifdef __cplusplus 561 } 562 #endif 563