BaseUsbHost example program

Dependencies:   BaseUsbHost FATFileSystem mbed mbed-rtos

example1_UsbFlashDrive.cpp

Committer:
va009039
Date:
2012-12-05
Revision:
1:80205a2de336
Parent:
0:2a9734a95d55

File content as of revision 1:80205a2de336:

#if 0
#include "mbed.h"
#include "rtos.h"
#include "BaseUsbHost.h"
#define DEBUG
#include "BaseUsbHostDebug.h"
#define TEST
#include "BaseUsbHostTest.h"
#include "UsbFlashDrive.h"

DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
Serial pc(USBTX, USBRX);

BaseUsbHost *UsbHost;
UsbHub* hub;
ControlEp* ctlEp = NULL;
UsbFlashDrive* drive;
int main() {
    pc.baud(921600);
    printf("%s\n", __FILE__);

    UsbHost = new BaseUsbHost;
    TEST_ASSERT_TRUE(UsbHost);

    UsbHub* hub = new UsbHub();
    TEST_ASSERT_TRUE(hub);

    for(int i = 0; i < MAX_HUB_PORT; i++) {
        if (UsbFlashDrive::check(hub->PortEp[i])) {
            ctlEp = hub->PortEp[i];
            break;
        }
    }
    if (ctlEp == NULL) {
        error("USB Flash Drive not found\n");
    }
    TEST_ASSERT(ctlEp);

    drive = new UsbFlashDrive("usb", ctlEp);
    TEST_ASSERT(drive);

    const int size_table[] = {
        1024*1,
        1024*2,
        1024*4,
        1024*8,
        1024*16,
        1024*32,
        1024*64,
        1024*128,
        1204*256,
        1204*512,
    };
    
    printf("USB FLASH DRIVE read/write test\n");
    
    char path[32];
    int size;
    Timer t;
    for(int n = 0; n <= 9; n++) {
        int file_size = size_table[n];
        sprintf(path, "/usb/test%d.txt", n);
        FILE* fp = fopen(path, "wb");
        size = 0;
        t.reset();
        t.start();
        if (fp) {
            for(int i = 0; i < file_size; i++) {
                int c = i & 0xff;
                fputc(c, fp);
                size++;
            }
            t.stop();
            fclose(fp);
        }
        printf("write file %d bytes %d ms %s\n", size, t.read_ms(), path);
        led1 = !led1;
    }
    
    for(int n = 0; n <=9; n++) {
        sprintf(path, "/usb/test%d.txt", n);
        FILE* fp = fopen(path, "rb");
        size = 0;
        t.reset();
        t.start();
        if (fp) {
            while(1) {
                int c = fgetc(fp);
                if (c == EOF) {
                    break;
                }
                size++;
            } 
            t.stop();
            fclose(fp);
        }
        printf("read file %d bytes %d ms %s\n", size, t.read_ms(), path);
        led2 = !led2;
    }

    exit(1);
}

#endif