bsp_board.c 26 KB

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