bsp_board.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. // line 79 - change filter length to adjust echo cancellation
  2. #include "bsp_board.h"
  3. #include <stdbool.h>
  4. #include "esp_err.h"
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/task.h"
  7. #include "string.h"
  8. #include "driver/gpio.h"
  9. #include "esp_log.h"
  10. #include "driver/i2s_std.h"
  11. #include "driver/i2s_types.h"
  12. #include "esp_aec.h"
  13. #include "tca9555_driver.h"
  14. static const char *TAG = "board";
  15. static int s_play_sample_rate = 16000;
  16. static int s_play_channel_format = 1;
  17. static int s_bits_per_chan = 16;
  18. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  19. static i2s_chan_handle_t tx_handle = NULL; // I2S tx channel handler
  20. static i2s_chan_handle_t rx_handle = NULL; // I2S rx channel handler
  21. #endif
  22. static const audio_codec_data_if_t *record_data_if = NULL;
  23. static const audio_codec_ctrl_if_t *record_ctrl_if = NULL;
  24. static const audio_codec_if_t *record_codec_if = NULL;
  25. static esp_codec_dev_handle_t record_dev = NULL;
  26. static const audio_codec_data_if_t *play_data_if = NULL;
  27. static const audio_codec_ctrl_if_t *play_ctrl_if = NULL;
  28. static const audio_codec_gpio_if_t *play_gpio_if = NULL;
  29. static const audio_codec_if_t *play_codec_if = NULL;
  30. static esp_codec_dev_handle_t play_dev = NULL;
  31. static i2c_master_bus_handle_t i2c_bus_handle = NULL;
  32. static bool s_volume_buttons_task_started = false;
  33. static aec_handle_t *aec = NULL;
  34. static int frame_size = 0;
  35. static int16_t *mic = NULL;
  36. static int16_t *ref = NULL;
  37. static int16_t *out = NULL;
  38. static esp_err_t bsp_aec_init(void)
  39. {
  40. /* aec_create_from_config() is used instead of aec_create() so the NLP
  41. * (residual echo suppression) level can be pushed to its strongest
  42. * setting; a longer filter_length also helps cancel longer echo tails. */
  43. aec_config_t aec_cfg = {
  44. .mic_num = 1,
  45. .ref_num = 1,
  46. .out_num = 1,
  47. .filter_length = 8,
  48. .sample_rate = 16000,
  49. .caps = MALLOC_CAP_8BIT,
  50. .mode = AEC_MODE_VOIP_HIGH_PERF,
  51. .nlp_level = AEC_NLP_LEVEL_AGGR,
  52. };
  53. aec = aec_create_from_config(&aec_cfg);
  54. if (aec == NULL) {
  55. ESP_LOGE(TAG, "AEC create failed");
  56. return ESP_ERR_NO_MEM;
  57. }
  58. frame_size = aec_get_chunksize(aec);
  59. mic = heap_caps_aligned_alloc(16, frame_size * sizeof(int16_t), MALLOC_CAP_8BIT);
  60. ref = heap_caps_aligned_alloc(16, frame_size * sizeof(int16_t), MALLOC_CAP_8BIT);
  61. out = heap_caps_aligned_alloc(16, frame_size * sizeof(int16_t), MALLOC_CAP_8BIT);
  62. if (mic == NULL || ref == NULL || out == NULL) {
  63. ESP_LOGE(TAG, "AEC buffer allocation failed for %d samples", frame_size);
  64. free(mic);
  65. free(ref);
  66. free(out);
  67. mic = NULL;
  68. ref = NULL;
  69. out = NULL;
  70. aec_destroy(aec);
  71. aec = NULL;
  72. frame_size = 0;
  73. return ESP_ERR_NO_MEM;
  74. }
  75. ESP_LOGI(TAG, "AEC ready: %d samples/frame at 16 kHz", frame_size);
  76. return ESP_OK;
  77. }
  78. static esp_err_t bsp_codec_deinit(void);
  79. static void bsp_aec_deinit(void)
  80. {
  81. if (aec != NULL) {
  82. aec_destroy(aec);
  83. aec = NULL;
  84. }
  85. free(mic);
  86. free(ref);
  87. free(out);
  88. mic = NULL;
  89. ref = NULL;
  90. out = NULL;
  91. frame_size = 0;
  92. }
  93. esp_err_t bsp_aec_reset(void)
  94. {
  95. bsp_aec_deinit();
  96. return bsp_aec_init();
  97. }
  98. int bsp_aec_get_frame_size(void)
  99. {
  100. return frame_size;
  101. }
  102. esp_err_t bsp_aec_process_frame(const int16_t *near_end,
  103. const int16_t *far_end,
  104. int16_t *processed)
  105. {
  106. if (aec == NULL || mic == NULL || ref == NULL || out == NULL ||
  107. near_end == NULL || far_end == NULL || processed == NULL) {
  108. return ESP_ERR_INVALID_STATE;
  109. }
  110. memcpy(mic, near_end, frame_size * sizeof(int16_t));
  111. memcpy(ref, far_end, frame_size * sizeof(int16_t));
  112. aec_process(aec, mic, ref, out);
  113. memcpy(processed, out, frame_size * sizeof(int16_t));
  114. return ESP_OK;
  115. }
  116. esp_codec_dev_handle_t esp_ret_play_dev(void)
  117. {
  118. return play_dev;
  119. }
  120. i2c_master_bus_handle_t esp_ret_i2c_handle(void)
  121. {
  122. return i2c_bus_handle;
  123. }
  124. static esp_err_t i2c_master_init(void)
  125. {
  126. const i2c_master_bus_config_t bus_config = {
  127. .i2c_port = I2C_NUM,
  128. .sda_io_num = GPIO_I2C_SDA,
  129. .scl_io_num = GPIO_I2C_SCL,
  130. .clk_source = I2C_CLK_SRC_DEFAULT,
  131. };
  132. esp_err_t ret = i2c_new_master_bus(&bus_config, &i2c_bus_handle);
  133. if (ret != ESP_OK) {
  134. ESP_LOGE(TAG, "Failed to initialize I2C bus: %s", esp_err_to_name(ret));
  135. return ret;
  136. }
  137. ESP_LOGI(TAG, "I2C bus initialized successfully");
  138. return ESP_OK;
  139. }
  140. esp_err_t bsp_codec_adc_init(int sample_rate)
  141. { ESP_LOGI(TAG,"ADC INIT START");
  142. esp_err_t ret_val = ESP_OK;
  143. audio_codec_i2s_cfg_t i2s_cfg = {
  144. .port = I2S_NUM_1,
  145. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  146. .rx_handle = rx_handle,
  147. .tx_handle = NULL,
  148. #endif
  149. };
  150. record_data_if = audio_codec_new_i2s_data(&i2s_cfg);
  151. if(record_data_if == NULL)
  152. {
  153. ESP_LOGE(TAG,"record_data_if creation failed");
  154. return ESP_FAIL;
  155. }
  156. audio_codec_i2c_cfg_t i2c_cfg = {
  157. .addr = ES7210_CODEC_DEFAULT_ADDR,
  158. .bus_handle = i2c_bus_handle
  159. };
  160. record_ctrl_if = audio_codec_new_i2c_ctrl(&i2c_cfg);
  161. es7210_codec_cfg_t es7210_cfg = {
  162. .ctrl_if = record_ctrl_if,
  163. .mic_selected =
  164. ES7210_SEL_MIC1 |
  165. ES7210_SEL_MIC2 |
  166. ES7210_SEL_MIC3 |
  167. ES7210_SEL_MIC4,
  168. };
  169. record_codec_if = es7210_codec_new(&es7210_cfg);
  170. ESP_LOGI(TAG,"ES7210 IF=%p", record_codec_if);
  171. esp_codec_dev_cfg_t dev_cfg = {
  172. .codec_if = record_codec_if,
  173. .data_if = record_data_if,
  174. .dev_type = ESP_CODEC_DEV_TYPE_IN,
  175. };
  176. record_dev = esp_codec_dev_new(&dev_cfg);
  177. if(record_dev == NULL)
  178. {
  179. ESP_LOGE(TAG,"Failed creating ADC codec");
  180. return ESP_FAIL;
  181. }
  182. esp_codec_dev_sample_info_t fs = {
  183. .sample_rate = sample_rate,
  184. .channel = 2,
  185. .bits_per_sample = 16,
  186. };
  187. ret_val = esp_codec_dev_open(record_dev, &fs);
  188. if(ret_val != ESP_OK)
  189. {
  190. ESP_LOGE(TAG,
  191. "Codec open failed %s",
  192. esp_err_to_name(ret_val));
  193. return ret_val;
  194. }
  195. esp_codec_dev_set_in_channel_gain(
  196. record_dev,
  197. ESP_CODEC_DEV_MAKE_CHANNEL_MASK(0),
  198. RECORD_VOLUME);
  199. esp_codec_dev_set_in_channel_gain(
  200. record_dev,
  201. ESP_CODEC_DEV_MAKE_CHANNEL_MASK(1),
  202. RECORD_VOLUME);
  203. return ret_val;
  204. }
  205. esp_err_t bsp_codec_dac_init(int sample_rate, int channel_format, int bits_per_chan)
  206. {
  207. esp_err_t ret_val = ESP_OK;
  208. audio_codec_i2s_cfg_t i2s_cfg = {
  209. .port = I2S_NUM_1,
  210. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  211. .rx_handle = NULL,
  212. .tx_handle = tx_handle,
  213. #endif
  214. };
  215. play_data_if = audio_codec_new_i2s_data(&i2s_cfg);
  216. audio_codec_i2c_cfg_t i2c_cfg = {.addr = ES8311_CODEC_DEFAULT_ADDR,.bus_handle = i2c_bus_handle};
  217. play_ctrl_if = audio_codec_new_i2c_ctrl(&i2c_cfg);
  218. play_gpio_if = audio_codec_new_gpio();
  219. // New output codec interface
  220. es8311_codec_cfg_t es8311_cfg = {
  221. .codec_mode = ESP_CODEC_DEV_WORK_MODE_DAC,
  222. .ctrl_if = play_ctrl_if,
  223. .gpio_if = play_gpio_if,
  224. .pa_pin = GPIO_PWR_CTRL,
  225. .use_mclk = false,
  226. };
  227. play_codec_if = es8311_codec_new(&es8311_cfg);
  228. // New output codec device
  229. esp_codec_dev_cfg_t dev_cfg = {
  230. .codec_if = play_codec_if,
  231. .data_if = play_data_if,
  232. .dev_type = ESP_CODEC_DEV_TYPE_OUT,
  233. };
  234. play_dev = esp_codec_dev_new(&dev_cfg);
  235. esp_codec_dev_sample_info_t fs = {
  236. .bits_per_sample = bits_per_chan,
  237. .sample_rate = sample_rate,
  238. .channel = channel_format,
  239. };
  240. esp_codec_dev_set_out_vol(play_dev, PLAYER_VOLUME);
  241. esp_codec_dev_open(play_dev, &fs);
  242. return ret_val;
  243. }
  244. static esp_err_t bsp_codec_adc_deinit()
  245. {
  246. esp_err_t ret_val = ESP_OK;
  247. if (record_dev) {
  248. esp_codec_dev_close(record_dev);
  249. esp_codec_dev_delete(record_dev);
  250. record_dev = NULL;
  251. }
  252. // Delete codec interface
  253. if (record_codec_if) {
  254. audio_codec_delete_codec_if(record_codec_if);
  255. record_codec_if = NULL;
  256. }
  257. // Delete codec control interface
  258. if (record_ctrl_if) {
  259. audio_codec_delete_ctrl_if(record_ctrl_if);
  260. record_ctrl_if = NULL;
  261. }
  262. // Delete codec data interface
  263. if (record_data_if) {
  264. audio_codec_delete_data_if(record_data_if);
  265. record_data_if = NULL;
  266. }
  267. return ret_val;
  268. }
  269. static esp_err_t bsp_codec_dac_deinit()
  270. {
  271. esp_err_t ret_val = ESP_OK;
  272. if (play_dev) {
  273. esp_codec_dev_close(play_dev);
  274. esp_codec_dev_delete(play_dev);
  275. play_dev = NULL;
  276. }
  277. // Delete codec interface
  278. if (play_codec_if) {
  279. audio_codec_delete_codec_if(play_codec_if);
  280. play_codec_if = NULL;
  281. }
  282. // Delete codec control interface
  283. if (play_ctrl_if) {
  284. audio_codec_delete_ctrl_if(play_ctrl_if);
  285. play_ctrl_if = NULL;
  286. }
  287. if (play_gpio_if) {
  288. audio_codec_delete_gpio_if(play_gpio_if);
  289. play_gpio_if = NULL;
  290. }
  291. // Delete codec data interface
  292. if (play_data_if) {
  293. audio_codec_delete_data_if(play_data_if);
  294. play_data_if = NULL;
  295. }
  296. return ret_val;
  297. }
  298. esp_err_t esp_audio_set_play_vol(int volume)
  299. {
  300. if (!play_dev) {
  301. ESP_LOGE(TAG, "DAC codec init fail");
  302. return ESP_FAIL;
  303. }
  304. esp_codec_dev_set_out_vol(play_dev, volume);
  305. return ESP_OK;
  306. }
  307. esp_err_t esp_audio_get_play_vol(int *volume)
  308. {
  309. if (!play_dev) {
  310. ESP_LOGE(TAG, "DAC codec init fail");
  311. return ESP_FAIL;
  312. }
  313. esp_codec_dev_get_out_vol(play_dev, volume);
  314. return ESP_OK;
  315. }
  316. static esp_err_t bsp_i2s_init(int i2s_num, uint32_t sample_rate, int channel_format, int bits_per_chan)
  317. {
  318. esp_err_t ret_val = ESP_OK;
  319. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  320. i2s_slot_mode_t channel_fmt;
  321. if (channel_format == 1) {
  322. channel_fmt = I2S_SLOT_MODE_MONO;
  323. } else if (channel_format == 2) {
  324. channel_fmt = I2S_SLOT_MODE_STEREO;
  325. } else {
  326. ESP_LOGE(TAG, "Unable to configure channel_format %d", channel_format);
  327. channel_format = 1;
  328. channel_fmt = I2S_SLOT_MODE_MONO;
  329. }
  330. if (bits_per_chan != 16 && bits_per_chan != 32) {
  331. ESP_LOGE(TAG, "Unable to configure bits_per_chan %d", bits_per_chan);
  332. bits_per_chan = 32;
  333. }
  334. i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(i2s_num, I2S_ROLE_MASTER);
  335. ret_val |= i2s_new_channel(&chan_cfg, &tx_handle, &rx_handle);
  336. i2s_std_config_t std_cfg = I2S_CONFIG_DEFAULT(sample_rate, channel_fmt, bits_per_chan);
  337. ret_val |= i2s_channel_init_std_mode(tx_handle, &std_cfg);
  338. ret_val |= i2s_channel_init_std_mode(rx_handle, &std_cfg);
  339. ret_val |= i2s_channel_enable(tx_handle);
  340. ret_val |= i2s_channel_enable(rx_handle);
  341. #else
  342. i2s_channel_fmt_t channel_fmt = I2S_CHANNEL_FMT_RIGHT_LEFT;
  343. if (channel_format == 1) {
  344. channel_fmt = I2S_CHANNEL_FMT_ONLY_LEFT;
  345. } else if (channel_format == 2) {
  346. channel_fmt = I2S_CHANNEL_FMT_RIGHT_LEFT;
  347. } else {
  348. ESP_LOGE(TAG, "Unable to configure channel_format %d", channel_format);
  349. channel_format = 1;
  350. channel_fmt = I2S_CHANNEL_FMT_ONLY_LEFT;
  351. }
  352. if (bits_per_chan != 16 && bits_per_chan != 32) {
  353. ESP_LOGE(TAG, "Unable to configure bits_per_chan %d", bits_per_chan);
  354. bits_per_chan = 16;
  355. }
  356. i2s_config_t i2s_config = I2S_CONFIG_DEFAULT(sample_rate, channel_fmt, bits_per_chan);
  357. i2s_pin_config_t pin_config = {
  358. .bck_io_num = GPIO_I2S_SCLK,
  359. .ws_io_num = GPIO_I2S_LRCK,
  360. .data_out_num = GPIO_I2S_DOUT,
  361. .data_in_num = GPIO_I2S_SDIN,
  362. .mck_io_num = GPIO_I2S_MCLK,
  363. };
  364. ret_val |= i2s_driver_install(i2s_num, &i2s_config, 0, NULL);
  365. ret_val |= i2s_set_pin(i2s_num, &pin_config);
  366. #endif
  367. return ret_val;
  368. }
  369. static esp_err_t bsp_codec_init(
  370. int adc_sample_rate,
  371. int dac_sample_rate,
  372. int dac_channel_format,
  373. int dac_bits_per_chan)
  374. {
  375. esp_err_t ret;
  376. ret = bsp_codec_adc_init(adc_sample_rate);
  377. if(ret != ESP_OK)
  378. {
  379. ESP_LOGE(TAG,"ADC init failed: %s",
  380. esp_err_to_name(ret));
  381. return ret;
  382. }
  383. ret = bsp_codec_dac_init(
  384. dac_sample_rate,
  385. dac_channel_format,
  386. dac_bits_per_chan);
  387. if(ret != ESP_OK)
  388. {
  389. ESP_LOGE(TAG,"DAC init failed: %s",
  390. esp_err_to_name(ret));
  391. }
  392. ret = bsp_aec_init();
  393. if (ret != ESP_OK) {
  394. bsp_codec_deinit();
  395. return ret;
  396. }
  397. return ret;
  398. }
  399. static esp_err_t bsp_codec_deinit()
  400. {
  401. esp_err_t ret_val = ESP_OK;
  402. ret_val |= bsp_codec_adc_deinit();
  403. ret_val |= bsp_codec_dac_deinit();
  404. bsp_aec_deinit();
  405. return ret_val;
  406. }
  407. esp_err_t esp_audio_play(const int16_t* data, int length, uint32_t ticks_to_wait)
  408. {
  409. esp_err_t ret = ESP_OK;
  410. if (!play_dev) {
  411. return ESP_FAIL;
  412. }
  413. int out_length= length;
  414. int audio_time = 1;
  415. audio_time *= (16000 / s_play_sample_rate);
  416. audio_time *= (2 / s_play_channel_format);
  417. int *data_out = NULL;
  418. if (s_bits_per_chan != 32) {
  419. out_length = length * 2;
  420. data_out = malloc(out_length);
  421. for (int i = 0; i < length / sizeof(int16_t); i++) {
  422. int ret = data[i];
  423. data_out[i] = ret << 16;
  424. }
  425. }
  426. int *data_out_1 = NULL;
  427. if (s_play_channel_format != 2 || s_play_sample_rate != 16000) {
  428. out_length *= audio_time;
  429. data_out_1 = malloc(out_length);
  430. int *tmp_data = NULL;
  431. if (data_out != NULL) {
  432. tmp_data = data_out;
  433. } else {
  434. tmp_data = (int *)data;
  435. }
  436. for (int i = 0; i < out_length / (audio_time * sizeof(int)); i++) {
  437. for (int j = 0; j < audio_time; j++) {
  438. data_out_1[audio_time * i + j] = tmp_data[i];
  439. }
  440. }
  441. if (data_out != NULL) {
  442. free(data_out);
  443. data_out = NULL;
  444. }
  445. }
  446. if (data_out != NULL) {
  447. ret = esp_codec_dev_write(play_dev, (void *)data_out, out_length);
  448. free(data_out);
  449. } else if (data_out_1 != NULL) {
  450. ret = esp_codec_dev_write(play_dev, (void *)data_out_1, out_length);
  451. free(data_out_1);
  452. } else {
  453. ret = esp_codec_dev_write(play_dev, (void *)data, length);
  454. }
  455. return ret;
  456. }
  457. esp_err_t esp_get_feed_data(
  458. bool is_get_raw_channel,
  459. int16_t *buffer,
  460. int buffer_len)
  461. {
  462. if(record_dev == NULL)
  463. {
  464. ESP_LOGE(TAG,"record_dev NULL");
  465. return ESP_ERR_INVALID_STATE;
  466. }
  467. esp_err_t ret;
  468. ret = esp_codec_dev_read(
  469. record_dev,
  470. buffer,
  471. buffer_len);
  472. if(ret != ESP_OK)
  473. {
  474. return ret;
  475. }
  476. // Check microphone data
  477. int64_t sum = 0;
  478. for(int i=0;i<buffer_len/sizeof(int16_t);i++)
  479. {
  480. sum += abs(buffer[i]);
  481. }
  482. ESP_LOGI(TAG,
  483. "MIC level=%lld",
  484. sum/(buffer_len/sizeof(int16_t)));
  485. return ESP_OK;
  486. }
  487. static void bsp_volume_buttons_task(void *arg)
  488. {
  489. int last_key1 = 1;
  490. int last_key3 = 1;
  491. while (io_expander == NULL) {
  492. vTaskDelay(pdMS_TO_TICKS(VOLUME_BUTTON_POLL_MS));
  493. }
  494. while (1) {
  495. uint32_t mask = 0;
  496. if (esp_io_expander_get_level(io_expander, KEY_VOLUME_UP_PIN | KEY_VOLUME_DOWN_PIN, &mask) != ESP_OK) {
  497. vTaskDelay(pdMS_TO_TICKS(VOLUME_BUTTON_POLL_MS));
  498. continue;
  499. }
  500. int key1 = (mask & KEY_VOLUME_UP_PIN) ? 1 : 0;
  501. int key3 = (mask & KEY_VOLUME_DOWN_PIN) ? 1 : 0;
  502. if (key1 == 0 && last_key1 != 0) {
  503. int current_vol = 0;
  504. if (esp_audio_get_play_vol(&current_vol) == ESP_OK) {
  505. current_vol += VOLUME_STEP;
  506. if (current_vol > VOLUME_MAX_LEVEL) {
  507. current_vol = VOLUME_MAX_LEVEL;
  508. }
  509. if (esp_audio_set_play_vol(current_vol) == ESP_OK) {
  510. ESP_LOGI(TAG, "Volume up -> %d", current_vol);
  511. }
  512. }
  513. }
  514. if (key3 == 0 && last_key3 != 0) {
  515. int current_vol = 0;
  516. if (esp_audio_get_play_vol(&current_vol) == ESP_OK) {
  517. current_vol -= VOLUME_STEP;
  518. if (current_vol < VOLUME_MIN_LEVEL) {
  519. current_vol = VOLUME_MIN_LEVEL;
  520. }
  521. if (esp_audio_set_play_vol(current_vol) == ESP_OK) {
  522. ESP_LOGI(TAG, "Volume down -> %d", current_vol);
  523. }
  524. }
  525. }
  526. last_key1 = key1;
  527. last_key3 = key3;
  528. vTaskDelay(pdMS_TO_TICKS(VOLUME_BUTTON_POLL_MS));
  529. }
  530. }
  531. static esp_err_t bsp_volume_buttons_init(void)
  532. {
  533. if (s_volume_buttons_task_started) {
  534. return ESP_OK;
  535. }
  536. BaseType_t task_created = xTaskCreate(bsp_volume_buttons_task, "vol_btns", 4096, NULL, 5, NULL);
  537. if (task_created != pdPASS) {
  538. return ESP_FAIL;
  539. }
  540. s_volume_buttons_task_started = true;
  541. return ESP_OK;
  542. }
  543. esp_err_t esp_board_init(uint32_t sample_rate, int channel_format, int bits_per_chan)
  544. {
  545. /*!< Initialize I2C bus, used for audio codec*/
  546. i2c_master_init();
  547. s_play_sample_rate = sample_rate;
  548. if (channel_format != 2 && channel_format != 1) {
  549. ESP_LOGE(TAG, "Unable to configure channel_format");
  550. channel_format = 2;
  551. }
  552. s_play_channel_format = channel_format;
  553. if (bits_per_chan != 32 && bits_per_chan != 16) {
  554. ESP_LOGE(TAG, "Unable to configure bits_per_chan");
  555. bits_per_chan = 32;
  556. }
  557. s_bits_per_chan = bits_per_chan;
  558. bsp_i2s_init(I2S_NUM_1, 16000, 2, 16);
  559. // Because record and play use the same i2s.
  560. esp_err_t codec_ret;
  561. codec_ret = bsp_codec_init(16000,16000,2,16);
  562. if(codec_ret != ESP_OK)
  563. {
  564. ESP_LOGE(TAG,
  565. "bsp_codec_init failed: %s",
  566. esp_err_to_name(codec_ret));
  567. }
  568. else
  569. {
  570. ESP_LOGI(TAG,"bsp_codec_init OK");
  571. }
  572. gpio_set_level(GPIO_PWR_CTRL, 1);
  573. esp_err_t button_ret = bsp_volume_buttons_init();
  574. if (button_ret != ESP_OK) {
  575. ESP_LOGW(TAG, "Failed to initialize volume buttons: %s", esp_err_to_name(button_ret));
  576. }
  577. return ESP_OK;
  578. }