No changes
Fork of nRF51822 by
source/nRF5xGap.h@481:1762975cb3cb, 2015-11-26 (annotated)
- Committer:
- rgrover1
- Date:
- Thu Nov 26 15:02:39 2015 +0000
- Revision:
- 481:1762975cb3cb
- Parent:
- 480:cf44bb89e3f1
- Child:
- 482:0efda47cc288
Synchronized with git rev 611d03b0
Author: Rohit Grover
version v2.0.8
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 388:db85a09c27ef | 1 | /* mbed Microcontroller Library |
rgrover1 | 388:db85a09c27ef | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 388:db85a09c27ef | 3 | * |
rgrover1 | 388:db85a09c27ef | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 388:db85a09c27ef | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 388:db85a09c27ef | 6 | * You may obtain a copy of the License at |
rgrover1 | 388:db85a09c27ef | 7 | * |
rgrover1 | 388:db85a09c27ef | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 388:db85a09c27ef | 9 | * |
rgrover1 | 388:db85a09c27ef | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 388:db85a09c27ef | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 388:db85a09c27ef | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 388:db85a09c27ef | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 388:db85a09c27ef | 14 | * limitations under the License. |
rgrover1 | 388:db85a09c27ef | 15 | */ |
rgrover1 | 388:db85a09c27ef | 16 | |
rgrover1 | 388:db85a09c27ef | 17 | #ifndef __NRF5x_GAP_H__ |
rgrover1 | 388:db85a09c27ef | 18 | #define __NRF5x_GAP_H__ |
rgrover1 | 388:db85a09c27ef | 19 | |
rgrover1 | 388:db85a09c27ef | 20 | #include "mbed.h" |
rgrover1 | 388:db85a09c27ef | 21 | #include "ble/blecommon.h" |
rgrover1 | 388:db85a09c27ef | 22 | #include "ble.h" |
rgrover1 | 388:db85a09c27ef | 23 | #include "ble/GapAdvertisingParams.h" |
rgrover1 | 388:db85a09c27ef | 24 | #include "ble/GapAdvertisingData.h" |
rgrover1 | 388:db85a09c27ef | 25 | #include "ble/Gap.h" |
rgrover1 | 388:db85a09c27ef | 26 | #include "ble/GapScanningParams.h" |
rgrover1 | 388:db85a09c27ef | 27 | |
rgrover1 | 388:db85a09c27ef | 28 | #include "nrf_soc.h" |
rgrover1 | 479:f6e4a3f0a09a | 29 | |
rgrover1 | 479:f6e4a3f0a09a | 30 | extern "C" { |
rgrover1 | 388:db85a09c27ef | 31 | #include "ble_radio_notification.h" |
rgrover1 | 479:f6e4a3f0a09a | 32 | } |
rgrover1 | 479:f6e4a3f0a09a | 33 | |
rgrover1 | 388:db85a09c27ef | 34 | #include "btle_security.h" |
rgrover1 | 388:db85a09c27ef | 35 | |
rgrover1 | 396:e5b0385fc6f1 | 36 | void radioNotificationStaticCallback(bool param); |
rgrover1 | 396:e5b0385fc6f1 | 37 | |
rgrover1 | 388:db85a09c27ef | 38 | /**************************************************************************/ |
rgrover1 | 388:db85a09c27ef | 39 | /*! |
rgrover1 | 388:db85a09c27ef | 40 | \brief |
rgrover1 | 388:db85a09c27ef | 41 | |
rgrover1 | 388:db85a09c27ef | 42 | */ |
rgrover1 | 388:db85a09c27ef | 43 | /**************************************************************************/ |
rgrover1 | 388:db85a09c27ef | 44 | class nRF5xGap : public Gap |
rgrover1 | 388:db85a09c27ef | 45 | { |
rgrover1 | 388:db85a09c27ef | 46 | public: |
rgrover1 | 388:db85a09c27ef | 47 | static nRF5xGap &getInstance(); |
rgrover1 | 388:db85a09c27ef | 48 | |
rgrover1 | 388:db85a09c27ef | 49 | /* Functions that must be implemented from Gap */ |
rgrover1 | 388:db85a09c27ef | 50 | virtual ble_error_t setAddress(AddressType_t type, const Address_t address); |
rgrover1 | 388:db85a09c27ef | 51 | virtual ble_error_t getAddress(AddressType_t *typeP, Address_t address); |
rgrover1 | 388:db85a09c27ef | 52 | virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &); |
rgrover1 | 388:db85a09c27ef | 53 | |
rgrover1 | 405:665704963abb | 54 | virtual uint16_t getMinAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MIN);} |
rgrover1 | 405:665704963abb | 55 | virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_NONCON_INTERVAL_MIN);} |
rgrover1 | 405:665704963abb | 56 | virtual uint16_t getMaxAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MAX);} |
rgrover1 | 388:db85a09c27ef | 57 | |
rgrover1 | 388:db85a09c27ef | 58 | virtual ble_error_t startAdvertising(const GapAdvertisingParams &); |
rgrover1 | 388:db85a09c27ef | 59 | virtual ble_error_t stopAdvertising(void); |
rgrover1 | 388:db85a09c27ef | 60 | virtual ble_error_t connect(const Address_t, Gap::AddressType_t peerAddrType, const ConnectionParams_t *connectionParams, const GapScanningParams *scanParams); |
rgrover1 | 388:db85a09c27ef | 61 | virtual ble_error_t disconnect(Handle_t connectionHandle, DisconnectionReason_t reason); |
rgrover1 | 388:db85a09c27ef | 62 | virtual ble_error_t disconnect(DisconnectionReason_t reason); |
rgrover1 | 388:db85a09c27ef | 63 | |
rgrover1 | 388:db85a09c27ef | 64 | virtual ble_error_t setDeviceName(const uint8_t *deviceName); |
rgrover1 | 388:db85a09c27ef | 65 | virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP); |
rgrover1 | 388:db85a09c27ef | 66 | virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance); |
rgrover1 | 388:db85a09c27ef | 67 | virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP); |
rgrover1 | 388:db85a09c27ef | 68 | |
rgrover1 | 388:db85a09c27ef | 69 | virtual ble_error_t setTxPower(int8_t txPower); |
rgrover1 | 388:db85a09c27ef | 70 | virtual void getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP); |
rgrover1 | 388:db85a09c27ef | 71 | |
rgrover1 | 388:db85a09c27ef | 72 | void setConnectionHandle(uint16_t con_handle); |
rgrover1 | 388:db85a09c27ef | 73 | uint16_t getConnectionHandle(void); |
rgrover1 | 388:db85a09c27ef | 74 | |
rgrover1 | 388:db85a09c27ef | 75 | virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params); |
rgrover1 | 388:db85a09c27ef | 76 | virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params); |
rgrover1 | 388:db85a09c27ef | 77 | virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params); |
rgrover1 | 388:db85a09c27ef | 78 | |
rgrover1 | 396:e5b0385fc6f1 | 79 | virtual ble_error_t initRadioNotification(void) { |
rgrover1 | 396:e5b0385fc6f1 | 80 | if (ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, NRF_RADIO_NOTIFICATION_DISTANCE_800US, radioNotificationStaticCallback) == NRF_SUCCESS) { |
rgrover1 | 396:e5b0385fc6f1 | 81 | return BLE_ERROR_NONE; |
rgrover1 | 396:e5b0385fc6f1 | 82 | } |
rgrover1 | 396:e5b0385fc6f1 | 83 | |
rgrover1 | 396:e5b0385fc6f1 | 84 | return BLE_ERROR_UNSPECIFIED; |
rgrover1 | 388:db85a09c27ef | 85 | } |
rgrover1 | 388:db85a09c27ef | 86 | |
rgrover1 | 430:db7edc9ad0bc | 87 | /* Observer role is not supported by S110, return BLE_ERROR_NOT_IMPLEMENTED */ |
rgrover1 | 455:e33de7c4574c | 88 | #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110) |
rgrover1 | 388:db85a09c27ef | 89 | virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams) { |
rgrover1 | 388:db85a09c27ef | 90 | ble_gap_scan_params_t scanParams = { |
rgrover1 | 388:db85a09c27ef | 91 | .active = scanningParams.getActiveScanning(), /**< If 1, perform active scanning (scan requests). */ |
rgrover1 | 388:db85a09c27ef | 92 | .selective = 0, /**< If 1, ignore unknown devices (non whitelisted). */ |
rgrover1 | 388:db85a09c27ef | 93 | .p_whitelist = NULL, /**< Pointer to whitelist, NULL if none is given. */ |
rgrover1 | 388:db85a09c27ef | 94 | .interval = scanningParams.getInterval(), /**< Scan interval between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */ |
rgrover1 | 388:db85a09c27ef | 95 | .window = scanningParams.getWindow(), /**< Scan window between 0x0004 and 0x4000 in 0.625ms units (2.5ms to 10.24s). */ |
rgrover1 | 388:db85a09c27ef | 96 | .timeout = scanningParams.getTimeout(), /**< Scan timeout between 0x0001 and 0xFFFF in seconds, 0x0000 disables timeout. */ |
rgrover1 | 388:db85a09c27ef | 97 | }; |
rgrover1 | 388:db85a09c27ef | 98 | |
rgrover1 | 388:db85a09c27ef | 99 | if (sd_ble_gap_scan_start(&scanParams) != NRF_SUCCESS) { |
rgrover1 | 388:db85a09c27ef | 100 | return BLE_ERROR_PARAM_OUT_OF_RANGE; |
rgrover1 | 388:db85a09c27ef | 101 | } |
rgrover1 | 388:db85a09c27ef | 102 | |
rgrover1 | 388:db85a09c27ef | 103 | return BLE_ERROR_NONE; |
rgrover1 | 388:db85a09c27ef | 104 | } |
rgrover1 | 388:db85a09c27ef | 105 | |
rgrover1 | 388:db85a09c27ef | 106 | virtual ble_error_t stopScan(void) { |
rgrover1 | 388:db85a09c27ef | 107 | if (sd_ble_gap_scan_stop() == NRF_SUCCESS) { |
rgrover1 | 388:db85a09c27ef | 108 | return BLE_ERROR_NONE; |
rgrover1 | 388:db85a09c27ef | 109 | } |
rgrover1 | 388:db85a09c27ef | 110 | |
rgrover1 | 388:db85a09c27ef | 111 | return BLE_STACK_BUSY; |
rgrover1 | 388:db85a09c27ef | 112 | } |
rgrover1 | 430:db7edc9ad0bc | 113 | #endif |
rgrover1 | 388:db85a09c27ef | 114 | |
rgrover1 | 388:db85a09c27ef | 115 | private: |
rgrover1 | 479:f6e4a3f0a09a | 116 | bool radioNotificationCallbackParam; /* parameter to be passed into the Timeout-generated radio notification callback. */ |
rgrover1 | 479:f6e4a3f0a09a | 117 | Timeout radioNotificationTimeout; |
rgrover1 | 479:f6e4a3f0a09a | 118 | |
rgrover1 | 479:f6e4a3f0a09a | 119 | /* |
rgrover1 | 481:1762975cb3cb | 120 | * A helper function to post radio notification callbacks with low interrupt priority. |
rgrover1 | 479:f6e4a3f0a09a | 121 | */ |
rgrover1 | 479:f6e4a3f0a09a | 122 | void postRadioNotificationCallback(void) { |
rgrover1 | 481:1762975cb3cb | 123 | #ifdef YOTTA_CFG_MBED_OS |
rgrover1 | 481:1762975cb3cb | 124 | /* |
rgrover1 | 481:1762975cb3cb | 125 | * In mbed OS, all user-facing BLE events (interrupts) are posted to the |
rgrover1 | 481:1762975cb3cb | 126 | * MINAR scheduler to be executed as callbacks in thread mode. MINAR guards |
rgrover1 | 481:1762975cb3cb | 127 | * its critical sections from interrupts by acquiring CriticalSectionLock, |
rgrover1 | 481:1762975cb3cb | 128 | * which results in a call to sd_nvic_critical_region_enter(). Thus, it is |
rgrover1 | 481:1762975cb3cb | 129 | * safe to invoke MINAR APIs from interrupt context as long as those |
rgrover1 | 481:1762975cb3cb | 130 | * interrupts are blocked by sd_nvic_critical_region_enter(). |
rgrover1 | 481:1762975cb3cb | 131 | * |
rgrover1 | 481:1762975cb3cb | 132 | * Radio notifications are a special case for the above. The Radio |
rgrover1 | 481:1762975cb3cb | 133 | * Notification IRQ is handled at a very high priority--higher than the |
rgrover1 | 481:1762975cb3cb | 134 | * level blocked by sd_nvic_critical_region_enter(). Thus Radio Notification |
rgrover1 | 481:1762975cb3cb | 135 | * events can preempt MINAR's critical sections. Using MINAR APIs (such as |
rgrover1 | 481:1762975cb3cb | 136 | * posting an event) directly in processRadioNotification() may result in a |
rgrover1 | 481:1762975cb3cb | 137 | * race condition ending in a hard-fault. |
rgrover1 | 481:1762975cb3cb | 138 | * |
rgrover1 | 481:1762975cb3cb | 139 | * The solution is to *not* call MINAR APIs directly from the Radio |
rgrover1 | 481:1762975cb3cb | 140 | * Notification handling; i.e. to do the bulk of RadioNotification |
rgrover1 | 481:1762975cb3cb | 141 | * processing at a reduced priority which respects MINAR's critical |
rgrover1 | 481:1762975cb3cb | 142 | * sections. Unfortunately, on a cortex-M0, there is no clean way to demote |
rgrover1 | 481:1762975cb3cb | 143 | * priority for the currently executing interrupt--we wouldn't want to |
rgrover1 | 481:1762975cb3cb | 144 | * demote the radio notification handling anyway because it is sensitive to |
rgrover1 | 481:1762975cb3cb | 145 | * timing, and the system expects to finish this handling very quickly. The |
rgrover1 | 481:1762975cb3cb | 146 | * workaround is to employ a Timeout to trigger |
rgrover1 | 481:1762975cb3cb | 147 | * postRadioNotificationCallback() after a very short delay (~0 us) and post |
rgrover1 | 481:1762975cb3cb | 148 | * the MINAR callback that context. |
rgrover1 | 481:1762975cb3cb | 149 | * |
rgrover1 | 481:1762975cb3cb | 150 | * !!!WARNING!!! Radio notifications are very time critical events. The |
rgrover1 | 481:1762975cb3cb | 151 | * current solution is expected to work under the assumption that |
rgrover1 | 481:1762975cb3cb | 152 | * postRadioNotificationCalback() will be executed BEFORE the next radio |
rgrover1 | 481:1762975cb3cb | 153 | * notification event is generated. |
rgrover1 | 481:1762975cb3cb | 154 | */ |
rgrover1 | 479:f6e4a3f0a09a | 155 | minar::Scheduler::postCallback( |
rgrover1 | 479:f6e4a3f0a09a | 156 | mbed::util::FunctionPointer1<void, bool>(&radioNotificationCallback, &FunctionPointerWithContext<bool>::call).bind(radioNotificationCallbackParam) |
rgrover1 | 479:f6e4a3f0a09a | 157 | ); |
rgrover1 | 481:1762975cb3cb | 158 | #else |
rgrover1 | 481:1762975cb3cb | 159 | /* |
rgrover1 | 481:1762975cb3cb | 160 | * In mbed classic, all user-facing BLE events execute callbacks in interrupt |
rgrover1 | 481:1762975cb3cb | 161 | * mode. Radio Notifications are a special case because its IRQ is handled at |
rgrover1 | 481:1762975cb3cb | 162 | * a very high priority. Thus Radio Notification events can preempt other |
rgrover1 | 481:1762975cb3cb | 163 | * operations that require interaction with the SoftDevice such as advertising |
rgrover1 | 481:1762975cb3cb | 164 | * payload updates and changing the Gap state. Therefore, executing a Radio |
rgrover1 | 481:1762975cb3cb | 165 | * Notification callback directly from processRadioNotification() may result |
rgrover1 | 481:1762975cb3cb | 166 | * in a race condition ending in a hard-fault. |
rgrover1 | 481:1762975cb3cb | 167 | * |
rgrover1 | 481:1762975cb3cb | 168 | * The solution is to *not* execute the Radio Notification callback directly |
rgrover1 | 481:1762975cb3cb | 169 | * from the Radio Notification handling; i.e. to do the bulk of the |
rgrover1 | 481:1762975cb3cb | 170 | * Radio Notification processing at a reduced priority. Unfortunately, on a |
rgrover1 | 481:1762975cb3cb | 171 | * cortex-M0, there is no clean way to demote priority for the currently |
rgrover1 | 481:1762975cb3cb | 172 | * executing interrupt--we wouldn't want to demote the radio notification |
rgrover1 | 481:1762975cb3cb | 173 | * handling anyway because it is sensitive to timing, and the system expects |
rgrover1 | 481:1762975cb3cb | 174 | * to finish this handling very quickly. The workaround is to employ a Timeout |
rgrover1 | 481:1762975cb3cb | 175 | * to trigger postRadioNotificationCallback() after a very short delay (~0 us) |
rgrover1 | 481:1762975cb3cb | 176 | * and execute the callback in that context. |
rgrover1 | 481:1762975cb3cb | 177 | * |
rgrover1 | 481:1762975cb3cb | 178 | * !!!WARNING!!! Radio notifications are very time critical events. The |
rgrover1 | 481:1762975cb3cb | 179 | * current solution is expected to work under the assumption that |
rgrover1 | 481:1762975cb3cb | 180 | * postRadioNotificationCalback() will be executed BEFORE the next radio |
rgrover1 | 481:1762975cb3cb | 181 | * notification event is generated. |
rgrover1 | 481:1762975cb3cb | 182 | */ |
rgrover1 | 481:1762975cb3cb | 183 | radioNotificationCallback.call(radioNotificationCallbackParam); |
rgrover1 | 481:1762975cb3cb | 184 | #endif /* #ifdef YOTTA_CFG_MBED_OS */ |
rgrover1 | 480:cf44bb89e3f1 | 185 | } |
rgrover1 | 479:f6e4a3f0a09a | 186 | |
rgrover1 | 396:e5b0385fc6f1 | 187 | /** |
rgrover1 | 396:e5b0385fc6f1 | 188 | * A helper function to process radio-notification events; to be called internally. |
rgrover1 | 396:e5b0385fc6f1 | 189 | * @param param [description] |
rgrover1 | 396:e5b0385fc6f1 | 190 | */ |
rgrover1 | 396:e5b0385fc6f1 | 191 | void processRadioNotificationEvent(bool param) { |
rgrover1 | 479:f6e4a3f0a09a | 192 | radioNotificationCallbackParam = param; |
rgrover1 | 479:f6e4a3f0a09a | 193 | radioNotificationTimeout.attach_us(this, &nRF5xGap::postRadioNotificationCallback, 0); |
rgrover1 | 396:e5b0385fc6f1 | 194 | } |
rgrover1 | 396:e5b0385fc6f1 | 195 | friend void radioNotificationStaticCallback(bool param); /* allow invocations of processRadioNotificationEvent() */ |
rgrover1 | 396:e5b0385fc6f1 | 196 | |
rgrover1 | 396:e5b0385fc6f1 | 197 | private: |
rgrover1 | 388:db85a09c27ef | 198 | uint16_t m_connectionHandle; |
rgrover1 | 388:db85a09c27ef | 199 | nRF5xGap() { |
rgrover1 | 388:db85a09c27ef | 200 | m_connectionHandle = BLE_CONN_HANDLE_INVALID; |
rgrover1 | 388:db85a09c27ef | 201 | } |
rgrover1 | 388:db85a09c27ef | 202 | |
rgrover1 | 388:db85a09c27ef | 203 | nRF5xGap(nRF5xGap const &); |
rgrover1 | 388:db85a09c27ef | 204 | void operator=(nRF5xGap const &); |
rgrover1 | 388:db85a09c27ef | 205 | }; |
rgrover1 | 388:db85a09c27ef | 206 | |
rgrover1 | 388:db85a09c27ef | 207 | #endif // ifndef __NRF5x_GAP_H__ |