audioin_hal.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2020 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file
  8. * @brief Audio In HAL
  9. */
  10. #ifndef SYS_LOG_DOMAIN
  11. #define SYS_LOG_DOMAIN "hal_ain"
  12. #endif
  13. #include <assert.h>
  14. #include <audio_hal.h>
  15. srd_callback spdifrx_srd_callback = NULL;
  16. srd_callback i2srx_srd_callback = NULL;
  17. hal_audio_in_context_t hal_audio_in_context;
  18. static inline hal_audio_in_context_t* _hal_audio_in_get_context(void)
  19. {
  20. return &hal_audio_in_context;
  21. }
  22. int hal_audio_in_init(void)
  23. {
  24. hal_audio_in_context_t* audio_in = _hal_audio_in_get_context();
  25. audio_in->ain_dev = (struct device *)device_get_binding(CONFIG_AUDIO_IN_ACTS_DEV_NAME);
  26. if (!audio_in->ain_dev) {
  27. SYS_LOG_ERR("device not found\n");
  28. return -ENODEV;
  29. }
  30. SYS_LOG_INF("success \n");
  31. return 0;
  32. }
  33. void* hal_ain_channel_open(audio_in_init_param_t *init_param)
  34. {
  35. hal_audio_in_context_t* audio_in = _hal_audio_in_get_context();
  36. ain_param_t ain_param = {0};
  37. adc_setting_t adc_setting;
  38. #ifdef CONFIG_AUDIO_OUT_I2SRX_SUPPORT
  39. i2srx_setting_t i2srx_setting = {0};
  40. #endif
  41. #ifdef CONFIG_AUDIO_OUT_SPDIFRX_SUPPORT
  42. spdifrx_setting_t spdifrx_setting = {0};
  43. #endif
  44. memset(&ain_param, 0, sizeof(ain_param_t));
  45. ain_param.sample_rate = init_param->sample_rate;
  46. ain_param.callback = init_param->callback;
  47. ain_param.cb_data = init_param->callback_data;
  48. ain_param.reload_setting.reload_addr = init_param->reload_addr;
  49. ain_param.reload_setting.reload_len = init_param->reload_len;
  50. ain_param.channel_type = init_param->channel_type;
  51. switch (init_param->channel_type) {
  52. case AUDIO_CHANNEL_ADC:
  53. {
  54. uint8_t i;
  55. adc_setting.device = init_param->audio_device;
  56. for (i = 0; i < ADC_CH_NUM_MAX; i++)
  57. adc_setting.gain.ch_gain[i] = init_param->input_gain;
  58. ain_param.adc_setting = &adc_setting;
  59. break;
  60. }
  61. #ifdef CONFIG_AUDIO_OUT_I2SRX_SUPPORT
  62. case AUDIO_CHANNEL_I2SRX:
  63. {
  64. i2srx_setting.mode = I2S_SLAVE_MODE;
  65. i2srx_setting.srd_callback = i2srx_srd_callback;
  66. ain_param.i2srx_setting = &i2srx_setting;
  67. break;
  68. }
  69. #endif
  70. #ifdef CONFIG_AUDIO_OUT_SPDIFRX_SUPPORT
  71. case AUDIO_CHANNEL_SPDIFRX:
  72. {
  73. spdifrx_setting.srd_callback = spdifrx_srd_callback;
  74. ain_param.spdifrx_setting = &spdifrx_setting;
  75. break;
  76. }
  77. #endif
  78. default:
  79. {
  80. SYS_LOG_ERR("not support: %d", init_param->channel_type);
  81. return NULL;
  82. }
  83. }
  84. return audio_in_open((struct device *)audio_in->ain_dev, &ain_param);
  85. }
  86. int hal_ain_channel_start(void* ain_channel_handle)
  87. {
  88. int result;
  89. hal_audio_in_context_t* audio_in = _hal_audio_in_get_context();
  90. assert(audio_in->ain_dev);
  91. result = audio_in_start((struct device *)audio_in->ain_dev, ain_channel_handle);
  92. return result;
  93. }
  94. int hal_ain_channel_read_data(void* ain_channel_handle, u8_t *data, u32_t data_size)
  95. {
  96. int result = 0;
  97. #if 0
  98. hal_audio_in_context_t* audio_in = _hal_audio_in_get_context();
  99. assert(audio_in->ain_dev);
  100. #endif
  101. return result;
  102. }
  103. int hal_ain_channel_stop(void* ain_channel_handle)
  104. {
  105. int result;
  106. hal_audio_in_context_t* audio_in = _hal_audio_in_get_context();
  107. assert(audio_in->ain_dev);
  108. result = audio_in_stop(audio_in->ain_dev, ain_channel_handle);
  109. return result;
  110. }
  111. int hal_ain_channel_close(void* ain_channel_handle)
  112. {
  113. int result;
  114. hal_audio_in_context_t* audio_in = _hal_audio_in_get_context();
  115. assert(audio_in->ain_dev);
  116. result = audio_in_close(audio_in->ain_dev, ain_channel_handle);
  117. return result;
  118. }
  119. int hal_ain_channel_set_volume(void* ain_channel_handle, adc_gain *adc_volume)
  120. {
  121. int result;
  122. hal_audio_in_context_t* audio_in = _hal_audio_in_get_context();
  123. assert(audio_in->ain_dev);
  124. result = audio_in_control(audio_in->ain_dev, ain_channel_handle, AIN_CMD_SET_ADC_GAIN, adc_volume);
  125. return 0;
  126. }
  127. int hal_ain_set_contrl_callback(u8_t channel_type, srd_callback callback)
  128. {
  129. if (channel_type == AUDIO_CHANNEL_SPDIFRX) {
  130. spdifrx_srd_callback = callback;
  131. } else if (channel_type == AUDIO_CHANNEL_I2SRX) {
  132. i2srx_srd_callback = callback;
  133. } else {
  134. SYS_LOG_ERR("not support channel type: %d", channel_type);
  135. }
  136. return 0;
  137. }
  138. int hal_ain_set_aec_record_back(void* ain_channel_handle, uint8_t enable)
  139. {
  140. int result;
  141. hal_audio_in_context_t* audio_in = _hal_audio_in_get_context();
  142. assert(audio_in->ain_dev);
  143. result = audio_in_control(audio_in->ain_dev, ain_channel_handle, AIN_CMD_AEC_CONTROL, &enable);
  144. return result;
  145. }