NRF51822 as bluetooth to UART

Dependencies:   BLE_API BLE_LoopbackUART mbed nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Files at this revision

API Documentation at this revision

Comitter:
zcw607
Date:
Tue Feb 24 01:46:20 2015 +0000
Parent:
11:add794159852
Commit message:
initial commit

Changed in this revision

BLE_LoopbackUART.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r add794159852 -r fc9171411cf5 BLE_LoopbackUART.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BLE_LoopbackUART.lib	Tue Feb 24 01:46:20 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_LoopbackUART/#add794159852
diff -r add794159852 -r fc9171411cf5 main.cpp
--- a/main.cpp	Tue Dec 09 08:55:59 2014 +0000
+++ b/main.cpp	Tue Feb 24 01:46:20 2015 +0000
@@ -30,7 +30,10 @@
 
 BLEDevice  ble;
 DigitalOut led1(LED1);
+Serial pc(USBTX, USBRX);
 
+static uint8_t rx_buf[20];
+static uint8_t rx_len=0;
 UARTService *uartServicePtr;
 
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
@@ -44,11 +47,26 @@
 {
     if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
         uint16_t bytesRead = params->len;
-        DEBUG("received %u bytes\n\r", bytesRead);
-        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
+        for(uint16_t index=0; index<bytesRead; index++)
+        {
+            pc.putc(params->data[index]);        
+        }
     }
 }
 
+void uartCB(void)
+{   
+    while(pc.readable())    
+    {
+        rx_buf[rx_len++] = pc.getc();    
+        if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
+        {
+            ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), rx_buf, rx_len);
+            rx_len = 0;
+            break;
+        }
+    }
+}
 void periodicCallback(void)
 {
     led1 = !led1;
@@ -57,6 +75,9 @@
 int main(void)
 {
     led1 = 1;
+    pc.baud(115200);
+    
+    pc.attach( uartCB , pc.RxIrq);
     Ticker ticker;
     ticker.attach(periodicCallback, 1);