2021-07-19 10:58:08 -04:00
|
|
|
/*
|
2022-04-19 06:20:07 -04:00
|
|
|
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
2021-07-19 10:58:08 -04:00
|
|
|
*
|
2022-04-19 06:20:07 -04:00
|
|
|
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
2021-07-19 10:58:08 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2022-04-19 06:20:07 -04:00
|
|
|
#include "freertos/event_groups.h"
|
2021-07-19 10:58:08 -04:00
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "usb/usb_host.h"
|
|
|
|
#include "msc_host.h"
|
|
|
|
#include "msc_host_vfs.h"
|
|
|
|
#include "ffconf.h"
|
|
|
|
#include "esp_vfs.h"
|
|
|
|
#include "errno.h"
|
|
|
|
#include "hal/usb_hal.h"
|
2022-04-19 06:20:07 -04:00
|
|
|
#include "driver/gpio.h"
|
|
|
|
#include <esp_vfs_fat.h>
|
2021-07-19 10:58:08 -04:00
|
|
|
|
2022-04-19 06:20:07 -04:00
|
|
|
#define USB_DISCONNECT_PIN GPIO_NUM_10
|
2021-07-19 10:58:08 -04:00
|
|
|
|
2022-04-19 06:20:07 -04:00
|
|
|
#define READY_TO_UNINSTALL (HOST_NO_CLIENT | HOST_ALL_FREE)
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
HOST_NO_CLIENT = 0x1,
|
|
|
|
HOST_ALL_FREE = 0x2,
|
|
|
|
DEVICE_CONNECTED = 0x4,
|
|
|
|
DEVICE_DISCONNECTED = 0x8,
|
|
|
|
DEVICE_ADDRESS_MASK = 0xFF0,
|
|
|
|
} app_event_t;
|
|
|
|
|
|
|
|
static const char *TAG = "example";
|
|
|
|
static EventGroupHandle_t usb_flags;
|
2021-07-19 10:58:08 -04:00
|
|
|
|
|
|
|
static void msc_event_cb(const msc_host_event_t *event, void *arg)
|
|
|
|
{
|
|
|
|
if (event->event == MSC_DEVICE_CONNECTED) {
|
|
|
|
ESP_LOGI(TAG, "MSC device connected");
|
2022-04-19 06:20:07 -04:00
|
|
|
// Obtained USB device address is placed after application events
|
|
|
|
xEventGroupSetBits(usb_flags, DEVICE_CONNECTED | (event->device.address << 4));
|
2021-07-19 10:58:08 -04:00
|
|
|
} else if (event->event == MSC_DEVICE_DISCONNECTED) {
|
2022-04-19 06:20:07 -04:00
|
|
|
xEventGroupSetBits(usb_flags, DEVICE_DISCONNECTED);
|
2021-07-19 10:58:08 -04:00
|
|
|
ESP_LOGI(TAG, "MSC device disconnected");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-08-11 09:45:01 -04:00
|
|
|
printf("\t Sector size: %"PRIu32"\n", info->sector_size);
|
|
|
|
printf("\t Sector count: %"PRIu32"\n", info->sector_count);
|
2021-07-19 10:58:08 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:20:07 -04:00
|
|
|
static bool file_exists(const char *file_path)
|
|
|
|
{
|
|
|
|
struct stat buffer;
|
|
|
|
return stat(file_path, &buffer) == 0;
|
|
|
|
}
|
|
|
|
|
2021-07-19 10:58:08 -04:00
|
|
|
static void file_operations(void)
|
|
|
|
{
|
|
|
|
const char *directory = "/usb/esp";
|
|
|
|
const char *file_path = "/usb/esp/test.txt";
|
|
|
|
|
|
|
|
struct stat s = {0};
|
|
|
|
bool directory_exists = stat(directory, &s) == 0;
|
|
|
|
if (!directory_exists) {
|
|
|
|
if (mkdir(directory, 0775) != 0) {
|
|
|
|
ESP_LOGE(TAG, "mkdir failed with errno: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:20:07 -04:00
|
|
|
if (!file_exists(file_path)) {
|
|
|
|
ESP_LOGI(TAG, "Creating file");
|
|
|
|
FILE *f = fopen(file_path, "w");
|
|
|
|
if (f == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Failed to open file for writing");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fprintf(f, "Hello World!\n");
|
|
|
|
fclose(f);
|
2021-07-19 10:58:08 -04:00
|
|
|
}
|
|
|
|
|
2022-04-19 06:20:07 -04:00
|
|
|
FILE *f;
|
2021-07-19 10:58:08 -04:00
|
|
|
ESP_LOGI(TAG, "Reading file");
|
|
|
|
f = fopen(file_path, "r");
|
|
|
|
if (f == NULL) {
|
|
|
|
ESP_LOGE(TAG, "Failed to open file for reading");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
char line[64];
|
|
|
|
fgets(line, sizeof(line), f);
|
|
|
|
fclose(f);
|
|
|
|
// strip newline
|
|
|
|
char *pos = strchr(line, '\n');
|
|
|
|
if (pos) {
|
|
|
|
*pos = '\0';
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Read from file: '%s'", line);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handles common USB host library events
|
|
|
|
static void handle_usb_events(void *args)
|
|
|
|
{
|
|
|
|
while (1) {
|
|
|
|
uint32_t event_flags;
|
|
|
|
usb_host_lib_handle_events(portMAX_DELAY, &event_flags);
|
2022-04-19 06:20:07 -04:00
|
|
|
|
2021-07-19 10:58:08 -04:00
|
|
|
// Release devices once all clients has deregistered
|
|
|
|
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS) {
|
|
|
|
usb_host_device_free_all();
|
2022-04-19 06:20:07 -04:00
|
|
|
xEventGroupSetBits(usb_flags, HOST_NO_CLIENT);
|
2021-07-19 10:58:08 -04:00
|
|
|
}
|
|
|
|
// Give ready_to_uninstall_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) {
|
2022-04-19 06:20:07 -04:00
|
|
|
xEventGroupSetBits(usb_flags, HOST_ALL_FREE);
|
2021-07-19 10:58:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t wait_for_msc_device(void)
|
|
|
|
{
|
2022-04-19 06:20:07 -04:00
|
|
|
EventBits_t event;
|
|
|
|
|
2021-07-19 10:58:08 -04:00
|
|
|
ESP_LOGI(TAG, "Waiting for USB stick to be connected");
|
2022-04-19 06:20:07 -04:00
|
|
|
event = xEventGroupWaitBits(usb_flags, DEVICE_CONNECTED | DEVICE_ADDRESS_MASK,
|
|
|
|
pdTRUE, pdFALSE, portMAX_DELAY);
|
|
|
|
ESP_LOGI(TAG, "connection...");
|
|
|
|
// Extract USB device address from event group bits
|
|
|
|
return (event & DEVICE_ADDRESS_MASK) >> 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool wait_for_event(EventBits_t event, TickType_t timeout)
|
|
|
|
{
|
|
|
|
return xEventGroupWaitBits(usb_flags, event, pdTRUE, pdTRUE, timeout) & event;
|
2021-07-19 10:58:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void app_main(void)
|
|
|
|
{
|
|
|
|
msc_host_device_handle_t msc_device;
|
2022-04-19 06:20:07 -04:00
|
|
|
msc_host_vfs_handle_t vfs_handle;
|
|
|
|
msc_host_device_info_t info;
|
2021-07-19 10:58:08 -04:00
|
|
|
BaseType_t task_created;
|
|
|
|
|
2022-04-19 06:20:07 -04:00
|
|
|
const gpio_config_t input_pin = {
|
2022-08-11 09:45:01 -04:00
|
|
|
.pin_bit_mask = BIT64(USB_DISCONNECT_PIN),
|
2022-04-19 06:20:07 -04:00
|
|
|
.mode = GPIO_MODE_INPUT,
|
|
|
|
.pull_up_en = GPIO_PULLUP_ENABLE,
|
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK( gpio_config(&input_pin) );
|
2021-07-19 10:58:08 -04:00
|
|
|
|
2022-04-19 06:20:07 -04:00
|
|
|
usb_flags = xEventGroupCreate();
|
|
|
|
assert(usb_flags);
|
2021-07-19 10:58:08 -04:00
|
|
|
|
2022-04-19 06:20:07 -04:00
|
|
|
const usb_host_config_t host_config = { .intr_flags = ESP_INTR_FLAG_LEVEL1 };
|
2021-07-19 10:58:08 -04:00
|
|
|
ESP_ERROR_CHECK( usb_host_install(&host_config) );
|
|
|
|
task_created = xTaskCreate(handle_usb_events, "usb_events", 2048, NULL, 2, NULL);
|
|
|
|
assert(task_created);
|
|
|
|
|
|
|
|
const msc_host_driver_config_t msc_config = {
|
|
|
|
.create_backround_task = true,
|
|
|
|
.task_priority = 5,
|
|
|
|
.stack_size = 2048,
|
|
|
|
.callback = msc_event_cb,
|
|
|
|
};
|
|
|
|
ESP_ERROR_CHECK( msc_host_install(&msc_config) );
|
|
|
|
|
|
|
|
const esp_vfs_fat_mount_config_t mount_config = {
|
|
|
|
.format_if_mount_failed = false,
|
|
|
|
.max_files = 3,
|
|
|
|
.allocation_unit_size = 1024,
|
|
|
|
};
|
|
|
|
|
2022-04-19 06:20:07 -04:00
|
|
|
do {
|
|
|
|
uint8_t device_address = wait_for_msc_device();
|
2021-07-19 10:58:08 -04:00
|
|
|
|
2022-04-19 06:20:07 -04:00
|
|
|
ESP_ERROR_CHECK( msc_host_install_device(device_address, &msc_device) );
|
2021-07-19 10:58:08 -04:00
|
|
|
|
2022-04-19 06:20:07 -04:00
|
|
|
msc_host_print_descriptors(msc_device);
|
2021-07-19 10:58:08 -04:00
|
|
|
|
2022-04-19 06:20:07 -04:00
|
|
|
ESP_ERROR_CHECK( msc_host_get_device_info(msc_device, &info) );
|
|
|
|
print_device_info(&info);
|
|
|
|
|
|
|
|
ESP_ERROR_CHECK( msc_host_vfs_register(msc_device, "/usb", &mount_config, &vfs_handle) );
|
|
|
|
|
|
|
|
while (!wait_for_event(DEVICE_DISCONNECTED, 200)) {
|
|
|
|
file_operations();
|
|
|
|
}
|
2021-07-19 10:58:08 -04:00
|
|
|
|
2022-04-19 06:20:07 -04:00
|
|
|
xEventGroupClearBits(usb_flags, READY_TO_UNINSTALL);
|
|
|
|
ESP_ERROR_CHECK( msc_host_vfs_unregister(vfs_handle) );
|
|
|
|
ESP_ERROR_CHECK( msc_host_uninstall_device(msc_device) );
|
|
|
|
|
|
|
|
} while (gpio_get_level(USB_DISCONNECT_PIN) != 0);
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Uninitializing USB ...");
|
|
|
|
ESP_ERROR_CHECK( msc_host_uninstall() );
|
|
|
|
wait_for_event(READY_TO_UNINSTALL, portMAX_DELAY);
|
|
|
|
ESP_ERROR_CHECK( usb_host_uninstall() );
|
2021-07-19 10:58:08 -04:00
|
|
|
ESP_LOGI(TAG, "Done");
|
|
|
|
}
|