main/task_sdcard.c

changeset 106
191dccfe4252
parent 105
9e00845dc1ee
child 119
1cef3c25426b
equal deleted inserted replaced
105:9e00845dc1ee 106:191dccfe4252
28 EventGroupHandle_t xEventGroupSDcard; ///< SD card events. 28 EventGroupHandle_t xEventGroupSDcard; ///< SD card events.
29 29
30 int card_handle = -1; 30 int card_handle = -1;
31 static const char *TAG = "task_sdcard"; 31 static const char *TAG = "task_sdcard";
32 static sdmmc_card_t* card = NULL; 32 static sdmmc_card_t* card = NULL;
33 static uint8_t s_pdrv = 0;
34 //static char * s_base_path = NULL;
35 static uint8_t pdrv = FF_DRV_NOT_USED; 33 static uint8_t pdrv = FF_DRV_NOT_USED;
34 char drv[3] = {'0', ':', 0};
36 35
37 36
38 #define SDCARD_HOST_SLOT VSPI_HOST ///< HSPI_HOST is used by the TFT 37 #define SDCARD_HOST_SLOT VSPI_HOST ///< HSPI_HOST is used by the TFT
39 #define SDCARD_PIN_NUM_MISO 2 ///< MISO pin 38 #define SDCARD_PIN_NUM_MISO 2 ///< MISO pin
40 #define SDCARD_PIN_NUM_MOSI 15 ///< MOSI pin 39 #define SDCARD_PIN_NUM_MOSI 15 ///< MOSI pin
213 } 212 }
214 } 213 }
215 214
216 215
217 216
218 static esp_err_t mount_prepare_mem(const char *base_path, BYTE *out_pdrv, char **out_dup_path, sdmmc_card_t** out_card) 217 static esp_err_t mount_prepare_mem(const char *base_path, BYTE *out_pdrv, char **out_dup_path)
219 { 218 {
220 esp_err_t err = ESP_OK; 219 esp_err_t err = ESP_OK;
221 char* dup_path = NULL; 220 char* dup_path = NULL;
222 221
223 // connect SDMMC driver to FATFS 222 // connect SDMMC driver to FATFS
229 } 228 }
230 229
231 // not using ff_memalloc here, as allocation in internal RAM is preferred 230 // not using ff_memalloc here, as allocation in internal RAM is preferred
232 card = (sdmmc_card_t*)malloc(sizeof(sdmmc_card_t)); 231 card = (sdmmc_card_t*)malloc(sizeof(sdmmc_card_t));
233 if (card == NULL) { 232 if (card == NULL) {
234 ESP_LOGD(TAG, "could not locate new sdmmc_card_t"); 233 ESP_LOGD(TAG, "could not malloc sdmmc_card_t");
235 err = ESP_ERR_NO_MEM; 234 err = ESP_ERR_NO_MEM;
236 goto cleanup; 235 goto cleanup;
237 } 236 }
238 237
239 dup_path = strdup(base_path); 238 dup_path = strdup(base_path);
241 ESP_LOGD(TAG, "could not copy base_path"); 240 ESP_LOGD(TAG, "could not copy base_path");
242 err = ESP_ERR_NO_MEM; 241 err = ESP_ERR_NO_MEM;
243 goto cleanup; 242 goto cleanup;
244 } 243 }
245 244
246 *out_card = card;
247 *out_pdrv = pdrv; 245 *out_pdrv = pdrv;
248 *out_dup_path = dup_path; 246 *out_dup_path = dup_path;
249 return ESP_OK; 247 return ESP_OK;
250 cleanup: 248 cleanup:
251 free(card); 249 free(card);
285 if (card != NULL) { 283 if (card != NULL) {
286 ESP_LOGE(TAG, "card not NULL"); 284 ESP_LOGE(TAG, "card not NULL");
287 return ESP_ERR_INVALID_STATE; 285 return ESP_ERR_INVALID_STATE;
288 } 286 }
289 287
290 err = mount_prepare_mem(base_path, &pdrv, &dup_path, &card); 288 err = mount_prepare_mem(base_path, &pdrv, &dup_path);
291 if (err != ESP_OK) { 289 if (err != ESP_OK) {
292 ESP_LOGE(TAG, "mount_prepare failed"); 290 ESP_LOGE(TAG, "mount_prepare_mem 0x%x", err);
293 return err; 291 return err;
294 } 292 }
295 293
296 //the init() function is usually empty, doesn't require any deinit to revert it 294 //the init() function is usually empty, doesn't require any deinit to revert it
297 err = (*host_config->init)(); 295 err = (*host_config->init)();
298 if (err != ESP_OK) { 296 if (err != ESP_OK) {
299 ESP_LOGE(TAG, "host init returned rc=0x%x", err); 297 ESP_LOGE(TAG, "host_config->init() 0x%x", err);
300 goto fail; 298 goto fail;
301 } 299 }
302 300
303 // configure SD host 301 // configure SD host
304 err = my_init_sdspi_host(host_config->slot, slot_config, &card_handle); 302 err = my_init_sdspi_host(host_config->slot, slot_config, &card_handle);
305 if (err != ESP_OK) { 303 if (err != ESP_OK) {
306 ESP_LOGE(TAG, "my_init_sdspi_host() rc=0x%x", err); 304 ESP_LOGE(TAG, "my_init_sdspi_host() 0x%x", err);
307 goto fail; 305 goto fail;
308 } 306 }
309 return ESP_OK; 307 return ESP_OK;
310 308
311 fail: 309 fail:
335 const sdmmc_host_t* host_config = host_config_input; 333 const sdmmc_host_t* host_config = host_config_input;
336 FATFS* fs = NULL; 334 FATFS* fs = NULL;
337 esp_err_t err = ESP_OK; 335 esp_err_t err = ESP_OK;
338 336
339 if (card == NULL) { 337 if (card == NULL) {
340 ESP_LOGE(TAG, "card not NULL"); 338 ESP_LOGE(TAG, "card NULL");
341 return ESP_ERR_INVALID_STATE; 339 return ESP_ERR_INVALID_STATE;
342 } 340 }
343 341
344 /* 342 /*
345 * The `slot` argument inside host_config should be replaced by the SD SPI handled returned 343 * The `slot` argument inside host_config should be replaced by the SD SPI handled returned
354 352
355 // probe and initialize card 353 // probe and initialize card
356 err = sdmmc_card_init(host_config, card); 354 err = sdmmc_card_init(host_config, card);
357 if (err != ESP_OK) { 355 if (err != ESP_OK) {
358 if (err != ESP_ERR_INVALID_RESPONSE) { // No card present, do not log 356 if (err != ESP_ERR_INVALID_RESPONSE) { // No card present, do not log
359 ESP_LOGI(TAG, "sdmmc_card_init failed 0x(%x)", err); 357 ESP_LOGI(TAG, "sdmmc_card_init failed 0x%x", err);
360 } 358 }
361 goto fail; 359 goto fail;
362 } 360 }
363 361
364 if (out_card != NULL) { 362 if (out_card != NULL) {
367 365
368 /* 366 /*
369 * mount to vfs fat 367 * mount to vfs fat
370 */ 368 */
371 ff_diskio_register_sdmmc(pdrv, card); 369 ff_diskio_register_sdmmc(pdrv, card);
372 s_pdrv = pdrv;
373 ESP_LOGD(TAG, "using pdrv=%i", pdrv); 370 ESP_LOGD(TAG, "using pdrv=%i", pdrv);
374 char drv[3] = {(char)('0' + pdrv), ':', 0}; 371 drv[0] = (char)('0' + pdrv);
375 372
376 // connect FATFS to VFS 373 // connect FATFS to VFS
377 err = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs); 374 err = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs);
378 if (err == ESP_ERR_INVALID_STATE) { 375 if (err == ESP_ERR_INVALID_STATE) {
379 // it's okay, already registered with VFS 376 // it's okay, already registered with VFS
381 ESP_LOGI(TAG, "esp_vfs_fat_register failed 0x(%x)", err); 378 ESP_LOGI(TAG, "esp_vfs_fat_register failed 0x(%x)", err);
382 goto fail; 379 goto fail;
383 } 380 }
384 381
385 // Try to mount partition 382 // Try to mount partition
383 // See: esp-idf/components/fatfs/src/ff.h
386 FRESULT res = f_mount(fs, drv, 1); 384 FRESULT res = f_mount(fs, drv, 1);
387 if (res != FR_OK) { 385 if (res != FR_OK) {
388 err = ESP_FAIL; 386 err = ESP_FAIL;
389 ESP_LOGD(TAG, "f_mount failed (%d)", res); 387 ESP_LOGD(TAG, "f_mount failed (%d)", res);
390 goto fail; 388 goto fail;
410 { 408 {
411 if (card == NULL) { 409 if (card == NULL) {
412 return ESP_ERR_INVALID_STATE; 410 return ESP_ERR_INVALID_STATE;
413 } 411 }
414 // unmount 412 // unmount
415 char drv[3] = {(char)('0' + s_pdrv), ':', 0};
416 f_mount(0, drv, 0); 413 f_mount(0, drv, 0);
417 return ESP_OK; 414 return ESP_OK;
418 } 415 }
419 416
420 417
465 json_log = malloc(sizeof(JSON_log)); 462 json_log = malloc(sizeof(JSON_log));
466 463
467 ESP_LOGI(TAG, "Start SD card"); 464 ESP_LOGI(TAG, "Start SD card");
468 sdmmc_host_t host = SDSPI_HOST_DEFAULT(); 465 sdmmc_host_t host = SDSPI_HOST_DEFAULT();
469 host.slot = SDCARD_HOST_SLOT; // HSPI_HOST is in use by the TFT. 466 host.slot = SDCARD_HOST_SLOT; // HSPI_HOST is in use by the TFT.
467 //host.flags = SDMMC_HOST_FLAG_SPI | SDMMC_HOST_FLAG_DEINIT_ARG | SDMMC_HOST_FLAG_1BIT;
468 //host.max_freq_khz = 10000;
470 spi_bus_config_t bus_cfg = { 469 spi_bus_config_t bus_cfg = {
471 .mosi_io_num = SDCARD_PIN_NUM_MOSI, 470 .mosi_io_num = SDCARD_PIN_NUM_MOSI,
472 .miso_io_num = SDCARD_PIN_NUM_MISO, 471 .miso_io_num = SDCARD_PIN_NUM_MISO,
473 .sclk_io_num = SDCARD_PIN_NUM_CLK, 472 .sclk_io_num = SDCARD_PIN_NUM_CLK,
474 .quadwp_io_num = -1, 473 .quadwp_io_num = -1,
489 slot_config.host_id = host.slot; 488 slot_config.host_id = host.slot;
490 489
491 /* 490 /*
492 * No errors from the sdspi_transaction driver. 491 * No errors from the sdspi_transaction driver.
493 */ 492 */
494 // esp_log_level_set("sdspi_transaction", ESP_LOG_NONE); 493 esp_log_level_set("sdspi_transaction", ESP_LOG_NONE);
495 494
496 /* 495 /*
497 * Options for mounting the filesystem. 496 * Options for mounting the filesystem.
498 * If format_if_mount_failed is set to true, SD card will be partitioned and 497 * If format_if_mount_failed is set to true, SD card will be partitioned and
499 * formatted in case when mounting fails. 498 * formatted in case when mounting fails.
511 } else { 510 } else {
512 ESP_LOGE(TAG, "SPI card interface failed, abort task"); 511 ESP_LOGE(TAG, "SPI card interface failed, abort task");
513 vTaskDelete(NULL); 512 vTaskDelete(NULL);
514 return; 513 return;
515 } 514 }
516
517 xEventGroupSDcard = xEventGroupCreate(); 515 xEventGroupSDcard = xEventGroupCreate();
518 516
519 /* 517 /*
520 * Task loop, continues check of the inserted cards. 518 * Task loop, continues check of the inserted cards.
521 */ 519 */
530 ESP_LOGI(TAG, "SD card mounted on /sdcard"); 528 ESP_LOGI(TAG, "SD card mounted on /sdcard");
531 sdcard_state->card_present = true; 529 sdcard_state->card_present = true;
532 530
533 DIR* dir = opendir("/sdcard/w/log"); 531 DIR* dir = opendir("/sdcard/w/log");
534 if (dir == NULL) { 532 if (dir == NULL) {
533 ret = mkdir("/sdcard/w", 0755);
534 ESP_LOGI(TAG, "mkdir(/sdcard/w)=%d", ret);
535 ret = mkdir("/sdcard/w/log", 0755); 535 ret = mkdir("/sdcard/w/log", 0755);
536 ESP_LOGI(TAG, "mkdir(/sdcard/w/log)=%d", ret);
536 } else { 537 } else {
537 closedir(dir); 538 closedir(dir);
538 } 539 }
539 esp_log_level_set("sdmmc_sd", ESP_LOG_INFO); 540 esp_log_level_set("sdmmc_sd", ESP_LOG_INFO);
540 } 541 }

mercurial