This is a modified "BLE_Default_App". It doesn't use retargetStdout() which allows it to work with online Libs available on 20150123 (nRF51822, BLE_API)
Dependencies: BLE_API mbed nRF51822
App for FOTA testing:
- IO:
- Blinks a LED on platform to show App alive
- LEDs also follow buttons to show immediate response.
- BLE UART has ping message, and also echos back any received bytes (Use with Android App: nRF UART 2.0).
- It compiles and runs as FOTA and non-FOTA platform which may assist debugging
- platform "Nordic nRF51822 FOTA" for FOTA Loading
- platform "Nordic nRF51822" for mbed drive loading
- Supports mbed and non-mbed PCBs (all compiled using platform "Nordic nRF51822 FOTA")
- Compile options available for:
- nRF51822-mkit (Includes USB Serial Debug)
- nRF51822-EK
- nRF51822-Beacon (nRFBeacon)
- Atomwear (Button and LED on main PCB only)
- *If anyone gets this working with other publicly available PCBs let me know and we can add it in
main.cpp@2:0931d94f42a2, 2015-01-24 (annotated)
- Committer:
- prussell
- Date:
- Sat Jan 24 01:34:23 2015 +0000
- Revision:
- 2:0931d94f42a2
- Parent:
- 1:832e6e27cc49
- Child:
- 3:3651f1a58d9e
Enabled Data Echo on data sent from App [nRF UART v2.0]
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
prussell | 0:aa7314f3fa84 | 1 | /* mbed Microcontroller Library |
prussell | 0:aa7314f3fa84 | 2 | * Copyright (c) 2006-2013 ARM Limited |
prussell | 0:aa7314f3fa84 | 3 | * |
prussell | 0:aa7314f3fa84 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
prussell | 0:aa7314f3fa84 | 5 | * you may not use this file except in compliance with the License. |
prussell | 0:aa7314f3fa84 | 6 | * You may obtain a copy of the License at |
prussell | 0:aa7314f3fa84 | 7 | * |
prussell | 0:aa7314f3fa84 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
prussell | 0:aa7314f3fa84 | 9 | * |
prussell | 0:aa7314f3fa84 | 10 | * Unless required by applicable law or agreed to in writing, software |
prussell | 0:aa7314f3fa84 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
prussell | 0:aa7314f3fa84 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
prussell | 0:aa7314f3fa84 | 13 | * See the License for the specific language governing permissions and |
prussell | 0:aa7314f3fa84 | 14 | * limitations under the License. |
prussell | 0:aa7314f3fa84 | 15 | */ |
prussell | 0:aa7314f3fa84 | 16 | |
prussell | 0:aa7314f3fa84 | 17 | #include "mbed.h" |
prussell | 0:aa7314f3fa84 | 18 | #include "BLEDevice.h" |
prussell | 0:aa7314f3fa84 | 19 | |
prussell | 0:aa7314f3fa84 | 20 | #include "DFUService.h" |
prussell | 0:aa7314f3fa84 | 21 | #include "UARTService.h" |
prussell | 0:aa7314f3fa84 | 22 | #include "DeviceInformationService.h" |
prussell | 0:aa7314f3fa84 | 23 | |
prussell | 0:aa7314f3fa84 | 24 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
prussell | 0:aa7314f3fa84 | 25 | * it will have an impact on code-size and power consumption. */ |
prussell | 0:aa7314f3fa84 | 26 | |
prussell | 0:aa7314f3fa84 | 27 | #if NEED_CONSOLE_OUTPUT |
prussell | 0:aa7314f3fa84 | 28 | #define DEBUG(...) { printf(__VA_ARGS__); } |
prussell | 0:aa7314f3fa84 | 29 | #else |
prussell | 0:aa7314f3fa84 | 30 | #define DEBUG(...) /* nothing */ |
prussell | 0:aa7314f3fa84 | 31 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
prussell | 0:aa7314f3fa84 | 32 | |
prussell | 0:aa7314f3fa84 | 33 | BLEDevice ble; |
prussell | 0:aa7314f3fa84 | 34 | UARTService *uartServicePtr; |
prussell | 0:aa7314f3fa84 | 35 | |
prussell | 0:aa7314f3fa84 | 36 | //const char *deviceName = "DefaultApp"; |
prussell | 2:0931d94f42a2 | 37 | const char *pcDeviceName = "FOTA4.2"; |
prussell | 1:832e6e27cc49 | 38 | const char *pcPing = "f4\n"; // Need "\n" to for BLE to Tx. |
prussell | 0:aa7314f3fa84 | 39 | |
prussell | 0:aa7314f3fa84 | 40 | void periodicCallback(void) |
prussell | 0:aa7314f3fa84 | 41 | { |
prussell | 0:aa7314f3fa84 | 42 | //DEBUG("ping\r\n"); |
prussell | 0:aa7314f3fa84 | 43 | DEBUG("p4\n"); |
prussell | 0:aa7314f3fa84 | 44 | |
prussell | 0:aa7314f3fa84 | 45 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t *)pcPing, strlen(pcPing)); // BLE UART = Ping Message |
prussell | 0:aa7314f3fa84 | 46 | } |
prussell | 0:aa7314f3fa84 | 47 | |
prussell | 0:aa7314f3fa84 | 48 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
prussell | 0:aa7314f3fa84 | 49 | { |
prussell | 0:aa7314f3fa84 | 50 | //DEBUG("Disconnected!\n\r"); |
prussell | 0:aa7314f3fa84 | 51 | DEBUG("Disconnected!\n"); |
prussell | 0:aa7314f3fa84 | 52 | //DEBUG("Restarting the advertising process\n\r"); |
prussell | 0:aa7314f3fa84 | 53 | DEBUG("Restart Advertising\n"); |
prussell | 0:aa7314f3fa84 | 54 | ble.startAdvertising(); |
prussell | 0:aa7314f3fa84 | 55 | } |
prussell | 0:aa7314f3fa84 | 56 | |
prussell | 0:aa7314f3fa84 | 57 | void onDataWritten(const GattCharacteristicWriteCBParams *params) |
prussell | 0:aa7314f3fa84 | 58 | { |
prussell | 0:aa7314f3fa84 | 59 | //if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->txCharacteristic.getValueAttribute().getHandle())) |
prussell | 0:aa7314f3fa84 | 60 | if ((uartServicePtr != NULL) && (params->charHandle == uartServicePtr->getTXCharacteristicHandle())) { |
prussell | 0:aa7314f3fa84 | 61 | uint16_t bytesRead = params->len; |
prussell | 0:aa7314f3fa84 | 62 | DEBUG("BLE-UART Echo Rx[%u]=[%*s]\n", bytesRead, params->len, params->data); |
prussell | 0:aa7314f3fa84 | 63 | ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead); |
prussell | 0:aa7314f3fa84 | 64 | } else { |
prussell | 0:aa7314f3fa84 | 65 | DEBUG("Unknown Characteristic Written, Handle:%d\n", params->charHandle); |
prussell | 0:aa7314f3fa84 | 66 | } |
prussell | 0:aa7314f3fa84 | 67 | } |
prussell | 0:aa7314f3fa84 | 68 | |
prussell | 0:aa7314f3fa84 | 69 | int main(void) |
prussell | 0:aa7314f3fa84 | 70 | { |
prussell | 0:aa7314f3fa84 | 71 | Ticker ticker; |
prussell | 0:aa7314f3fa84 | 72 | ticker.attach(periodicCallback, 1); |
prussell | 0:aa7314f3fa84 | 73 | |
prussell | 0:aa7314f3fa84 | 74 | //DEBUG("Initialising BTLE transport\n\r"); |
prussell | 2:0931d94f42a2 | 75 | DEBUG("BLE Init %s\n", pcDeviceName); |
prussell | 0:aa7314f3fa84 | 76 | ble.init(); |
prussell | 0:aa7314f3fa84 | 77 | ble.onDisconnection(disconnectionCallback); |
prussell | 2:0931d94f42a2 | 78 | ble.onDataWritten(onDataWritten); |
prussell | 0:aa7314f3fa84 | 79 | |
prussell | 0:aa7314f3fa84 | 80 | ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
prussell | 0:aa7314f3fa84 | 81 | //ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)deviceName, strlen(deviceName)); |
prussell | 0:aa7314f3fa84 | 82 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)pcDeviceName, strlen(pcDeviceName)); |
prussell | 0:aa7314f3fa84 | 83 | |
prussell | 0:aa7314f3fa84 | 84 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); /* needs to be connectable to allow use of DFUService */ |
prussell | 0:aa7314f3fa84 | 85 | ble.setAdvertisingInterval(1600); /* 1s; in multiples of 0.625ms. */ |
prussell | 0:aa7314f3fa84 | 86 | ble.startAdvertising(); |
prussell | 0:aa7314f3fa84 | 87 | |
prussell | 0:aa7314f3fa84 | 88 | DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1000", "hw-rev1", "fw-rev1"); |
prussell | 0:aa7314f3fa84 | 89 | |
prussell | 0:aa7314f3fa84 | 90 | /* Enable over-the-air firmware updates. Instantiating DFUSservice introduces a |
prussell | 0:aa7314f3fa84 | 91 | * control characteristic which can be used to trigger the application to |
prussell | 0:aa7314f3fa84 | 92 | * handover control to a resident bootloader. */ |
prussell | 0:aa7314f3fa84 | 93 | DFUService dfu(ble); |
prussell | 0:aa7314f3fa84 | 94 | |
prussell | 0:aa7314f3fa84 | 95 | /* Setup a BLE service for console output. Redirect stdout to BLE-UART. */ |
prussell | 0:aa7314f3fa84 | 96 | UARTService uartService(ble); |
prussell | 0:aa7314f3fa84 | 97 | uartServicePtr = &uartService; |
prussell | 0:aa7314f3fa84 | 98 | // uartService.retargetStdout(); //PR: Doesn't work with online libs BLE_API and nRF51822 updated 2015Jan23 |
prussell | 0:aa7314f3fa84 | 99 | |
prussell | 0:aa7314f3fa84 | 100 | for (;;) { |
prussell | 0:aa7314f3fa84 | 101 | ble.waitForEvent(); |
prussell | 0:aa7314f3fa84 | 102 | } |
prussell | 0:aa7314f3fa84 | 103 | } |