Compare commits

...

2 commits

Author SHA1 Message Date
Patrick Moessler
cf9bc9bdab add psram tinyusb drive 2024-06-13 13:11:43 +02:00
Patrick Moessler
b6af2508f1 correct SPIRAM lines mode 2024-06-13 13:09:07 +02:00
4 changed files with 667 additions and 599 deletions

View file

@ -7,9 +7,11 @@
#include <errno.h> #include <errno.h>
#include <dirent.h> #include <dirent.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "esp_heap_caps.h"
#include "esp_partition.h" #include "esp_partition.h"
#include "esp_check.h" #include "esp_check.h"
#include "tinyusb.h" #include "tinyusb.h"
#include "tusb_msc_psram.h"
#include "tusb_msc_storage.h" #include "tusb_msc_storage.h"
#include "tusb_cdc_acm.h" #include "tusb_cdc_acm.h"
@ -92,31 +94,41 @@ static void file_operations(void)
ESP_LOGI(TAG, "Read from file: '%s'", line); ESP_LOGI(TAG, "Read from file: '%s'", line);
} }
static esp_err_t storage_init_spiflash(wl_handle_t *wl_handle) // static esp_err_t storage_init_spiflash(wl_handle_t *wl_handle)
{ // {
ESP_LOGI(TAG, "Initializing wear levelling"); // 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); // const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_FAT, NULL);
if (data_partition == NULL) { // if (data_partition == NULL) {
ESP_LOGE(TAG, "Failed to find FATFS partition. Check the partition table."); // ESP_LOGE(TAG, "Failed to find FATFS partition. Check the partition table.");
return ESP_ERR_NOT_FOUND; // return ESP_ERR_NOT_FOUND;
} // }
return wl_mount(data_partition, wl_handle); // return wl_mount(data_partition, wl_handle);
} // }
void app_main(void) void app_main(void)
{ {
ESP_LOGI(TAG, "Initializing storage..."); ESP_LOGI(TAG, "Initializing storage...");
static wl_handle_t wl_handle = WL_INVALID_HANDLE; uint8_t* const psram_fs_mem = heap_caps_malloc(4*1024*1024, MALLOC_CAP_SPIRAM);
ESP_ERROR_CHECK(storage_init_spiflash(&wl_handle));
const tinyusb_msc_spiflash_config_t config_spi = { const tinyusb_msc_psram_config_t config_psram = {
.wl_handle = wl_handle .memory=psram_fs_mem,
.memory_size = 4*1024*1024
}; };
ESP_ERROR_CHECK(tinyusb_msc_storage_init_spiflash(&config_spi));
ESP_ERROR_CHECK(tinyusb_msc_storage_mount(BASE_PATH)); ESP_ERROR_CHECK(tinyusb_msc_storage_init_psram(&config_psram));
ESP_ERROR_CHECK(tinyusb_msc_psram_mount(BASE_PATH));
// static wl_handle_t wl_handle = WL_INVALID_HANDLE;
// ESP_ERROR_CHECK(storage_init_spiflash(&wl_handle));
// const tinyusb_msc_spiflash_config_t config_spi = {
// .wl_handle = wl_handle
// };
// ESP_ERROR_CHECK(tinyusb_msc_storage_init_spiflash(&config_spi));
// ESP_ERROR_CHECK(tinyusb_msc_storage_mount(BASE_PATH));
file_operations(); file_operations();
ESP_LOGI(TAG, "USB Composite initialization"); ESP_LOGI(TAG, "USB Composite initialization");

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,126 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
#include <stdint.h>
#include <tusb_msc_storage.h>
/**
* @brief Configuration structure for psram initialization
*
* User configurable parameters that are used while
* initializing the PSRAM media.
*/
typedef struct {
uint8_t* memory; /*!< pointer to memory in PSRAM */
size_t memory_size; /*!< requested size. note: allocated file system may be smaller due to sector size */
tusb_msc_callback_t callback_mount_changed; /*!< Pointer to the function callback that will be delivered AFTER mount/unmount operation is successfully finished */
tusb_msc_callback_t callback_premount_changed; /*!< Pointer to the function callback that will be delivered BEFORE mount/unmount operation is started */
const esp_vfs_fat_mount_config_t mount_config; /*!< FATFS mount config */
} tinyusb_msc_psram_config_t;
/**
* @brief Register storage type psram with tinyusb driver
*
* @param config pointer to the psram configuration
* @return esp_err_t
* - ESP_OK, if success;
* - ESP_ERR_NO_MEM, if there was no memory to allocate storage components;
*/
esp_err_t tinyusb_msc_storage_init_psram(const tinyusb_msc_psram_config_t *config);
/**
* @brief Deregister storage with tinyusb driver and frees the memory
*
*/
void tinyusb_msc_psram_deinit(void);
/**
* @brief Register a callback invoking on MSC event. If the callback had been
* already registered, it will be overwritten
*
* @param event_type - type of registered event for a callback
* @param callback - callback function
* @return esp_err_t - ESP_OK or ESP_ERR_INVALID_ARG
*/
esp_err_t tinyusb_msc_register_callback(tinyusb_msc_event_type_t event_type,
tusb_msc_callback_t callback);
/**
* @brief Unregister a callback invoking on MSC event.
*
* @param event_type - type of registered event for a callback
* @return esp_err_t - ESP_OK or ESP_ERR_INVALID_ARG
*/
esp_err_t tinyusb_msc_unregister_callback(tinyusb_msc_event_type_t event_type);
/**
* @brief Mount the storage partition locally on the firmware application.
*
* Get the available drive number. Register spi flash partition.
* Connect POSIX and C standard library IO function with FATFS.
* Mounts the partition.
* This API is used by the firmware application. If the storage partition is
* mounted by this API, host (PC) can't access the storage via MSC.
* When this function is called from the tinyusb callback functions, care must be taken
* so as to make sure that user callbacks must be completed within a
* specific time. Otherwise, MSC device may re-appear again on Host.
*
* @param base_path path prefix where FATFS should be registered
* @return esp_err_t
* - ESP_OK, if success;
* - ESP_ERR_NOT_FOUND if the maximum count of volumes is already mounted
* - ESP_ERR_NO_MEM if not enough memory or too many VFSes already registered;
*/
esp_err_t tinyusb_msc_psram_mount(const char *base_path);
/**
* @brief Unmount the storage partition from the firmware application.
*
* Unmount the partition. Unregister diskio driver.
* Unregister the SPI flash partition.
* Finally, Un-register FATFS from VFS.
* After this function is called, storage device can be seen (recognized) by host (PC).
* When this function is called from the tinyusb callback functions, care must be taken
* so as to make sure that user callbacks must be completed within a specific time.
* Otherwise, MSC device may not appear on Host.
*
* @return esp_err_t
* - ESP_OK on success
* - ESP_ERR_INVALID_STATE if FATFS is not registered in VFS
*/
esp_err_t tinyusb_msc_psram_unmount(void);
/**
* @brief Get number of sectors in storage media
*
* @return usable size, in bytes
*/
uint32_t tinyusb_msc_psram_get_sector_count(void);
/**
* @brief Get sector size of storage media
*
* @return sector count
*/
uint32_t tinyusb_msc_psram_get_sector_size(void);
/**
* @brief Get status if storage media is exposed over USB to Host
*
* @return bool
* - true, if the storage media is exposed to Host
* - false, if the stoarge media is mounted on application (not exposed to Host)
*/
bool tinyusb_msc_psram_in_use_by_usb_host(void);
#ifdef __cplusplus
}
#endif

View file

@ -5,6 +5,7 @@ CONFIG_IDF_TARGET="esp32s3"
CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y CONFIG_ESPTOOLPY_FLASHSIZE_16MB=y
CONFIG_PARTITION_TABLE_CUSTOM=y CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_SPIRAM=y CONFIG_SPIRAM=y
CONFIG_SPIRAM_MODE_OCT=y
CONFIG_SPIRAM_USE_CAPS_ALLOC=y CONFIG_SPIRAM_USE_CAPS_ALLOC=y
CONFIG_WL_SECTOR_SIZE_512=y CONFIG_WL_SECTOR_SIZE_512=y
CONFIG_WL_SECTOR_MODE_PERF=y CONFIG_WL_SECTOR_MODE_PERF=y