bsp_board.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  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 "string.h"
  7. #include "driver/gpio.h"
  8. #include "esp_err.h"
  9. #include "esp_log.h"
  10. #include "esp_rom_sys.h"
  11. #include "esp_check.h"
  12. #include "esp_vfs_fat.h"
  13. #include "sdmmc_cmd.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 "dirent.h"
  21. #include "driver/sdmmc_host.h"
  22. #include <errno.h>
  23. #if ((SOC_SDMMC_HOST_SUPPORTED) && (FUNC_SDMMC_EN))
  24. #include "driver/sdmmc_host.h"
  25. #endif /* ((SOC_SDMMC_HOST_SUPPORTED) && (FUNC_SDMMC_EN)) */
  26. #define ADC_I2S_CHANNEL 4
  27. static sdmmc_card_t *card;
  28. static const char *TAG = "board";
  29. static int s_play_sample_rate = 16000;
  30. static int s_play_channel_format = 1;
  31. static int s_bits_per_chan = 16;
  32. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  33. static i2s_chan_handle_t tx_handle = NULL; // I2S tx channel handler
  34. static i2s_chan_handle_t rx_handle = NULL; // I2S rx channel handler
  35. #endif
  36. static audio_codec_data_if_t *record_data_if = NULL;
  37. static audio_codec_ctrl_if_t *record_ctrl_if = NULL;
  38. static audio_codec_if_t *record_codec_if = NULL;
  39. static esp_codec_dev_handle_t record_dev = NULL;
  40. static audio_codec_data_if_t *play_data_if = NULL;
  41. static audio_codec_ctrl_if_t *play_ctrl_if = NULL;
  42. static audio_codec_gpio_if_t *play_gpio_if = NULL;
  43. static audio_codec_if_t *play_codec_if = NULL;
  44. static esp_codec_dev_handle_t play_dev = NULL;
  45. static i2c_master_bus_handle_t i2c_bus_handle = NULL;
  46. static uint32_t SDCard_Size = 0;
  47. int mic_level = 0;
  48. esp_codec_dev_handle_t esp_ret_play_dev(void)
  49. {
  50. return play_dev;
  51. }
  52. i2c_master_bus_handle_t esp_ret_i2c_handle(void)
  53. {
  54. return i2c_bus_handle;
  55. }
  56. static esp_err_t i2c_master_init(void)
  57. {
  58. const i2c_master_bus_config_t bus_config = {
  59. .i2c_port = I2C_NUM,
  60. .sda_io_num = GPIO_I2C_SDA,
  61. .scl_io_num = GPIO_I2C_SCL,
  62. .clk_source = I2C_CLK_SRC_DEFAULT,
  63. };
  64. esp_err_t ret = i2c_new_master_bus(&bus_config, &i2c_bus_handle);
  65. if (ret != ESP_OK) {
  66. ESP_LOGE(TAG, "Failed to initialize I2C bus: %s", esp_err_to_name(ret));
  67. return ret;
  68. }
  69. ESP_LOGI(TAG, "I2C bus initialized successfully");
  70. return ESP_OK; // 返回 ESP_OK 表示成功
  71. }
  72. esp_err_t bsp_codec_adc_init(int sample_rate)
  73. {
  74. esp_err_t ret_val = ESP_OK;
  75. // Do initialize of related interface: data_if, ctrl_if and gpio_if
  76. audio_codec_i2s_cfg_t i2s_cfg = {
  77. .port = I2S_NUM_1,
  78. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  79. .rx_handle = rx_handle,
  80. .tx_handle = NULL,
  81. #endif
  82. };
  83. record_data_if = audio_codec_new_i2s_data(&i2s_cfg);
  84. audio_codec_i2c_cfg_t i2c_cfg = {.addr = ES7210_CODEC_DEFAULT_ADDR,.bus_handle = i2c_bus_handle};
  85. record_ctrl_if = audio_codec_new_i2c_ctrl(&i2c_cfg);
  86. // New input codec interface
  87. es7210_codec_cfg_t es7210_cfg = {
  88. .ctrl_if = record_ctrl_if,
  89. .mic_selected = ES7210_SEL_MIC1 | ES7210_SEL_MIC2 | ES7210_SEL_MIC3 | ES7210_SEL_MIC4,
  90. };
  91. record_codec_if = es7210_codec_new(&es7210_cfg);
  92. // New input codec device
  93. esp_codec_dev_cfg_t dev_cfg = {
  94. .codec_if = record_codec_if,
  95. .data_if = record_data_if,
  96. .dev_type = ESP_CODEC_DEV_TYPE_IN,
  97. };
  98. record_dev = esp_codec_dev_new(&dev_cfg);
  99. esp_codec_dev_sample_info_t fs = {
  100. .sample_rate = 16000,
  101. .channel = 2,
  102. .bits_per_sample = 32,
  103. };
  104. esp_codec_dev_open(record_dev, &fs);
  105. // esp_codec_dev_set_in_gain(record_dev, RECORD_VOLUME);
  106. esp_codec_dev_set_in_channel_gain(record_dev, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(0), RECORD_VOLUME);
  107. esp_codec_dev_set_in_channel_gain(record_dev, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(1), RECORD_VOLUME);
  108. esp_codec_dev_set_in_channel_gain(record_dev, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(2), RECORD_VOLUME);
  109. esp_codec_dev_set_in_channel_gain(record_dev, ESP_CODEC_DEV_MAKE_CHANNEL_MASK(3), RECORD_VOLUME);
  110. return ret_val;
  111. }
  112. esp_err_t bsp_codec_dac_init(int sample_rate, int channel_format, int bits_per_chan)
  113. {
  114. esp_err_t ret_val = ESP_OK;
  115. // Do initialize of related interface: data_if, ctrl_if and gpio_if
  116. audio_codec_i2s_cfg_t i2s_cfg = {
  117. .port = I2S_NUM_1,
  118. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  119. .rx_handle = NULL,
  120. .tx_handle = tx_handle,
  121. #endif
  122. };
  123. play_data_if = audio_codec_new_i2s_data(&i2s_cfg);
  124. audio_codec_i2c_cfg_t i2c_cfg = {.addr = ES8311_CODEC_DEFAULT_ADDR,.bus_handle = i2c_bus_handle};
  125. play_ctrl_if = audio_codec_new_i2c_ctrl(&i2c_cfg);
  126. play_gpio_if = audio_codec_new_gpio();
  127. // New output codec interface
  128. es8311_codec_cfg_t es8311_cfg = {
  129. .codec_mode = ESP_CODEC_DEV_WORK_MODE_DAC,
  130. .ctrl_if = play_ctrl_if,
  131. .gpio_if = play_gpio_if,
  132. .pa_pin = GPIO_PWR_CTRL,
  133. .use_mclk = false,
  134. };
  135. play_codec_if = es8311_codec_new(&es8311_cfg);
  136. // New output codec device
  137. esp_codec_dev_cfg_t dev_cfg = {
  138. .codec_if = play_codec_if,
  139. .data_if = play_data_if,
  140. .dev_type = ESP_CODEC_DEV_TYPE_OUT,
  141. };
  142. play_dev = esp_codec_dev_new(&dev_cfg);
  143. esp_codec_dev_sample_info_t fs = {
  144. .bits_per_sample = bits_per_chan,
  145. .sample_rate = sample_rate,
  146. .channel = channel_format,
  147. };
  148. esp_codec_dev_set_out_vol(play_dev, PLAYER_VOLUME);
  149. esp_codec_dev_open(play_dev, &fs);
  150. return ret_val;
  151. }
  152. static esp_err_t bsp_codec_adc_deinit()
  153. {
  154. esp_err_t ret_val = ESP_OK;
  155. if (record_dev) {
  156. esp_codec_dev_close(record_dev);
  157. esp_codec_dev_delete(record_dev);
  158. record_dev = NULL;
  159. }
  160. // Delete codec interface
  161. if (record_codec_if) {
  162. audio_codec_delete_codec_if(record_codec_if);
  163. record_codec_if = NULL;
  164. }
  165. // Delete codec control interface
  166. if (record_ctrl_if) {
  167. audio_codec_delete_ctrl_if(record_ctrl_if);
  168. record_ctrl_if = NULL;
  169. }
  170. // Delete codec data interface
  171. if (record_data_if) {
  172. audio_codec_delete_data_if(record_data_if);
  173. record_data_if = NULL;
  174. }
  175. return ret_val;
  176. }
  177. static esp_err_t bsp_codec_dac_deinit()
  178. {
  179. esp_err_t ret_val = ESP_OK;
  180. if (play_dev) {
  181. esp_codec_dev_close(play_dev);
  182. esp_codec_dev_delete(play_dev);
  183. play_dev = NULL;
  184. }
  185. // Delete codec interface
  186. if (play_codec_if) {
  187. audio_codec_delete_codec_if(play_codec_if);
  188. play_codec_if = NULL;
  189. }
  190. // Delete codec control interface
  191. if (play_ctrl_if) {
  192. audio_codec_delete_ctrl_if(play_ctrl_if);
  193. play_ctrl_if = NULL;
  194. }
  195. if (play_gpio_if) {
  196. audio_codec_delete_gpio_if(play_gpio_if);
  197. play_gpio_if = NULL;
  198. }
  199. // Delete codec data interface
  200. if (play_data_if) {
  201. audio_codec_delete_data_if(play_data_if);
  202. play_data_if = NULL;
  203. }
  204. return ret_val;
  205. }
  206. esp_err_t esp_audio_set_play_vol(int volume)
  207. {
  208. if (!play_dev) {
  209. ESP_LOGE(TAG, "DAC codec init fail");
  210. return ESP_FAIL;
  211. }
  212. esp_codec_dev_set_out_vol(play_dev, volume);
  213. return ESP_OK;
  214. }
  215. esp_err_t esp_audio_get_play_vol(int *volume)
  216. {
  217. if (!play_dev) {
  218. ESP_LOGE(TAG, "DAC codec init fail");
  219. return ESP_FAIL;
  220. }
  221. esp_codec_dev_get_out_vol(play_dev, volume);
  222. return ESP_OK;
  223. }
  224. // 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)
  225. static esp_err_t bsp_i2s_init(int i2s_num, uint32_t sample_rate, int channel_format, int bits_per_chan)
  226. {
  227. esp_err_t ret_val = ESP_OK;
  228. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  229. i2s_slot_mode_t channel_fmt = I2S_SLOT_MODE_STEREO;
  230. if (channel_format == 1) {
  231. channel_fmt = I2S_SLOT_MODE_MONO;
  232. } else if (channel_format == 2) {
  233. channel_fmt = I2S_SLOT_MODE_STEREO;
  234. } else {
  235. ESP_LOGE(TAG, "Unable to configure channel_format %d", channel_format);
  236. channel_format = 1;
  237. channel_fmt = I2S_SLOT_MODE_MONO;
  238. }
  239. if (bits_per_chan != 16 && bits_per_chan != 32) {
  240. ESP_LOGE(TAG, "Unable to configure bits_per_chan %d", bits_per_chan);
  241. bits_per_chan = 32;
  242. }
  243. i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(i2s_num, I2S_ROLE_MASTER);
  244. ret_val |= i2s_new_channel(&chan_cfg, &tx_handle, &rx_handle);
  245. i2s_std_config_t std_cfg = I2S_CONFIG_DEFAULT(sample_rate, channel_fmt, bits_per_chan);
  246. ret_val |= i2s_channel_init_std_mode(tx_handle, &std_cfg);
  247. ret_val |= i2s_channel_init_std_mode(rx_handle, &std_cfg);
  248. ret_val |= i2s_channel_enable(tx_handle);
  249. ret_val |= i2s_channel_enable(rx_handle);
  250. #else
  251. i2s_channel_fmt_t channel_fmt = I2S_CHANNEL_FMT_RIGHT_LEFT;
  252. if (channel_format == 1) {
  253. channel_fmt = I2S_CHANNEL_FMT_ONLY_LEFT;
  254. } else if (channel_format == 2) {
  255. channel_fmt = I2S_CHANNEL_FMT_RIGHT_LEFT;
  256. } else {
  257. ESP_LOGE(TAG, "Unable to configure channel_format %d", channel_format);
  258. channel_format = 1;
  259. channel_fmt = I2S_CHANNEL_FMT_ONLY_LEFT;
  260. }
  261. if (bits_per_chan != 16 && bits_per_chan != 32) {
  262. ESP_LOGE(TAG, "Unable to configure bits_per_chan %d", bits_per_chan);
  263. bits_per_chan = 16;
  264. }
  265. i2s_config_t i2s_config = I2S_CONFIG_DEFAULT(sample_rate, channel_fmt, bits_per_chan);
  266. i2s_pin_config_t pin_config = {
  267. .bck_io_num = GPIO_I2S_SCLK,
  268. .ws_io_num = GPIO_I2S_LRCK,
  269. .data_out_num = GPIO_I2S_DOUT,
  270. .data_in_num = GPIO_I2S_SDIN,
  271. .mck_io_num = GPIO_I2S_MCLK,
  272. };
  273. ret_val |= i2s_driver_install(i2s_num, &i2s_config, 0, NULL);
  274. ret_val |= i2s_set_pin(i2s_num, &pin_config);
  275. #endif
  276. return ret_val;
  277. }
  278. static esp_err_t bsp_i2s_deinit(int i2s_num)
  279. {
  280. esp_err_t ret_val = ESP_OK;
  281. #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
  282. if (i2s_num == I2S_NUM_1 && rx_handle) {
  283. ret_val |= i2s_channel_disable(rx_handle);
  284. ret_val |= i2s_del_channel(rx_handle);
  285. rx_handle = NULL;
  286. } else if (i2s_num == I2S_NUM_0 && tx_handle) {
  287. ret_val |= i2s_channel_disable(tx_handle);
  288. ret_val |= i2s_del_channel(tx_handle);
  289. tx_handle = NULL;
  290. }
  291. #else
  292. ret_val |= i2s_stop(i2s_num);
  293. ret_val |= i2s_driver_uninstall(i2s_num);
  294. #endif
  295. return ret_val;
  296. }
  297. static esp_err_t bsp_codec_init(int adc_sample_rate, int dac_sample_rate, int dac_channel_format, int dac_bits_per_chan)
  298. {
  299. esp_err_t ret_val = ESP_OK;
  300. ret_val |= bsp_codec_adc_init(adc_sample_rate);
  301. ret_val |= bsp_codec_dac_init(dac_sample_rate, dac_channel_format, dac_bits_per_chan);
  302. return ret_val;
  303. }
  304. static esp_err_t bsp_codec_deinit()
  305. {
  306. esp_err_t ret_val = ESP_OK;
  307. ret_val |= bsp_codec_adc_deinit();
  308. ret_val |= bsp_codec_dac_deinit();
  309. return ret_val;
  310. }
  311. esp_err_t esp_audio_play(const int16_t* data, int length, uint32_t ticks_to_wait)
  312. {
  313. size_t bytes_write = 0;
  314. esp_err_t ret = ESP_OK;
  315. if (!play_dev) {
  316. return ESP_FAIL;
  317. }
  318. int out_length= length;
  319. int audio_time = 1;
  320. audio_time *= (16000 / s_play_sample_rate);
  321. audio_time *= (2 / s_play_channel_format);
  322. int *data_out = NULL;
  323. if (s_bits_per_chan != 32) {
  324. out_length = length * 2;
  325. data_out = malloc(out_length);
  326. for (int i = 0; i < length / sizeof(int16_t); i++) {
  327. int ret = data[i];
  328. data_out[i] = ret << 16;
  329. }
  330. }
  331. int *data_out_1 = NULL;
  332. if (s_play_channel_format != 2 || s_play_sample_rate != 16000) {
  333. out_length *= audio_time;
  334. data_out_1 = malloc(out_length);
  335. int *tmp_data = NULL;
  336. if (data_out != NULL) {
  337. tmp_data = data_out;
  338. } else {
  339. tmp_data = (int *)data;
  340. }
  341. for (int i = 0; i < out_length / (audio_time * sizeof(int)); i++) {
  342. for (int j = 0; j < audio_time; j++) {
  343. data_out_1[audio_time * i + j] = tmp_data[i];
  344. }
  345. }
  346. if (data_out != NULL) {
  347. free(data_out);
  348. data_out = NULL;
  349. }
  350. }
  351. if (data_out != NULL) {
  352. ret = esp_codec_dev_write(play_dev, (void *)data_out, out_length);
  353. free(data_out);
  354. } else if (data_out_1 != NULL) {
  355. ret = esp_codec_dev_write(play_dev, (void *)data_out_1, out_length);
  356. free(data_out_1);
  357. } else {
  358. ret = esp_codec_dev_write(play_dev, (void *)data, length);
  359. }
  360. return ret;
  361. }
  362. int get_mic_level (void) {
  363. return mic_level;
  364. }
  365. esp_err_t esp_get_feed_data(bool is_get_raw_channel, int16_t *buffer, int buffer_len)
  366. {
  367. esp_err_t ret = ESP_OK;
  368. size_t bytes_read;
  369. int audio_chunksize = buffer_len / (sizeof(int16_t) * ADC_I2S_CHANNEL);
  370. ret = esp_codec_dev_read(record_dev, (void *)buffer, buffer_len);
  371. int64_t sum = 0;
  372. for(int i = 0; i < buffer_len/sizeof(int16_t); i++) {
  373. sum += abs(buffer[i]);
  374. }
  375. ESP_LOGI("MIC", "Level=%lld", sum/(buffer_len/sizeof(int16_t)));
  376. mic_level = sum/(buffer_len/sizeof(int16_t));
  377. if (!is_get_raw_channel) {
  378. for (int i = 0; i < audio_chunksize; i++) {
  379. int16_t ref = buffer[4 * i + 0];
  380. buffer[3 * i + 0] = buffer[4 * i + 1];
  381. buffer[3 * i + 1] = buffer[4 * i + 3];
  382. buffer[3 * i + 2] = ref;
  383. }
  384. }
  385. return ret;
  386. }
  387. int esp_get_feed_channel(void)
  388. {
  389. return ADC_I2S_CHANNEL;
  390. }
  391. char* esp_get_input_format(void)
  392. {
  393. return "RMNM";
  394. }
  395. esp_err_t esp_board_init(uint32_t sample_rate, int channel_format, int bits_per_chan)
  396. {
  397. /*!< Initialize I2C bus, used for audio codec*/
  398. i2c_master_init();
  399. s_play_sample_rate = sample_rate;
  400. if (channel_format != 2 && channel_format != 1) {
  401. ESP_LOGE(TAG, "Unable to configure channel_format");
  402. channel_format = 2;
  403. }
  404. s_play_channel_format = channel_format;
  405. if (bits_per_chan != 32 && bits_per_chan != 16) {
  406. ESP_LOGE(TAG, "Unable to configure bits_per_chan");
  407. bits_per_chan = 32;
  408. }
  409. s_bits_per_chan = bits_per_chan;
  410. bsp_i2s_init(I2S_NUM_1, 16000, 2, 32);
  411. // Because record and play use the same i2s.
  412. bsp_codec_init(16000, 16000, 2, 32);
  413. /* Initialize PA */
  414. /*gpio_config_t io_conf;
  415. memset(&io_conf, 0, sizeof(io_conf));
  416. io_conf.intr_type = GPIO_INTR_DISABLE;
  417. io_conf.mode = GPIO_MODE_OUTPUT;
  418. io_conf.pin_bit_mask = ((1ULL << GPIO_PWR_CTRL));
  419. io_conf.pull_down_en = 0;
  420. io_conf.pull_up_en = 0;
  421. gpio_config(&io_conf);
  422. */
  423. gpio_set_level(GPIO_PWR_CTRL, 1);
  424. return ESP_OK;
  425. }
  426. esp_err_t esp_sdcard_init(char *mount_point, size_t max_files)
  427. {
  428. if (NULL != card) {
  429. return ESP_ERR_INVALID_STATE;
  430. }
  431. /* Check if SD crad is supported */
  432. if (!FUNC_SDMMC_EN && !FUNC_SDSPI_EN) {
  433. ESP_LOGE(TAG, "SDMMC and SDSPI not supported on this board!");
  434. return ESP_ERR_NOT_SUPPORTED;
  435. }
  436. esp_err_t ret_val = ESP_OK;
  437. /**
  438. * @brief Options for mounting the filesystem.
  439. * If format_if_mount_failed is set to true, SD card will be partitioned and
  440. * formatted in case when mounting fails.
  441. *
  442. */
  443. esp_vfs_fat_sdmmc_mount_config_t mount_config = {
  444. .format_if_mount_failed = false,
  445. .max_files = max_files,
  446. .allocation_unit_size = 16 * 1024
  447. };
  448. /**
  449. * @brief Use settings defined above to initialize SD card and mount FAT filesystem.
  450. * Note: esp_vfs_fat_sdmmc/sdspi_mount is all-in-one convenience functions.
  451. * Please check its source code and implement error recovery when developing
  452. * production applications.
  453. *
  454. */
  455. sdmmc_host_t host =
  456. #if FUNC_SDMMC_EN
  457. SDMMC_HOST_DEFAULT();
  458. #else
  459. SDSPI_HOST_DEFAULT();
  460. spi_bus_config_t bus_cfg = {
  461. .mosi_io_num = GPIO_SDSPI_MOSI,
  462. .miso_io_num = GPIO_SDSPI_MISO,
  463. .sclk_io_num = GPIO_SDSPI_SCLK,
  464. .quadwp_io_num = GPIO_NUM_NC,
  465. .quadhd_io_num = GPIO_NUM_NC,
  466. .max_transfer_sz = 4000,
  467. };
  468. ret_val = spi_bus_initialize(host.slot, &bus_cfg, SPI_DMA_CH_AUTO);
  469. if (ret_val != ESP_OK) {
  470. ESP_LOGE(TAG, "Failed to initialize bus.");
  471. return ret_val;
  472. }
  473. #endif
  474. /**
  475. * @brief This initializes the slot without card detect (CD) and write protect (WP) signals.
  476. * Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
  477. *
  478. */
  479. #if FUNC_SDMMC_EN
  480. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  481. #else
  482. sdspi_device_config_t slot_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  483. #endif
  484. #if FUNC_SDMMC_EN
  485. /* Config SD data width. 0, 4 or 8. Currently for SD card, 8 bit is not supported. */
  486. slot_config.width = SDMMC_BUS_WIDTH;
  487. /**
  488. * @brief On chips where the GPIOs used for SD card can be configured, set them in
  489. * the slot_config structure.
  490. *
  491. */
  492. #if SOC_SDMMC_USE_GPIO_MATRIX
  493. slot_config.clk = GPIO_SDMMC_CLK;
  494. slot_config.cmd = GPIO_SDMMC_CMD;
  495. slot_config.d0 = GPIO_SDMMC_D0;
  496. slot_config.d1 = GPIO_SDMMC_D1;
  497. slot_config.d2 = GPIO_SDMMC_D2;
  498. slot_config.d3 = GPIO_SDMMC_D3;
  499. #endif
  500. slot_config.cd = GPIO_SDMMC_DET;
  501. slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
  502. #else
  503. slot_config.gpio_cs = GPIO_SDSPI_CS;
  504. slot_config.host_id = host.slot;
  505. #endif
  506. /**
  507. * @brief Enable internal pullups on enabled pins. The internal pullups
  508. * are insufficient however, please make sure 10k external pullups are
  509. * connected on the bus. This is for debug / example purpose only.
  510. */
  511. /* get FAT filesystem on SD card registered in VFS. */
  512. ret_val =
  513. #if FUNC_SDMMC_EN
  514. esp_vfs_fat_sdmmc_mount(mount_point, &host, &slot_config, &mount_config, &card);
  515. #else
  516. esp_vfs_fat_sdspi_mount(mount_point, &host, &slot_config, &mount_config, &card);
  517. #endif
  518. /* Check for SDMMC mount result. */
  519. if (ret_val != ESP_OK) {
  520. if (ret_val == ESP_FAIL) {
  521. ESP_LOGE(TAG, "Failed to mount filesystem. "
  522. "If you want the card to be formatted, set the EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.");
  523. } else {
  524. ESP_LOGE(TAG, "Failed to initialize the card (%s). "
  525. "Make sure SD card lines have pull-up resistors in place.", esp_err_to_name(ret_val));
  526. }
  527. return ret_val;
  528. }
  529. /* Card has been initialized, print its properties. */
  530. sdmmc_card_print_info(stdout, card);
  531. SDCard_Size = ((uint64_t) card->csd.capacity) * card->csd.sector_size / (1024 * 1024);
  532. return ret_val;
  533. }
  534. esp_err_t esp_sdcard_deinit(char *mount_point)
  535. {
  536. if (NULL == mount_point) {
  537. return ESP_ERR_INVALID_STATE;
  538. }
  539. /* Unmount an SD card from the FAT filesystem and release resources acquired */
  540. esp_err_t ret_val = esp_vfs_fat_sdcard_unmount(mount_point, card);
  541. /* Make SD/MMC card information structure pointer NULL */
  542. card = NULL;
  543. return ret_val;
  544. }
  545. #define MOUNT_POINT "/sdcard"
  546. static const char *SD_TAG = "SD";
  547. uint16_t Folder_retrieval(const char* directory, const char* fileExtension, char File_Name[][MAX_FILE_NAME_SIZE], uint16_t maxFiles)
  548. {
  549. DIR *dir = opendir(directory);
  550. if (dir == NULL) {
  551. ESP_LOGE(SD_TAG, "Path: <%s> does not exist", directory);
  552. return 0;
  553. }
  554. uint16_t fileCount = 0;
  555. struct dirent *entry;
  556. while ((entry = readdir(dir)) != NULL && fileCount < maxFiles) {
  557. if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
  558. continue;
  559. }
  560. const char *dot = strrchr(entry->d_name, '.');
  561. if (dot != NULL && dot != entry->d_name) {
  562. if (strcasecmp(dot, fileExtension) == 0) {
  563. strncpy(File_Name[fileCount], entry->d_name, MAX_FILE_NAME_SIZE - 1);
  564. File_Name[fileCount][MAX_FILE_NAME_SIZE - 1] = '\0';
  565. char filePath[MAX_PATH_SIZE];
  566. snprintf(filePath, MAX_PATH_SIZE, "%s/%s", directory, entry->d_name);
  567. printf("File found: %s\r\n", filePath);
  568. fileCount++;
  569. }
  570. }
  571. else{
  572. // printf("No extension found for file: %s\r\n", entry->d_name);
  573. }
  574. }
  575. closedir(dir);
  576. if (fileCount > 0) {
  577. ESP_LOGI(SD_TAG, "Retrieved %d files with extension '%s'", fileCount, fileExtension);
  578. } else {
  579. ESP_LOGW(SD_TAG, "No files with extension '%s' found in directory: %s", fileExtension, directory);
  580. }
  581. return fileCount;
  582. }