summaryrefslogtreecommitdiff
path: root/examples/host
diff options
context:
space:
mode:
authorhathach <[email protected]>2022-11-19 13:44:07 +0700
committerhathach <[email protected]>2022-11-19 13:44:07 +0700
commitbb570e02d6a8b34fe00c90f5cd8ad736d990ed42 (patch)
tree64fae38d8095ad947eead76298aaf906d6db339d /examples/host
parenta6001fc8f2cfe0db2cf5ba410b49961781ce1803 (diff)
msc explorer ls work great
Diffstat (limited to 'examples/host')
-rw-r--r--examples/host/msc_file_explorer/src/main.c2
-rw-r--r--examples/host/msc_file_explorer/src/msc_app.c216
-rw-r--r--examples/host/msc_file_explorer/src/tusb_config.h5
3 files changed, 194 insertions, 29 deletions
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; i<CFG_TUH_DEVICE_MAX; i++) _disk_busy[i] = false;
+
// disable stdout buffered for echoing typing command
setbuf(stdout, NULL);
@@ -78,6 +90,14 @@ bool msc_app_init(void)
_cli->writeChar = 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<CFG_TUH_DEVICE_MAX; i++)
@@ -169,3 +185,153 @@ void tuh_msc_umount_cb(uint8_t dev_addr)
// }
// }
}
+
+//--------------------------------------------------------------------+
+// DiskIO
+//--------------------------------------------------------------------+
+
+static void wait_for_disk_io(BYTE pdrv)
+{
+ while(_disk_busy[pdrv])
+ {
+ tuh_task();
+ }
+}
+
+static bool disk_io_complete(uint8_t dev_addr, msc_cbw_t const* cbw, msc_csw_t const* csw)
+{
+ (void) dev_addr; (void) cbw; (void) csw;
+ _disk_busy[dev_addr-1] = false;
+ return true;
+}
+
+DSTATUS disk_status (
+ BYTE pdrv /* Physical drive nmuber to identify the drive */
+)
+{
+ uint8_t dev_addr = pdrv + 1;
+ return tuh_msc_mounted(dev_addr) ? 0 : STA_NODISK;
+}
+
+DSTATUS disk_initialize (
+ BYTE pdrv /* Physical drive nmuber to identify the drive */
+)
+{
+ (void) pdrv;
+ return 0; // nothing to do
+}
+
+DRESULT disk_read (
+ BYTE pdrv, /* Physical drive nmuber to identify the drive */
+ BYTE *buff, /* Data buffer to store read data */
+ LBA_t sector, /* Start sector in LBA */
+ UINT count /* Number of sectors to read */
+)
+{
+ uint8_t const dev_addr = pdrv + 1;
+ uint8_t const lun = 0;
+
+ _disk_busy[pdrv] = true;
+ tuh_msc_read10(dev_addr, lun, buff, sector, count, disk_io_complete);
+ wait_for_disk_io(pdrv);
+
+ return RES_OK;
+}
+
+#if FF_FS_READONLY == 0
+
+DRESULT disk_write (
+ BYTE pdrv, /* Physical drive nmuber to identify the drive */
+ const BYTE *buff, /* Data to be written */
+ LBA_t sector, /* Start sector in LBA */
+ UINT count /* Number of sectors to write */
+)
+{
+ uint8_t const dev_addr = pdrv + 1;
+ uint8_t const lun = 0;
+
+ _disk_busy[pdrv] = true;
+ tuh_msc_write10(dev_addr, lun, buff, sector, count, disk_io_complete);
+ wait_for_disk_io(pdrv);
+
+ return RES_OK;
+}
+
+#endif
+
+DRESULT disk_ioctl (
+ BYTE pdrv, /* Physical drive nmuber (0..) */
+ BYTE cmd, /* Control code */
+ void *buff /* Buffer to send/receive control data */
+)
+{
+ uint8_t const dev_addr = pdrv + 1;
+ uint8_t const lun = 0;
+ switch ( cmd )
+ {
+ case CTRL_SYNC:
+ // nothing to do since we do blocking
+ return RES_OK;
+
+ case GET_SECTOR_COUNT:
+ *((DWORD*) buff) = tuh_msc_get_block_count(dev_addr, lun);
+ return RES_OK;
+
+ case GET_SECTOR_SIZE:
+ *((WORD*) buff) = tuh_msc_get_block_size(dev_addr, lun);
+ return RES_OK;
+
+ case GET_BLOCK_SIZE:
+ *((DWORD*) buff) = 1; // erase block size in units of sector size
+ return RES_OK;
+
+ default:
+ return RES_PARERR;
+ }
+
+ return RES_OK;
+}
+
+//--------------------------------------------------------------------+
+// CLI Commands
+//--------------------------------------------------------------------+
+
+void cli_cmd_ls(EmbeddedCli *cli, char *args, void *context)
+{
+ (void) cli;
+ (void) context;
+
+ uint16_t argc = embeddedCliGetTokenCount(args);
+
+ // only support 1 argument
+ if ( argc > 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
}