2017-01-08 16:54:04 -05:00
|
|
|
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2017-02-01 10:55:25 -05:00
|
|
|
#include <string.h>
|
2017-01-08 16:54:04 -05:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_vfs.h"
|
|
|
|
#include "esp_vfs_fat.h"
|
2018-01-08 03:21:02 -05:00
|
|
|
#include "vfs_fat_internal.h"
|
2017-01-08 16:54:04 -05:00
|
|
|
#include "driver/sdmmc_host.h"
|
2017-07-04 01:58:36 -04:00
|
|
|
#include "driver/sdspi_host.h"
|
2017-01-08 16:54:04 -05:00
|
|
|
#include "sdmmc_cmd.h"
|
2019-06-19 12:07:51 -04:00
|
|
|
#include "diskio_impl.h"
|
|
|
|
#include "diskio_sdmmc.h"
|
2017-01-08 16:54:04 -05:00
|
|
|
|
|
|
|
static const char* TAG = "vfs_fat_sdmmc";
|
|
|
|
static sdmmc_card_t* s_card = NULL;
|
2017-01-31 10:23:25 -05:00
|
|
|
static uint8_t s_pdrv = 0;
|
2017-02-01 10:55:25 -05:00
|
|
|
static char * s_base_path = NULL;
|
2017-01-08 16:54:04 -05:00
|
|
|
|
|
|
|
esp_err_t esp_vfs_fat_sdmmc_mount(const char* base_path,
|
|
|
|
const sdmmc_host_t* host_config,
|
2017-07-04 01:58:36 -04:00
|
|
|
const void* slot_config,
|
|
|
|
const esp_vfs_fat_mount_config_t* mount_config,
|
2017-01-08 16:54:04 -05:00
|
|
|
sdmmc_card_t** out_card)
|
|
|
|
{
|
|
|
|
const size_t workbuf_size = 4096;
|
|
|
|
void* workbuf = NULL;
|
2017-04-19 03:03:30 -04:00
|
|
|
FATFS* fs = NULL;
|
2017-01-08 16:54:04 -05:00
|
|
|
|
|
|
|
if (s_card != NULL) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
2017-02-01 10:55:25 -05:00
|
|
|
|
|
|
|
// connect SDMMC driver to FATFS
|
|
|
|
BYTE pdrv = 0xFF;
|
|
|
|
if (ff_diskio_get_drive(&pdrv) != ESP_OK || pdrv == 0xFF) {
|
|
|
|
ESP_LOGD(TAG, "the maximum count of volumes is already mounted");
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_base_path = strdup(base_path);
|
|
|
|
if(!s_base_path){
|
|
|
|
ESP_LOGD(TAG, "could not copy base_path");
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
2017-07-04 01:58:36 -04:00
|
|
|
esp_err_t err = ESP_OK;
|
2018-11-28 12:08:59 -05:00
|
|
|
// not using ff_memalloc here, as allocation in internal RAM is preferred
|
2017-07-04 01:58:36 -04:00
|
|
|
s_card = malloc(sizeof(sdmmc_card_t));
|
|
|
|
if (s_card == NULL) {
|
|
|
|
err = ESP_ERR_NO_MEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
2017-02-01 10:55:25 -05:00
|
|
|
|
2017-07-04 01:58:36 -04:00
|
|
|
err = (*host_config->init)();
|
2017-02-24 08:04:22 -05:00
|
|
|
if (err != ESP_OK) {
|
2017-07-04 01:58:36 -04:00
|
|
|
ESP_LOGD(TAG, "host init returned rc=0x%x", err);
|
|
|
|
goto fail;
|
2017-02-24 08:04:22 -05:00
|
|
|
}
|
|
|
|
|
2017-07-04 01:58:36 -04:00
|
|
|
// configure SD slot
|
|
|
|
if (host_config->flags == SDMMC_HOST_FLAG_SPI) {
|
|
|
|
err = sdspi_host_init_slot(host_config->slot,
|
|
|
|
(const sdspi_slot_config_t*) slot_config);
|
|
|
|
} else {
|
|
|
|
err = sdmmc_host_init_slot(host_config->slot,
|
|
|
|
(const sdmmc_slot_config_t*) slot_config);
|
|
|
|
}
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGD(TAG, "slot_config returned rc=0x%x", err);
|
2017-02-24 08:04:22 -05:00
|
|
|
goto fail;
|
2017-01-08 16:54:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// probe and initialize card
|
2017-02-24 08:04:22 -05:00
|
|
|
err = sdmmc_card_init(host_config, s_card);
|
2017-01-08 16:54:04 -05:00
|
|
|
if (err != ESP_OK) {
|
|
|
|
ESP_LOGD(TAG, "sdmmc_card_init failed 0x(%x)", err);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (out_card != NULL) {
|
|
|
|
*out_card = s_card;
|
|
|
|
}
|
|
|
|
|
2017-01-31 10:23:25 -05:00
|
|
|
ff_diskio_register_sdmmc(pdrv, s_card);
|
|
|
|
s_pdrv = pdrv;
|
2017-05-04 07:57:11 -04:00
|
|
|
ESP_LOGD(TAG, "using pdrv=%i", pdrv);
|
2017-01-31 10:23:25 -05:00
|
|
|
char drv[3] = {(char)('0' + pdrv), ':', 0};
|
2017-01-08 16:54:04 -05:00
|
|
|
|
|
|
|
// connect FATFS to VFS
|
2017-01-31 10:23:25 -05:00
|
|
|
err = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs);
|
2017-01-08 16:54:04 -05:00
|
|
|
if (err == ESP_ERR_INVALID_STATE) {
|
|
|
|
// it's okay, already registered with VFS
|
|
|
|
} else if (err != ESP_OK) {
|
|
|
|
ESP_LOGD(TAG, "esp_vfs_fat_register failed 0x(%x)", err);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to mount partition
|
2017-01-31 10:23:25 -05:00
|
|
|
FRESULT res = f_mount(fs, drv, 1);
|
2017-01-08 16:54:04 -05:00
|
|
|
if (res != FR_OK) {
|
|
|
|
err = ESP_FAIL;
|
|
|
|
ESP_LOGW(TAG, "failed to mount card (%d)", res);
|
|
|
|
if (!(res == FR_NO_FILESYSTEM && mount_config->format_if_mount_failed)) {
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
ESP_LOGW(TAG, "partitioning card");
|
2018-11-28 12:08:59 -05:00
|
|
|
workbuf = ff_memalloc(workbuf_size);
|
2018-01-08 03:21:44 -05:00
|
|
|
if (workbuf == NULL) {
|
|
|
|
err = ESP_ERR_NO_MEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
DWORD plist[] = {100, 0, 0, 0};
|
2017-01-31 10:23:25 -05:00
|
|
|
res = f_fdisk(s_pdrv, plist, workbuf);
|
2017-01-08 16:54:04 -05:00
|
|
|
if (res != FR_OK) {
|
|
|
|
err = ESP_FAIL;
|
|
|
|
ESP_LOGD(TAG, "f_fdisk failed (%d)", res);
|
|
|
|
goto fail;
|
|
|
|
}
|
2018-01-08 03:21:02 -05:00
|
|
|
size_t alloc_unit_size = esp_vfs_fat_get_allocation_unit_size(
|
|
|
|
s_card->csd.sector_size,
|
|
|
|
mount_config->allocation_unit_size);
|
|
|
|
ESP_LOGW(TAG, "formatting card, allocation unit size=%d", alloc_unit_size);
|
|
|
|
res = f_mkfs(drv, FM_ANY, alloc_unit_size, workbuf, workbuf_size);
|
2017-01-08 16:54:04 -05:00
|
|
|
if (res != FR_OK) {
|
|
|
|
err = ESP_FAIL;
|
|
|
|
ESP_LOGD(TAG, "f_mkfs failed (%d)", res);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
free(workbuf);
|
2017-12-10 22:06:29 -05:00
|
|
|
workbuf = NULL;
|
2017-01-08 16:54:04 -05:00
|
|
|
ESP_LOGW(TAG, "mounting again");
|
2017-01-31 10:23:25 -05:00
|
|
|
res = f_mount(fs, drv, 0);
|
2017-01-08 16:54:04 -05:00
|
|
|
if (res != FR_OK) {
|
|
|
|
err = ESP_FAIL;
|
|
|
|
ESP_LOGD(TAG, "f_mount failed after formatting (%d)", res);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
|
|
|
|
fail:
|
2017-12-07 23:17:58 -05:00
|
|
|
host_config->deinit();
|
2017-01-08 16:54:04 -05:00
|
|
|
free(workbuf);
|
2017-04-19 03:03:30 -04:00
|
|
|
if (fs) {
|
|
|
|
f_mount(NULL, drv, 0);
|
|
|
|
}
|
2017-02-02 05:24:38 -05:00
|
|
|
esp_vfs_fat_unregister_path(base_path);
|
2017-02-01 10:55:25 -05:00
|
|
|
ff_diskio_unregister(pdrv);
|
2017-01-08 16:54:04 -05:00
|
|
|
free(s_card);
|
|
|
|
s_card = NULL;
|
2019-10-08 07:17:34 -04:00
|
|
|
free(s_base_path);
|
|
|
|
s_base_path = NULL;
|
2017-01-08 16:54:04 -05:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
esp_err_t esp_vfs_fat_sdmmc_unmount(void)
|
2017-01-08 16:54:04 -05:00
|
|
|
{
|
|
|
|
if (s_card == NULL) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
// unmount
|
2017-01-31 10:23:25 -05:00
|
|
|
char drv[3] = {(char)('0' + s_pdrv), ':', 0};
|
|
|
|
f_mount(0, drv, 0);
|
2017-01-08 16:54:04 -05:00
|
|
|
// release SD driver
|
2019-07-30 00:53:48 -04:00
|
|
|
esp_err_t (*host_deinit)(void) = s_card->host.deinit;
|
2017-02-01 10:55:25 -05:00
|
|
|
ff_diskio_unregister(s_pdrv);
|
2017-01-08 16:54:04 -05:00
|
|
|
free(s_card);
|
|
|
|
s_card = NULL;
|
2017-12-07 23:17:58 -05:00
|
|
|
(*host_deinit)();
|
2017-02-02 05:24:38 -05:00
|
|
|
esp_err_t err = esp_vfs_fat_unregister_path(s_base_path);
|
2017-02-01 10:55:25 -05:00
|
|
|
free(s_base_path);
|
|
|
|
s_base_path = NULL;
|
|
|
|
return err;
|
2017-01-08 16:54:04 -05:00
|
|
|
}
|