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