bsp_board.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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 "esp_vfs_fat.h"
  15. #include "sdmmc_cmd.h"
  16. #include "driver/i2s_std.h"
  17. #include "driver/i2s_types.h"
  18. #include "driver/i2s_pdm.h"
  19. #include "driver/i2s_tdm.h"
  20. #include <sys/unistd.h>
  21. #include <sys/stat.h>
  22. #include "dirent.h"
  23. #include "driver/sdmmc_host.h"
  24. #include <errno.h>
  25. #include "esp_aec.h"
  26. #include "tca9555_driver.h"
  27. #if ((SOC_SDMMC_HOST_SUPPORTED) && (FUNC_SDMMC_EN))
  28. #include "driver/sdmmc_host.h"
  29. #endif /* ((SOC_SDMMC_HOST_SUPPORTED) && (FUNC_SDMMC_EN)) */
  30. #define ADC_I2S_CHANNEL 2
  31. #define ADC_BITS_PER_SAMPLE 32
  32. static sdmmc_card_t *card;
  33. static const char *TAG = "board";
  34. static int s_play_sample_rate = 16000;
  35. static int s_play_channel_format = 1;
  36. static int s_bits_per_chan = 16;
  37. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  38. static i2s_chan_handle_t tx_handle = NULL; // I2S tx channel handler
  39. static i2s_chan_handle_t rx_handle = NULL; // I2S rx channel handler
  40. #endif
  41. static audio_codec_data_if_t *record_data_if = NULL;
  42. static audio_codec_ctrl_if_t *record_ctrl_if = NULL;
  43. static audio_codec_if_t *record_codec_if = NULL;
  44. static esp_codec_dev_handle_t record_dev = NULL;
  45. static audio_codec_data_if_t *play_data_if = NULL;
  46. static audio_codec_ctrl_if_t *play_ctrl_if = NULL;
  47. static audio_codec_gpio_if_t *play_gpio_if = NULL;
  48. static audio_codec_if_t *play_codec_if = NULL;
  49. static esp_codec_dev_handle_t play_dev = NULL;
  50. static i2c_master_bus_handle_t i2c_bus_handle = NULL;
  51. static uint32_t SDCard_Size = 0;
  52. static bool s_volume_buttons_task_started = false;
  53. static aec_handle_t *aec = NULL;
  54. static int frame_size = 0;
  55. static int16_t *mic = NULL;
  56. static int16_t *ref = NULL;
  57. static int16_t *out = NULL;
  58. static esp_err_t bsp_aec_init(void)
  59. {
  60. /* Espressif's direct AEC flow: create, query the algorithm frame size,
  61. * then allocate aligned signed-16-bit audio buffers for each frame.
  62. * aec_create_from_config() is used instead of aec_create() so the NLP
  63. * (residual echo suppression) level can be pushed to its strongest
  64. * setting; a longer filter_length also helps cancel longer echo tails. */
  65. aec_config_t aec_cfg = {
  66. .mic_num = 1,
  67. .ref_num = 1,
  68. .out_num = 1,
  69. .filter_length = 8,
  70. .sample_rate = 16000,
  71. .caps = MALLOC_CAP_8BIT,
  72. .mode = AEC_MODE_VOIP_HIGH_PERF,
  73. .nlp_level = AEC_NLP_LEVEL_AGGR,
  74. };
  75. aec = aec_create_from_config(&aec_cfg);
  76. if (aec == NULL) {
  77. ESP_LOGE(TAG, "AEC create failed");
  78. return ESP_ERR_NO_MEM;
  79. }
  80. frame_size = aec_get_chunksize(aec);
  81. mic = heap_caps_aligned_alloc(16, frame_size * sizeof(int16_t), MALLOC_CAP_8BIT);
  82. ref = heap_caps_aligned_alloc(16, frame_size * sizeof(int16_t), MALLOC_CAP_8BIT);
  83. out = heap_caps_aligned_alloc(16, frame_size * sizeof(int16_t), MALLOC_CAP_8BIT);
  84. if (mic == NULL || ref == NULL || out == NULL) {
  85. ESP_LOGE(TAG, "AEC buffer allocation failed for %d samples", frame_size);
  86. free(mic);
  87. free(ref);
  88. free(out);
  89. mic = NULL;
  90. ref = NULL;
  91. out = NULL;
  92. aec_destroy(aec);
  93. aec = NULL;
  94. frame_size = 0;
  95. return ESP_ERR_NO_MEM;
  96. }
  97. ESP_LOGI(TAG, "AEC ready: %d samples/frame at 16 kHz", frame_size);
  98. return ESP_OK;
  99. }
  100. static esp_err_t bsp_codec_deinit(void);
  101. static void bsp_aec_deinit(void)
  102. {
  103. if (aec != NULL) {
  104. aec_destroy(aec);
  105. aec = NULL;
  106. }
  107. free(mic);
  108. free(ref);
  109. free(out);
  110. mic = NULL;
  111. ref = NULL;
  112. out = NULL;
  113. frame_size = 0;
  114. }
  115. int bsp_aec_get_frame_size(void)
  116. {
  117. return frame_size;
  118. }
  119. esp_err_t bsp_aec_process_frame(const int16_t *near_end,
  120. const int16_t *far_end,
  121. int16_t *processed)
  122. {
  123. if (aec == NULL || mic == NULL || ref == NULL || out == NULL ||
  124. near_end == NULL || far_end == NULL || processed == NULL) {
  125. return ESP_ERR_INVALID_STATE;
  126. }
  127. memcpy(mic, near_end, frame_size * sizeof(int16_t));
  128. memcpy(ref, far_end, frame_size * sizeof(int16_t));
  129. aec_process(aec, mic, ref, out);
  130. memcpy(processed, out, frame_size * sizeof(int16_t));
  131. return ESP_OK;
  132. }
  133. int mic_level = 0;
  134. esp_codec_dev_handle_t esp_ret_play_dev(void)
  135. {
  136. return play_dev;
  137. }
  138. i2c_master_bus_handle_t esp_ret_i2c_handle(void)
  139. {
  140. return i2c_bus_handle;
  141. }
  142. static esp_err_t i2c_master_init(void)
  143. {
  144. const i2c_master_bus_config_t bus_config = {
  145. .i2c_port = I2C_NUM,
  146. .sda_io_num = GPIO_I2C_SDA,
  147. .scl_io_num = GPIO_I2C_SCL,
  148. .clk_source = I2C_CLK_SRC_DEFAULT,
  149. };
  150. esp_err_t ret = i2c_new_master_bus(&bus_config, &i2c_bus_handle);
  151. if (ret != ESP_OK) {
  152. ESP_LOGE(TAG, "Failed to initialize I2C bus: %s", esp_err_to_name(ret));
  153. return ret;
  154. }
  155. ESP_LOGI(TAG, "I2C bus initialized successfully");
  156. return ESP_OK; // 返回 ESP_OK 表示成功
  157. }
  158. esp_err_t bsp_codec_adc_init(int sample_rate)
  159. { ESP_LOGI(TAG,"ADC INIT START");
  160. esp_err_t ret_val = ESP_OK;
  161. audio_codec_i2s_cfg_t i2s_cfg = {
  162. .port = I2S_NUM_1,
  163. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  164. .rx_handle = rx_handle,
  165. .tx_handle = NULL,
  166. #endif
  167. };
  168. record_data_if = audio_codec_new_i2s_data(&i2s_cfg);
  169. if(record_data_if == NULL)
  170. {
  171. ESP_LOGE(TAG,"record_data_if creation failed");
  172. return ESP_FAIL;
  173. }
  174. audio_codec_i2c_cfg_t i2c_cfg = {
  175. .addr = ES7210_CODEC_DEFAULT_ADDR,
  176. .bus_handle = i2c_bus_handle
  177. };
  178. record_ctrl_if = audio_codec_new_i2c_ctrl(&i2c_cfg);
  179. es7210_codec_cfg_t es7210_cfg = {
  180. .ctrl_if = record_ctrl_if,
  181. /*
  182. * Microphone initialization for the ES7210 ADC codec.
  183. * This board supports multiple mic inputs, and the current code enables all
  184. * 4 available mic channels:
  185. * MIC1 + MIC2 + MIC3 + MIC4
  186. *
  187. * For a 2-mic board, the intended setup is typically to enable only the two
  188. * physical microphones that are wired to the board, for example:
  189. * .mic_selected = ES7210_SEL_MIC1 | ES7210_SEL_MIC2;
  190. *
  191. * In other words, the current configuration is not "one mic only"; it is
  192. * configured to allow all entries in the codec, even though the app logic only
  193. * reads one channel from the resulting stream.
  194. */
  195. .mic_selected =
  196. ES7210_SEL_MIC1 |
  197. ES7210_SEL_MIC2 |
  198. ES7210_SEL_MIC3 |
  199. ES7210_SEL_MIC4,
  200. };
  201. record_codec_if = es7210_codec_new(&es7210_cfg);
  202. ESP_LOGI(TAG,"ES7210 IF=%p", record_codec_if);
  203. esp_codec_dev_cfg_t dev_cfg = {
  204. .codec_if = record_codec_if,
  205. .data_if = record_data_if,
  206. .dev_type = ESP_CODEC_DEV_TYPE_IN,
  207. };
  208. record_dev = esp_codec_dev_new(&dev_cfg);
  209. if(record_dev == NULL)
  210. {
  211. ESP_LOGE(TAG,"Failed creating ADC codec");
  212. return ESP_FAIL;
  213. }
  214. esp_codec_dev_sample_info_t fs = {
  215. .sample_rate = sample_rate,
  216. /*
  217. * Stereo is opened here, which matches a 2-channel mic stream.
  218. * If the board is wired as a 2-mic stereo pair, the code can use both
  219. * channels by reading left and right samples from the interleaved buffer.
  220. * Example:
  221. * // .mic_selected = ES7210_SEL_MIC1 | ES7210_SEL_MIC2;
  222. * // left = mic_buffer[i * 2 + 0];
  223. * // right = mic_buffer[i * 2 + 1];
  224. */
  225. .channel = 2,
  226. .bits_per_sample = 16,
  227. };
  228. ret_val = esp_codec_dev_open(record_dev, &fs);
  229. if(ret_val != ESP_OK)
  230. {
  231. ESP_LOGE(TAG,
  232. "Codec open failed %s",
  233. esp_err_to_name(ret_val));
  234. return ret_val;
  235. }
  236. esp_codec_dev_set_in_channel_gain(
  237. record_dev,
  238. ESP_CODEC_DEV_MAKE_CHANNEL_MASK(0),
  239. RECORD_VOLUME);
  240. esp_codec_dev_set_in_channel_gain(
  241. record_dev,
  242. ESP_CODEC_DEV_MAKE_CHANNEL_MASK(1),
  243. RECORD_VOLUME);
  244. return ret_val;
  245. }
  246. esp_err_t bsp_codec_dac_init(int sample_rate, int channel_format, int bits_per_chan)
  247. {
  248. esp_err_t ret_val = ESP_OK;
  249. // Do initialize of related interface: data_if, ctrl_if and gpio_if
  250. audio_codec_i2s_cfg_t i2s_cfg = {
  251. .port = I2S_NUM_1,
  252. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  253. .rx_handle = NULL,
  254. .tx_handle = tx_handle,
  255. #endif
  256. };
  257. play_data_if = audio_codec_new_i2s_data(&i2s_cfg);
  258. audio_codec_i2c_cfg_t i2c_cfg = {.addr = ES8311_CODEC_DEFAULT_ADDR,.bus_handle = i2c_bus_handle};
  259. play_ctrl_if = audio_codec_new_i2c_ctrl(&i2c_cfg);
  260. play_gpio_if = audio_codec_new_gpio();
  261. // New output codec interface
  262. es8311_codec_cfg_t es8311_cfg = {
  263. .codec_mode = ESP_CODEC_DEV_WORK_MODE_DAC,
  264. .ctrl_if = play_ctrl_if,
  265. .gpio_if = play_gpio_if,
  266. .pa_pin = GPIO_PWR_CTRL,
  267. .use_mclk = false,
  268. };
  269. play_codec_if = es8311_codec_new(&es8311_cfg);
  270. // New output codec device
  271. esp_codec_dev_cfg_t dev_cfg = {
  272. .codec_if = play_codec_if,
  273. .data_if = play_data_if,
  274. .dev_type = ESP_CODEC_DEV_TYPE_OUT,
  275. };
  276. play_dev = esp_codec_dev_new(&dev_cfg);
  277. esp_codec_dev_sample_info_t fs = {
  278. .bits_per_sample = bits_per_chan,
  279. .sample_rate = sample_rate,
  280. .channel = channel_format,
  281. };
  282. esp_codec_dev_set_out_vol(play_dev, PLAYER_VOLUME);
  283. esp_codec_dev_open(play_dev, &fs);
  284. return ret_val;
  285. }
  286. static esp_err_t bsp_codec_adc_deinit()
  287. {
  288. esp_err_t ret_val = ESP_OK;
  289. if (record_dev) {
  290. esp_codec_dev_close(record_dev);
  291. esp_codec_dev_delete(record_dev);
  292. record_dev = NULL;
  293. }
  294. // Delete codec interface
  295. if (record_codec_if) {
  296. audio_codec_delete_codec_if(record_codec_if);
  297. record_codec_if = NULL;
  298. }
  299. // Delete codec control interface
  300. if (record_ctrl_if) {
  301. audio_codec_delete_ctrl_if(record_ctrl_if);
  302. record_ctrl_if = NULL;
  303. }
  304. // Delete codec data interface
  305. if (record_data_if) {
  306. audio_codec_delete_data_if(record_data_if);
  307. record_data_if = NULL;
  308. }
  309. return ret_val;
  310. }
  311. static esp_err_t bsp_codec_dac_deinit()
  312. {
  313. esp_err_t ret_val = ESP_OK;
  314. if (play_dev) {
  315. esp_codec_dev_close(play_dev);
  316. esp_codec_dev_delete(play_dev);
  317. play_dev = NULL;
  318. }
  319. // Delete codec interface
  320. if (play_codec_if) {
  321. audio_codec_delete_codec_if(play_codec_if);
  322. play_codec_if = NULL;
  323. }
  324. // Delete codec control interface
  325. if (play_ctrl_if) {
  326. audio_codec_delete_ctrl_if(play_ctrl_if);
  327. play_ctrl_if = NULL;
  328. }
  329. if (play_gpio_if) {
  330. audio_codec_delete_gpio_if(play_gpio_if);
  331. play_gpio_if = NULL;
  332. }
  333. // Delete codec data interface
  334. if (play_data_if) {
  335. audio_codec_delete_data_if(play_data_if);
  336. play_data_if = NULL;
  337. }
  338. return ret_val;
  339. }
  340. esp_err_t esp_audio_set_play_vol(int volume)
  341. {
  342. if (!play_dev) {
  343. ESP_LOGE(TAG, "DAC codec init fail");
  344. return ESP_FAIL;
  345. }
  346. esp_codec_dev_set_out_vol(play_dev, volume);
  347. return ESP_OK;
  348. }
  349. esp_err_t esp_audio_get_play_vol(int *volume)
  350. {
  351. if (!play_dev) {
  352. ESP_LOGE(TAG, "DAC codec init fail");
  353. return ESP_FAIL;
  354. }
  355. esp_codec_dev_get_out_vol(play_dev, volume);
  356. return ESP_OK;
  357. }
  358. // static esp_err_t bsp_i2s_init(i2s_port_t i2s_num, uint32_t sample_rate, i2s_channel_fmt_t channel_format, i2s_bits_per_chan_t bits_per_chan)
  359. static esp_err_t bsp_i2s_init(int i2s_num, uint32_t sample_rate, int channel_format, int bits_per_chan)
  360. {
  361. esp_err_t ret_val = ESP_OK;
  362. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  363. i2s_slot_mode_t channel_fmt = I2S_SLOT_MODE_STEREO;
  364. if (channel_format == 1) {
  365. channel_fmt = I2S_SLOT_MODE_MONO;
  366. } else if (channel_format == 2) {
  367. channel_fmt = I2S_SLOT_MODE_STEREO;
  368. } else {
  369. ESP_LOGE(TAG, "Unable to configure channel_format %d", channel_format);
  370. channel_format = 1;
  371. channel_fmt = I2S_SLOT_MODE_MONO;
  372. }
  373. if (bits_per_chan != 16 && bits_per_chan != 32) {
  374. ESP_LOGE(TAG, "Unable to configure bits_per_chan %d", bits_per_chan);
  375. bits_per_chan = 32;
  376. }
  377. i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(i2s_num, I2S_ROLE_MASTER);
  378. ret_val |= i2s_new_channel(&chan_cfg, &tx_handle, &rx_handle);
  379. i2s_std_config_t std_cfg = I2S_CONFIG_DEFAULT(sample_rate, channel_fmt, bits_per_chan);
  380. ret_val |= i2s_channel_init_std_mode(tx_handle, &std_cfg);
  381. ret_val |= i2s_channel_init_std_mode(rx_handle, &std_cfg);
  382. ret_val |= i2s_channel_enable(tx_handle);
  383. ret_val |= i2s_channel_enable(rx_handle);
  384. #else
  385. i2s_channel_fmt_t channel_fmt = I2S_CHANNEL_FMT_RIGHT_LEFT;
  386. if (channel_format == 1) {
  387. channel_fmt = I2S_CHANNEL_FMT_ONLY_LEFT;
  388. } else if (channel_format == 2) {
  389. channel_fmt = I2S_CHANNEL_FMT_RIGHT_LEFT;
  390. } else {
  391. ESP_LOGE(TAG, "Unable to configure channel_format %d", channel_format);
  392. channel_format = 1;
  393. channel_fmt = I2S_CHANNEL_FMT_ONLY_LEFT;
  394. }
  395. if (bits_per_chan != 16 && bits_per_chan != 32) {
  396. ESP_LOGE(TAG, "Unable to configure bits_per_chan %d", bits_per_chan);
  397. bits_per_chan = 16;
  398. }
  399. i2s_config_t i2s_config = I2S_CONFIG_DEFAULT(sample_rate, channel_fmt, bits_per_chan);
  400. i2s_pin_config_t pin_config = {
  401. .bck_io_num = GPIO_I2S_SCLK,
  402. .ws_io_num = GPIO_I2S_LRCK,
  403. .data_out_num = GPIO_I2S_DOUT,
  404. .data_in_num = GPIO_I2S_SDIN,
  405. .mck_io_num = GPIO_I2S_MCLK,
  406. };
  407. ret_val |= i2s_driver_install(i2s_num, &i2s_config, 0, NULL);
  408. ret_val |= i2s_set_pin(i2s_num, &pin_config);
  409. #endif
  410. return ret_val;
  411. }
  412. static esp_err_t bsp_i2s_deinit(int i2s_num)
  413. {
  414. esp_err_t ret_val = ESP_OK;
  415. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  416. if (i2s_num == I2S_NUM_1 && rx_handle) {
  417. ret_val |= i2s_channel_disable(rx_handle);
  418. ret_val |= i2s_del_channel(rx_handle);
  419. rx_handle = NULL;
  420. } else if (i2s_num == I2S_NUM_0 && tx_handle) {
  421. ret_val |= i2s_channel_disable(tx_handle);
  422. ret_val |= i2s_del_channel(tx_handle);
  423. tx_handle = NULL;
  424. }
  425. #else
  426. ret_val |= i2s_stop(i2s_num);
  427. ret_val |= i2s_driver_uninstall(i2s_num);
  428. #endif
  429. return ret_val;
  430. }
  431. static esp_err_t bsp_codec_init(
  432. int adc_sample_rate,
  433. int dac_sample_rate,
  434. int dac_channel_format,
  435. int dac_bits_per_chan)
  436. {
  437. //ESP_LOGE(TAG, "bsp_codec_adc_init START");
  438. esp_err_t ret;
  439. ret = bsp_codec_adc_init(adc_sample_rate);
  440. if(ret != ESP_OK)
  441. {
  442. ESP_LOGE(TAG,"ADC init failed: %s",
  443. esp_err_to_name(ret));
  444. return ret;
  445. }
  446. ret = bsp_codec_dac_init(
  447. dac_sample_rate,
  448. dac_channel_format,
  449. dac_bits_per_chan);
  450. if(ret != ESP_OK)
  451. {
  452. ESP_LOGE(TAG,"DAC init failed: %s",
  453. esp_err_to_name(ret));
  454. }
  455. ret = bsp_aec_init();
  456. if (ret != ESP_OK) {
  457. bsp_codec_deinit();
  458. return ret;
  459. }
  460. return ret;
  461. }
  462. static esp_err_t bsp_codec_deinit()
  463. {
  464. esp_err_t ret_val = ESP_OK;
  465. ret_val |= bsp_codec_adc_deinit();
  466. ret_val |= bsp_codec_dac_deinit();
  467. bsp_aec_deinit();
  468. return ret_val;
  469. }
  470. esp_err_t esp_audio_play(const int16_t* data, int length, uint32_t ticks_to_wait)
  471. {
  472. size_t bytes_write = 0;
  473. esp_err_t ret = ESP_OK;
  474. if (!play_dev) {
  475. return ESP_FAIL;
  476. }
  477. int out_length= length;
  478. int audio_time = 1;
  479. audio_time *= (16000 / s_play_sample_rate);
  480. audio_time *= (2 / s_play_channel_format);
  481. int *data_out = NULL;
  482. if (s_bits_per_chan != 32) {
  483. out_length = length * 2;
  484. data_out = malloc(out_length);
  485. for (int i = 0; i < length / sizeof(int16_t); i++) {
  486. int ret = data[i];
  487. data_out[i] = ret << 16;
  488. }
  489. }
  490. int *data_out_1 = NULL;
  491. if (s_play_channel_format != 2 || s_play_sample_rate != 16000) {
  492. out_length *= audio_time;
  493. data_out_1 = malloc(out_length);
  494. int *tmp_data = NULL;
  495. if (data_out != NULL) {
  496. tmp_data = data_out;
  497. } else {
  498. tmp_data = (int *)data;
  499. }
  500. for (int i = 0; i < out_length / (audio_time * sizeof(int)); i++) {
  501. for (int j = 0; j < audio_time; j++) {
  502. data_out_1[audio_time * i + j] = tmp_data[i];
  503. }
  504. }
  505. if (data_out != NULL) {
  506. free(data_out);
  507. data_out = NULL;
  508. }
  509. }
  510. if (data_out != NULL) {
  511. ret = esp_codec_dev_write(play_dev, (void *)data_out, out_length);
  512. free(data_out);
  513. } else if (data_out_1 != NULL) {
  514. ret = esp_codec_dev_write(play_dev, (void *)data_out_1, out_length);
  515. free(data_out_1);
  516. } else {
  517. ret = esp_codec_dev_write(play_dev, (void *)data, length);
  518. }
  519. return ret;
  520. }
  521. int get_mic_level (void) {
  522. return mic_level;
  523. }
  524. esp_err_t esp_get_feed_data(
  525. bool is_get_raw_channel,
  526. int16_t *buffer,
  527. int buffer_len)
  528. {
  529. // ESP_LOGE(TAG, "NEW esp_get_feed_data CALLED");
  530. if(record_dev == NULL)
  531. {
  532. ESP_LOGE(TAG,"record_dev NULL");
  533. return ESP_ERR_INVALID_STATE;
  534. }
  535. // ESP_LOGI(TAG,
  536. // "read buffer=%p size=%d",
  537. // buffer,
  538. // buffer_len);
  539. esp_err_t ret;
  540. ret = esp_codec_dev_read(
  541. record_dev,
  542. buffer,
  543. buffer_len);
  544. // ESP_LOGI(TAG,
  545. // "codec read ret=%s",
  546. // esp_err_to_name(ret));
  547. if(ret != ESP_OK)
  548. {
  549. return ret;
  550. }
  551. // Check microphone data
  552. int64_t sum = 0;
  553. for(int i=0;i<buffer_len/sizeof(int16_t);i++)
  554. {
  555. sum += abs(buffer[i]);
  556. }
  557. ESP_LOGI(TAG,
  558. "MIC level=%lld",
  559. sum/(buffer_len/sizeof(int16_t)));
  560. return ESP_OK;
  561. }
  562. int esp_get_feed_channel(void)
  563. {
  564. return ADC_I2S_CHANNEL;
  565. }
  566. char* esp_get_input_format(void)
  567. {
  568. return "RM";
  569. }
  570. static void bsp_volume_buttons_task(void *arg)
  571. {
  572. /* K1/K3 are wired through the TCA9555 IO expander, not raw ESP32 GPIOs
  573. * (GPIO0 is the BOOT/RESET strapping pin, so it must not be used here). */
  574. int last_key1 = 1;
  575. int last_key3 = 1;
  576. while (io_expander == NULL) {
  577. vTaskDelay(pdMS_TO_TICKS(VOLUME_BUTTON_POLL_MS));
  578. }
  579. while (1) {
  580. uint32_t mask = 0;
  581. if (esp_io_expander_get_level(io_expander, KEY_VOLUME_UP_PIN | KEY_VOLUME_DOWN_PIN, &mask) != ESP_OK) {
  582. vTaskDelay(pdMS_TO_TICKS(VOLUME_BUTTON_POLL_MS));
  583. continue;
  584. }
  585. int key1 = (mask & KEY_VOLUME_UP_PIN) ? 1 : 0;
  586. int key3 = (mask & KEY_VOLUME_DOWN_PIN) ? 1 : 0;
  587. if (key1 == 0 && last_key1 != 0) {
  588. int current_vol = 0;
  589. if (esp_audio_get_play_vol(&current_vol) == ESP_OK) {
  590. current_vol += VOLUME_STEP;
  591. if (current_vol > VOLUME_MAX_LEVEL) {
  592. current_vol = VOLUME_MAX_LEVEL;
  593. }
  594. if (esp_audio_set_play_vol(current_vol) == ESP_OK) {
  595. ESP_LOGI(TAG, "Volume up -> %d", current_vol);
  596. }
  597. }
  598. }
  599. if (key3 == 0 && last_key3 != 0) {
  600. int current_vol = 0;
  601. if (esp_audio_get_play_vol(&current_vol) == ESP_OK) {
  602. current_vol -= VOLUME_STEP;
  603. if (current_vol < VOLUME_MIN_LEVEL) {
  604. current_vol = VOLUME_MIN_LEVEL;
  605. }
  606. if (esp_audio_set_play_vol(current_vol) == ESP_OK) {
  607. ESP_LOGI(TAG, "Volume down -> %d", current_vol);
  608. }
  609. }
  610. }
  611. last_key1 = key1;
  612. last_key3 = key3;
  613. vTaskDelay(pdMS_TO_TICKS(VOLUME_BUTTON_POLL_MS));
  614. }
  615. }
  616. static esp_err_t bsp_volume_buttons_init(void)
  617. {
  618. if (s_volume_buttons_task_started) {
  619. return ESP_OK;
  620. }
  621. /* K1/K3 input direction is already configured on the TCA9555 expander by
  622. * tca9555_driver_init(); no direct GPIO configuration is needed here. */
  623. BaseType_t task_created = xTaskCreate(bsp_volume_buttons_task, "vol_btns", 4096, NULL, 5, NULL);
  624. if (task_created != pdPASS) {
  625. return ESP_FAIL;
  626. }
  627. s_volume_buttons_task_started = true;
  628. return ESP_OK;
  629. }
  630. esp_err_t esp_board_init(uint32_t sample_rate, int channel_format, int bits_per_chan)
  631. {
  632. /*!< Initialize I2C bus, used for audio codec*/
  633. i2c_master_init();
  634. s_play_sample_rate = sample_rate;
  635. if (channel_format != 2 && channel_format != 1) {
  636. ESP_LOGE(TAG, "Unable to configure channel_format");
  637. channel_format = 2;
  638. }
  639. s_play_channel_format = channel_format;
  640. if (bits_per_chan != 32 && bits_per_chan != 16) {
  641. ESP_LOGE(TAG, "Unable to configure bits_per_chan");
  642. bits_per_chan = 32;
  643. }
  644. s_bits_per_chan = bits_per_chan;
  645. bsp_i2s_init(I2S_NUM_1, 16000, 2, 16);
  646. // Because record and play use the same i2s.
  647. esp_err_t codec_ret;
  648. codec_ret = bsp_codec_init(16000,16000,2,16);
  649. if(codec_ret != ESP_OK)
  650. {
  651. ESP_LOGE(TAG,
  652. "bsp_codec_init failed: %s",
  653. esp_err_to_name(codec_ret));
  654. }
  655. else
  656. {
  657. ESP_LOGI(TAG,"bsp_codec_init OK");
  658. }
  659. /* Initialize PA */
  660. /*gpio_config_t io_conf;
  661. memset(&io_conf, 0, sizeof(io_conf));
  662. io_conf.intr_type = GPIO_INTR_DISABLE;
  663. io_conf.mode = GPIO_MODE_OUTPUT;
  664. io_conf.pin_bit_mask = ((1ULL << GPIO_PWR_CTRL));
  665. io_conf.pull_down_en = 0;
  666. io_conf.pull_up_en = 0;
  667. gpio_config(&io_conf);
  668. */
  669. gpio_set_level(GPIO_PWR_CTRL, 1);
  670. esp_err_t button_ret = bsp_volume_buttons_init();
  671. if (button_ret != ESP_OK) {
  672. ESP_LOGW(TAG, "Failed to initialize volume buttons: %s", esp_err_to_name(button_ret));
  673. }
  674. return ESP_OK;
  675. }
  676. esp_err_t esp_sdcard_init(char *mount_point, size_t max_files)
  677. {
  678. if (NULL != card) {
  679. return ESP_ERR_INVALID_STATE;
  680. }
  681. /* Check if SD crad is supported */
  682. if (!FUNC_SDMMC_EN && !FUNC_SDSPI_EN) {
  683. ESP_LOGE(TAG, "SDMMC and SDSPI not supported on this board!");
  684. return ESP_ERR_NOT_SUPPORTED;
  685. }
  686. esp_err_t ret_val = ESP_OK;
  687. /**
  688. * @brief Options for mounting the filesystem.
  689. * If format_if_mount_failed is set to true, SD card will be partitioned and
  690. * formatted in case when mounting fails.
  691. *
  692. */
  693. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  694. .format_if_mount_failed = false,
  695. .max_files = max_files,
  696. .allocation_unit_size = 16 * 1024
  697. };
  698. /**
  699. * @brief Use settings defined above to initialize SD card and mount FAT filesystem.
  700. * Note: esp_vfs_fat_sdmmc/sdspi_mount is all-in-one convenience functions.
  701. * Please check its source code and implement error recovery when developing
  702. * production applications.
  703. *
  704. */
  705. sdmmc_host_t host =
  706. #if FUNC_SDMMC_EN
  707. SDMMC_HOST_DEFAULT();
  708. #else
  709. SDSPI_HOST_DEFAULT();
  710. spi_bus_config_t bus_cfg = {
  711. .mosi_io_num = GPIO_SDSPI_MOSI,
  712. .miso_io_num = GPIO_SDSPI_MISO,
  713. .sclk_io_num = GPIO_SDSPI_SCLK,
  714. .quadwp_io_num = GPIO_NUM_NC,
  715. .quadhd_io_num = GPIO_NUM_NC,
  716. .max_transfer_sz = 4000,
  717. };
  718. ret_val = spi_bus_initialize(host.slot, &bus_cfg, SPI_DMA_CH_AUTO);
  719. if (ret_val != ESP_OK) {
  720. ESP_LOGE(TAG, "Failed to initialize bus.");
  721. return ret_val;
  722. }
  723. #endif
  724. /**
  725. * @brief This initializes the slot without card detect (CD) and write protect (WP) signals.
  726. * Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
  727. *
  728. */
  729. #if FUNC_SDMMC_EN
  730. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  731. #else
  732. sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  733. #endif
  734. #if FUNC_SDMMC_EN
  735. /* Config SD data width. 0, 4 or 8. Currently for SD card, 8 bit is not supported. */
  736. slot_config.width = SDMMC_BUS_WIDTH;
  737. /**
  738. * @brief On chips where the GPIOs used for SD card can be configured, set them in
  739. * the slot_config structure.
  740. *
  741. */
  742. #if SOC_SDMMC_USE_GPIO_MATRIX
  743. slot_config.clk = GPIO_SDMMC_CLK;
  744. slot_config.cmd = GPIO_SDMMC_CMD;
  745. slot_config.d0 = GPIO_SDMMC_D0;
  746. slot_config.d1 = GPIO_SDMMC_D1;
  747. slot_config.d2 = GPIO_SDMMC_D2;
  748. slot_config.d3 = GPIO_SDMMC_D3;
  749. #endif
  750. slot_config.cd = GPIO_SDMMC_DET;
  751. slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
  752. #else
  753. slot_config.gpio_cs = GPIO_SDSPI_CS;
  754. slot_config.host_id = host.slot;
  755. #endif
  756. /**
  757. * @brief Enable internal pullups on enabled pins. The internal pullups
  758. * are insufficient however, please make sure 10k external pullups are
  759. * connected on the bus. This is for debug / example purpose only.
  760. */
  761. /* get FAT filesystem on SD card registered in VFS. */
  762. ret_val =
  763. #if FUNC_SDMMC_EN
  764. esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card);
  765. #else
  766. esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
  767. #endif
  768. /* Check for SDMMC mount result. */
  769. if (ret_val != ESP_OK) {
  770. if (ret_val == ESP_FAIL) {
  771. ESP_LOGE(TAG, "Failed to mount filesystem. "
  772. "If you want the card to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
  773. } else {
  774. ESP_LOGE(TAG, "Failed to initialize the card (%s). "
  775. "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret_val));
  776. }
  777. return ret_val;
  778. }
  779. /* Card has been initialized, print its properties. */
  780. sdmmc_card_print_info(stdout, card);
  781. SDCard_Size = ((uint64_t) card->csd.capacity) * card->csd.sector_size / (1024 * 1024);
  782. return ret_val;
  783. }
  784. esp_err_t esp_sdcard_deinit(char *mount_point)
  785. {
  786. if (NULL == mount_point) {
  787. return ESP_ERR_INVALID_STATE;
  788. }
  789. /* Unmount an SD card from the FAT filesystem and release resources acquired */
  790. esp_err_t ret_val = esp_vfs_fat_sdcard_unmount(mount_point, card);
  791. /* Make SD/MMC card information structure pointer NULL */
  792. card = NULL;
  793. return ret_val;
  794. }
  795. #define MOUNT_POINT "/sdcard"
  796. static const char *SD_TAG = "SD";
  797. uint16_t Folder_retrieval(const char* directory, const char* fileExtension, char File_Name[][MAX_FILE_NAME_SIZE], uint16_t maxFiles)
  798. {
  799. DIR *dir = opendir(directory);
  800. if (dir == NULL) {
  801. ESP_LOGE(SD_TAG, "Path: <%s> does not exist", directory);
  802. return 0;
  803. }
  804. uint16_t fileCount = 0;
  805. struct dirent *entry;
  806. while ((entry = readdir(dir)) != NULL && fileCount < maxFiles) {
  807. if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
  808. continue;
  809. }
  810. const char *dot = strrchr(entry->d_name, '.');
  811. if (dot != NULL && dot != entry->d_name) {
  812. if (strcasecmp(dot, fileExtension) == 0) {
  813. strncpy(File_Name[fileCount], entry->d_name, MAX_FILE_NAME_SIZE - 1);
  814. File_Name[fileCount][MAX_FILE_NAME_SIZE - 1] = '\0';
  815. char filePath[MAX_PATH_SIZE];
  816. snprintf(filePath, MAX_PATH_SIZE, "%s/%s", directory, entry->d_name);
  817. printf("File found: %s\r\n", filePath);
  818. fileCount++;
  819. }
  820. }
  821. else{
  822. // printf("No extension found for file: %s\r\n", entry->d_name);
  823. }
  824. }
  825. closedir(dir);
  826. if (fileCount > 0) {
  827. ESP_LOGI(SD_TAG, "Retrieved %d files with extension '%s'", fileCount, fileExtension);
  828. } else {
  829. ESP_LOGW(SD_TAG, "No files with extension '%s' found in directory: %s", fileExtension, directory);
  830. }
  831. return fileCount;
  832. }