forked from espressif/esp-idf
vfs_fat_spiflash: use esp_check
This commit is contained in:
@@ -285,7 +285,7 @@ esp_err_t esp_vfs_fat_spiflash_mount_ro(const char* base_path,
|
|||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* - ESP_OK on success
|
* - ESP_OK on success
|
||||||
* - ESP_ERR_INVALID_STATE if esp_vfs_fat_spiflash_mount_rw_wl hasn't been called
|
* - ESP_ERR_INVALID_STATE if esp_vfs_fat_spiflash_mount_ro hasn't been called
|
||||||
*/
|
*/
|
||||||
esp_err_t esp_vfs_fat_spiflash_unmount_ro(const char* base_path, const char* partition_label);
|
esp_err_t esp_vfs_fat_spiflash_unmount_ro(const char* base_path, const char* partition_label);
|
||||||
|
|
||||||
|
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "esp_check.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "esp_vfs.h"
|
#include "esp_vfs.h"
|
||||||
#include "esp_vfs_fat.h"
|
#include "esp_vfs_fat.h"
|
||||||
@@ -24,7 +25,7 @@ esp_err_t esp_vfs_fat_spiflash_mount_rw_wl(const char* base_path,
|
|||||||
const esp_vfs_fat_mount_config_t* mount_config,
|
const esp_vfs_fat_mount_config_t* mount_config,
|
||||||
wl_handle_t* wl_handle)
|
wl_handle_t* wl_handle)
|
||||||
{
|
{
|
||||||
esp_err_t result = ESP_OK;
|
esp_err_t ret = ESP_OK;
|
||||||
const size_t workbuf_size = 4096;
|
const size_t workbuf_size = 4096;
|
||||||
void *workbuf = NULL;
|
void *workbuf = NULL;
|
||||||
|
|
||||||
@@ -32,16 +33,10 @@ esp_err_t esp_vfs_fat_spiflash_mount_rw_wl(const char* base_path,
|
|||||||
ESP_PARTITION_SUBTYPE_ANY : ESP_PARTITION_SUBTYPE_DATA_FAT;
|
ESP_PARTITION_SUBTYPE_ANY : ESP_PARTITION_SUBTYPE_DATA_FAT;
|
||||||
const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
|
const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
|
||||||
subtype, partition_label);
|
subtype, partition_label);
|
||||||
if (data_partition == NULL) {
|
ESP_RETURN_ON_FALSE(data_partition, ESP_ERR_NOT_FOUND, TAG, "Failed to find FATFS partition (type='data', subtype='fat', partition_label='%s'). Check the partition table.", partition_label);
|
||||||
ESP_LOGE(TAG, "Failed to find FATFS partition (type='data', subtype='fat', partition_label='%s'). Check the partition table.", partition_label);
|
|
||||||
return ESP_ERR_NOT_FOUND;
|
ESP_RETURN_ON_ERROR(wl_mount(data_partition, wl_handle), TAG, "failed to mount wear levelling layer. ret = %i", ret);
|
||||||
}
|
|
||||||
|
|
||||||
result = wl_mount(data_partition, wl_handle);
|
|
||||||
if (result != ESP_OK) {
|
|
||||||
ESP_LOGE(TAG, "failed to mount wear levelling layer. result = %i", result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
// connect driver to FATFS
|
// connect driver to FATFS
|
||||||
BYTE pdrv = 0xFF;
|
BYTE pdrv = 0xFF;
|
||||||
if (ff_diskio_get_drive(&pdrv) != ESP_OK) {
|
if (ff_diskio_get_drive(&pdrv) != ESP_OK) {
|
||||||
@@ -50,18 +45,14 @@ esp_err_t esp_vfs_fat_spiflash_mount_rw_wl(const char* base_path,
|
|||||||
}
|
}
|
||||||
ESP_LOGD(TAG, "using pdrv=%i", pdrv);
|
ESP_LOGD(TAG, "using pdrv=%i", pdrv);
|
||||||
char drv[3] = {(char)('0' + pdrv), ':', 0};
|
char drv[3] = {(char)('0' + pdrv), ':', 0};
|
||||||
|
ESP_GOTO_ON_ERROR(ff_diskio_register_wl_partition(pdrv, *wl_handle), fail, TAG, "ff_diskio_register_wl_partition failed pdrv=%i, error - 0x(%x)", pdrv, ret);
|
||||||
|
|
||||||
result = ff_diskio_register_wl_partition(pdrv, *wl_handle);
|
|
||||||
if (result != ESP_OK) {
|
|
||||||
ESP_LOGE(TAG, "ff_diskio_register_wl_partition failed pdrv=%i, error - 0x(%x)", pdrv, result);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
FATFS *fs;
|
FATFS *fs;
|
||||||
result = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs);
|
ret = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs);
|
||||||
if (result == ESP_ERR_INVALID_STATE) {
|
if (ret == ESP_ERR_INVALID_STATE) {
|
||||||
// it's okay, already registered with VFS
|
// it's okay, already registered with VFS
|
||||||
} else if (result != ESP_OK) {
|
} else if (ret != ESP_OK) {
|
||||||
ESP_LOGD(TAG, "esp_vfs_fat_register failed 0x(%x)", result);
|
ESP_LOGD(TAG, "esp_vfs_fat_register failed 0x(%x)", ret);
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,12 +62,12 @@ esp_err_t esp_vfs_fat_spiflash_mount_rw_wl(const char* base_path,
|
|||||||
ESP_LOGW(TAG, "f_mount failed (%d)", fresult);
|
ESP_LOGW(TAG, "f_mount failed (%d)", fresult);
|
||||||
if (!((fresult == FR_NO_FILESYSTEM || fresult == FR_INT_ERR)
|
if (!((fresult == FR_NO_FILESYSTEM || fresult == FR_INT_ERR)
|
||||||
&& mount_config->format_if_mount_failed)) {
|
&& mount_config->format_if_mount_failed)) {
|
||||||
result = ESP_FAIL;
|
ret = ESP_FAIL;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
workbuf = ff_memalloc(workbuf_size);
|
workbuf = ff_memalloc(workbuf_size);
|
||||||
if (workbuf == NULL) {
|
if (workbuf == NULL) {
|
||||||
result = ESP_ERR_NO_MEM;
|
ret = ESP_ERR_NO_MEM;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
size_t alloc_unit_size = esp_vfs_fat_get_allocation_unit_size(
|
size_t alloc_unit_size = esp_vfs_fat_get_allocation_unit_size(
|
||||||
@@ -85,20 +76,12 @@ esp_err_t esp_vfs_fat_spiflash_mount_rw_wl(const char* base_path,
|
|||||||
ESP_LOGI(TAG, "Formatting FATFS partition, allocation unit size=%d", alloc_unit_size);
|
ESP_LOGI(TAG, "Formatting FATFS partition, allocation unit size=%d", alloc_unit_size);
|
||||||
const MKFS_PARM opt = {(BYTE)(FM_ANY | FM_SFD), 0, 0, 0, alloc_unit_size};
|
const MKFS_PARM opt = {(BYTE)(FM_ANY | FM_SFD), 0, 0, 0, alloc_unit_size};
|
||||||
fresult = f_mkfs(drv, &opt, workbuf, workbuf_size);
|
fresult = f_mkfs(drv, &opt, workbuf, workbuf_size);
|
||||||
if (fresult != FR_OK) {
|
ESP_GOTO_ON_FALSE(fresult == FR_OK, ESP_FAIL, fail, TAG, "f_mkfs failed (%d)", fresult);
|
||||||
result = ESP_FAIL;
|
|
||||||
ESP_LOGE(TAG, "f_mkfs failed (%d)", fresult);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
free(workbuf);
|
free(workbuf);
|
||||||
workbuf = NULL;
|
workbuf = NULL;
|
||||||
ESP_LOGI(TAG, "Mounting again");
|
ESP_LOGI(TAG, "Mounting again");
|
||||||
fresult = f_mount(fs, drv, 0);
|
fresult = f_mount(fs, drv, 0);
|
||||||
if (fresult != FR_OK) {
|
ESP_GOTO_ON_FALSE(fresult == FR_OK, ESP_FAIL, fail, TAG, "f_mount failed after formatting (%d)", fresult);
|
||||||
result = ESP_FAIL;
|
|
||||||
ESP_LOGE(TAG, "f_mount failed after formatting (%d)", fresult);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
|
|
||||||
@@ -106,17 +89,15 @@ fail:
|
|||||||
free(workbuf);
|
free(workbuf);
|
||||||
esp_vfs_fat_unregister_path(base_path);
|
esp_vfs_fat_unregister_path(base_path);
|
||||||
ff_diskio_unregister(pdrv);
|
ff_diskio_unregister(pdrv);
|
||||||
return result;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t esp_vfs_fat_spiflash_unmount_rw_wl(const char* base_path, wl_handle_t wl_handle)
|
esp_err_t esp_vfs_fat_spiflash_unmount_rw_wl(const char* base_path, wl_handle_t wl_handle)
|
||||||
{
|
{
|
||||||
BYTE pdrv = ff_diskio_get_pdrv_wl(wl_handle);
|
BYTE pdrv = ff_diskio_get_pdrv_wl(wl_handle);
|
||||||
if (pdrv == 0xff) {
|
ESP_RETURN_ON_FALSE(pdrv != 0xff, ESP_ERR_INVALID_STATE, TAG, "partition isn't registered, call esp_vfs_fat_spiflash_mount_rw_wl first");
|
||||||
return ESP_ERR_INVALID_STATE;
|
|
||||||
}
|
|
||||||
char drv[3] = {(char)('0' + pdrv), ':', 0};
|
|
||||||
|
|
||||||
|
char drv[3] = {(char)('0' + pdrv), ':', 0};
|
||||||
f_mount(0, drv, 0);
|
f_mount(0, drv, 0);
|
||||||
ff_diskio_unregister(pdrv);
|
ff_diskio_unregister(pdrv);
|
||||||
ff_diskio_clear_pdrv_wl(wl_handle);
|
ff_diskio_clear_pdrv_wl(wl_handle);
|
||||||
@@ -127,19 +108,43 @@ esp_err_t esp_vfs_fat_spiflash_unmount_rw_wl(const char* base_path, wl_handle_t
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// esp_err_t esp_vfs_fat_spiflash_format_rw_wl(const char* base_path,const char* partition_label, wl_handle_t wl_handle)
|
||||||
|
// {
|
||||||
|
// esp_err_t err = ESP_FAIL;
|
||||||
|
// BYTE pdrv = ff_diskio_get_pdrv_wl(wl_handle);
|
||||||
|
// if (pdrv != 0xff) {
|
||||||
|
// ESP_LOGD(TAG, "handle ID: pdrv", pdrv);
|
||||||
|
// err = esp_vfs_fat_spiflash_unmount_rw_wl(base_path, wl_handle);
|
||||||
|
// if (err != ESP_OK) {
|
||||||
|
// ESP_LOGE(TAG, "unmounting fail");
|
||||||
|
// return err;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// const esp_partition_t *partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
|
||||||
|
// ESP_PARTITION_SUBTYPE_DATA_FAT,
|
||||||
|
// partition_label);
|
||||||
|
// if (!partition) {
|
||||||
|
// ESP_LOGE(TAG, "Failed to find FATFS partition (type='data', subtype='fat', partition_label='%s'). Check the partition table.", partition_label);
|
||||||
|
// return ESP_ERR_NOT_FOUND;
|
||||||
|
// }
|
||||||
|
// err = esp_partition_erase_range(partition, 0, partition->size);
|
||||||
|
// if (err != ESP_OK) {
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
esp_err_t esp_vfs_fat_spiflash_mount_ro(const char* base_path,
|
esp_err_t esp_vfs_fat_spiflash_mount_ro(const char* base_path,
|
||||||
const char* partition_label,
|
const char* partition_label,
|
||||||
const esp_vfs_fat_mount_config_t* mount_config)
|
const esp_vfs_fat_mount_config_t* mount_config)
|
||||||
{
|
{
|
||||||
esp_err_t result = ESP_OK;
|
esp_err_t ret = ESP_OK;
|
||||||
|
|
||||||
const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
|
const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
|
||||||
ESP_PARTITION_SUBTYPE_DATA_FAT, partition_label);
|
ESP_PARTITION_SUBTYPE_DATA_FAT, partition_label);
|
||||||
if (data_partition == NULL) {
|
ESP_RETURN_ON_FALSE(data_partition, ESP_ERR_NOT_FOUND, TAG, "Failed to find FATFS partition (type='data', subtype='fat', partition_label='%s'). Check the partition table.", partition_label);
|
||||||
ESP_LOGE(TAG, "Failed to find FATFS partition (type='data', subtype='fat', partition_label='%s'). Check the partition table.", partition_label);
|
|
||||||
return ESP_ERR_NOT_FOUND;
|
|
||||||
}
|
|
||||||
|
|
||||||
// connect driver to FATFS
|
// connect driver to FATFS
|
||||||
BYTE pdrv = 0xFF;
|
BYTE pdrv = 0xFF;
|
||||||
@@ -149,19 +154,14 @@ esp_err_t esp_vfs_fat_spiflash_mount_ro(const char* base_path,
|
|||||||
}
|
}
|
||||||
ESP_LOGD(TAG, "using pdrv=%i", pdrv);
|
ESP_LOGD(TAG, "using pdrv=%i", pdrv);
|
||||||
char drv[3] = {(char)('0' + pdrv), ':', 0};
|
char drv[3] = {(char)('0' + pdrv), ':', 0};
|
||||||
|
ESP_GOTO_ON_ERROR(ff_diskio_register_raw_partition(pdrv, data_partition), fail, TAG, "ff_diskio_register_raw_partition failed pdrv=%i, error - 0x(%x)", pdrv, ret);
|
||||||
result = ff_diskio_register_raw_partition(pdrv, data_partition);
|
|
||||||
if (result != ESP_OK) {
|
|
||||||
ESP_LOGE(TAG, "ff_diskio_register_raw_partition failed pdrv=%i, error - 0x(%x)", pdrv, result);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
FATFS *fs;
|
FATFS *fs;
|
||||||
result = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs);
|
ret = esp_vfs_fat_register(base_path, drv, mount_config->max_files, &fs);
|
||||||
if (result == ESP_ERR_INVALID_STATE) {
|
if (ret == ESP_ERR_INVALID_STATE) {
|
||||||
// it's okay, already registered with VFS
|
// it's okay, already registered with VFS
|
||||||
} else if (result != ESP_OK) {
|
} else if (ret != ESP_OK) {
|
||||||
ESP_LOGD(TAG, "esp_vfs_fat_register failed 0x(%x)", result);
|
ESP_LOGD(TAG, "esp_vfs_fat_register failed 0x(%x)", ret);
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,7 +169,7 @@ esp_err_t esp_vfs_fat_spiflash_mount_ro(const char* base_path,
|
|||||||
FRESULT fresult = f_mount(fs, drv, 1);
|
FRESULT fresult = f_mount(fs, drv, 1);
|
||||||
if (fresult != FR_OK) {
|
if (fresult != FR_OK) {
|
||||||
ESP_LOGW(TAG, "f_mount failed (%d)", fresult);
|
ESP_LOGW(TAG, "f_mount failed (%d)", fresult);
|
||||||
result = ESP_FAIL;
|
ret = ESP_FAIL;
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@@ -177,25 +177,19 @@ esp_err_t esp_vfs_fat_spiflash_mount_ro(const char* base_path,
|
|||||||
fail:
|
fail:
|
||||||
esp_vfs_fat_unregister_path(base_path);
|
esp_vfs_fat_unregister_path(base_path);
|
||||||
ff_diskio_unregister(pdrv);
|
ff_diskio_unregister(pdrv);
|
||||||
return result;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t esp_vfs_fat_spiflash_unmount_ro(const char* base_path, const char* partition_label)
|
esp_err_t esp_vfs_fat_spiflash_unmount_ro(const char* base_path, const char* partition_label)
|
||||||
{
|
{
|
||||||
const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
|
const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA,
|
||||||
ESP_PARTITION_SUBTYPE_DATA_FAT, partition_label);
|
ESP_PARTITION_SUBTYPE_DATA_FAT, partition_label);
|
||||||
|
ESP_RETURN_ON_FALSE(data_partition, ESP_ERR_NOT_FOUND, TAG, "Failed to find FATFS partition (type='data', subtype='fat', partition_label='%s'). Check the partition table.", partition_label);
|
||||||
if (data_partition == NULL) {
|
|
||||||
ESP_LOGE(TAG, "Failed to find FATFS partition (type='data', subtype='fat', partition_label='%s'). Check the partition table.", partition_label);
|
|
||||||
return ESP_ERR_NOT_FOUND;
|
|
||||||
}
|
|
||||||
|
|
||||||
BYTE pdrv = ff_diskio_get_pdrv_raw(data_partition);
|
BYTE pdrv = ff_diskio_get_pdrv_raw(data_partition);
|
||||||
if (pdrv == 0xff) {
|
ESP_RETURN_ON_FALSE(pdrv != 0xff, ESP_ERR_INVALID_STATE, TAG, "partition isn't registered, call esp_vfs_fat_spiflash_mount_ro first");
|
||||||
return ESP_ERR_INVALID_STATE;
|
|
||||||
}
|
|
||||||
char drv[3] = {(char)('0' + pdrv), ':', 0};
|
|
||||||
|
|
||||||
|
char drv[3] = {(char)('0' + pdrv), ':', 0};
|
||||||
f_mount(0, drv, 0);
|
f_mount(0, drv, 0);
|
||||||
ff_diskio_unregister(pdrv);
|
ff_diskio_unregister(pdrv);
|
||||||
esp_err_t err = esp_vfs_fat_unregister_path(base_path);
|
esp_err_t err = esp_vfs_fat_unregister_path(base_path);
|
||||||
|
Reference in New Issue
Block a user