Skip to content

Commit

Permalink
msc explorer ls work great
Browse files Browse the repository at this point in the history
  • Loading branch information
hathach committed Nov 19, 2022
1 parent a6001fc commit bb570e0
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 5,902 deletions.
2 changes: 1 addition & 1 deletion examples/host/msc_file_explorer/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
216 changes: 191 additions & 25 deletions examples/host/msc_file_explorer/src/msc_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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);

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -127,36 +160,19 @@ 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)
{
(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++)
Expand All @@ -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);
}
5 changes: 2 additions & 3 deletions examples/host/msc_file_explorer/src/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit bb570e0

Please sign in to comment.