USBMSD and CDC composite device testing code mixed up from several existing programs/libraries
Dependencies: USBCDCMSC USBDevice USBFileSystem USBSDFileSystem USBSPIFileSystem mbed
Diff: main.cpp
- Revision:
- 0:07ed6626e186
- Child:
- 1:30c83d964f38
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Apr 21 09:15:46 2015 +0000 @@ -0,0 +1,146 @@ +#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); + +//#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) { + pc.printf("Could not open file for write\n"); + } else + { + fprintf(fp, "[%d]Hello fun SD Card World!\n", i); + pc.printf("appending: '[%d]Hello fun SD Card World!'\n", i); + fclose(fp); + } +} + +int main() +{ + 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"); + wait (60); + + pc.printf("create directory and append to log file\n"); + mkdir("/sd/mydir", 0777); + mylog (0); + + pc.printf("Entering loop of 1 min cycle time\n"); + + int i = 1; + while(1) { + myled = 0; + wait(5); + //sd.disconnect (); + mylog (i++); + //sd.connect (); + myled = 1; + wait(55); + } + return 0; +} +#endif //BLE