uart mirroring with everything stripped down.

Fork of MoxDes-mbed-os-BLE-USB-passthru by Thomas Moxon

Committer:
moxondesign
Date:
Fri Apr 28 20:51:34 2017 +0000
Revision:
0:2d1d68397ff7
Child:
1:5b50f5283781
Local Loopback Tested Good.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
moxondesign 0:2d1d68397ff7 1 /* mbed BLE Library - 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 0:2d1d68397ff7 11 * as well as exposing the phyical UART device providing a transparent bridge
moxondesign 0:2d1d68397ff7 12 * between the two interfaces. By default,
moxondesign 0:2d1d68397ff7 13 * UART_TX on pin P0_12, and UART-RX on pin P0_11
moxondesign 0:2d1d68397ff7 14 * "Serial dev(P0_12, P0_11); // TX,RX
moxondesign 0:2d1d68397ff7 15 *
moxondesign 0:2d1d68397ff7 16 * Set "UART_LoopBack" to enable local loopback test instead of pass-thru.
moxondesign 0:2d1d68397ff7 17 *
moxondesign 0:2d1d68397ff7 18 * Notes :
moxondesign 0:2d1d68397ff7 19 * 1) Advertises as "BLART" for BLe-uART
moxondesign 0:2d1d68397ff7 20 * 2) ble/services/UARTService is 20 byte max packets or about 4800 Baud (be patient)
moxondesign 0:2d1d68397ff7 21 * 3) transparent bridge (add command interpretor, etc.)
moxondesign 0:2d1d68397ff7 22 *
moxondesign 0:2d1d68397ff7 23 * License :
moxondesign 0:2d1d68397ff7 24 *
moxondesign 0:2d1d68397ff7 25 * Licensed under the Apache License, Version 2.0 (the "License");
moxondesign 0:2d1d68397ff7 26 * you may not use this file except in compliance with the License.
moxondesign 0:2d1d68397ff7 27 * You may obtain a copy of the License at
moxondesign 0:2d1d68397ff7 28 *
moxondesign 0:2d1d68397ff7 29 * http://www.apache.org/licenses/LICENSE-2.0
moxondesign 0:2d1d68397ff7 30 *
moxondesign 0:2d1d68397ff7 31 * Unless required by applicable law or agreed to in writing, software
moxondesign 0:2d1d68397ff7 32 * distributed under the License is distributed on an "AS IS" BASIS,
moxondesign 0:2d1d68397ff7 33 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
moxondesign 0:2d1d68397ff7 34 * See the License for the specific language governing permissions and
moxondesign 0:2d1d68397ff7 35 * limitations under the License.
moxondesign 0:2d1d68397ff7 36 */
moxondesign 0:2d1d68397ff7 37
moxondesign 0:2d1d68397ff7 38 /* mbed platform defines */
moxondesign 0:2d1d68397ff7 39 #include "mbed.h"
moxondesign 0:2d1d68397ff7 40 #include "ble/BLE.h"
moxondesign 0:2d1d68397ff7 41 #include "ble/services/UARTService.h"
moxondesign 0:2d1d68397ff7 42
moxondesign 0:2d1d68397ff7 43 /* got debug? */
moxondesign 0:2d1d68397ff7 44 /* Set this if you need debug messages on the console */
moxondesign 0:2d1d68397ff7 45 /* Negative impact on code-size and power consumption */
moxondesign 0:2d1d68397ff7 46 #define DEBUG_OUTPUT 0
moxondesign 0:2d1d68397ff7 47 #if DEBUG_OUTPUT
moxondesign 0:2d1d68397ff7 48 #define DEBUG(...) { printf(__VA_ARGS__); }
moxondesign 0:2d1d68397ff7 49 #else
moxondesign 0:2d1d68397ff7 50 #define DEBUG(...) /* no output mapped */
moxondesign 0:2d1d68397ff7 51 #endif
moxondesign 0:2d1d68397ff7 52
moxondesign 0:2d1d68397ff7 53 /* instantiate BLE services and softUART service */
moxondesign 0:2d1d68397ff7 54 BLEDevice ble;
moxondesign 0:2d1d68397ff7 55 UARTService *uartServicePtr;
moxondesign 0:2d1d68397ff7 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 0:2d1d68397ff7 60 /* Serial uart1(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 0:2d1d68397ff7 63 Serial uart1(P0_12, P0_11);
moxondesign 0:2d1d68397ff7 64
moxondesign 0:2d1d68397ff7 65 /* define some blinky LED fun */
moxondesign 0:2d1d68397ff7 66 DigitalOut led1(LED1);
moxondesign 0:2d1d68397ff7 67 //DigitalOut led2(LED2);
moxondesign 0:2d1d68397ff7 68 //DigitalOut led3(LED3);
moxondesign 0:2d1d68397ff7 69 //DigitalOut led4(LED4);
moxondesign 0:2d1d68397ff7 70
moxondesign 0:2d1d68397ff7 71 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
moxondesign 0:2d1d68397ff7 72 {
moxondesign 0:2d1d68397ff7 73 #if DEBUG_OUTPUT
moxondesign 0:2d1d68397ff7 74 DEBUG("Disconnected!\n\r");
moxondesign 0:2d1d68397ff7 75 DEBUG("Restarting the advertising process\n\r");
moxondesign 0:2d1d68397ff7 76 #endif
moxondesign 0:2d1d68397ff7 77 ble.startAdvertising();
moxondesign 0:2d1d68397ff7 78 }
moxondesign 0:2d1d68397ff7 79
moxondesign 0:2d1d68397ff7 80 void onDataWritten(const GattWriteCallbackParams *params)
moxondesign 0:2d1d68397ff7 81 {
moxondesign 0:2d1d68397ff7 82 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
moxondesign 0:2d1d68397ff7 83 uint16_t bytesRead = params->len;
moxondesign 0:2d1d68397ff7 84
moxondesign 0:2d1d68397ff7 85 #if DEBUG_OUTPUT
moxondesign 0:2d1d68397ff7 86 DEBUG("BLART received %u bytes\n\r", bytesRead);
moxondesign 0:2d1d68397ff7 87 #endif
moxondesign 0:2d1d68397ff7 88 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
moxondesign 0:2d1d68397ff7 89 }
moxondesign 0:2d1d68397ff7 90 }
moxondesign 0:2d1d68397ff7 91
moxondesign 0:2d1d68397ff7 92 void theTickCallback(void)
moxondesign 0:2d1d68397ff7 93 {
moxondesign 0:2d1d68397ff7 94 /* toggle the LED each timer tick (1 sec) */
moxondesign 0:2d1d68397ff7 95 led1 = !led1;
moxondesign 0:2d1d68397ff7 96 }
moxondesign 0:2d1d68397ff7 97
moxondesign 0:2d1d68397ff7 98 int main(void)
moxondesign 0:2d1d68397ff7 99 {
moxondesign 0:2d1d68397ff7 100 /* set up a 1 sec timer to toggle the LED */
moxondesign 0:2d1d68397ff7 101 led1 = 1;
moxondesign 0:2d1d68397ff7 102 Ticker ticker;
moxondesign 0:2d1d68397ff7 103 ticker.attach(theTickCallback, 1);
moxondesign 0:2d1d68397ff7 104
moxondesign 0:2d1d68397ff7 105 /* initialze the BLE services */
moxondesign 0:2d1d68397ff7 106 #if DEBUG_OUTPUT
moxondesign 0:2d1d68397ff7 107 DEBUG("Initialising the nRF5x\n\r");
moxondesign 0:2d1d68397ff7 108 #endif
moxondesign 0:2d1d68397ff7 109 ble.init();
moxondesign 0:2d1d68397ff7 110 ble.onDisconnection(disconnectionCallback);
moxondesign 0:2d1d68397ff7 111 ble.onDataWritten(onDataWritten);
moxondesign 0:2d1d68397ff7 112
moxondesign 0:2d1d68397ff7 113 /* setup the BLE advertising */
moxondesign 0:2d1d68397ff7 114 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
moxondesign 0:2d1d68397ff7 115 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
moxondesign 0:2d1d68397ff7 116 /* my names "BLART", what's yours? */
moxondesign 0:2d1d68397ff7 117 #if DEBUG_OUTPUT
moxondesign 0:2d1d68397ff7 118 DEBUG("Advertising nRF5x as BLART i.e. BLe-uART\n\r");
moxondesign 0:2d1d68397ff7 119 #endif
moxondesign 0:2d1d68397ff7 120 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
moxondesign 0:2d1d68397ff7 121 (const uint8_t *)"BLART", sizeof("BLART") - 1);
moxondesign 0:2d1d68397ff7 122 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
moxondesign 0:2d1d68397ff7 123 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
moxondesign 0:2d1d68397ff7 124
moxondesign 0:2d1d68397ff7 125 /* Advertising Rate at 1000ms, a multiple of the 0.625ms base timer */
moxondesign 0:2d1d68397ff7 126 ble.setAdvertisingInterval(1000);
moxondesign 0:2d1d68397ff7 127 ble.startAdvertising();
moxondesign 0:2d1d68397ff7 128
moxondesign 0:2d1d68397ff7 129 /* start the BLE UARTServices */
moxondesign 0:2d1d68397ff7 130 UARTService uartService(ble);
moxondesign 0:2d1d68397ff7 131 uartServicePtr = &uartService;
moxondesign 0:2d1d68397ff7 132
moxondesign 0:2d1d68397ff7 133 /* main */
moxondesign 0:2d1d68397ff7 134 while (true) {
moxondesign 0:2d1d68397ff7 135 /* call wait to give other threads a chance to run */
moxondesign 0:2d1d68397ff7 136 ble.waitForEvent();
moxondesign 0:2d1d68397ff7 137 }
moxondesign 0:2d1d68397ff7 138 }