NRF51822 as bluetooth to UART

Dependencies:   BLE_API BLE_LoopbackUART mbed nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Committer:
zcw607
Date:
Tue Feb 24 01:46:20 2015 +0000
Revision:
12:fc9171411cf5
Parent:
10:633cec718bf4
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:e910d9bb040f 1 /* mbed Microcontroller Library
yihui 0:e910d9bb040f 2 * Copyright (c) 2006-2013 ARM Limited
yihui 0:e910d9bb040f 3 *
yihui 0:e910d9bb040f 4 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 0:e910d9bb040f 5 * you may not use this file except in compliance with the License.
yihui 0:e910d9bb040f 6 * You may obtain a copy of the License at
yihui 0:e910d9bb040f 7 *
yihui 0:e910d9bb040f 8 * http://www.apache.org/licenses/LICENSE-2.0
yihui 0:e910d9bb040f 9 *
yihui 0:e910d9bb040f 10 * Unless required by applicable law or agreed to in writing, software
yihui 0:e910d9bb040f 11 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 0:e910d9bb040f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 0:e910d9bb040f 13 * See the License for the specific language governing permissions and
yihui 0:e910d9bb040f 14 * limitations under the License.
yihui 0:e910d9bb040f 15 */
yihui 0:e910d9bb040f 16
yihui 0:e910d9bb040f 17 #include "mbed.h"
Rohit Grover 2:e060367b9024 18 #include "BLEDevice.h"
yihui 0:e910d9bb040f 19
rgrover1 6:e0fc9072e853 20 #include "UARTService.h"
yihui 0:e910d9bb040f 21
rgrover1 6:e0fc9072e853 22 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
Rohit Grover 2:e060367b9024 23 * it will have an impact on code-size and power consumption. */
yihui 0:e910d9bb040f 24
Rohit Grover 2:e060367b9024 25 #if NEED_CONSOLE_OUTPUT
rgrover1 6:e0fc9072e853 26 #define DEBUG(...) { printf(__VA_ARGS__); }
yihui 0:e910d9bb040f 27 #else
Rohit Grover 2:e060367b9024 28 #define DEBUG(...) /* nothing */
Rohit Grover 2:e060367b9024 29 #endif /* #if NEED_CONSOLE_OUTPUT */
yihui 0:e910d9bb040f 30
Rohit Grover 2:e060367b9024 31 BLEDevice ble;
Rohit Grover 2:e060367b9024 32 DigitalOut led1(LED1);
zcw607 12:fc9171411cf5 33 Serial pc(USBTX, USBRX);
yihui 0:e910d9bb040f 34
zcw607 12:fc9171411cf5 35 static uint8_t rx_buf[20];
zcw607 12:fc9171411cf5 36 static uint8_t rx_len=0;
rgrover1 6:e0fc9072e853 37 UARTService *uartServicePtr;
yihui 0:e910d9bb040f 38
rgrover1 5:4bc41267a03a 39 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
yihui 0:e910d9bb040f 40 {
Rohit Grover 2:e060367b9024 41 DEBUG("Disconnected!\n\r");
Rohit Grover 2:e060367b9024 42 DEBUG("Restarting the advertising process\n\r");
Rohit Grover 2:e060367b9024 43 ble.startAdvertising();
Rohit Grover 2:e060367b9024 44 }
yihui 0:e910d9bb040f 45
rgrover1 6:e0fc9072e853 46 void onDataWritten(const GattCharacteristicWriteCBParams *params)
yihui 0:e910d9bb040f 47 {
rgrover1 6:e0fc9072e853 48 if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) {
rgrover1 5:4bc41267a03a 49 uint16_t bytesRead = params->len;
zcw607 12:fc9171411cf5 50 for(uint16_t index=0; index<bytesRead; index++)
zcw607 12:fc9171411cf5 51 {
zcw607 12:fc9171411cf5 52 pc.putc(params->data[index]);
zcw607 12:fc9171411cf5 53 }
yihui 0:e910d9bb040f 54 }
Rohit Grover 2:e060367b9024 55 }
yihui 0:e910d9bb040f 56
zcw607 12:fc9171411cf5 57 void uartCB(void)
zcw607 12:fc9171411cf5 58 {
zcw607 12:fc9171411cf5 59 while(pc.readable())
zcw607 12:fc9171411cf5 60 {
zcw607 12:fc9171411cf5 61 rx_buf[rx_len++] = pc.getc();
zcw607 12:fc9171411cf5 62 if(rx_len>=20 || rx_buf[rx_len-1]=='\0' || rx_buf[rx_len-1]=='\n')
zcw607 12:fc9171411cf5 63 {
zcw607 12:fc9171411cf5 64 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), rx_buf, rx_len);
zcw607 12:fc9171411cf5 65 rx_len = 0;
zcw607 12:fc9171411cf5 66 break;
zcw607 12:fc9171411cf5 67 }
zcw607 12:fc9171411cf5 68 }
zcw607 12:fc9171411cf5 69 }
Rohit Grover 2:e060367b9024 70 void periodicCallback(void)
Rohit Grover 2:e060367b9024 71 {
rgrover1 6:e0fc9072e853 72 led1 = !led1;
Rohit Grover 2:e060367b9024 73 }
yihui 0:e910d9bb040f 74
yihui 0:e910d9bb040f 75 int main(void)
yihui 0:e910d9bb040f 76 {
Rohit Grover 2:e060367b9024 77 led1 = 1;
zcw607 12:fc9171411cf5 78 pc.baud(115200);
zcw607 12:fc9171411cf5 79
zcw607 12:fc9171411cf5 80 pc.attach( uartCB , pc.RxIrq);
Rohit Grover 2:e060367b9024 81 Ticker ticker;
Rohit Grover 2:e060367b9024 82 ticker.attach(periodicCallback, 1);
yihui 0:e910d9bb040f 83
Rohit Grover 2:e060367b9024 84 DEBUG("Initialising the nRF51822\n\r");
Rohit Grover 2:e060367b9024 85 ble.init();
Rohit Grover 2:e060367b9024 86 ble.onDisconnection(disconnectionCallback);
Rohit Grover 2:e060367b9024 87 ble.onDataWritten(onDataWritten);
yihui 0:e910d9bb040f 88
Rohit Grover 2:e060367b9024 89 /* setup advertising */
Rohit Grover 2:e060367b9024 90 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
Rohit Grover 2:e060367b9024 91 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Rohit Grover 2:e060367b9024 92 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
rgrover1 6:e0fc9072e853 93 (const uint8_t *)"BLE UART", sizeof("BLE UART") - 1);
Rohit Grover 2:e060367b9024 94 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
rgrover1 6:e0fc9072e853 95 (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
yihui 0:e910d9bb040f 96
rgrover1 10:633cec718bf4 97 ble.setAdvertisingInterval(Gap::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000));
Rohit Grover 2:e060367b9024 98 ble.startAdvertising();
yihui 0:e910d9bb040f 99
rgrover1 6:e0fc9072e853 100 UARTService uartService(ble);
rgrover1 6:e0fc9072e853 101 uartServicePtr = &uartService;
yihui 0:e910d9bb040f 102
Rohit Grover 2:e060367b9024 103 while (true) {
Rohit Grover 2:e060367b9024 104 ble.waitForEvent();
yihui 0:e910d9bb040f 105 }
yihui 0:e910d9bb040f 106 }