bsp_board.c 25 KB

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