dac_external.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Squeezelite for esp32
  3. *
  4. * (c) Sebastien 2019
  5. * Philippe G. 2019, philippe_44@outlook.com
  6. *
  7. * This software is released under the MIT License.
  8. * https://opensource.org/licenses/MIT
  9. *
  10. */
  11. #define LOG_LOCAL_LEVEL ESP_LOG_INFO
  12. #include "Config.h"
  13. #include "adac.h"
  14. #include "cJSON.h"
  15. #include "driver/i2c.h"
  16. #include "esp_log.h"
  17. #include "esp_check.h"
  18. #include "gpio_exp.h"
  19. #include "inttypes.h"
  20. #include "string.h"
  21. #include <driver/i2s.h>
  22. #include <freertos/FreeRTOS.h>
  23. #include <freertos/task.h>
  24. static const char TAG[] = "DAC external";
  25. static void speaker(bool active);
  26. static void headset(bool active);
  27. static bool volume(unsigned left, unsigned right) { return false; }
  28. static void power(adac_power_e mode);
  29. static bool init(sys_dac_config* config, i2s_config_t* i2s_config, bool* mck);
  30. // static bool i2c_json_execute(char *set);
  31. static bool i2c_execute_cmd(sys_dac_control_type cmd_type);
  32. const struct adac_s dac_external = {sys_dac_models_I2S, init, adac_deinit, power, speaker, headset, volume};
  33. static int i2c_addr;
  34. extern sys_dac_default_sets* default_dac_sets;
  35. static EXT_RAM_ATTR sys_dac_control_set* i2c_default_controlset = NULL;
  36. static EXT_RAM_ATTR sys_dac_control_set* i2c_controlset = NULL;
  37. /****************************************************************************************
  38. * init
  39. */
  40. static bool init(sys_dac_config* config, i2s_config_t* i2s_config, bool* mck) {
  41. #ifdef BYTES_PER_FRAME
  42. uint32_t bytes_per_frame = BYTES_PER_FRAME;
  43. #else
  44. uint32_t bytes_per_frame = 4;
  45. #endif
  46. i2c_addr = adac_init(config);
  47. if (!i2c_addr) return true;
  48. ESP_LOGI(TAG, "DAC on I2C @%d", i2c_addr);
  49. ESP_LOGD(TAG, "Checkinf if there's a default control set for DAC %s in the list of %d presets", sys_dac_models_name(config->model),
  50. default_dac_sets->sets_count);
  51. for (int i = 0; i < default_dac_sets->sets_count; i++) {
  52. ESP_LOGD(TAG, "Checkinf if preset #%d (%s[%d]) matches %s[%d]", i + 1, sys_dac_models_name(default_dac_sets->sets[i].model),
  53. default_dac_sets->sets[i].bytes_per_frame, sys_dac_models_name(config->model), bytes_per_frame);
  54. if (default_dac_sets->sets[i].bytes_per_frame == bytes_per_frame && default_dac_sets->sets[i].model == config->model) {
  55. if (!default_dac_sets->sets[i].valid) {
  56. ESP_LOGE(TAG, "DAC %s is unsupported with %d bytes per frame", sys_dac_models_name(config->model), bytes_per_frame);
  57. return false;
  58. }
  59. i2c_default_controlset = &default_dac_sets->sets[i].set;
  60. }
  61. }
  62. if (config->has_daccontrolset && config->daccontrolset.commands_count > 0) {
  63. i2c_controlset = &config->daccontrolset;
  64. }
  65. if (!i2c_controlset && !i2c_default_controlset) {
  66. ESP_LOGE(TAG, "DAC configuration invalid for %s", sys_dac_models_name(config->model));
  67. return false;
  68. }
  69. if (mck) {
  70. *mck = i2c_controlset != NULL ? i2c_controlset->mclk_needed : i2c_default_controlset->mclk_needed;
  71. ESP_LOGD(TAG, "Master clock is %s", (*mck) ? "NEEDED" : "NOT NEEDED");
  72. }
  73. if (!i2c_execute_cmd(sys_dac_control_type_INIT)) {
  74. ESP_LOGE(TAG, "could not intialize DAC");
  75. return false;
  76. }
  77. return true;
  78. }
  79. /****************************************************************************************
  80. * power
  81. */
  82. static void power(adac_power_e mode) {
  83. if (mode == ADAC_STANDBY || mode == ADAC_OFF)
  84. i2c_execute_cmd(sys_dac_control_type_POWER_OFF);
  85. else
  86. i2c_execute_cmd(sys_dac_control_type_POWER_ON);
  87. }
  88. /****************************************************************************************
  89. * speaker
  90. */
  91. static void speaker(bool active) {
  92. if (active)
  93. i2c_execute_cmd(sys_dac_control_type_SPEAKER_ON);
  94. else
  95. i2c_execute_cmd(sys_dac_control_type_SPEAKER_OFF);
  96. }
  97. /****************************************************************************************
  98. * headset
  99. */
  100. static void headset(bool active) {
  101. if (active)
  102. i2c_execute_cmd(sys_dac_control_type_HEADSET_ON);
  103. else
  104. i2c_execute_cmd(sys_dac_control_type_HEADSET_OFF);
  105. }
  106. /****************************************************************************************
  107. *
  108. */
  109. static const sys_dac_control_itm* i2c_get_controlset_cmd(sys_dac_control_set* set, sys_dac_control_type cmd_type, pb_size_t* items_count) {
  110. ESP_RETURN_ON_FALSE(set!=NULL,NULL,TAG,"Invalid set");
  111. ESP_RETURN_ON_FALSE(items_count!=NULL,NULL,TAG,"Invalid items count");
  112. ESP_LOGD(TAG,"Looking for command %s in control from a list of %d commands",sys_dac_control_type_name(cmd_type),set->commands_count);
  113. *items_count = 0;
  114. for (int i = 0; i < set->commands_count; i++) {
  115. if (set->commands[i].type == cmd_type) {
  116. *items_count = set->commands[i].items_count;
  117. return set->commands[i].items;
  118. }
  119. }
  120. ESP_LOGD(TAG,"No control set found for command %s",sys_dac_control_type_name(cmd_type));
  121. return NULL;
  122. }
  123. /****************************************************************************************
  124. *
  125. */
  126. bool i2c_execute_cmd(sys_dac_control_type cmd_type) {
  127. pb_size_t items_count;
  128. const sys_dac_control_itm* cmd = NULL;
  129. esp_err_t err = ESP_OK;
  130. ESP_LOGD(TAG, "Getting control set for command %s", sys_dac_control_type_name(cmd_type));
  131. if(i2c_controlset){
  132. cmd = i2c_get_controlset_cmd(i2c_controlset, cmd_type, &items_count);
  133. }
  134. if (!cmd || items_count == 0) {
  135. ESP_LOGD(TAG, "Not found. Checking if defaults are known %s", sys_dac_control_type_name(cmd_type));
  136. cmd = i2c_get_controlset_cmd(i2c_default_controlset, cmd_type, &items_count);
  137. }
  138. if (!cmd || items_count == 0) {
  139. ESP_LOGD(TAG, "No commands for %s", sys_dac_control_type_name(cmd_type));
  140. return true;
  141. }
  142. for (pb_size_t i = 0; i < items_count && err==ESP_OK; i++) {
  143. switch (cmd->which_item_type) {
  144. case sys_dac_control_itm_reg_action_tag:
  145. ESP_LOGD(TAG, "Setting reg %d mode %s value %d ", cmd->item_type.reg_action.reg,
  146. sys_dac_control_mode_name(cmd->item_type.reg_action.mode), cmd->item_type.reg_action.val);
  147. if (cmd->item_type.reg_action.mode == sys_dac_control_mode_NOTHING) {
  148. err = adac_write_byte(i2c_addr, cmd->item_type.reg_action.reg, cmd->item_type.reg_action.val);
  149. } else if (cmd->item_type.reg_action.mode == sys_dac_control_mode_OR) {
  150. uint8_t data = adac_read_byte(i2c_addr, cmd->item_type.reg_action.reg);
  151. data |= (uint8_t)cmd->item_type.reg_action.val;
  152. err = adac_write_byte(i2c_addr, cmd->item_type.reg_action.reg, data);
  153. } else if (cmd->item_type.reg_action.mode == sys_dac_control_mode_AND) {
  154. uint8_t data = adac_read_byte(i2c_addr, cmd->item_type.reg_action.reg);
  155. data &= (uint8_t)cmd->item_type.reg_action.val;
  156. err = adac_write_byte(i2c_addr, cmd->item_type.reg_action.reg, data);
  157. }
  158. break;
  159. case sys_dac_control_itm_regs_action_tag:
  160. ESP_LOGD(TAG, "Setting reg %d with %d byte(s)", cmd->item_type.regs_action.reg, cmd->item_type.regs_action.vals_count);
  161. err = adac_write(i2c_addr, cmd->item_type.regs_action.reg, cmd->item_type.regs_action.vals, cmd->item_type.regs_action.vals_count);
  162. break;
  163. case sys_dac_control_itm_gpio_action_tag:
  164. ESP_LOGI(TAG, "set GPIO %d at %s", cmd->item_type.gpio_action.gpio, sys_dac_control_lvl_name(cmd->item_type.gpio_action.level));
  165. err = gpio_set_direction_x(cmd->item_type.gpio_action.gpio, GPIO_MODE_OUTPUT);
  166. if(err == ESP_OK) err = gpio_set_level_x(cmd->item_type.gpio_action.gpio, cmd->item_type.gpio_action.level - sys_dac_control_lvl_LV_0);
  167. break;
  168. case sys_dac_control_itm_delay_action_tag:
  169. vTaskDelay(pdMS_TO_TICKS(cmd->item_type.delay_action.delay));
  170. ESP_LOGI(TAG, "DAC waiting %" PRIu64 " ms", cmd->item_type.delay_action.delay);
  171. break;
  172. default:
  173. break;
  174. }
  175. }
  176. return (err==ESP_OK);
  177. }