mbed OS V5 BLE UARTService to USB/UART PassThru Example

Moxon Design : mbed OS V5 BLE UartService to USBUART passthru example

This example will advertise on BLE as "BLART" and allow BLE serial echo to the USB Serial port. Open a Bluetooth terminal application, like Nordic "nRF UART" and connect to "BLART", open a terminal emulator program, like "TeraTERM", and connect to the USB COM Port (like JLink CDC UART Port), and echo characters back and forth. Set UART_LoopBack = 1 for local loopback (e.g. BLE to BLE, USB-UART to USB-UART)

Committer:
moxondesign
Date:
Sat Apr 29 01:32:11 2017 +0000
Revision:
2:3e203cc2a88f
Parent:
1:5b50f5283781
mbed OS V5 working version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
moxondesign 1:5b50f5283781 1 /* mbed BLE Library - USB/UART Serial Pass-thru Example
moxondesign 0:2d1d68397ff7 2 *
moxondesign 0:2d1d68397ff7 3 * author : Moxon Design
moxondesign 0:2d1d68397ff7 4 * date : 4/27/2017
moxondesign 0:2d1d68397ff7 5 * platforms : Rigado BLE-350/300, Nordic rRF52-DK, rRF51-DK
moxondesign 0:2d1d68397ff7 6 * requires : mBed Platform Version 5+
moxondesign 0:2d1d68397ff7 7
moxondesign 0:2d1d68397ff7 8 * Overview:
moxondesign 0:2d1d68397ff7 9 *
moxondesign 0:2d1d68397ff7 10 * Employs the mBed platform ble/services/UARTService for UART emulation,
moxondesign 1:5b50f5283781 11 * as well as exposing the phyical UART device connected to the USB host,
moxondesign 1:5b50f5283781 12 * providing a transparent bridge between the two interfaces. By default,
moxondesign 1:5b50f5283781 13 * Serial dev(USBTX, USBRX);
moxondesign 0:2d1d68397ff7 14 *
moxondesign 0:2d1d68397ff7 15 * Set "UART_LoopBack" to enable local loopback test instead of pass-thru.
moxondesign 0:2d1d68397ff7 16 *
moxondesign 0:2d1d68397ff7 17 * Notes :
moxondesign 1:5b50f5283781 18 * 1) Advertises as "BLART" for BLe-usbUART
moxondesign 0:2d1d68397ff7 19 * 2) ble/services/UARTService is 20 byte max packets or about 4800 Baud (be patient)
moxondesign 1:5b50f5283781 20 * 3) transparent bridge
moxondesign 0:2d1d68397ff7 21 *
moxondesign 0:2d1d68397ff7 22 * License :
moxondesign 0:2d1d68397ff7 23 *
moxondesign 0:2d1d68397ff7 24 * Licensed under the Apache License, Version 2.0 (the "License");
moxondesign 0:2d1d68397ff7 25 * you may not use this file except in compliance with the License.
moxondesign 0:2d1d68397ff7 26 * You may obtain a copy of the License at
moxondesign 0:2d1d68397ff7 27 *
moxondesign 0:2d1d68397ff7 28 * http://www.apache.org/licenses/LICENSE-2.0
moxondesign 0:2d1d68397ff7 29 *
moxondesign 0:2d1d68397ff7 30 * Unless required by applicable law or agreed to in writing, software
moxondesign 0:2d1d68397ff7 31 * distributed under the License is distributed on an "AS IS" BASIS,
moxondesign 0:2d1d68397ff7 32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
moxondesign 0:2d1d68397ff7 33 * See the License for the specific language governing permissions and
moxondesign 0:2d1d68397ff7 34 * limitations under the License.
moxondesign 0:2d1d68397ff7 35 */
moxondesign 0:2d1d68397ff7 36
moxondesign 0:2d1d68397ff7 37 /* mbed platform defines */
moxondesign 0:2d1d68397ff7 38 #include "mbed.h"
moxondesign 0:2d1d68397ff7 39 #include "ble/BLE.h"
moxondesign 0:2d1d68397ff7 40 #include "ble/services/UARTService.h"
moxondesign 0:2d1d68397ff7 41
moxondesign 0:2d1d68397ff7 42 /* got debug? */
moxondesign 0:2d1d68397ff7 43 /* Set this if you need debug messages on the console */
moxondesign 0:2d1d68397ff7 44 /* Negative impact on code-size and power consumption */
moxondesign 0:2d1d68397ff7 45 #define DEBUG_OUTPUT 0
moxondesign 0:2d1d68397ff7 46 #if DEBUG_OUTPUT
moxondesign 0:2d1d68397ff7 47 #define DEBUG(...) { printf(__VA_ARGS__); }
moxondesign 0:2d1d68397ff7 48 #else
moxondesign 0:2d1d68397ff7 49 #define DEBUG(...) /* no output mapped */
moxondesign 0:2d1d68397ff7 50 #endif
moxondesign 0:2d1d68397ff7 51
moxondesign 0:2d1d68397ff7 52 /* instantiate BLE services and softUART service */
moxondesign 0:2d1d68397ff7 53 BLEDevice ble;
moxondesign 0:2d1d68397ff7 54 UARTService *uartServicePtr;
moxondesign 1:5b50f5283781 55 int UART_LoopBack = 0;
moxondesign 1:5b50f5283781 56
moxondesign 0:2d1d68397ff7 57 /* instantiate hardware UART devices */
moxondesign 0:2d1d68397ff7 58 /* devicename(TXD, RXD); */
moxondesign 0:2d1d68397ff7 59 /* for "JLink CDC UART Port" use the define below : */
moxondesign 1:5b50f5283781 60 /* Serial dev(USBTX, USBRX); */
moxondesign 0:2d1d68397ff7 61 /* otherwise use the hardware UART on 0.12 & 0.11 */
moxondesign 0:2d1d68397ff7 62 /* (a.k.a. d1 & D1 in Arduino parlance... */
moxondesign 1:5b50f5283781 63 //Serial dev(P0_12, P0_11);
moxondesign 1:5b50f5283781 64 Serial dev(USBTX, USBRX);
moxondesign 1:5b50f5283781 65 static uint8_t rx_buf[32];
moxondesign 1:5b50f5283781 66 static uint8_t rx_len=0;
moxondesign 1:5b50f5283781 67 int tx_buf;
moxondesign 1:5b50f5283781 68 static uint8_t tx_len=0;
moxondesign 1:5b50f5283781 69
moxondesign 0:2d1d68397ff7 70 /* define some blinky LED fun */
moxondesign 0:2d1d68397ff7 71 DigitalOut led1(LED1);
moxondesign 0:2d1d68397ff7 72 //DigitalOut led2(LED2);
moxondesign 0:2d1d68397ff7 73 //DigitalOut led3(LED3);
moxondesign 0:2d1d68397ff7 74 //DigitalOut led4(LED4);
moxondesign 0:2d1d68397ff7 75
moxondesign 1:5b50f5283781 76 void theBLEdisconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
moxondesign 0:2d1d68397ff7 77 {
moxondesign 0:2d1d68397ff7 78 #if DEBUG_OUTPUT
moxondesign 0:2d1d68397ff7 79 DEBUG("Disconnected!\n\r");
moxondesign 0:2d1d68397ff7 80 DEBUG("Restarting the advertising process\n\r");
moxondesign 0:2d1d68397ff7 81 #endif
moxondesign 0:2d1d68397ff7 82 ble.startAdvertising();
moxondesign 0:2d1d68397ff7 83 }
moxondesign 0:2d1d68397ff7 84
moxondesign 1:5b50f5283781 85 void theBLEonDataWritten(const GattWriteCallbackParams *params)
moxondesign 0:2d1d68397ff7 86 {
moxondesign 0:2d1d68397ff7 87 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
moxondesign 0:2d1d68397ff7 88 uint16_t bytesRead = params->len;
moxondesign 1:5b50f5283781 89 uint8_t byteCnt;
moxondesign 0:2d1d68397ff7 90
moxondesign 0:2d1d68397ff7 91 #if DEBUG_OUTPUT
moxondesign 1:5b50f5283781 92 DEBUG("BLART BLE received %u bytes\n\r", bytesRead);
moxondesign 0:2d1d68397ff7 93 #endif
moxondesign 1:5b50f5283781 94
moxondesign 1:5b50f5283781 95 if (UART_LoopBack == 0) {
moxondesign 1:5b50f5283781 96 /* pass thru BLE UART data to UART1 */
moxondesign 1:5b50f5283781 97 for (byteCnt = 0; byteCnt < bytesRead; byteCnt++) {
moxondesign 1:5b50f5283781 98 dev.putc(params->data[byteCnt]);
moxondesign 1:5b50f5283781 99 }
moxondesign 1:5b50f5283781 100 } else {
moxondesign 1:5b50f5283781 101 /* otherwise, loopback BLE UART data to itself */
moxondesign 1:5b50f5283781 102 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
moxondesign 1:5b50f5283781 103 }
moxondesign 0:2d1d68397ff7 104 }
moxondesign 0:2d1d68397ff7 105 }
moxondesign 0:2d1d68397ff7 106
moxondesign 1:5b50f5283781 107 void theUARTonDataWritten(void) {
moxondesign 1:5b50f5283781 108 while(dev.readable())
moxondesign 1:5b50f5283781 109 {
moxondesign 1:5b50f5283781 110 rx_buf[rx_len++] = dev.getc();
moxondesign 1:5b50f5283781 111 #if DEBUG_OUTPUT
moxondesign 1:5b50f5283781 112 DEBUG("BLART BLE received %u \n\r", rx_buf[rx_len]);
moxondesign 1:5b50f5283781 113 #endif
moxondesign 1:5b50f5283781 114 if (UART_LoopBack == 0) {
moxondesign 1:5b50f5283781 115 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), rx_buf, rx_len);
moxondesign 1:5b50f5283781 116 rx_len = 0;
moxondesign 1:5b50f5283781 117 } else {
moxondesign 1:5b50f5283781 118 for (tx_len = 0; tx_len < rx_len; tx_len++) {
moxondesign 1:5b50f5283781 119 tx_buf = rx_buf[tx_len];
moxondesign 1:5b50f5283781 120 dev.putc(tx_buf);
moxondesign 1:5b50f5283781 121 }
moxondesign 1:5b50f5283781 122 rx_len = 0;
moxondesign 1:5b50f5283781 123 }
moxondesign 1:5b50f5283781 124 }
moxondesign 1:5b50f5283781 125 }
moxondesign 1:5b50f5283781 126
moxondesign 0:2d1d68397ff7 127 void theTickCallback(void)
moxondesign 0:2d1d68397ff7 128 {
moxondesign 0:2d1d68397ff7 129 /* toggle the LED each timer tick (1 sec) */
moxondesign 0:2d1d68397ff7 130 led1 = !led1;
moxondesign 0:2d1d68397ff7 131 }
moxondesign 0:2d1d68397ff7 132
moxondesign 0:2d1d68397ff7 133 int main(void)
moxondesign 0:2d1d68397ff7 134 {
moxondesign 0:2d1d68397ff7 135 /* set up a 1 sec timer to toggle the LED */
moxondesign 0:2d1d68397ff7 136 led1 = 1;
moxondesign 0:2d1d68397ff7 137 Ticker ticker;
moxondesign 0:2d1d68397ff7 138 ticker.attach(theTickCallback, 1);
moxondesign 0:2d1d68397ff7 139
moxondesign 1:5b50f5283781 140 /* attach the hardwate UART1 data received callback */
moxondesign 1:5b50f5283781 141 dev.attach( &theUARTonDataWritten , dev.RxIrq);
moxondesign 1:5b50f5283781 142
moxondesign 0:2d1d68397ff7 143 /* initialze the BLE services */
moxondesign 0:2d1d68397ff7 144 #if DEBUG_OUTPUT
moxondesign 0:2d1d68397ff7 145 DEBUG("Initialising the nRF5x\n\r");
moxondesign 0:2d1d68397ff7 146 #endif
moxondesign 0:2d1d68397ff7 147 ble.init();
moxondesign 1:5b50f5283781 148 ble.onDisconnection(theBLEdisconnectionCallback);
moxondesign 1:5b50f5283781 149 ble.onDataWritten(theBLEonDataWritten);
moxondesign 0:2d1d68397ff7 150
moxondesign 0:2d1d68397ff7 151 /* setup the BLE advertising */
moxondesign 0:2d1d68397ff7 152 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
moxondesign 0:2d1d68397ff7 153 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
moxondesign 0:2d1d68397ff7 154 /* my names "BLART", what's yours? */
moxondesign 0:2d1d68397ff7 155 #if DEBUG_OUTPUT
moxondesign 0:2d1d68397ff7 156 DEBUG("Advertising nRF5x as BLART i.e. BLe-uART\n\r");
moxondesign 0:2d1d68397ff7 157 #endif
moxondesign 0:2d1d68397ff7 158 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
moxondesign 0:2d1d68397ff7 159 (const uint8_t *)"BLART", sizeof("BLART") - 1);
moxondesign 0:2d1d68397ff7 160 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
moxondesign 0:2d1d68397ff7 161 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
moxondesign 0:2d1d68397ff7 162
moxondesign 0:2d1d68397ff7 163 /* Advertising Rate at 1000ms, a multiple of the 0.625ms base timer */
moxondesign 0:2d1d68397ff7 164 ble.setAdvertisingInterval(1000);
moxondesign 0:2d1d68397ff7 165 ble.startAdvertising();
moxondesign 0:2d1d68397ff7 166
moxondesign 0:2d1d68397ff7 167 /* start the BLE UARTServices */
moxondesign 0:2d1d68397ff7 168 UARTService uartService(ble);
moxondesign 0:2d1d68397ff7 169 uartServicePtr = &uartService;
moxondesign 0:2d1d68397ff7 170
moxondesign 1:5b50f5283781 171 /* start the hardware UART1 */
moxondesign 1:5b50f5283781 172 //uart1.printf("Hello! My name is BLART, what's yours?\n");
moxondesign 1:5b50f5283781 173
moxondesign 1:5b50f5283781 174 /* main loop */
moxondesign 0:2d1d68397ff7 175 while (true) {
moxondesign 0:2d1d68397ff7 176 /* call wait to give other threads a chance to run */
moxondesign 0:2d1d68397ff7 177 ble.waitForEvent();
moxondesign 0:2d1d68397ff7 178 }
moxondesign 0:2d1d68397ff7 179 }