USBMSD and CDC composite device testing code mixed up from several existing programs/libraries

Dependencies:   USBCDCMSC USBDevice USBFileSystem USBSDFileSystem USBSPIFileSystem mbed

main.cpp

Committer:
k4zuki
Date:
2015-04-21
Revision:
2:c70070f88944
Parent:
1:30c83d964f38
Child:
4:bbe1bc2eb8fb

File content as of revision 2:c70070f88944:

#include "mbed.h"
#include "USBSDFileSystem.h"
#include "USBCDCMSC.h"

DigitalOut myled(LED1);

USBSDFileSystem sd("sd", p5, p6, p7, p8); // the pinout on the mbed testbed board // mosi, miso, sclk, cs
// P0_9,P0_8,P0_10,P0_7
USBCDCMSC pc(&sd,0x1f00, 0x2012, 0x0001);

//#define BLE
#ifdef BLE
/*
//Serial pc(USBTX,USBRX); //->USBCDCMSC pc(&sd);
Serial ble(p9,p10);
// P0_19,P0_18

//LocalFileSystem local( "local" ); //->USBSDFileSystem sd("sd", p5, p6, p7, p8); // the pinout on the mbed testbed board // mosi, miso, sclk, cs
//#define     SOURCE_FILE         "/local/_bin"
#define     SOURCE_FILE         "/sd/_bin"

int file_size( FILE *fp );
enum XMODEM_CONST{
SOH = (0x01), 
STX = (0x02),
EOT = (0x04),
ACK = (0x06),
DLE = (0x10),
NAK = (0x15),
CAN = (0x18),
};

int main()
{
    uint8_t recieve;
    uint8_t read;
    int filesize=0;
    FILE* fp;
    ble.baud(57600);
    int crc=0x00;
    
    if ( NULL == (fp = fopen( SOURCE_FILE, "rb" )) ) {
        exit(1);
//        return ( ERROR_AT_FILE_OPEN );
    }else{
        filesize=file_size(fp);
        pc.printf("0x%04X\n\r",filesize);
    }

    while(1) {
        recieve=ble.getc();
        if(recieve == STX) {
            ble.putc(SOH);
            pc.putc('!');
            break;
        }
    }
    ble.putc(filesize&0xff);
    ble.putc( (filesize>>8)&0xff);
    while(1) {
        recieve=ble.getc();
        if(recieve == ACK) {
            pc.printf("ok!\n\r");
//            ble.putc(0x01);
            break;
        }
    }
    for(int i=0;i<filesize;i++){
        read=getc(fp);
        ble.putc(read);
        crc=crc^read;
        if((i%16)==0){
            pc.printf("\n\r");
        }
        pc.printf("%02X ",read);
    }
    pc.printf("\n\r0x%02X ",crc);
    while(1) {
        recieve=ble.getc();
        if(recieve == crc) {
            ble.putc(ACK);
            pc.printf("-=-=DONE=-=-\n\r");
            break;
        }
    }
    fclose(fp);
    myled = 1;
    while(1) {
        recieve=ble.getc();
        pc.putc(recieve);
        wait_ms(20);
    }
}

int file_size( FILE *fp )
{
    int     size;

    fseek( fp, 0, SEEK_END ); // seek to end of file
    size    = ftell( fp );    // get current file pointer
    fseek( fp, 0, SEEK_SET ); // seek back to beginning of file

    return size;
}
*/
#else //BLE

void mylog (int i)
{
    FILE *fp = fopen("/sd/mydir/sdtest.txt", "a");
    if(fp == NULL) {
        printf("Could not open file for write\n\r");
    } else
    {
      fprintf(fp, "[%d]Hello fun SD Card World!\n\r", i);
      printf("appending: '[%d]Hello fun SD Card World!'\n\r", i);
      fclose(fp); 
    }
}

int main() 
{
    /* pc.printf() and/or pc. getc() is not working. CDC packet dissappears*/
//    sd.connect (false);
    sd.usbMode (1); // allow fopen/fprintf when connected with USB-drive.
    pc.printf("Hello World!\n\n");   
    
    pc.printf("now a USB drive should appear on your PC\n");
    for(int i=0;i<60;i++){
        myled = 1;
        wait_ms (500);
        myled = 0;
        wait_ms (500);
    }
       
    pc.printf("create directory and append to log file\n");
//    mkdir("/sd/mydir", 0777);
    mylog (0);    

    for(int i=0;i<60;i++){
        myled = 1;
        wait_ms (500);
        myled = 0;
        wait_ms (500);
        mylog (i);
    }
//    sd.usbMode (0); // allow fopen/fprintf when connected with USB-drive.
//    sd.disconnect ();

    pc.printf("Entering loop of 1 min cycle time\n");
    sd.connect(true);
    int i = 1;
    int recieve=0 ;
    while(true) {
        myled = 0;
        recieve=pc.getc();
        if(recieve != NULL){
            pc.putc('+');
        }
        myled = 1;
        wait_ms(20);
    }
    return 0;
}
#endif //BLE