dac_external.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. #include <freertos/FreeRTOS.h>
  22. #include <freertos/task.h>
  23. #include <driver/i2s.h>
  24. #include "esp_log.h"
  25. #include "config.h"
  26. #include "adac.h"
  27. static bool init(int i2c_port_num, int i2s_num, i2s_config_t *config);
  28. static void deinit(void) { };
  29. static void speaker(bool active) { };
  30. static void headset(bool active) { } ;
  31. static void volume(unsigned left, unsigned right) { };
  32. static void power(adac_power_e mode) { };
  33. struct adac_s dac_external = { init, deinit, power, speaker, headset, volume };
  34. static char TAG[] = "DAC external";
  35. static bool init(int i2c_port_num, int i2s_num, i2s_config_t *config) {
  36. #if !defined(CONFIG_SQUEEZEAMP) && !defined(CONFIG_A1S)
  37. i2s_pin_config_t i2s_pin_config = (i2s_pin_config_t) { .bck_io_num = CONFIG_I2S_BCK_IO, .ws_io_num = CONFIG_I2S_WS_IO,
  38. .data_out_num = CONFIG_I2S_DO_IO, .data_in_num = -1 };
  39. char *nvs_item = config_alloc_get(NVS_TYPE_STR, "dac_config");
  40. if (nvs_item) {
  41. char *p;
  42. if ((p = strcasestr(nvs_item, "bck")) != NULL) i2s_pin_config.bck_io_num = atoi(strchr(p, '=') + 1);
  43. if ((p = strcasestr(nvs_item, "ws")) != NULL) i2s_pin_config.ws_io_num = atoi(strchr(p, '=') + 1);
  44. if ((p = strcasestr(nvs_item, "do")) != NULL) i2s_pin_config.data_out_num = atoi(strchr(p, '=') + 1);
  45. free(nvs_item);
  46. }
  47. if (i2s_pin_config.bck_io_num != -1 && i2s_pin_config.ws_io_num != -1 && i2s_pin_config.data_out_num != -1) {
  48. i2s_driver_install(i2s_num, config, 0, NULL);
  49. i2s_set_pin(i2s_num, &i2s_pin_config);
  50. ESP_LOGI(TAG, "DAC using I2S bck:%u, ws:%u, do:%u", i2s_pin_config.bck_io_num, i2s_pin_config.ws_io_num, i2s_pin_config.data_out_num);
  51. return true;
  52. } else {
  53. ESP_LOGI(TAG, "Cannot initialize I2S for SPDIF bck:%d ws:%d do:%d", i2s_pin_config.bck_io_num,
  54. i2s_pin_config.ws_io_num,
  55. i2s_pin_config.data_out_num);
  56. return false;
  57. }
  58. #else
  59. return true;
  60. #endif
  61. }