bsp_board.c 19 KB

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