BLE FOTA APP

Dependencies:   BLE_API mbed

It doesn't work with the default FOTA bootloader. It use NVIC_SystemReset() to enter a bootloader.

Committer:
yihui
Date:
Fri Oct 10 03:36:28 2014 +0000
Revision:
1:a607cd9655d7
Parent:
0:29068834cf22
use NVIC_SystemReset() to run bootloader

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 0:29068834cf22 1 /* mbed Microcontroller Library
rgrover1 0:29068834cf22 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 0:29068834cf22 3 *
rgrover1 0:29068834cf22 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 0:29068834cf22 5 * you may not use this file except in compliance with the License.
rgrover1 0:29068834cf22 6 * You may obtain a copy of the License at
rgrover1 0:29068834cf22 7 *
rgrover1 0:29068834cf22 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 0:29068834cf22 9 *
rgrover1 0:29068834cf22 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 0:29068834cf22 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 0:29068834cf22 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 0:29068834cf22 13 * See the License for the specific language governing permissions and
rgrover1 0:29068834cf22 14 * limitations under the License.
rgrover1 0:29068834cf22 15 */
rgrover1 0:29068834cf22 16
rgrover1 0:29068834cf22 17 #include "mbed.h"
rgrover1 0:29068834cf22 18 #include "BLEDevice.h"
rgrover1 0:29068834cf22 19
rgrover1 0:29068834cf22 20 #include "DFUService.h"
rgrover1 0:29068834cf22 21 #include "UARTService.h"
rgrover1 0:29068834cf22 22 #include "DeviceInformationService.h"
rgrover1 0:29068834cf22 23
rgrover1 0:29068834cf22 24 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
rgrover1 0:29068834cf22 25 * it will have an impact on code-size and power consumption. */
rgrover1 0:29068834cf22 26
rgrover1 0:29068834cf22 27 #if NEED_CONSOLE_OUTPUT
rgrover1 0:29068834cf22 28 #define DEBUG(...) { printf(__VA_ARGS__); }
rgrover1 0:29068834cf22 29 #else
rgrover1 0:29068834cf22 30 #define DEBUG(...) /* nothing */
rgrover1 0:29068834cf22 31 #endif /* #if NEED_CONSOLE_OUTPUT */
rgrover1 0:29068834cf22 32
rgrover1 0:29068834cf22 33 BLEDevice ble;
rgrover1 0:29068834cf22 34
rgrover1 0:29068834cf22 35 const char *deviceName = "DefaultApp";
rgrover1 0:29068834cf22 36
rgrover1 0:29068834cf22 37 void periodicCallback(void)
rgrover1 0:29068834cf22 38 {
rgrover1 0:29068834cf22 39 DEBUG("ping\r\n");
rgrover1 0:29068834cf22 40 }
rgrover1 0:29068834cf22 41
rgrover1 0:29068834cf22 42 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
rgrover1 0:29068834cf22 43 {
rgrover1 0:29068834cf22 44 DEBUG("Disconnected!\n\r");
rgrover1 0:29068834cf22 45 DEBUG("Restarting the advertising process\n\r");
rgrover1 0:29068834cf22 46 ble.startAdvertising();
rgrover1 0:29068834cf22 47 }
rgrover1 0:29068834cf22 48
rgrover1 0:29068834cf22 49 int main(void)
rgrover1 0:29068834cf22 50 {
rgrover1 0:29068834cf22 51 Ticker ticker;
rgrover1 0:29068834cf22 52 ticker.attach(periodicCallback, 1);
rgrover1 0:29068834cf22 53
rgrover1 0:29068834cf22 54 DEBUG("Initialising BTLE transport\n\r");
rgrover1 0:29068834cf22 55 ble.init();
rgrover1 0:29068834cf22 56 ble.onDisconnection(disconnectionCallback);
rgrover1 0:29068834cf22 57
rgrover1 0:29068834cf22 58 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 0:29068834cf22 59 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)deviceName, strlen(deviceName));
rgrover1 0:29068834cf22 60
rgrover1 0:29068834cf22 61 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); /* needs to be connectable to allow use of DFUService */
rgrover1 0:29068834cf22 62 ble.setAdvertisingInterval(1600); /* 1s; in multiples of 0.625ms. */
rgrover1 0:29068834cf22 63 ble.startAdvertising();
rgrover1 0:29068834cf22 64
rgrover1 0:29068834cf22 65 DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1000", "hw-rev1", "fw-rev1");
rgrover1 0:29068834cf22 66
rgrover1 0:29068834cf22 67 /* Enable over-the-air firmware updates. Instantiating DFUSservice introduces a
rgrover1 0:29068834cf22 68 * control characteristic which can be used to trigger the application to
rgrover1 0:29068834cf22 69 * handover control to a resident bootloader. */
rgrover1 0:29068834cf22 70 DFUService dfu(ble);
rgrover1 0:29068834cf22 71
rgrover1 0:29068834cf22 72 /* Setup a BLE service for console output. Redirect stdout to BLE-UART. */
rgrover1 0:29068834cf22 73 UARTService uartService(ble);
rgrover1 0:29068834cf22 74 uartService.retargetStdout();
rgrover1 0:29068834cf22 75
rgrover1 0:29068834cf22 76 for (;;) {
rgrover1 0:29068834cf22 77 ble.waitForEvent();
rgrover1 0:29068834cf22 78 }
rgrover1 0:29068834cf22 79 }