DA14580 Bluetooth Smart IC writer library
Dependents: 11u35_usbLocalFilesystem
Diff: DA14580.cpp
- Revision:
- 5:45e9f3723a08
- Parent:
- 3:a9684679d1ec
- Child:
- 6:db0ae78150a3
--- a/DA14580.cpp Mon Oct 05 04:51:58 2015 +0000 +++ b/DA14580.cpp Tue Nov 10 01:12:06 2015 +0000 @@ -1,13 +1,18 @@ -#include "mbed.h" +/** +@file DA14580.cpp +*/ +//#include "mbed.h" #include "DA14580.h" DA14580::DA14580( PinName TX, PinName RX, PinName RESET ) : _ble(TX,RX), _reset(RESET) { + _ble.baud(57600); init(); } DA14580::DA14580( RawSerial &ble, PinName RESET ) : _ble(ble), _reset(RESET) { + _ble.baud(57600); init(); } @@ -18,13 +23,12 @@ void DA14580::init() { - _ble.baud(57600); _crc = 0x00; _recieve = 0; _read = 0; _filesize = 0; - _reset.write(0); - _timeout = 100; + _reset.write(_RESET); + _timeout = _TIMEOUT; _status = SUCCESS; } @@ -32,67 +36,82 @@ { _status = SUCCESS; - + /* + while( _ble.readable() ) { + _recieve = _ble.getc(); + } + */ _fp = fopen( LOADER_FILE, "rb" ); if (_fp) { _filesize = file_size(_fp); - _reset.write(1); - while((_timeout--) >0) { - if( _ble.readable() ) { - _recieve = _ble.getc(); - if(_recieve == STX) { - _ble.putc(SOH); - break; - } - } - } - if(_timeout <= 0) { - _status = E_TIMEOUT_STX; - } else { - _timeout = 100; - _ble.putc(_filesize & 0xff); - _ble.putc( (_filesize >> 8) & 0xff); - while((_timeout--) >0) { - if( _ble.readable() ) { - _recieve = _ble.getc(); - if(_recieve == ACK) { - break; - } - } - } - if(_timeout <= 0) { - _status = E_ACK_NOT_RETURNED; - } else { - _timeout = 100; - for(int i = 0; i < _filesize; i++) { - _read = getc(_fp); - _ble.putc(_read); - _crc = _crc ^ _read; - if((i % 16) == 0) { - } - } - while((_timeout--) >0) { - if( _ble.readable() ) { - _recieve = _ble.getc(); - if(_recieve == _crc) { - _ble.putc(ACK); - break; - } - } - } - if(_timeout <= 0) { - _status = E_ACK_NOT_RETURNED; - } - } - } - fclose(_fp); -#if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler -#warning "free(_fp)" - free(_fp); -#endif } else { _status = E_FILE_NOT_FOUND; } + if(_status == SUCCESS) { + _reset.write(_BOOT); + wait_us(1); + while(1) { +// if( _ble.readable() ) { + while( _ble.readable() ) { + _recieve = _ble.getc(); + } + wait_us(150); //DEBUG + if(_recieve == STX) { + _ble.putc(SOH); + wait_us(150); //DEBUG + _ble.putc(_filesize & 0xff); + wait_us(150); //DEBUG + _ble.putc( (_filesize >> 8) & 0xff); + wait_us(150); //DEBUG + _reset.write(_RESET); //DEBUG + _reset.write(_BOOT); //DEBUG + _reset.write(_RESET); //DEBUG + break; + } +// } + } + +// _ble.putc(_filesize & 0xff); +// _ble.putc( (_filesize >> 8) & 0xff); + while(1) { +// if( _ble.readable() ) { + _recieve = _ble.getc(); + if(_recieve == ACK) { + break; + } else { + _status = E_ACK_NOT_RETURNED; + break; + } +// } + } + + if(_status == SUCCESS) { + for(int i = 0; i < _filesize; i++) { + _read = getc(_fp); + _ble.putc(_read); + _crc = _crc ^ _read; + wait_us(150); //DEBUG + } + while(1) { +// if( _ble.readable() ) { + _recieve = _ble.getc(); + if(_recieve == _crc) { + _ble.putc(ACK); + wait_us(150); //DEBUG + break; + } else { + _status = E_CRC_MISMATCH; + break; + } +// } + } + } + } + fclose(_fp); +#if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler +#warning "free(_fp)" + free(_fp); +#endif return _status;