Skip to content

Commit

Permalink
feat(msc_host): Add format MSC support
Browse files Browse the repository at this point in the history
Added test case for public API format

Closes espressif/esp-idf#14890
  • Loading branch information
igi540 committed Dec 11, 2024
1 parent e5feb3f commit ad575c0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
4 changes: 4 additions & 0 deletions host/class/msc/usb_host_msc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [Unreleased]

- Added public API support for formatting

## 1.1.3

- Implemented request sense, to get sense data from USB device in case of an error
Expand Down
16 changes: 15 additions & 1 deletion host/class/msc/usb_host_msc/include/usb/msc_host_vfs.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -16,6 +16,20 @@ extern "C" {

typedef struct msc_host_vfs *msc_host_vfs_handle_t; /**< VFS handle to attached Mass Storage device */

/**
* @brief Format MSC device.
*
* @param[in] device Device handle obtained from MSC callback provided upon initialization
* @param[in] mount_config Mount configuration
* @param[in] vfs_handle Handle to MSC device associated with registered VFS
* @return esp_err_t
* @return
* - ESP_OK: Format completed
* - ESP_ERR_INVALID_ARG: All arguments must be present and couldn't be NULL
* - ESP_ERR_MSC_FORMAT_FAILED: Formatting failed
*/
esp_err_t msc_host_vfs_format(msc_host_device_handle_t device, const esp_vfs_fat_mount_config_t *mount_config, const msc_host_vfs_handle_t vfs_handle);

/**
* @brief Register MSC device to Virtual filesystem.
*
Expand Down
14 changes: 13 additions & 1 deletion host/class/msc/usb_host_msc/src/msc_host_vfs.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -52,6 +52,18 @@ static esp_err_t msc_format_storage(size_t block_size, size_t allocation_size, c
return ESP_OK;
}

esp_err_t msc_host_vfs_format(msc_host_device_handle_t device, const esp_vfs_fat_mount_config_t *mount_config, const msc_host_vfs_handle_t vfs_handle)
{
MSC_RETURN_ON_INVALID_ARG(device);
MSC_RETURN_ON_INVALID_ARG(mount_config);
MSC_RETURN_ON_INVALID_ARG(vfs_handle);

size_t block_size = ((msc_device_t *)device)->disk.block_size;
size_t alloc_size = mount_config->allocation_unit_size;

return msc_format_storage(block_size, alloc_size, vfs_handle->drive);
}

static void dealloc_msc_vfs(msc_host_vfs_t *vfs)
{
free(vfs->base_path);
Expand Down
20 changes: 20 additions & 0 deletions host/class/msc/usb_host_msc/test_app/main/test_msc.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,26 @@ TEST_CASE("can_be_formated", "[usb_msc]")
mount_config.format_if_mount_failed = false;
}

/**
* @brief USB MSC API format testcase
* @attention This testcase deletes all content on the USB MSC device.
* The device must be reset in order to contain the FILE_NAME again.
*/
TEST_CASE("can_be_formated_by_api", "[usb_msc]")
{
printf("Create file on MSC device\n");
msc_setup();
write_read_file(FILE_NAME);

printf("Format storage device using msc_host_vfs_format\n");
esp_err_t ret = msc_host_vfs_format(device, &mount_config, vfs_handle);
TEST_ASSERT_EQUAL(ESP_OK, ret);

printf("Verify file does not exist after formatting\n");
TEST_ASSERT_FALSE(file_exists(FILE_NAME));
msc_teardown();
}

static void print_device_info(msc_host_device_info_t *info)
{
const size_t megabyte = 1024 * 1024;
Expand Down

0 comments on commit ad575c0

Please sign in to comment.