From 8af790da452e8da988573c23d6cd3f15ec2ee88b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20M=C3=BAdry?= Date: Sun, 5 Jun 2022 14:37:21 +0200 Subject: [PATCH] fatfs: Add mock option to ff_sdmmc_status function to increase IO speed Adds `disk_status_check_enable` field to `esp_vfs_fat_mount_config_t` struct to control if ff_sdmmc_status is mocked or not. --- components/fatfs/diskio/diskio_sdmmc.c | 14 ++++++++++++-- components/fatfs/diskio/diskio_sdmmc.h | 26 +++++++++++++------------- components/fatfs/vfs/esp_vfs_fat.h | 11 +++++++++++ components/fatfs/vfs/vfs_fat_sdmmc.c | 1 + docs/sphinx-known-warnings.txt | 2 ++ tools/ci/check_copyright_ignore.txt | 1 - 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/components/fatfs/diskio/diskio_sdmmc.c b/components/fatfs/diskio/diskio_sdmmc.c index 19f6d5698c..0bf53f7d62 100644 --- a/components/fatfs/diskio/diskio_sdmmc.c +++ b/components/fatfs/diskio/diskio_sdmmc.c @@ -12,6 +12,7 @@ #include "esp_compiler.h" static sdmmc_card_t* s_cards[FF_VOLUMES] = { NULL }; +static bool s_disk_status_check_en[FF_VOLUMES] = { }; static const char* TAG = "diskio_sdmmc"; @@ -38,9 +39,12 @@ DSTATUS ff_sdmmc_initialize (BYTE pdrv) return ff_sdmmc_card_available(pdrv); } -DSTATUS ff_sdmmc_status (BYTE pdrv) +DSTATUS ff_sdmmc_status(BYTE pdrv) { - return ff_sdmmc_card_available(pdrv); + if (s_disk_status_check_en[pdrv]) { + return ff_sdmmc_card_available(pdrv); + } + return 0; } DRESULT ff_sdmmc_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count) @@ -115,6 +119,11 @@ DRESULT ff_sdmmc_ioctl (BYTE pdrv, BYTE cmd, void* buff) return RES_ERROR; } +void ff_sdmmc_set_disk_status_check(BYTE pdrv, bool enable) +{ + s_disk_status_check_en[pdrv] = enable; +} + void ff_diskio_register_sdmmc(BYTE pdrv, sdmmc_card_t* card) { static const ff_diskio_impl_t sdmmc_impl = { @@ -125,6 +134,7 @@ void ff_diskio_register_sdmmc(BYTE pdrv, sdmmc_card_t* card) .ioctl = &ff_sdmmc_ioctl }; s_cards[pdrv] = card; + s_disk_status_check_en[pdrv] = false; ff_diskio_register(pdrv, &sdmmc_impl); } diff --git a/components/fatfs/diskio/diskio_sdmmc.h b/components/fatfs/diskio/diskio_sdmmc.h index 538a9c6da0..2ba4c2d9be 100644 --- a/components/fatfs/diskio/diskio_sdmmc.h +++ b/components/fatfs/diskio/diskio_sdmmc.h @@ -1,16 +1,8 @@ -// Copyright 2017-2019 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. +/* + * SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #pragma once @@ -21,6 +13,14 @@ extern "C" { #endif +/** + * @brief Enable/disable SD card status checking + * + * @param pdrv drive number + * @param enable mock ff_sdmmc_status function (return 0) + */ +void ff_sdmmc_set_disk_status_check(BYTE pdrv, bool enable); + /** * Register SD/MMC diskio driver * diff --git a/components/fatfs/vfs/esp_vfs_fat.h b/components/fatfs/vfs/esp_vfs_fat.h index 4925e758fc..ba8de9dbeb 100644 --- a/components/fatfs/vfs/esp_vfs_fat.h +++ b/components/fatfs/vfs/esp_vfs_fat.h @@ -85,6 +85,17 @@ typedef struct { * sector size. */ size_t allocation_unit_size; + /** + * Enables real ff_disk_status function implementation for SD cards + * (ff_sdmmc_status). Possibly slows down IO performance. + * + * Try to enable if you need to handle situations when SD cards + * are not unmounted properly before physical removal + * or you are experiencing issues with SD cards. + * + * Doesn't do anything for other memory storage media. + */ + bool disk_status_check_enable; } esp_vfs_fat_mount_config_t; // Compatibility definition diff --git a/components/fatfs/vfs/vfs_fat_sdmmc.c b/components/fatfs/vfs/vfs_fat_sdmmc.c index 0782a8f7f1..5f2d32ac12 100644 --- a/components/fatfs/vfs/vfs_fat_sdmmc.c +++ b/components/fatfs/vfs/vfs_fat_sdmmc.c @@ -85,6 +85,7 @@ static esp_err_t mount_to_vfs_fat(const esp_vfs_fat_mount_config_t *mount_config FATFS* fs = NULL; esp_err_t err; ff_diskio_register_sdmmc(pdrv, card); + ff_sdmmc_set_disk_status_check(pdrv, mount_config->disk_status_check_enable); ESP_LOGD(TAG, "using pdrv=%i", pdrv); char drv[3] = {(char)('0' + pdrv), ':', 0}; diff --git a/docs/sphinx-known-warnings.txt b/docs/sphinx-known-warnings.txt index 5d3793a4f5..e2cc29704e 100644 --- a/docs/sphinx-known-warnings.txt +++ b/docs/sphinx-known-warnings.txt @@ -38,3 +38,5 @@ wear-levelling.rst:line: WARNING: Duplicate C++ declaration, also defined at api Declaration is '.. cpp:member:: int max_files'. wear-levelling.rst:line: WARNING: Duplicate C++ declaration, also defined at api-reference/storage/fatfs:line. Declaration is '.. cpp:member:: size_t allocation_unit_size'. +wear-levelling.rst:line: WARNING: Duplicate C++ declaration, also defined at api-reference/storage/fatfs:line. +Declaration is '.. cpp:member:: bool disk_status_check_enable'. diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index 4dfd30df98..8e4597e85d 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -730,7 +730,6 @@ components/fatfs/diskio/diskio.c components/fatfs/diskio/diskio_impl.h components/fatfs/diskio/diskio_rawflash.c components/fatfs/diskio/diskio_rawflash.h -components/fatfs/diskio/diskio_sdmmc.h components/fatfs/diskio/diskio_wl.h components/fatfs/port/freertos/ffsystem.c components/fatfs/port/linux/ffsystem.c