a writer program for "DA14580" BLE chip. use LocalFileSystem, so only for mbed-LPC1768/mbed-LPC11U24
Diff: main.cpp
- Revision:
- 0:08d27693851c
- Child:
- 1:26c972ffac64
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Oct 22 09:58:22 2014 +0000 @@ -0,0 +1,85 @@ +#include "mbed.h" + +DigitalOut myled(LED1); + +Serial pc(USBTX,USBRX); +Serial ble(p28,p27); + +LocalFileSystem local( "local" ); + +#define SOURCE_FILE "/local/_bin" + +int file_size( FILE *fp ); + +int main() +{ + char recieve; + char 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 == 0x02) { + ble.putc(0x01); + pc.putc('!'); + break; + } + } + ble.putc(filesize&0xff); + ble.putc( (filesize>>8)&0xff); + while(1) { + recieve=ble.getc(); + if(recieve == 0x06) { + 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(0x06); + pc.printf("-=-=DONE=-=-\n\r"); + break; + } + } + fp.close(); + while(1) { + myled = 1; + wait(0.2); + myled = 0; + wait(0.2); + } +} + +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; +}