BLE to UART

Dependencies:   BLE_API Buffer mbed nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Files at this revision

API Documentation at this revision

Comitter:
yihui
Date:
Fri May 22 07:23:32 2015 +0000
Parent:
11:add794159852
Commit message:
basic BLE2UART function

Changed in this revision

BLE_API.lib Show annotated file Show diff for this revision Revisions of this file
Buffer.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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
nRF51822.lib Show annotated file Show diff for this revision Revisions of this file
--- a/BLE_API.lib	Tue Dec 09 08:55:59 2014 +0000
+++ b/BLE_API.lib	Fri May 22 07:23:32 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#0e9201b67e2f
+http://mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/#ecbc3405c66e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Buffer.lib	Fri May 22 07:23:32 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/sam_grove/code/Buffer/#7b754354b99c
--- a/main.cpp	Tue Dec 09 08:55:59 2014 +0000
+++ b/main.cpp	Fri May 22 07:23:32 2015 +0000
@@ -16,8 +16,9 @@
 
 #include "mbed.h"
 #include "BLEDevice.h"
-
 #include "UARTService.h"
+#include "DFUService.h"
+#include "Buffer.h"
 
 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
                                * it will have an impact on code-size and power consumption. */
@@ -28,15 +29,63 @@
 #define DEBUG(...) /* nothing */
 #endif /* #if NEED_CONSOLE_OUTPUT */
 
+Buffer <char> uartRxBuffer(0x100);
+//Serial uart(p8, p7);
+Serial uart(USBTX, USBRX);
+
 BLEDevice  ble;
-DigitalOut led1(LED1);
+DigitalOut led1(p28);
 
 UARTService *uartServicePtr;
 
+uint8_t bleIsConnected = 0;
+uint8_t bleTxFlag = 0;
+
+bool rxPayloadUpdated = false;
+uint8_t rxPayload[20 + 1] = {0,};
+uint8_t txPayload[20 + 1] = {0,};
+
+void uart2ble(void)
+{
+    uint16_t bytesToWrite = 0;
+    for (int i = 0; i < 20; i++) {
+        if (uartRxBuffer.available()) {
+            txPayload[bytesToWrite] = uartRxBuffer;
+            bytesToWrite++;
+        }
+    }
+
+    if (bytesToWrite != 0) {
+        bleTxFlag = 1;
+
+        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), txPayload, bytesToWrite);
+    } else {
+        bleTxFlag = 0;
+    }
+}
+
+void resetToRunBootloader()
+{
+    DEBUG("Reset to run bootloader\r\n");
+    NRF_POWER->GPREGRET = 0x02; 
+    NVIC_SystemReset();
+}
+
+void connectionCallback(Gap::Handle_t, Gap::addr_type_t peerAddrType, const Gap::address_t peerAddr, const Gap::ConnectionParams_t *)
+{
+    DEBUG("Connected\r\n");
+    bleIsConnected = 1;
+    bleTxFlag = 0;
+}
+
 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
 {
     DEBUG("Disconnected!\n\r");
     DEBUG("Restarting the advertising process\n\r");
+    
+    bleTxFlag = 0;
+    bleIsConnected = 0;
+    
     ble.startAdvertising();
 }
 
@@ -45,10 +94,21 @@
     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);
+        // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
+        
+        for (int i = 0; i < bytesRead; i++) {
+            uart.putc(params->data[i]);
+        }
     }
 }
 
+void onDataSent(unsigned count)
+{
+    DEBUG("onDataSent\r\n");
+
+    uart2ble();
+}
+
 void periodicCallback(void)
 {
     led1 = !led1;
@@ -58,12 +118,14 @@
 {
     led1 = 1;
     Ticker ticker;
-    ticker.attach(periodicCallback, 1);
+    ticker.attach(periodicCallback, 0.01);
 
     DEBUG("Initialising the nRF51822\n\r");
     ble.init();
+    ble.onConnection(connectionCallback);
     ble.onDisconnection(disconnectionCallback);
     ble.onDataWritten(onDataWritten);
+    ble.onDataSent(onDataSent);
 
     /* setup advertising */
     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
@@ -76,10 +138,28 @@
     ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000));
     ble.startAdvertising();
 
+    DFUService dfuService(ble);
     UARTService uartService(ble);
     uartServicePtr = &uartService;
 
     while (true) {
-        ble.waitForEvent();
+        uint32_t timeout = 1000;
+        while (timeout) {
+            timeout--;
+            if (uart.readable()) {
+                uartRxBuffer.put((char)uart.getc());
+                timeout = 1000;
+            }
+        }
+        
+        if (bleIsConnected && bleTxFlag == 0 && uartRxBuffer.available()) {
+            uart2ble();
+            bleTxFlag = 1;
+        }
     }
 }
+
+extern "C" void HardFault_Handler()
+{
+    NVIC_SystemReset();
+}
--- a/mbed.bld	Tue Dec 09 08:55:59 2014 +0000
+++ b/mbed.bld	Fri May 22 07:23:32 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/7e07b6fb45cf
\ No newline at end of file
--- a/nRF51822.lib	Tue Dec 09 08:55:59 2014 +0000
+++ b/nRF51822.lib	Fri May 22 07:23:32 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#cdcc094ab166
+http://mbed.org/teams/Nordic-Semiconductor/code/nRF51822/#e95e35845e1c