From bb570e02d6a8b34fe00c90f5cd8ad736d990ed42 Mon Sep 17 00:00:00 2001 From: hathach Date: Sat, 19 Nov 2022 13:44:07 +0700 Subject: msc explorer ls work great --- examples/host/msc_file_explorer/src/main.c | 2 +- examples/host/msc_file_explorer/src/msc_app.c | 216 +++++++++++++++++++--- examples/host/msc_file_explorer/src/tusb_config.h | 5 +- 3 files changed, 194 insertions(+), 29 deletions(-) (limited to 'examples') diff --git a/examples/host/msc_file_explorer/src/main.c b/examples/host/msc_file_explorer/src/main.c index 13c473d9b..f46e5092b 100644 --- a/examples/host/msc_file_explorer/src/main.c +++ b/examples/host/msc_file_explorer/src/main.c @@ -43,7 +43,7 @@ int main(void) { board_init(); - printf("TinyUSB Host CDC MSC HID Example\r\n"); + printf("TinyUSB Host MSC Explorer Example\r\n"); // init host stack on configured roothub port tuh_init(BOARD_TUH_RHPORT); diff --git a/examples/host/msc_file_explorer/src/msc_app.c b/examples/host/msc_file_explorer/src/msc_app.c index a9976be23..a8603c9df 100644 --- a/examples/host/msc_file_explorer/src/msc_app.c +++ b/examples/host/msc_file_explorer/src/msc_app.c @@ -25,14 +25,18 @@ #include "tusb.h" +#include "ff.h" +#include "diskio.h" + +// lib/embedded-cli #define EMBEDDED_CLI_IMPL -#include "embedded_cli.h" // lib/embedded-cli +#include "embedded_cli.h" //--------------------------------------------------------------------+ // MACRO TYPEDEF CONSTANT ENUM DECLARATION //--------------------------------------------------------------------+ -// embedded cli configuration +//------------- embedded-cli -------------// #define CLI_BUFFER_SIZE 256 #define CLI_RX_BUFFER_SIZE 16 #define CLI_CMD_BUFFER_SIZE 32 @@ -42,12 +46,18 @@ static EmbeddedCli *_cli; static CLI_UINT cli_buffer[BYTES_TO_CLI_UINTS(CLI_BUFFER_SIZE)]; +//------------- Elm Chan FatFS -------------// +static FATFS fatfs[CFG_TUH_DEVICE_MAX]; // for simplicity only support 1 LUN per device +static volatile bool _disk_busy[CFG_TUH_DEVICE_MAX]; + static scsi_inquiry_resp_t inquiry_resp; //--------------------------------------------------------------------+ // //--------------------------------------------------------------------+ +void cli_cmd_ls(EmbeddedCli *cli, char *args, void *context); + void cli_write_char(EmbeddedCli *cli, char c) { (void) cli; @@ -62,6 +72,8 @@ void cli_cmd_unknown(EmbeddedCli *cli, CliCommand *command) bool msc_app_init(void) { + for(size_t i=0; iwriteChar = cli_write_char; + embeddedCliAddBinding(_cli, (CliCommandBinding) { + "ls", + "Usage: ls [FILE]...\r\n\tList information about the FILEs (the current directory by default).", + true, + NULL, + cli_cmd_ls + }); + return true; } @@ -117,6 +137,19 @@ bool inquiry_complete_cb(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const printf("Disk Size: %lu MB\r\n", block_count / ((1024*1024)/block_size)); printf("Block Count = %lu, Block Size: %lu\r\n", block_count, block_size); + // For simplicity: we only mount 1 LUN per device + uint8_t const drive_num = dev_addr-1; + char drive_path[3] = "0:"; + drive_path[0] += drive_num; + + if ( f_mount(&fatfs[drive_num], drive_path, 1) != FR_OK ) + { + puts("mount failed"); + } + + f_chdrive(drive_path); // change to newly mounted drive + f_chdir("/"); // root as current dir + return true; } @@ -127,24 +160,6 @@ void tuh_msc_mount_cb(uint8_t dev_addr) uint8_t const lun = 0; tuh_msc_inquiry(dev_addr, lun, &inquiry_resp, inquiry_complete_cb); -// -// //------------- file system (only 1 LUN support) -------------// -// uint8_t phy_disk = dev_addr-1; -// disk_initialize(phy_disk); -// -// if ( disk_is_ready(phy_disk) ) -// { -// if ( f_mount(phy_disk, &fatfs[phy_disk]) != FR_OK ) -// { -// puts("mount failed"); -// return; -// } -// -// f_chdrive(phy_disk); // change to newly mounted drive -// f_chdir("/"); // root as current dir -// -// cli_init(); -// } } void tuh_msc_umount_cb(uint8_t dev_addr) @@ -152,11 +167,12 @@ void tuh_msc_umount_cb(uint8_t dev_addr) (void) dev_addr; printf("A MassStorage device is unmounted\r\n"); -// uint8_t phy_disk = dev_addr-1; -// -// f_mount(phy_disk, NULL); // unmount disk -// disk_deinitialize(phy_disk); -// + uint8_t const drive_num = dev_addr-1; + char drive_path[3] = "0:"; + drive_path[0] += drive_num; + + f_unmount(drive_path); + // if ( phy_disk == f_get_current_drive() ) // { // active drive is unplugged --> change to other drive // for(uint8_t i=0; i 1 ) return; + + // default is current directory + const char* dpath = "."; + if (argc) dpath = args; + + DIR dir; + if ( FR_OK != f_opendir(&dir, dpath) ) + { + printf("cannot access '%s': No such file or directory", dpath); + return; + } + + FILINFO fno; + while( (f_readdir(&dir, &fno) == FR_OK) && (fno.fname[0] != 0) ) + { + if ( fno.fname[0] != '.' ) // ignore . and .. entry + { + if ( fno.fattrib & AM_DIR ) + { + // directory + printf("/%s\n", fno.fname); + }else + { + printf("%-40s%lu KB\n", fno.fname, fno.fsize / 1000); + } + } + } + + f_closedir(&dir); +} diff --git a/examples/host/msc_file_explorer/src/tusb_config.h b/examples/host/msc_file_explorer/src/tusb_config.h index fd1215388..ddf8368f4 100644 --- a/examples/host/msc_file_explorer/src/tusb_config.h +++ b/examples/host/msc_file_explorer/src/tusb_config.h @@ -104,9 +104,8 @@ // max device support (excluding hub device) #define CFG_TUH_DEVICE_MAX (CFG_TUH_HUB ? 4 : 1) // hub typically has 4 ports -//------------- HID -------------// -#define CFG_TUH_HID_EPIN_BUFSIZE 64 -#define CFG_TUH_HID_EPOUT_BUFSIZE 64 +//------------- MSC -------------// +#define CFG_TUH_MSC_MAXLUN 4 // typical for most card reader #ifdef __cplusplus } -- cgit v1.3.1