Initial public release of the 2024A software.
This commit is contained in:
parent
7b9ad3edfd
commit
303e9e1dad
361 changed files with 60083 additions and 2 deletions
|
@ -0,0 +1,14 @@
|
|||
# The following lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
|
||||
set(EXTRA_COMPONENT_DIRS
|
||||
../../usb_host_msc
|
||||
../../../../../device/esp_tinyusb
|
||||
)
|
||||
|
||||
# "Trim" the build. Include the minimal set of components, main, and anything it depends on.
|
||||
set(COMPONENTS main)
|
||||
|
||||
project(test_app_usb_host_msc)
|
|
@ -0,0 +1,14 @@
|
|||
| Supported Targets | ESP32-S2 | ESP32-S3 |
|
||||
| ----------------- | -------- | -------- |
|
||||
|
||||
# USB: CDC Class test application
|
||||
|
||||
## MSC driver
|
||||
|
||||
Basic functionality such as MSC device install/uninstall, file operations,
|
||||
raw access to MSC device and sudden disconnect is tested.
|
||||
|
||||
### Hardware Required
|
||||
|
||||
This test requires two ESP32-S2/S3 boards with a interconnected USB peripherals,
|
||||
one acting as host running MSC host driver and another MSC device driver (tinyusb).
|
|
@ -0,0 +1,4 @@
|
|||
idf_component_register(SRC_DIRS .
|
||||
INCLUDE_DIRS .
|
||||
REQUIRES unity usb usb_host_msc esp_tinyusb
|
||||
WHOLE_ARCHIVE)
|
|
@ -0,0 +1,461 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "tinyusb.h"
|
||||
#include "esp_idf_version.h"
|
||||
#include "soc/soc_caps.h"
|
||||
#include "test_common.h"
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
#include "esp_check.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "tusb_msc_storage.h"
|
||||
#endif /* ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) */
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) && SOC_SDMMC_HOST_SUPPORTED
|
||||
#include "diskio_impl.h"
|
||||
#include "diskio_sdmmc.h"
|
||||
#endif /* ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) && SOC_SDMMC_HOST_SUPPORTED */
|
||||
|
||||
#if SOC_USB_OTG_SUPPORTED
|
||||
|
||||
/* sd-card configuration to be done by user */
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) && SOC_SDMMC_HOST_SUPPORTED
|
||||
#define SDMMC_BUS_WIDTH 4 /* Select the bus width of SD or MMC interface (4 or 1).
|
||||
Note that even if 1 line mode is used, D3 pin of the SD card must
|
||||
have a pull-up resistor connected. Otherwise the card may enter
|
||||
SPI mode, the only way to recover from which is to cycle power to the card. */
|
||||
#define PIN_CMD 35 /* CMD GPIO number */
|
||||
#define PIN_CLK 36 /* CLK GPIO number */
|
||||
#define PIN_D0 37 /* D0 GPIO number */
|
||||
#define PIN_D1 38 /* D1 GPIO number (applicable when width SDMMC_BUS_WIDTH is 4) */
|
||||
#define PIN_D2 33 /* D2 GPIO number (applicable when width SDMMC_BUS_WIDTH is 4) */
|
||||
#define PIN_D3 34 /* D3 GPIO number (applicable when width SDMMC_BUS_WIDTH is 4) */
|
||||
#endif /* ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) && SOC_SDMMC_HOST_SUPPORTED */
|
||||
|
||||
static const char *TAG = "msc_example";
|
||||
|
||||
/* TinyUSB descriptors
|
||||
********************************************************************* */
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
#define TUSB_DESC_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_MSC_DESC_LEN)
|
||||
|
||||
enum {
|
||||
ITF_NUM_MSC = 0,
|
||||
ITF_NUM_TOTAL
|
||||
};
|
||||
|
||||
enum {
|
||||
EDPT_MSC_OUT = 0x01,
|
||||
EDPT_MSC_IN = 0x81,
|
||||
};
|
||||
|
||||
static uint8_t const desc_configuration[] = {
|
||||
// Config number, interface count, string index, total length, attribute, power in mA
|
||||
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, TUSB_DESC_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100),
|
||||
|
||||
// Interface number, string index, EP Out & EP In address, EP size
|
||||
TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 0, EDPT_MSC_OUT, EDPT_MSC_IN, TUD_OPT_HIGH_SPEED ? 512 : 64),
|
||||
};
|
||||
|
||||
static tusb_desc_device_t descriptor_config = {
|
||||
.bLength = sizeof(descriptor_config),
|
||||
.bDescriptorType = TUSB_DESC_DEVICE,
|
||||
.bcdUSB = 0x0200,
|
||||
.bDeviceClass = TUSB_CLASS_MISC,
|
||||
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
|
||||
.bDeviceProtocol = MISC_PROTOCOL_IAD,
|
||||
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
|
||||
.idVendor = 0x303A, // This is Espressif VID. This needs to be changed according to Users / Customers
|
||||
.idProduct = 0x4002,
|
||||
.bcdDevice = 0x100,
|
||||
.iManufacturer = 0x01,
|
||||
.iProduct = 0x02,
|
||||
.iSerialNumber = 0x03,
|
||||
.bNumConfigurations = 0x01
|
||||
};
|
||||
|
||||
static char const *string_desc_arr[] = {
|
||||
(const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409)
|
||||
"TinyUSB", // 1: Manufacturer
|
||||
"TinyUSB Device", // 2: Product
|
||||
// We intentionally do not implement Serial String descriptor to make sure that the driver can handle it
|
||||
//"123456", // 3: Serials
|
||||
//"Test MSC", // 4. MSC
|
||||
};
|
||||
#endif /* ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) */
|
||||
/*********************************************************************** TinyUSB descriptors*/
|
||||
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
#define VBUS_MONITORING_GPIO_NUM GPIO_NUM_4
|
||||
static void configure_vbus_monitoring(void)
|
||||
{
|
||||
// Configure GPIO Pin for vbus monitoring
|
||||
const gpio_config_t vbus_gpio_config = {
|
||||
.pin_bit_mask = BIT64(VBUS_MONITORING_GPIO_NUM),
|
||||
.mode = GPIO_MODE_INPUT,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
.pull_up_en = true,
|
||||
.pull_down_en = false,
|
||||
};
|
||||
ESP_ERROR_CHECK(gpio_config(&vbus_gpio_config));
|
||||
}
|
||||
#endif /* ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) */
|
||||
|
||||
static void storage_init(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "USB MSC initialization");
|
||||
const tinyusb_config_t tusb_cfg = {
|
||||
.external_phy = false,
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
.device_descriptor = &descriptor_config,
|
||||
.configuration_descriptor = desc_configuration,
|
||||
.string_descriptor = string_desc_arr,
|
||||
.string_descriptor_count = sizeof(string_desc_arr) / sizeof(string_desc_arr[0]),
|
||||
.self_powered = true,
|
||||
.vbus_monitor_io = VBUS_MONITORING_GPIO_NUM
|
||||
#endif /* ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) */
|
||||
};
|
||||
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
|
||||
ESP_LOGI(TAG, "USB initialization DONE");
|
||||
}
|
||||
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
static esp_err_t storage_init_spiflash(wl_handle_t *wl_handle)
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing wear levelling");
|
||||
|
||||
const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, NULL);
|
||||
if (data_partition == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to find FATFS partition. Check the partition table.");
|
||||
return ESP_ERR_NOT_FOUND;
|
||||
}
|
||||
|
||||
return wl_mount(data_partition, wl_handle);
|
||||
}
|
||||
#endif /* ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) */
|
||||
|
||||
void device_app(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing storage...");
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
configure_vbus_monitoring();
|
||||
|
||||
static wl_handle_t wl_handle = WL_INVALID_HANDLE;
|
||||
ESP_ERROR_CHECK(storage_init_spiflash(&wl_handle));
|
||||
|
||||
tinyusb_msc_spiflash_config_t config_spi;
|
||||
config_spi.wl_handle = wl_handle;
|
||||
ESP_ERROR_CHECK(tinyusb_msc_storage_init_spiflash(&config_spi));
|
||||
#endif /* ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) */
|
||||
storage_init();
|
||||
while (1) {
|
||||
vTaskDelay(100);
|
||||
}
|
||||
}
|
||||
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) && SOC_SDMMC_HOST_SUPPORTED
|
||||
static esp_err_t storage_init_sdmmc(sdmmc_card_t **card)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
bool host_init = false;
|
||||
sdmmc_card_t *sd_card;
|
||||
|
||||
ESP_LOGI(TAG, "Initializing SDCard");
|
||||
|
||||
// By default, SD card frequency is initialized to SDMMC_FREQ_DEFAULT (20MHz)
|
||||
// For setting a specific frequency, use host.max_freq_khz (range 400kHz - 40MHz for SDMMC)
|
||||
// Example: for fixed frequency of 10MHz, use host.max_freq_khz = 10000;
|
||||
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
|
||||
|
||||
// This initializes the slot without card detect (CD) and write protect (WP) signals.
|
||||
// Modify slot_config.gpio_cd and slot_config.gpio_wp if your board has these signals.
|
||||
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
|
||||
|
||||
if (SDMMC_BUS_WIDTH == 4) {
|
||||
slot_config.width = 4;
|
||||
} else {
|
||||
slot_config.width = 1;
|
||||
}
|
||||
|
||||
// On chips where the GPIOs used for SD card can be configured, set the user defined values
|
||||
#ifdef CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
|
||||
slot_config.clk = PIN_CLK;
|
||||
slot_config.cmd = PIN_CMD;
|
||||
slot_config.d0 = PIN_D0;
|
||||
if (SDMMC_BUS_WIDTH == 4) {
|
||||
slot_config.d1 = PIN_D1;
|
||||
slot_config.d2 = PIN_D2;
|
||||
slot_config.d3 = PIN_D3;
|
||||
}
|
||||
#endif // CONFIG_SOC_SDMMC_USE_GPIO_MATRIX
|
||||
|
||||
// Enable internal pullups on enabled pins. The internal pullups
|
||||
// are insufficient however, please make sure 10k external pullups are
|
||||
// connected on the bus. This is for debug / example purpose only.
|
||||
slot_config.flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP;
|
||||
|
||||
// not using ff_memalloc here, as allocation in internal RAM is preferred
|
||||
sd_card = (sdmmc_card_t *)malloc(sizeof(sdmmc_card_t));
|
||||
ESP_GOTO_ON_FALSE(sd_card, ESP_ERR_NO_MEM, clean, TAG, "could not allocate new sdmmc_card_t");
|
||||
|
||||
ESP_GOTO_ON_ERROR((*host.init)(), clean, TAG, "Host Config Init fail");
|
||||
host_init = true;
|
||||
|
||||
ESP_GOTO_ON_ERROR(sdmmc_host_init_slot(host.slot, (const sdmmc_slot_config_t *) &slot_config),
|
||||
clean, TAG, "Host init slot fail");
|
||||
|
||||
ESP_GOTO_ON_ERROR(sdmmc_card_init(&host, sd_card),
|
||||
clean, TAG, "The detection pin of the slot is disconnected");
|
||||
|
||||
*card = sd_card;
|
||||
|
||||
return ESP_OK;
|
||||
|
||||
clean:
|
||||
if (host_init) {
|
||||
if (host.flags & SDMMC_HOST_FLAG_DEINIT_ARG) {
|
||||
host.deinit_p(host.slot);
|
||||
} else {
|
||||
(*host.deinit)();
|
||||
}
|
||||
}
|
||||
if (sd_card) {
|
||||
free(sd_card);
|
||||
sd_card = NULL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void device_app_sdmmc(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing storage...");
|
||||
configure_vbus_monitoring();
|
||||
static sdmmc_card_t *card = NULL;
|
||||
ESP_ERROR_CHECK(storage_init_sdmmc(&card));
|
||||
|
||||
tinyusb_msc_sdmmc_config_t config_sdmmc;
|
||||
config_sdmmc.card = card;
|
||||
ESP_ERROR_CHECK(tinyusb_msc_storage_init_sdmmc(&config_sdmmc));
|
||||
|
||||
storage_init();
|
||||
while (1) {
|
||||
vTaskDelay(100);
|
||||
}
|
||||
}
|
||||
#endif /* ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) && SOC_SDMMC_HOST_SUPPORTED */
|
||||
|
||||
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
// whether host does safe-eject
|
||||
static bool ejected = false;
|
||||
|
||||
// Some MCU doesn't have enough 8KB SRAM to store the whole disk
|
||||
// We will use Flash as read-only disk with board that has
|
||||
// CFG_EXAMPLE_MSC_READONLY defined
|
||||
|
||||
uint8_t msc_disk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] = {
|
||||
//------------- Block0: Boot Sector -------------//
|
||||
// byte_per_sector = DISK_BLOCK_SIZE; fat12_sector_num_16 = DISK_BLOCK_NUM;
|
||||
// sector_per_cluster = 1; reserved_sectors = 1;
|
||||
// fat_num = 1; fat12_root_entry_num = 16;
|
||||
// sector_per_fat = 1; sector_per_track = 1; head_num = 1; hidden_sectors = 0;
|
||||
// drive_number = 0x80; media_type = 0xf8; extended_boot_signature = 0x29;
|
||||
// filesystem_type = "FAT12 "; volume_serial_number = 0x1234; volume_label = "TinyUSB MSC";
|
||||
// FAT magic code at offset 510-511
|
||||
{
|
||||
0xEB, 0x3C, 0x90, 0x4D, 0x53, 0x44, 0x4F, 0x53, 0x35, 0x2E, 0x30, 0x00, 0x02, 0x01, 0x01, 0x00,
|
||||
0x01, 0x10, 0x00, 0x10, 0x00, 0xF8, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x29, 0x34, 0x12, 0x00, 0x00, 'T', 'i', 'n', 'y', 'U',
|
||||
'S', 'B', ' ', 'M', 'S', 'C', 0x46, 0x41, 0x54, 0x31, 0x32, 0x20, 0x20, 0x20, 0x00, 0x00,
|
||||
|
||||
// Zero up to 2 last bytes of FAT magic code
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 'F', 'A', 'T', '3', '2', ' ', ' ', ' ', 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xAA
|
||||
},
|
||||
|
||||
//------------- Block1: FAT12 Table -------------//
|
||||
{
|
||||
0xF8, 0xFF, 0xFF, 0xFF, 0x0F // // first 2 entries must be F8FF, third entry is cluster end of readme file
|
||||
},
|
||||
|
||||
//------------- Block2: Root Directory -------------//
|
||||
{
|
||||
// first entry is volume label
|
||||
'T', 'i', 'n', 'y', 'U', 'S', 'B', ' ', 'M', 'S', 'C', 0x08, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4F, 0x6D, 0x65, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
// second entry is readme file
|
||||
'R', 'E', 'A', 'D', 'M', 'E', ' ', ' ', 'T', 'X', 'T', 0x20, 0x00, 0xC6, 0x52, 0x6D,
|
||||
0x65, 0x43, 0x65, 0x43, 0x00, 0x00, 0x88, 0x6D, 0x65, 0x43, 0x02, 0x00,
|
||||
sizeof(README_CONTENTS) - 1, 0x00, 0x00, 0x00 // readme's files size (4 Bytes)
|
||||
},
|
||||
|
||||
//------------- Block3: Readme Content -------------//
|
||||
README_CONTENTS
|
||||
};
|
||||
|
||||
// Invoked when received SCSI_CMD_INQUIRY
|
||||
// Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively
|
||||
void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4])
|
||||
{
|
||||
(void) lun;
|
||||
|
||||
const char vid[] = "TinyUSB";
|
||||
const char pid[] = "Mass Storage";
|
||||
const char rev[] = "1.0";
|
||||
|
||||
memcpy(vendor_id, vid, strlen(vid));
|
||||
memcpy(product_id, pid, strlen(pid));
|
||||
memcpy(product_rev, rev, strlen(rev));
|
||||
}
|
||||
|
||||
// Invoked when received Test Unit Ready command.
|
||||
// return true allowing host to read/write this LUN e.g SD card inserted
|
||||
bool tud_msc_test_unit_ready_cb(uint8_t lun)
|
||||
{
|
||||
(void) lun;
|
||||
|
||||
// RAM disk is ready until ejected
|
||||
if (ejected) {
|
||||
tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Invoked when received SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size
|
||||
// Application update block count and block size
|
||||
void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size)
|
||||
{
|
||||
(void) lun;
|
||||
|
||||
*block_count = DISK_BLOCK_NUM;
|
||||
*block_size = DISK_BLOCK_SIZE;
|
||||
}
|
||||
|
||||
// Invoked when received Start Stop Unit command
|
||||
// - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
|
||||
// - Start = 1 : active mode, if load_eject = 1 : load disk storage
|
||||
bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject)
|
||||
{
|
||||
(void) lun;
|
||||
(void) power_condition;
|
||||
|
||||
if ( load_eject ) {
|
||||
if (start) {
|
||||
// load disk storage
|
||||
} else {
|
||||
// unload disk storage
|
||||
ejected = true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Callback invoked when received READ10 command.
|
||||
// Copy disk's data to buffer (up to bufsize) and return number of copied bytes.
|
||||
int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize)
|
||||
{
|
||||
(void) lun;
|
||||
|
||||
uint8_t const *addr = msc_disk[lba] + offset;
|
||||
memcpy(buffer, addr, bufsize);
|
||||
|
||||
return bufsize;
|
||||
}
|
||||
|
||||
// Callback invoked when received WRITE10 command.
|
||||
// Process data in buffer to disk's storage and return number of written bytes
|
||||
int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize)
|
||||
{
|
||||
(void) lun;
|
||||
|
||||
#ifndef CFG_EXAMPLE_MSC_READONLY
|
||||
uint8_t *addr = msc_disk[lba] + offset;
|
||||
memcpy(addr, buffer, bufsize);
|
||||
#else
|
||||
(void) lba; (void) offset; (void) buffer;
|
||||
#endif
|
||||
|
||||
return bufsize;
|
||||
}
|
||||
|
||||
// Callback invoked when received an SCSI command not in built-in list below
|
||||
// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
|
||||
// - READ10 and WRITE10 has their own callbacks
|
||||
int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, uint16_t bufsize)
|
||||
{
|
||||
// read10 & write10 has their own callback and MUST not be handled here
|
||||
|
||||
void const *response = NULL;
|
||||
uint16_t resplen = 0;
|
||||
|
||||
// most scsi handled is input
|
||||
bool in_xfer = true;
|
||||
|
||||
switch (scsi_cmd[0]) {
|
||||
case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
|
||||
// Host is about to read/write etc ... better not to disconnect disk
|
||||
resplen = 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
// Set Sense = Invalid Command Operation
|
||||
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
|
||||
|
||||
// negative means error -> tinyusb could stall and/or response with failed status
|
||||
resplen = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
// return resplen must not larger than bufsize
|
||||
if ( resplen > bufsize ) {
|
||||
resplen = bufsize;
|
||||
}
|
||||
|
||||
if ( response && (resplen > 0) ) {
|
||||
if (in_xfer) {
|
||||
memcpy(buffer, response, resplen);
|
||||
} else {
|
||||
// SCSI output
|
||||
}
|
||||
}
|
||||
|
||||
return resplen;
|
||||
}
|
||||
#endif /* ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) */
|
||||
|
||||
#endif /* SOC_USB_OTG_SUPPORTED */
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "unity.h"
|
||||
#include "esp_heap_caps.h"
|
||||
|
||||
static size_t before_free_8bit;
|
||||
static size_t before_free_32bit;
|
||||
|
||||
#define TEST_MEMORY_LEAK_THRESHOLD (-530)
|
||||
static void check_leak(size_t before_free, size_t after_free, const char *type)
|
||||
{
|
||||
ssize_t delta = after_free - before_free;
|
||||
printf("MALLOC_CAP_%s: Before %u bytes free, After %u bytes free (delta %d)\n", type, before_free, after_free, delta);
|
||||
TEST_ASSERT_MESSAGE(delta >= TEST_MEMORY_LEAK_THRESHOLD, "memory leak");
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
// ____ ___ ___________________ __ __
|
||||
// | | \/ _____/\______ \ _/ |_ ____ _______/ |_
|
||||
// | | /\_____ \ | | _/ \ __\/ __ \ / ___/\ __\.
|
||||
// | | / / \ | | \ | | \ ___/ \___ \ | |
|
||||
// |______/ /_______ / |______ / |__| \___ >____ > |__|
|
||||
// \/ \/ \/ \/
|
||||
printf(" ____ ___ ___________________ __ __ \r\n");
|
||||
printf("| | \\/ _____/\\______ \\ _/ |_ ____ _______/ |_ \r\n");
|
||||
printf("| | /\\_____ \\ | | _/ \\ __\\/ __ \\ / ___/\\ __\\\r\n");
|
||||
printf("| | / / \\ | | \\ | | \\ ___/ \\___ \\ | | \r\n");
|
||||
printf("|______/ /_______ / |______ / |__| \\___ >____ > |__| \r\n");
|
||||
printf(" \\/ \\/ \\/ \\/ \r\n");
|
||||
|
||||
UNITY_BEGIN();
|
||||
unity_run_menu();
|
||||
UNITY_END();
|
||||
}
|
||||
|
||||
/* setUp runs before every test */
|
||||
void setUp(void)
|
||||
{
|
||||
before_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||
before_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
||||
}
|
||||
|
||||
/* tearDown runs after every test */
|
||||
void tearDown(void)
|
||||
{
|
||||
size_t after_free_8bit = heap_caps_get_free_size(MALLOC_CAP_8BIT);
|
||||
size_t after_free_32bit = heap_caps_get_free_size(MALLOC_CAP_32BIT);
|
||||
check_leak(before_free_8bit, after_free_8bit, "8BIT");
|
||||
check_leak(before_free_32bit, after_free_32bit, "32BIT");
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "esp_idf_version.h"
|
||||
|
||||
enum {
|
||||
// FatFS only allows to format disks with number of blocks greater than 128
|
||||
DISK_BLOCK_NUM = 128 + 1,
|
||||
DISK_BLOCK_SIZE = 512
|
||||
};
|
||||
|
||||
void device_app(void);
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) && SOC_SDMMC_HOST_SUPPORTED
|
||||
void device_app_sdmmc(void);
|
||||
#endif /* ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) && SOC_SDMMC_HOST_SUPPORTED */
|
||||
|
||||
#define README_CONTENTS \
|
||||
"This is tinyusb's MassStorage Class demo.\r\n\r\n\
|
||||
If you find any bugs or get any questions, feel free to file an\r\n\
|
||||
issue at github.com/hathach/tinyusb"
|
|
@ -0,0 +1,549 @@
|
|||
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include "unity.h"
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <inttypes.h>
|
||||
#include "esp_private/usb_phy.h"
|
||||
#include "esp_private/msc_scsi_bot.h"
|
||||
#include "usb/usb_host.h"
|
||||
#include "usb/msc_host_vfs.h"
|
||||
#include "test_common.h"
|
||||
#include "esp_idf_version.h"
|
||||
#include "../private_include/msc_common.h"
|
||||
|
||||
#if SOC_USB_OTG_SUPPORTED
|
||||
|
||||
static const char *TAG = "APP";
|
||||
|
||||
#define ESP_OK_ASSERT(exp) TEST_ASSERT_EQUAL(ESP_OK, exp)
|
||||
|
||||
static esp_vfs_fat_mount_config_t mount_config = {
|
||||
.format_if_mount_failed = true,
|
||||
.max_files = 3,
|
||||
.allocation_unit_size = 1024,
|
||||
};
|
||||
|
||||
static QueueHandle_t app_queue;
|
||||
static SemaphoreHandle_t ready_to_deinit_usb;
|
||||
static msc_host_device_handle_t device;
|
||||
static msc_host_vfs_handle_t vfs_handle;
|
||||
static volatile bool waiting_for_sudden_disconnect;
|
||||
static usb_phy_handle_t phy_hdl = NULL;
|
||||
|
||||
static void force_conn_state(bool connected, TickType_t delay_ticks)
|
||||
{
|
||||
TEST_ASSERT(phy_hdl);
|
||||
if (delay_ticks > 0) {
|
||||
//Delay of 0 ticks causes a yield. So skip if delay_ticks is 0.
|
||||
vTaskDelay(delay_ticks);
|
||||
}
|
||||
ESP_ERROR_CHECK(usb_phy_action(phy_hdl, (connected) ? USB_PHY_ACTION_HOST_ALLOW_CONN : USB_PHY_ACTION_HOST_FORCE_DISCONN));
|
||||
}
|
||||
|
||||
static void msc_event_cb(const msc_host_event_t *event, void *arg)
|
||||
{
|
||||
if (waiting_for_sudden_disconnect) {
|
||||
waiting_for_sudden_disconnect = false;
|
||||
TEST_ASSERT_EQUAL(MSC_DEVICE_DISCONNECTED, event->event);
|
||||
}
|
||||
|
||||
if (event->event == MSC_DEVICE_CONNECTED) {
|
||||
printf("MSC_DEVICE_CONNECTED\n");
|
||||
} else {
|
||||
printf("MSC_DEVICE_DISCONNECTED\n");
|
||||
}
|
||||
|
||||
xQueueSend(app_queue, event, 10);
|
||||
}
|
||||
|
||||
static const char *TEST_STRING = "Hello World!";
|
||||
static const char *FILE_NAME = "/usb/ESP32.txt";
|
||||
|
||||
static void write_read_file(const char *file_path)
|
||||
{
|
||||
char line[64];
|
||||
|
||||
ESP_LOGI(TAG, "Writing file");
|
||||
FILE *f = fopen(file_path, "w");
|
||||
TEST_ASSERT(f);
|
||||
fprintf(f, TEST_STRING);
|
||||
fclose(f);
|
||||
|
||||
ESP_LOGI(TAG, "Reading file");
|
||||
TEST_ASSERT(fopen(file_path, "r"));
|
||||
fgets(line, sizeof(line), f);
|
||||
fclose(f);
|
||||
// strip newline
|
||||
char *pos = strchr(line, '\n');
|
||||
if (pos) {
|
||||
*pos = '\0';
|
||||
}
|
||||
TEST_ASSERT_EQUAL_STRING(line, TEST_STRING);
|
||||
ESP_LOGI(TAG, "Done");
|
||||
}
|
||||
|
||||
static bool file_exists(const char *file_path)
|
||||
{
|
||||
return ( access(file_path, F_OK) == 0 );
|
||||
}
|
||||
|
||||
// Handles common USB host library events
|
||||
static void handle_usb_events(void *args)
|
||||
{
|
||||
uint32_t end_flags = 0;
|
||||
|
||||
while (1) {
|
||||
uint32_t event_flags;
|
||||
usb_host_lib_handle_events(portMAX_DELAY, &event_flags);
|
||||
// Release devices once all clients has deregistered
|
||||
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS) {
|
||||
printf("USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS\n");
|
||||
usb_host_device_free_all();
|
||||
end_flags |= 1;
|
||||
}
|
||||
// Give ready_to_deinit_usb semaphore to indicate that USB Host library
|
||||
// can be deinitialized, and terminate this task.
|
||||
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_ALL_FREE) {
|
||||
printf("USB_HOST_LIB_EVENT_FLAGS_ALL_FREE\n");
|
||||
end_flags |= 2;
|
||||
}
|
||||
|
||||
if (end_flags == 3) {
|
||||
xSemaphoreGive(ready_to_deinit_usb);
|
||||
break;
|
||||
}
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief MSC driver handling task
|
||||
*
|
||||
* This task is only used if the MSC driver was installed with no background task
|
||||
*
|
||||
* @param[in] args Not used
|
||||
*/
|
||||
static void msc_task(void *args)
|
||||
{
|
||||
ESP_LOGI(TAG, "USB MSC handling start");
|
||||
while (msc_host_handle_events(portMAX_DELAY) == ESP_OK) {
|
||||
}
|
||||
ESP_LOGI(TAG, "USB MSC handling stop");
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
static void check_file_content(const char *file_path, const char *expected)
|
||||
{
|
||||
ESP_LOGI(TAG, "Reading %s:", file_path);
|
||||
FILE *file = fopen(file_path, "r");
|
||||
TEST_ASSERT_NOT_NULL_MESSAGE(file, "Could not open file");
|
||||
|
||||
char content[200];
|
||||
size_t read_cnt = fread(content, 1, sizeof(content), file);
|
||||
TEST_ASSERT_EQUAL_MESSAGE(strlen(expected), read_cnt, "Error in reading file");
|
||||
TEST_ASSERT_EQUAL_STRING(content, expected);
|
||||
fclose(file);
|
||||
}
|
||||
#endif /* ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) */
|
||||
|
||||
static void check_sudden_disconnect(void)
|
||||
{
|
||||
uint8_t data[512];
|
||||
const size_t DATA_SIZE = sizeof(data);
|
||||
|
||||
ESP_LOGI(TAG, "Creating test.tx");
|
||||
FILE *file = fopen("/usb/test.txt", "w");
|
||||
TEST_ASSERT(file);
|
||||
|
||||
ESP_LOGI(TAG, "Write data");
|
||||
TEST_ASSERT_EQUAL(DATA_SIZE, fwrite(data, 1, DATA_SIZE, file));
|
||||
TEST_ASSERT_EQUAL(DATA_SIZE, fwrite(data, 1, DATA_SIZE, file));
|
||||
TEST_ASSERT_EQUAL(0, fflush(file));
|
||||
|
||||
ESP_LOGI(TAG, "Trigger a disconnect");
|
||||
//Trigger a disconnect
|
||||
waiting_for_sudden_disconnect = true;
|
||||
force_conn_state(false, 0);
|
||||
|
||||
// Make sure flag was leared in callback
|
||||
vTaskDelay( pdMS_TO_TICKS(100) );
|
||||
TEST_ASSERT_FALSE(waiting_for_sudden_disconnect);
|
||||
|
||||
ESP_LOGI(TAG, "Write data after disconnect");
|
||||
TEST_ASSERT_NOT_EQUAL( DATA_SIZE, fwrite(data, 1, DATA_SIZE, file));
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
static void msc_test_init(void)
|
||||
{
|
||||
BaseType_t task_created;
|
||||
|
||||
ready_to_deinit_usb = xSemaphoreCreateBinary();
|
||||
|
||||
TEST_ASSERT( app_queue = xQueueCreate(5, sizeof(msc_host_event_t)) );
|
||||
|
||||
//Initialize the internal USB PHY to connect to the USB OTG peripheral. We manually install the USB PHY for testing
|
||||
usb_phy_config_t phy_config = {
|
||||
.controller = USB_PHY_CTRL_OTG,
|
||||
.target = USB_PHY_TARGET_INT,
|
||||
.otg_mode = USB_OTG_MODE_HOST,
|
||||
.otg_speed = USB_PHY_SPEED_UNDEFINED, //In Host mode, the speed is determined by the connected device
|
||||
};
|
||||
ESP_OK_ASSERT(usb_new_phy(&phy_config, &phy_hdl));
|
||||
const usb_host_config_t host_config = {
|
||||
.skip_phy_setup = true,
|
||||
.intr_flags = ESP_INTR_FLAG_LEVEL1,
|
||||
};
|
||||
ESP_OK_ASSERT( usb_host_install(&host_config) );
|
||||
|
||||
task_created = xTaskCreatePinnedToCore(handle_usb_events, "usb_events", 2 * 2048, NULL, 2, NULL, 0);
|
||||
TEST_ASSERT(task_created);
|
||||
}
|
||||
|
||||
static void msc_test_wait_and_install_device(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Waiting for USB stick to be connected");
|
||||
msc_host_event_t app_event;
|
||||
xQueueReceive(app_queue, &app_event, portMAX_DELAY);
|
||||
TEST_ASSERT_EQUAL(MSC_DEVICE_CONNECTED, app_event.event);
|
||||
uint8_t device_addr = app_event.device.address;
|
||||
|
||||
ESP_OK_ASSERT( msc_host_install_device(device_addr, &device) );
|
||||
ESP_OK_ASSERT( msc_host_vfs_register(device, "/usb", &mount_config, &vfs_handle) );
|
||||
}
|
||||
|
||||
static void msc_setup(void)
|
||||
{
|
||||
msc_test_init();
|
||||
const msc_host_driver_config_t msc_config = {
|
||||
.create_backround_task = true,
|
||||
.callback = msc_event_cb,
|
||||
.stack_size = 4096,
|
||||
.task_priority = 5,
|
||||
};
|
||||
ESP_OK_ASSERT( msc_host_install(&msc_config) );
|
||||
msc_test_wait_and_install_device();
|
||||
}
|
||||
|
||||
static void msc_test_uninstall_device(void)
|
||||
{
|
||||
ESP_OK_ASSERT( msc_host_vfs_unregister(vfs_handle) );
|
||||
ESP_OK_ASSERT( msc_host_uninstall_device(device) );
|
||||
}
|
||||
|
||||
static void msc_test_deinit(void)
|
||||
{
|
||||
ESP_OK_ASSERT( msc_host_uninstall() );
|
||||
|
||||
xSemaphoreTake(ready_to_deinit_usb, portMAX_DELAY);
|
||||
vSemaphoreDelete(ready_to_deinit_usb);
|
||||
vTaskDelay(10); // Wait to finish any ongoing USB operations
|
||||
ESP_OK_ASSERT( usb_host_uninstall() );
|
||||
//Tear down USB PHY
|
||||
ESP_OK_ASSERT(usb_del_phy(phy_hdl));
|
||||
phy_hdl = NULL;
|
||||
|
||||
vQueueDelete(app_queue);
|
||||
vTaskDelay(10); // Wait for FreeRTOS to clean up deleted tasks
|
||||
}
|
||||
|
||||
static void msc_teardown(void)
|
||||
{
|
||||
msc_test_uninstall_device();
|
||||
msc_test_deinit();
|
||||
}
|
||||
|
||||
static void write_read_sectors(void)
|
||||
{
|
||||
uint8_t write_data[DISK_BLOCK_SIZE];
|
||||
uint8_t read_data[DISK_BLOCK_SIZE];
|
||||
|
||||
memset(write_data, 0x55, DISK_BLOCK_SIZE);
|
||||
memset(read_data, 0, DISK_BLOCK_SIZE);
|
||||
|
||||
scsi_cmd_write10(device, write_data, 10, 1, DISK_BLOCK_SIZE);
|
||||
scsi_cmd_read10(device, read_data, 10, 1, DISK_BLOCK_SIZE);
|
||||
|
||||
TEST_ASSERT_EQUAL_MEMORY(write_data, read_data, DISK_BLOCK_SIZE);
|
||||
}
|
||||
|
||||
static void erase_storage(void)
|
||||
{
|
||||
uint8_t data[DISK_BLOCK_SIZE];
|
||||
memset(data, 0xFF, DISK_BLOCK_SIZE);
|
||||
|
||||
for (int block = 0; block < DISK_BLOCK_NUM; block++) {
|
||||
scsi_cmd_write10(device, data, block, 1, DISK_BLOCK_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("write_and_read_file", "[usb_msc]")
|
||||
{
|
||||
msc_setup();
|
||||
write_read_file(FILE_NAME);
|
||||
msc_teardown();
|
||||
}
|
||||
|
||||
TEST_CASE("sudden_disconnect", "[usb_msc]")
|
||||
{
|
||||
msc_setup();
|
||||
check_sudden_disconnect();
|
||||
msc_teardown();
|
||||
}
|
||||
|
||||
TEST_CASE("sectors_can_be_written_and_read", "[usb_msc]")
|
||||
{
|
||||
msc_setup();
|
||||
write_read_sectors();
|
||||
msc_teardown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check README content
|
||||
*
|
||||
* This test strictly requires our implementation of USB MSC Mock device.
|
||||
* This test will fail for usualW flash drives, as they don't have README.TXT file on them.
|
||||
*/
|
||||
#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0)
|
||||
TEST_CASE("check_README_content", "[usb_msc]")
|
||||
{
|
||||
msc_setup();
|
||||
check_file_content("/usb/README.TXT", README_CONTENTS);
|
||||
msc_teardown();
|
||||
}
|
||||
#endif /* ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) */
|
||||
|
||||
esp_err_t bot_execute_command(msc_device_t *device, uint8_t *cbw, void *data, size_t size);
|
||||
/**
|
||||
* @brief Error recovery testcase
|
||||
*
|
||||
* Various error cases:
|
||||
* - Accessing non-existent memory
|
||||
* - Invalid SCSI command
|
||||
* - USB transfer STALL
|
||||
*/
|
||||
TEST_CASE("error_recovery_1", "[usb_msc][ignore]")
|
||||
{
|
||||
msc_setup();
|
||||
uint8_t data[DISK_BLOCK_SIZE];
|
||||
esp_err_t err;
|
||||
|
||||
// Some flash disks will respond with stall, some with error in CSW, some with timeout
|
||||
printf("invalid bot command\n");
|
||||
uint32_t dummy_cbw[8] = {0x5342555, 2, 0, 0x55555555};
|
||||
err = bot_execute_command(device, (uint8_t *)dummy_cbw, data, 31);
|
||||
TEST_ASSERT_NOT_EQUAL(ESP_OK, err);
|
||||
|
||||
err = msc_host_reset_recovery(device);
|
||||
TEST_ASSERT_EQUAL(ESP_OK, err);
|
||||
|
||||
// Make sure we can read/write after the error was cleared
|
||||
printf("read write after reset\n");
|
||||
write_read_file(FILE_NAME);
|
||||
|
||||
msc_teardown();
|
||||
}
|
||||
|
||||
TEST_CASE("error_recovery_2", "[usb_msc][ignore]")
|
||||
{
|
||||
msc_setup();
|
||||
uint8_t data[DISK_BLOCK_SIZE];
|
||||
esp_err_t err;
|
||||
|
||||
// Write to and read from invalid sector
|
||||
// Some flash disks will respond with stall, some with error in CSW, some with timeout
|
||||
printf("read 10\n");
|
||||
err = scsi_cmd_read10(device, data, UINT32_MAX, 1, DISK_BLOCK_SIZE);
|
||||
TEST_ASSERT_NOT_EQUAL(ESP_OK, err);
|
||||
err = msc_host_reset_recovery(device);
|
||||
TEST_ASSERT_EQUAL(ESP_OK, err);
|
||||
printf("read write after reset\n");
|
||||
write_read_file(FILE_NAME);
|
||||
|
||||
msc_teardown();
|
||||
}
|
||||
|
||||
#define MAX_BUFFER_SIZE (1024 + 1) // Maximum buffer size for this test
|
||||
#define SETVBUF_TEST(_size) do { \
|
||||
printf("setvbuf %d\n", _size); \
|
||||
int err = setvbuf(file, NULL, _IOFBF, _size); \
|
||||
TEST_ASSERT_EQUAL_MESSAGE(0, err, "setvbuf failed"); \
|
||||
err = fseek(file, SEEK_SET, 0); \
|
||||
TEST_ASSERT_EQUAL_MESSAGE(0, err, "fseek failed"); \
|
||||
size_t write_cnt = fwrite(write_buf, 1, _size, file); \
|
||||
TEST_ASSERT_EQUAL(_size, write_cnt); \
|
||||
err = fseek(file, SEEK_SET, 0); \
|
||||
TEST_ASSERT_EQUAL_MESSAGE(0, err, "fseek failed"); \
|
||||
memset(read_buf, 0, MAX_BUFFER_SIZE + 1); \
|
||||
size_t read_cnt = fread(read_buf, 1, _size, file); \
|
||||
TEST_ASSERT_EQUAL_MESSAGE(_size, read_cnt, "Error in reading file"); \
|
||||
TEST_ASSERT_EQUAL_HEX8_ARRAY(write_buf, read_buf, _size); \
|
||||
TEST_ASSERT_EQUAL_MESSAGE(0, read_buf[_size], "Read buffer accessed outside of its boundaries"); \
|
||||
} while(0); \
|
||||
|
||||
/**
|
||||
* @brief setvbuf testcase
|
||||
*
|
||||
* From v1.1.0 the MSC driver reuses buffer from VFS for USB transfers.
|
||||
* Check that this feature works correctly with various buffer lengths.
|
||||
*/
|
||||
TEST_CASE("setvbuf", "[usb_msc]")
|
||||
{
|
||||
msc_setup();
|
||||
|
||||
FILE *file = fopen("/usb/test", "w+");
|
||||
TEST_ASSERT_NOT_NULL_MESSAGE(file, "Could not open file for writing");
|
||||
|
||||
char *write_buf = malloc(MAX_BUFFER_SIZE);
|
||||
char *read_buf = malloc(MAX_BUFFER_SIZE + 1); // 1 canary byte
|
||||
TEST_ASSERT_NOT_NULL(write_buf);
|
||||
TEST_ASSERT_NOT_NULL(read_buf);
|
||||
|
||||
// Initialize write buffer with some data
|
||||
for (int i = 0; i < MAX_BUFFER_SIZE; i++) {
|
||||
write_buf[i] = i & 0xFF;
|
||||
}
|
||||
|
||||
SETVBUF_TEST(64);
|
||||
SETVBUF_TEST(128);
|
||||
SETVBUF_TEST(500);
|
||||
SETVBUF_TEST(1000);
|
||||
SETVBUF_TEST(1023);
|
||||
SETVBUF_TEST(1024);
|
||||
SETVBUF_TEST(MAX_BUFFER_SIZE);
|
||||
|
||||
fclose(file);
|
||||
free(write_buf);
|
||||
free(read_buf);
|
||||
|
||||
msc_teardown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USB MSC 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", "[usb_msc]")
|
||||
{
|
||||
printf("Create file\n");
|
||||
msc_setup();
|
||||
write_read_file(FILE_NAME);
|
||||
msc_teardown();
|
||||
|
||||
printf("File exists after mounting again\n");
|
||||
msc_setup();
|
||||
TEST_ASSERT(file_exists(FILE_NAME));
|
||||
printf("Erase storage device\n");
|
||||
erase_storage();
|
||||
msc_teardown();
|
||||
|
||||
printf("Check file does not exist after formatting\n");
|
||||
mount_config.format_if_mount_failed = true;
|
||||
msc_setup();
|
||||
TEST_ASSERT_FALSE(file_exists(FILE_NAME));
|
||||
msc_teardown();
|
||||
mount_config.format_if_mount_failed = false;
|
||||
}
|
||||
|
||||
static void print_device_info(msc_host_device_info_t *info)
|
||||
{
|
||||
const size_t megabyte = 1024 * 1024;
|
||||
uint64_t capacity = ((uint64_t)info->sector_size * info->sector_count) / megabyte;
|
||||
|
||||
printf("Device info:\n");
|
||||
printf("\t Capacity: %llu MB\n", capacity);
|
||||
printf("\t Sector size: %"PRIu32"\n", info->sector_size);
|
||||
printf("\t Sector count: %"PRIu32"\n", info->sector_count);
|
||||
printf("\t PID: 0x%4X \n", info->idProduct);
|
||||
printf("\t VID: 0x%4X \n", info->idVendor);
|
||||
wprintf(L"\t iProduct: %S \n", info->iProduct);
|
||||
wprintf(L"\t iManufacturer: %S \n", info->iManufacturer);
|
||||
wprintf(L"\t iSerialNumber: %S \n", info->iSerialNumber);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USB MSC device_info testcase
|
||||
*
|
||||
* Simple testcase that only runs msc_host_get_device_info()
|
||||
* To make sure that we correctly handle missing string descriptors
|
||||
*/
|
||||
TEST_CASE("device_info", "[usb_msc]")
|
||||
{
|
||||
msc_setup();
|
||||
msc_host_device_info_t info;
|
||||
esp_err_t err = msc_host_get_device_info(device, &info);
|
||||
msc_teardown();
|
||||
TEST_ASSERT_EQUAL(ESP_OK, err);
|
||||
print_device_info(&info);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USB MSC driver with no background task
|
||||
*
|
||||
* Install the driver without background task
|
||||
* and make sure that everything works
|
||||
*/
|
||||
TEST_CASE("no_background_task", "[usb_msc]")
|
||||
{
|
||||
msc_test_init();
|
||||
const msc_host_driver_config_t msc_config = {
|
||||
.create_backround_task = false,
|
||||
.callback = msc_event_cb,
|
||||
.stack_size = 4096,
|
||||
.task_priority = 5,
|
||||
};
|
||||
ESP_OK_ASSERT( msc_host_install(&msc_config) );
|
||||
BaseType_t task_created = xTaskCreatePinnedToCore(msc_task, "msc_events", 2 * 2048, NULL, 2, NULL, 0);
|
||||
TEST_ASSERT(task_created);
|
||||
msc_test_wait_and_install_device();
|
||||
write_read_sectors(); // Do some dummy operations
|
||||
msc_teardown();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USB MSC driver with no background task
|
||||
*
|
||||
* Install and uninstall the driver without background task
|
||||
* without ever calling usb_host_client_handle_events()
|
||||
*/
|
||||
TEST_CASE("no_background_task_2", "[usb_msc]")
|
||||
{
|
||||
msc_test_init();
|
||||
const msc_host_driver_config_t msc_config = {
|
||||
.create_backround_task = false,
|
||||
.callback = msc_event_cb,
|
||||
};
|
||||
ESP_OK_ASSERT( msc_host_install(&msc_config) );
|
||||
|
||||
vTaskDelay(100); // Give USB Host Library some time for device enumeration
|
||||
|
||||
msc_test_deinit();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief USB MSC Device Mock
|
||||
*
|
||||
* We use this 'testcase' to provide MSC mock device for our DUT
|
||||
*/
|
||||
TEST_CASE("mock_device_app", "[usb_msc_device][ignore]")
|
||||
{
|
||||
device_app();
|
||||
}
|
||||
|
||||
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) && SOC_SDMMC_HOST_SUPPORTED
|
||||
TEST_CASE("mock_device_app", "[usb_msc_device_sdmmc][ignore]")
|
||||
{
|
||||
device_app_sdmmc();
|
||||
}
|
||||
#endif /* ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) && SOC_SDMMC_HOST_SUPPORTED */
|
||||
|
||||
#endif /* SOC_USB_OTG_SUPPORTED */
|
|
@ -0,0 +1,6 @@
|
|||
# Name, Type, SubType, Offset, Size, Flags
|
||||
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
|
||||
nvs, data, nvs, 0x9000, 0x6000,
|
||||
phy_init, data, phy, 0xf000, 0x1000,
|
||||
factory, app, factory, 0x10000, 1M,
|
||||
storage, data, fat, , 1M,
|
|
|
@ -0,0 +1,26 @@
|
|||
# SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
from typing import Tuple
|
||||
|
||||
import pytest
|
||||
from pytest_embedded_idf.dut import IdfDut
|
||||
|
||||
|
||||
@pytest.mark.esp32s2
|
||||
@pytest.mark.esp32s3
|
||||
@pytest.mark.usb_host
|
||||
@pytest.mark.parametrize('count', [
|
||||
2,
|
||||
], indirect=True)
|
||||
def test_usb_host_msc(dut: Tuple[IdfDut, IdfDut]) -> None:
|
||||
device = dut[0]
|
||||
host = dut[1]
|
||||
|
||||
# 2.1 Prepare USB device for MSC test
|
||||
device.expect_exact('Press ENTER to see the list of tests.')
|
||||
device.write('[usb_msc_device]')
|
||||
device.expect_exact('USB initialization DONE')
|
||||
|
||||
# 2.2 Run MSC test
|
||||
host.run_all_single_board_cases(group='usb_msc')
|
|
@ -0,0 +1,27 @@
|
|||
# Configure TinyUSB, it will be used to mock USB devices
|
||||
CONFIG_TINYUSB_MSC_ENABLED=y
|
||||
CONFIG_TINYUSB_CDC_ENABLED=n
|
||||
CONFIG_TINYUSB_CDC_COUNT=0
|
||||
CONFIG_TINYUSB_HID_COUNT=0
|
||||
|
||||
# Disable watchdogs, they'd get triggered during unity interactive menu
|
||||
CONFIG_ESP_INT_WDT=n
|
||||
CONFIG_ESP_TASK_WDT=n
|
||||
|
||||
# Run-time checks of Heap and Stack
|
||||
CONFIG_HEAP_POISONING_COMPREHENSIVE=y
|
||||
CONFIG_COMPILER_STACK_CHECK_MODE_STRONG=y
|
||||
CONFIG_COMPILER_STACK_CHECK=y
|
||||
|
||||
CONFIG_UNITY_ENABLE_BACKTRACE_ON_FAIL=y
|
||||
|
||||
CONFIG_COMPILER_CXX_EXCEPTIONS=y
|
||||
|
||||
CONFIG_PARTITION_TABLE_CUSTOM=y
|
||||
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
|
||||
CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
|
||||
CONFIG_WL_SECTOR_SIZE_512=y
|
||||
CONFIG_WL_SECTOR_MODE_PERF=y
|
||||
|
||||
CONFIG_FATFS_LFN_HEAP=y
|
Loading…
Add table
Add a link
Reference in a new issue