robo 8080
/
BLE_TEST_konashi
konashi/SBBLEのテスト
Fork of BLE_LoopbackUART by
nRF51822/nRF51822n.cpp@8:a62b8f7d5dcf, 2014-08-17 (annotated)
- Committer:
- robo8080
- Date:
- Sun Aug 17 00:53:23 2014 +0000
- Revision:
- 8:a62b8f7d5dcf
- Parent:
- 5:61109bce11fe
DeviceName??
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robo8080 | 5:61109bce11fe | 1 | /* mbed Microcontroller Library |
robo8080 | 5:61109bce11fe | 2 | * Copyright (c) 2006-2013 ARM Limited |
robo8080 | 5:61109bce11fe | 3 | * |
robo8080 | 5:61109bce11fe | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
robo8080 | 5:61109bce11fe | 5 | * you may not use this file except in compliance with the License. |
robo8080 | 5:61109bce11fe | 6 | * You may obtain a copy of the License at |
robo8080 | 5:61109bce11fe | 7 | * |
robo8080 | 5:61109bce11fe | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
robo8080 | 5:61109bce11fe | 9 | * |
robo8080 | 5:61109bce11fe | 10 | * Unless required by applicable law or agreed to in writing, software |
robo8080 | 5:61109bce11fe | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
robo8080 | 5:61109bce11fe | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
robo8080 | 5:61109bce11fe | 13 | * See the License for the specific language governing permissions and |
robo8080 | 5:61109bce11fe | 14 | * limitations under the License. |
robo8080 | 5:61109bce11fe | 15 | */ |
robo8080 | 5:61109bce11fe | 16 | |
robo8080 | 5:61109bce11fe | 17 | #include "mbed.h" |
robo8080 | 5:61109bce11fe | 18 | #include "nRF51822n.h" |
robo8080 | 5:61109bce11fe | 19 | #include "nrf_soc.h" |
robo8080 | 5:61109bce11fe | 20 | |
robo8080 | 5:61109bce11fe | 21 | #include "btle/btle.h" |
robo8080 | 5:61109bce11fe | 22 | |
robo8080 | 5:61109bce11fe | 23 | /** |
robo8080 | 5:61109bce11fe | 24 | * The singleton which represents the nRF51822 transport for the BLEDevice. |
robo8080 | 5:61109bce11fe | 25 | */ |
robo8080 | 5:61109bce11fe | 26 | static nRF51822n deviceInstance; |
robo8080 | 5:61109bce11fe | 27 | |
robo8080 | 5:61109bce11fe | 28 | /** |
robo8080 | 5:61109bce11fe | 29 | * BLE-API requires an implementation of the following function in order to |
robo8080 | 5:61109bce11fe | 30 | * obtain its transport handle. |
robo8080 | 5:61109bce11fe | 31 | */ |
robo8080 | 5:61109bce11fe | 32 | BLEDeviceInstanceBase * |
robo8080 | 5:61109bce11fe | 33 | createBLEDeviceInstance(void) |
robo8080 | 5:61109bce11fe | 34 | { |
robo8080 | 5:61109bce11fe | 35 | return (&deviceInstance); |
robo8080 | 5:61109bce11fe | 36 | } |
robo8080 | 5:61109bce11fe | 37 | |
robo8080 | 5:61109bce11fe | 38 | /**************************************************************************/ |
robo8080 | 5:61109bce11fe | 39 | /*! |
robo8080 | 5:61109bce11fe | 40 | @brief Constructor |
robo8080 | 5:61109bce11fe | 41 | */ |
robo8080 | 5:61109bce11fe | 42 | /**************************************************************************/ |
robo8080 | 5:61109bce11fe | 43 | nRF51822n::nRF51822n(void) |
robo8080 | 5:61109bce11fe | 44 | { |
robo8080 | 5:61109bce11fe | 45 | } |
robo8080 | 5:61109bce11fe | 46 | |
robo8080 | 5:61109bce11fe | 47 | /**************************************************************************/ |
robo8080 | 5:61109bce11fe | 48 | /*! |
robo8080 | 5:61109bce11fe | 49 | @brief Destructor |
robo8080 | 5:61109bce11fe | 50 | */ |
robo8080 | 5:61109bce11fe | 51 | /**************************************************************************/ |
robo8080 | 5:61109bce11fe | 52 | nRF51822n::~nRF51822n(void) |
robo8080 | 5:61109bce11fe | 53 | { |
robo8080 | 5:61109bce11fe | 54 | } |
robo8080 | 5:61109bce11fe | 55 | |
robo8080 | 5:61109bce11fe | 56 | const char *nRF51822n::getVersion(void) |
robo8080 | 5:61109bce11fe | 57 | { |
robo8080 | 5:61109bce11fe | 58 | static char versionString[10]; |
robo8080 | 5:61109bce11fe | 59 | static bool versionFetched = false; |
robo8080 | 5:61109bce11fe | 60 | |
robo8080 | 5:61109bce11fe | 61 | if (!versionFetched) { |
robo8080 | 5:61109bce11fe | 62 | ble_version_t version; |
robo8080 | 5:61109bce11fe | 63 | if (sd_ble_version_get(&version) == NRF_SUCCESS) { |
robo8080 | 5:61109bce11fe | 64 | snprintf(versionString, sizeof(versionString), "%u.%u", version.version_number, version.subversion_number); |
robo8080 | 5:61109bce11fe | 65 | versionFetched = true; |
robo8080 | 5:61109bce11fe | 66 | } else { |
robo8080 | 5:61109bce11fe | 67 | strncpy(versionString, "unknown", sizeof(versionString)); |
robo8080 | 5:61109bce11fe | 68 | } |
robo8080 | 5:61109bce11fe | 69 | } |
robo8080 | 5:61109bce11fe | 70 | |
robo8080 | 5:61109bce11fe | 71 | return versionString; |
robo8080 | 5:61109bce11fe | 72 | } |
robo8080 | 5:61109bce11fe | 73 | |
robo8080 | 5:61109bce11fe | 74 | /* (Valid values are -40, -20, -16, -12, -8, -4, 0, 4) */ |
robo8080 | 5:61109bce11fe | 75 | ble_error_t nRF51822n::setTxPower(int8_t txPower) |
robo8080 | 5:61109bce11fe | 76 | { |
robo8080 | 5:61109bce11fe | 77 | unsigned rc; |
robo8080 | 5:61109bce11fe | 78 | if ((rc = sd_ble_gap_tx_power_set(txPower)) != NRF_SUCCESS) { |
robo8080 | 5:61109bce11fe | 79 | switch (rc) { |
robo8080 | 5:61109bce11fe | 80 | case NRF_ERROR_BUSY: |
robo8080 | 5:61109bce11fe | 81 | return BLE_STACK_BUSY; |
robo8080 | 5:61109bce11fe | 82 | case NRF_ERROR_INVALID_PARAM: |
robo8080 | 5:61109bce11fe | 83 | default: |
robo8080 | 5:61109bce11fe | 84 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
robo8080 | 5:61109bce11fe | 85 | } |
robo8080 | 5:61109bce11fe | 86 | } |
robo8080 | 5:61109bce11fe | 87 | |
robo8080 | 5:61109bce11fe | 88 | return BLE_ERROR_NONE; |
robo8080 | 5:61109bce11fe | 89 | } |
robo8080 | 5:61109bce11fe | 90 | |
robo8080 | 5:61109bce11fe | 91 | /**************************************************************************/ |
robo8080 | 5:61109bce11fe | 92 | /*! |
robo8080 | 5:61109bce11fe | 93 | @brief Initialises anything required to start using BLE |
robo8080 | 5:61109bce11fe | 94 | |
robo8080 | 5:61109bce11fe | 95 | @returns ble_error_t |
robo8080 | 5:61109bce11fe | 96 | |
robo8080 | 5:61109bce11fe | 97 | @retval BLE_ERROR_NONE |
robo8080 | 5:61109bce11fe | 98 | Everything executed properly |
robo8080 | 5:61109bce11fe | 99 | |
robo8080 | 5:61109bce11fe | 100 | @section EXAMPLE |
robo8080 | 5:61109bce11fe | 101 | |
robo8080 | 5:61109bce11fe | 102 | @code |
robo8080 | 5:61109bce11fe | 103 | |
robo8080 | 5:61109bce11fe | 104 | @endcode |
robo8080 | 5:61109bce11fe | 105 | */ |
robo8080 | 5:61109bce11fe | 106 | /**************************************************************************/ |
robo8080 | 5:61109bce11fe | 107 | ble_error_t nRF51822n::init(void) |
robo8080 | 5:61109bce11fe | 108 | { |
robo8080 | 5:61109bce11fe | 109 | /* ToDo: Clear memory contents, reset the SD, etc. */ |
robo8080 | 5:61109bce11fe | 110 | btle_init(); |
robo8080 | 5:61109bce11fe | 111 | |
robo8080 | 5:61109bce11fe | 112 | reset(); |
robo8080 | 5:61109bce11fe | 113 | |
robo8080 | 5:61109bce11fe | 114 | return BLE_ERROR_NONE; |
robo8080 | 5:61109bce11fe | 115 | } |
robo8080 | 5:61109bce11fe | 116 | |
robo8080 | 5:61109bce11fe | 117 | /**************************************************************************/ |
robo8080 | 5:61109bce11fe | 118 | /*! |
robo8080 | 5:61109bce11fe | 119 | @brief Resets the BLE HW, removing any existing services and |
robo8080 | 5:61109bce11fe | 120 | characteristics |
robo8080 | 5:61109bce11fe | 121 | |
robo8080 | 5:61109bce11fe | 122 | @returns ble_error_t |
robo8080 | 5:61109bce11fe | 123 | |
robo8080 | 5:61109bce11fe | 124 | @retval BLE_ERROR_NONE |
robo8080 | 5:61109bce11fe | 125 | Everything executed properly |
robo8080 | 5:61109bce11fe | 126 | |
robo8080 | 5:61109bce11fe | 127 | @section EXAMPLE |
robo8080 | 5:61109bce11fe | 128 | |
robo8080 | 5:61109bce11fe | 129 | @code |
robo8080 | 5:61109bce11fe | 130 | |
robo8080 | 5:61109bce11fe | 131 | @endcode |
robo8080 | 5:61109bce11fe | 132 | */ |
robo8080 | 5:61109bce11fe | 133 | /**************************************************************************/ |
robo8080 | 5:61109bce11fe | 134 | ble_error_t nRF51822n::reset(void) |
robo8080 | 5:61109bce11fe | 135 | { |
robo8080 | 5:61109bce11fe | 136 | wait(0.5); |
robo8080 | 5:61109bce11fe | 137 | |
robo8080 | 5:61109bce11fe | 138 | /* Wait for the radio to come back up */ |
robo8080 | 5:61109bce11fe | 139 | wait(1); |
robo8080 | 5:61109bce11fe | 140 | |
robo8080 | 5:61109bce11fe | 141 | return BLE_ERROR_NONE; |
robo8080 | 5:61109bce11fe | 142 | } |
robo8080 | 5:61109bce11fe | 143 | |
robo8080 | 5:61109bce11fe | 144 | void |
robo8080 | 5:61109bce11fe | 145 | nRF51822n::waitForEvent(void) |
robo8080 | 5:61109bce11fe | 146 | { |
robo8080 | 5:61109bce11fe | 147 | sd_app_evt_wait(); |
robo8080 | 5:61109bce11fe | 148 | } |