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