Test code for a generic nrf51822

Dependencies:   mbed AES BLE_API nRF51822 smallAES

Committer:
f3d
Date:
Tue Feb 18 11:41:19 2020 +0000
Revision:
0:df3f7838f957
Generic NRF51822 example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
f3d 0:df3f7838f957 1 /* mbed Microcontroller Library
f3d 0:df3f7838f957 2 * Copyright (c) 2006-2013 ARM Limited
f3d 0:df3f7838f957 3 *
f3d 0:df3f7838f957 4 * Licensed under the Apache License, Version 2.0 (the "License");
f3d 0:df3f7838f957 5 * you may not use this file except in compliance with the License.
f3d 0:df3f7838f957 6 * You may obtain a copy of the License at
f3d 0:df3f7838f957 7 *
f3d 0:df3f7838f957 8 * http://www.apache.org/licenses/LICENSE-2.0
f3d 0:df3f7838f957 9 *
f3d 0:df3f7838f957 10 * Unless required by applicable law or agreed to in writing, software
f3d 0:df3f7838f957 11 * distributed under the License is distributed on an "AS IS" BASIS,
f3d 0:df3f7838f957 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
f3d 0:df3f7838f957 13 * See the License for the specific language governing permissions and
f3d 0:df3f7838f957 14 * limitations under the License.
f3d 0:df3f7838f957 15 */
f3d 0:df3f7838f957 16
f3d 0:df3f7838f957 17 #include "mbed.h"
f3d 0:df3f7838f957 18 #include "ble/BLE.h"
f3d 0:df3f7838f957 19 #include "LEDService.h"
f3d 0:df3f7838f957 20 #include "TemperatureService.h"
f3d 0:df3f7838f957 21 Serial pc(P0_9, P0_11);
f3d 0:df3f7838f957 22
f3d 0:df3f7838f957 23 /*
f3d 0:df3f7838f957 24 * All the LEDs on the micro:bit are part of the LED Matrix,
f3d 0:df3f7838f957 25 * In order to get simple blinking behaviour, we set column 0
f3d 0:df3f7838f957 26 * to be permanently at ground. If you want to use the LEDs as
f3d 0:df3f7838f957 27 * a screen, there is a display driver in the micro:bit 'DAL',
f3d 0:df3f7838f957 28 */
f3d 0:df3f7838f957 29 DigitalOut col1(P0_4, 0);
f3d 0:df3f7838f957 30 DigitalOut alivenessLED(P0_13, 0);
f3d 0:df3f7838f957 31 DigitalOut actuatedLED(P0_14, 0);
f3d 0:df3f7838f957 32 DigitalOut myled(P0_21);
f3d 0:df3f7838f957 33
f3d 0:df3f7838f957 34 const static char DEVICE_NAME[] = "NRF51811";
f3d 0:df3f7838f957 35 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
f3d 0:df3f7838f957 36
f3d 0:df3f7838f957 37 LEDService *ledServicePtr;
f3d 0:df3f7838f957 38 TempService *TempServicePtr;
f3d 0:df3f7838f957 39 Ticker ticker;
f3d 0:df3f7838f957 40
f3d 0:df3f7838f957 41 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
f3d 0:df3f7838f957 42 {
f3d 0:df3f7838f957 43 BLE::Instance().gap().startAdvertising();
f3d 0:df3f7838f957 44 }
f3d 0:df3f7838f957 45
f3d 0:df3f7838f957 46 void periodicCallback(void)
f3d 0:df3f7838f957 47 {
f3d 0:df3f7838f957 48 alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
f3d 0:df3f7838f957 49 }
f3d 0:df3f7838f957 50
f3d 0:df3f7838f957 51 /**
f3d 0:df3f7838f957 52 * This callback allows the LEDService to receive updates to the ledState Characteristic.
f3d 0:df3f7838f957 53 *
f3d 0:df3f7838f957 54 * @param[in] params
f3d 0:df3f7838f957 55 * Information about the characterisitc being updated.
f3d 0:df3f7838f957 56 */
f3d 0:df3f7838f957 57 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
f3d 0:df3f7838f957 58 if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
f3d 0:df3f7838f957 59 actuatedLED = *(params->data);
f3d 0:df3f7838f957 60 }
f3d 0:df3f7838f957 61 }
f3d 0:df3f7838f957 62 void onDataReadCallback(const GattReadCallbackParams *params) {
f3d 0:df3f7838f957 63 if ((params->handle==TempServicePtr->getValueHandle())) {
f3d 0:df3f7838f957 64 TempServicePtr->updateValue(42);
f3d 0:df3f7838f957 65 }
f3d 0:df3f7838f957 66 }
f3d 0:df3f7838f957 67 /**
f3d 0:df3f7838f957 68 * This function is called when the ble initialization process has failed
f3d 0:df3f7838f957 69 */
f3d 0:df3f7838f957 70 void onBleInitError(BLE &ble, ble_error_t error)
f3d 0:df3f7838f957 71 {
f3d 0:df3f7838f957 72 /* Initialization error handling should go here */
f3d 0:df3f7838f957 73 }
f3d 0:df3f7838f957 74
f3d 0:df3f7838f957 75 /**
f3d 0:df3f7838f957 76 * Callback triggered when the ble initialization process has finished
f3d 0:df3f7838f957 77 */
f3d 0:df3f7838f957 78 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
f3d 0:df3f7838f957 79 {
f3d 0:df3f7838f957 80 BLE& ble = params->ble;
f3d 0:df3f7838f957 81 ble_error_t error = params->error;
f3d 0:df3f7838f957 82
f3d 0:df3f7838f957 83 if (error != BLE_ERROR_NONE) {
f3d 0:df3f7838f957 84 /* In case of error, forward the error handling to onBleInitError */
f3d 0:df3f7838f957 85 onBleInitError(ble, error);
f3d 0:df3f7838f957 86 return;
f3d 0:df3f7838f957 87 }
f3d 0:df3f7838f957 88
f3d 0:df3f7838f957 89 /* Ensure that it is the default instance of BLE */
f3d 0:df3f7838f957 90 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
f3d 0:df3f7838f957 91 return;
f3d 0:df3f7838f957 92 }
f3d 0:df3f7838f957 93
f3d 0:df3f7838f957 94 ble.gap().onDisconnection(disconnectionCallback);
f3d 0:df3f7838f957 95 ble.gattServer().onDataWritten(onDataWrittenCallback);
f3d 0:df3f7838f957 96 ble.gattServer().onDataRead(onDataReadCallback);
f3d 0:df3f7838f957 97 bool initialValueForLEDCharacteristic = false;
f3d 0:df3f7838f957 98 ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
f3d 0:df3f7838f957 99
f3d 0:df3f7838f957 100 /* setup advertising */
f3d 0:df3f7838f957 101 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
f3d 0:df3f7838f957 102 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
f3d 0:df3f7838f957 103 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
f3d 0:df3f7838f957 104 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
f3d 0:df3f7838f957 105 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
f3d 0:df3f7838f957 106 ble.gap().startAdvertising();
f3d 0:df3f7838f957 107 }
f3d 0:df3f7838f957 108
f3d 0:df3f7838f957 109 int main(void)
f3d 0:df3f7838f957 110 {
f3d 0:df3f7838f957 111 ticker.attach(periodicCallback, 1); /* Blink LED every second */
f3d 0:df3f7838f957 112
f3d 0:df3f7838f957 113 BLE &ble = BLE::Instance();
f3d 0:df3f7838f957 114 ble.init(bleInitComplete);
f3d 0:df3f7838f957 115
f3d 0:df3f7838f957 116 /* SpinWait for initialization to complete. This is necessary because the
f3d 0:df3f7838f957 117 /* BLE object is used in the main loop below. */
f3d 0:df3f7838f957 118 while (ble.hasInitialized() == false) {
f3d 0:df3f7838f957 119 myled = 1;
f3d 0:df3f7838f957 120 wait(0.2);
f3d 0:df3f7838f957 121 myled = 0;
f3d 0:df3f7838f957 122 wait(0.1);
f3d 0:df3f7838f957 123
f3d 0:df3f7838f957 124 /* spin loop */ }
f3d 0:df3f7838f957 125
f3d 0:df3f7838f957 126 while (true) {
f3d 0:df3f7838f957 127 myled = 1;
f3d 0:df3f7838f957 128 wait(0.1);
f3d 0:df3f7838f957 129 myled = 0;
f3d 0:df3f7838f957 130 wait(0.1);
f3d 0:df3f7838f957 131 pc.printf("Hello\r\n");
f3d 0:df3f7838f957 132 // ble.waitForEvent();
f3d 0:df3f7838f957 133 }
f3d 0:df3f7838f957 134 }