This is a monitor program for BLE NRF51822 chip. You can check memory, register and system information.
Dependencies: BLE_API mbed nRF51822 nRF51_Vdd
Please refer follows.
/users/kenjiArai/code/debug_tools/
/users/kenjiArai/notebook/ble--tytaiyo-yuden-module-for-mbed/
main.cpp@0:08c935d6c28f, 2016-02-09 (annotated)
- Committer:
- kenjiArai
- Date:
- Tue Feb 09 11:43:18 2016 +0000
- Revision:
- 0:08c935d6c28f
- Child:
- 2:6a659d71d71f
This is a monitor program for BLE NRF51822 chip. You can check memory, register and system information.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenjiArai | 0:08c935d6c28f | 1 | /* mbed Microcontroller Library |
kenjiArai | 0:08c935d6c28f | 2 | * Copyright (c) 2006-2014 ARM Limited |
kenjiArai | 0:08c935d6c28f | 3 | * |
kenjiArai | 0:08c935d6c28f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kenjiArai | 0:08c935d6c28f | 5 | * you may not use this file except in compliance with the License. |
kenjiArai | 0:08c935d6c28f | 6 | * You may obtain a copy of the License at |
kenjiArai | 0:08c935d6c28f | 7 | * |
kenjiArai | 0:08c935d6c28f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kenjiArai | 0:08c935d6c28f | 9 | * |
kenjiArai | 0:08c935d6c28f | 10 | * Unless required by applicable law or agreed to in writing, software |
kenjiArai | 0:08c935d6c28f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kenjiArai | 0:08c935d6c28f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kenjiArai | 0:08c935d6c28f | 13 | * See the License for the specific language governing permissions and |
kenjiArai | 0:08c935d6c28f | 14 | * limitations under the License. |
kenjiArai | 0:08c935d6c28f | 15 | */ |
kenjiArai | 0:08c935d6c28f | 16 | |
kenjiArai | 0:08c935d6c28f | 17 | /* |
kenjiArai | 0:08c935d6c28f | 18 | * Modified by Kenji Arai |
kenjiArai | 0:08c935d6c28f | 19 | * http://www.page.sannet.ne.jp/kenjia/index.html |
kenjiArai | 0:08c935d6c28f | 20 | * http://mbed.org/users/kenjiArai/ |
kenjiArai | 0:08c935d6c28f | 21 | * |
kenjiArai | 0:08c935d6c28f | 22 | * Started: January 3rd, 2016 |
kenjiArai | 0:08c935d6c28f | 23 | * Revised: Feburary 5th, 2016 |
kenjiArai | 0:08c935d6c28f | 24 | */ |
kenjiArai | 0:08c935d6c28f | 25 | |
kenjiArai | 0:08c935d6c28f | 26 | // Include --------------------------------------------------------------------------------------- |
kenjiArai | 0:08c935d6c28f | 27 | #include "mbed.h" |
kenjiArai | 0:08c935d6c28f | 28 | #include "BLE.h" |
kenjiArai | 0:08c935d6c28f | 29 | #include "BatteryService.h" |
kenjiArai | 0:08c935d6c28f | 30 | #include "nRF51_Vdd.h" |
kenjiArai | 0:08c935d6c28f | 31 | |
kenjiArai | 0:08c935d6c28f | 32 | // Definition ------------------------------------------------------------------------------------ |
kenjiArai | 0:08c935d6c28f | 33 | #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console; |
kenjiArai | 0:08c935d6c28f | 34 | * it will have an impact on code-size and power consumption. */ |
kenjiArai | 0:08c935d6c28f | 35 | |
kenjiArai | 0:08c935d6c28f | 36 | #if NEED_CONSOLE_OUTPUT |
kenjiArai | 0:08c935d6c28f | 37 | #define DEBUG(...) { printf(__VA_ARGS__); } |
kenjiArai | 0:08c935d6c28f | 38 | #else |
kenjiArai | 0:08c935d6c28f | 39 | #define DEBUG(...) /* nothing */ |
kenjiArai | 0:08c935d6c28f | 40 | #endif /* #if NEED_CONSOLE_OUTPUT */ |
kenjiArai | 0:08c935d6c28f | 41 | |
kenjiArai | 0:08c935d6c28f | 42 | // Object ---------------------------------------------------------------------------------------- |
kenjiArai | 0:08c935d6c28f | 43 | BLE ble; |
kenjiArai | 0:08c935d6c28f | 44 | DigitalOut led1(LED1, 1); |
kenjiArai | 0:08c935d6c28f | 45 | Serial pc(USBTX, USBRX); |
kenjiArai | 0:08c935d6c28f | 46 | I2C i2c(P0_29,P0_28); |
kenjiArai | 0:08c935d6c28f | 47 | SPI spi(P0_20,P0_22,P0_25); |
kenjiArai | 0:08c935d6c28f | 48 | nRF51_Vdd vdd(3.6f, 2.6f); |
kenjiArai | 0:08c935d6c28f | 49 | Ticker t; |
kenjiArai | 0:08c935d6c28f | 50 | |
kenjiArai | 0:08c935d6c28f | 51 | // ROM / Constant data --------------------------------------------------------------------------- |
kenjiArai | 0:08c935d6c28f | 52 | const char *deviceName = "mbedMon"; |
kenjiArai | 0:08c935d6c28f | 53 | |
kenjiArai | 0:08c935d6c28f | 54 | // RAM ------------------------------------------------------------------------------------------- |
kenjiArai | 0:08c935d6c28f | 55 | BatteryService *batteryService = NULL; |
kenjiArai | 0:08c935d6c28f | 56 | uint8_t batteryLevel = 50; |
kenjiArai | 0:08c935d6c28f | 57 | |
kenjiArai | 0:08c935d6c28f | 58 | // Function prototypes --------------------------------------------------------------------------- |
kenjiArai | 0:08c935d6c28f | 59 | extern void debug_interface(uint8_t mode); |
kenjiArai | 0:08c935d6c28f | 60 | |
kenjiArai | 0:08c935d6c28f | 61 | //------------------------------------------------------------------------------------------------- |
kenjiArai | 0:08c935d6c28f | 62 | // Control Program |
kenjiArai | 0:08c935d6c28f | 63 | //------------------------------------------------------------------------------------------------- |
kenjiArai | 0:08c935d6c28f | 64 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *disconnectionParams) |
kenjiArai | 0:08c935d6c28f | 65 | { |
kenjiArai | 0:08c935d6c28f | 66 | DEBUG("Disconnected handle %u!\n\r", disconnectionParams->handle); |
kenjiArai | 0:08c935d6c28f | 67 | DEBUG("Restarting the advertising process\n\r"); |
kenjiArai | 0:08c935d6c28f | 68 | BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising |
kenjiArai | 0:08c935d6c28f | 69 | } |
kenjiArai | 0:08c935d6c28f | 70 | |
kenjiArai | 0:08c935d6c28f | 71 | void blink(void) |
kenjiArai | 0:08c935d6c28f | 72 | { |
kenjiArai | 0:08c935d6c28f | 73 | led1 = !led1; |
kenjiArai | 0:08c935d6c28f | 74 | } |
kenjiArai | 0:08c935d6c28f | 75 | |
kenjiArai | 0:08c935d6c28f | 76 | int main(void) |
kenjiArai | 0:08c935d6c28f | 77 | { |
kenjiArai | 0:08c935d6c28f | 78 | t.attach(blink, 1.0f); |
kenjiArai | 0:08c935d6c28f | 79 | |
kenjiArai | 0:08c935d6c28f | 80 | DEBUG("Initialising the nRF51822\r\n"); |
kenjiArai | 0:08c935d6c28f | 81 | ble.init(); |
kenjiArai | 0:08c935d6c28f | 82 | ble.setDeviceName((const uint8_t *)deviceName); |
kenjiArai | 0:08c935d6c28f | 83 | ble.onDisconnection(disconnectionCallback); |
kenjiArai | 0:08c935d6c28f | 84 | |
kenjiArai | 0:08c935d6c28f | 85 | batteryService = new BatteryService(ble, batteryLevel); |
kenjiArai | 0:08c935d6c28f | 86 | |
kenjiArai | 0:08c935d6c28f | 87 | /* setup advertising */ |
kenjiArai | 0:08c935d6c28f | 88 | ble.accumulateAdvertisingPayload |
kenjiArai | 0:08c935d6c28f | 89 | (GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
kenjiArai | 0:08c935d6c28f | 90 | ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
kenjiArai | 0:08c935d6c28f | 91 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
kenjiArai | 0:08c935d6c28f | 92 | (const uint8_t *)deviceName, |
kenjiArai | 0:08c935d6c28f | 93 | strlen(deviceName)); |
kenjiArai | 0:08c935d6c28f | 94 | ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */ |
kenjiArai | 0:08c935d6c28f | 95 | ble.startAdvertising(); |
kenjiArai | 0:08c935d6c28f | 96 | |
kenjiArai | 0:08c935d6c28f | 97 | // ****************************************************** |
kenjiArai | 0:08c935d6c28f | 98 | // Here is a example to impliment the monitor |
kenjiArai | 0:08c935d6c28f | 99 | // para: 1 -> goto montor and never comeback |
kenjiArai | 0:08c935d6c28f | 100 | // ****************************************************** |
kenjiArai | 0:08c935d6c28f | 101 | // debug_interface(1); |
kenjiArai | 0:08c935d6c28f | 102 | |
kenjiArai | 0:08c935d6c28f | 103 | while (true) { |
kenjiArai | 0:08c935d6c28f | 104 | // this will return upon any system event (such as an interrupt or a ticker wakeup) |
kenjiArai | 0:08c935d6c28f | 105 | ble.waitForEvent(); |
kenjiArai | 0:08c935d6c28f | 106 | |
kenjiArai | 0:08c935d6c28f | 107 | // the magic battery processing |
kenjiArai | 0:08c935d6c28f | 108 | batteryLevel++; |
kenjiArai | 0:08c935d6c28f | 109 | #if 1 |
kenjiArai | 0:08c935d6c28f | 110 | if (batteryLevel > 100) { |
kenjiArai | 0:08c935d6c28f | 111 | batteryLevel = 20; |
kenjiArai | 0:08c935d6c28f | 112 | } |
kenjiArai | 0:08c935d6c28f | 113 | DEBUG("Vdd: %4.3f\r\n", vdd.read_real_value()); |
kenjiArai | 0:08c935d6c28f | 114 | #else |
kenjiArai | 0:08c935d6c28f | 115 | DEBUG("Vdd: %f\r\n", vdd.read_real_value()); |
kenjiArai | 0:08c935d6c28f | 116 | batteryLevel = vdd.read(); |
kenjiArai | 0:08c935d6c28f | 117 | #endif |
kenjiArai | 0:08c935d6c28f | 118 | |
kenjiArai | 0:08c935d6c28f | 119 | batteryService->updateBatteryLevel(batteryLevel); |
kenjiArai | 0:08c935d6c28f | 120 | DEBUG("battery=%d\r\n", batteryLevel); |
kenjiArai | 0:08c935d6c28f | 121 | |
kenjiArai | 0:08c935d6c28f | 122 | // ****************************************************** |
kenjiArai | 0:08c935d6c28f | 123 | // Here is a example to impliment the monitor |
kenjiArai | 0:08c935d6c28f | 124 | // para: 0 -> if Uart RX Data is ready then goto montor |
kenjiArai | 0:08c935d6c28f | 125 | // ****************************************************** |
kenjiArai | 0:08c935d6c28f | 126 | debug_interface(0); |
kenjiArai | 0:08c935d6c28f | 127 | } |
kenjiArai | 0:08c935d6c28f | 128 | } |