DA14580 Bluetooth Smart IC writer library

Dependents:   11u35_usbLocalFilesystem

Revision:
0:3bdbabca8a09
Child:
3:a9684679d1ec
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DA14580.cpp	Wed Aug 19 14:41:13 2015 +0000
@@ -0,0 +1,110 @@
+#include "mbed.h"
+#include "DA14580.h"
+
+DA14580::DA14580( PinName TX, PinName RX, PinName RESET ) : _ble(TX,RX), _reset(RESET)
+{
+    init();
+}
+
+DA14580::DA14580( RawSerial &ble, PinName RESET ) : _ble(ble), _reset(RESET)
+{
+    init();
+}
+
+DA14580::~DA14580()
+{
+}
+
+void DA14580::init()
+{
+
+    _ble.baud(57600);
+    _crc = 0x00;
+    _recieve = 0;
+    _read = 0;
+    _filesize = 0;
+    _reset = 0;
+    _timeout = 100;
+    _status = SUCCESS;
+}
+
+int DA14580::load()
+{
+
+    _status = SUCCESS;
+
+    _fp = fopen( LOADER_FILE, "rb" );
+    if (_fp) {
+        _filesize = file_size(_fp);
+        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;
+    }
+    return _status;
+
+
+}
+
+int DA14580::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;
+}
+