Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MrBedfordVan 0:b9164b348919 1 /* mbed Microcontroller Library
MrBedfordVan 0:b9164b348919 2 * Copyright (c) 2006-2013 ARM Limited
MrBedfordVan 0:b9164b348919 3 *
MrBedfordVan 0:b9164b348919 4 * Licensed under the Apache License, Version 2.0 (the "License");
MrBedfordVan 0:b9164b348919 5 * you may not use this file except in compliance with the License.
MrBedfordVan 0:b9164b348919 6 * You may obtain a copy of the License at
MrBedfordVan 0:b9164b348919 7 *
MrBedfordVan 0:b9164b348919 8 * http://www.apache.org/licenses/LICENSE-2.0
MrBedfordVan 0:b9164b348919 9 *
MrBedfordVan 0:b9164b348919 10 * Unless required by applicable law or agreed to in writing, software
MrBedfordVan 0:b9164b348919 11 * distributed under the License is distributed on an "AS IS" BASIS,
MrBedfordVan 0:b9164b348919 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MrBedfordVan 0:b9164b348919 13 * See the License for the specific language governing permissions and
MrBedfordVan 0:b9164b348919 14 * limitations under the License.
MrBedfordVan 0:b9164b348919 15 */
MrBedfordVan 0:b9164b348919 16
MrBedfordVan 0:b9164b348919 17 #ifndef __NRF5x_GAP_H__
MrBedfordVan 0:b9164b348919 18 #define __NRF5x_GAP_H__
MrBedfordVan 0:b9164b348919 19
MrBedfordVan 0:b9164b348919 20 #ifdef YOTTA_CFG_MBED_OS
MrBedfordVan 0:b9164b348919 21 #include "mbed-drivers/mbed.h"
MrBedfordVan 0:b9164b348919 22 #else
MrBedfordVan 0:b9164b348919 23 #include "mbed.h"
MrBedfordVan 0:b9164b348919 24 #endif
MrBedfordVan 0:b9164b348919 25 #ifndef YOTTA_CFG_WHITELIST_MAX_SIZE
MrBedfordVan 0:b9164b348919 26 #define YOTTA_CFG_WHITELIST_MAX_SIZE BLE_GAP_WHITELIST_ADDR_MAX_COUNT
MrBedfordVan 0:b9164b348919 27 #elif YOTTA_CFG_WHITELIST_MAX_SIZE > BLE_GAP_WHITELIST_ADDR_MAX_COUNT
MrBedfordVan 0:b9164b348919 28 #undef YOTTA_CFG_WHITELIST_MAX_SIZE
MrBedfordVan 0:b9164b348919 29 #define YOTTA_CFG_WHITELIST_MAX_SIZE BLE_GAP_WHITELIST_ADDR_MAX_COUNT
MrBedfordVan 0:b9164b348919 30 #endif
MrBedfordVan 0:b9164b348919 31 #ifndef YOTTA_CFG_IRK_TABLE_MAX_SIZE
MrBedfordVan 0:b9164b348919 32 #define YOTTA_CFG_IRK_TABLE_MAX_SIZE BLE_GAP_WHITELIST_IRK_MAX_COUNT
MrBedfordVan 0:b9164b348919 33 #elif YOTTA_CFG_IRK_TABLE_MAX_SIZE > BLE_GAP_WHITELIST_IRK_MAX_COUNT
MrBedfordVan 0:b9164b348919 34 #undef YOTTA_CFG_IRK_TABLE_MAX_SIZE
MrBedfordVan 0:b9164b348919 35 #define YOTTA_CFG_IRK_TABLE_MAX_SIZE BLE_GAP_WHITELIST_IRK_MAX_COUNT
MrBedfordVan 0:b9164b348919 36 #endif
MrBedfordVan 0:b9164b348919 37 #include "ble/blecommon.h"
MrBedfordVan 0:b9164b348919 38 #include "ble.h"
MrBedfordVan 0:b9164b348919 39 #include "ble/GapAdvertisingParams.h"
MrBedfordVan 0:b9164b348919 40 #include "ble/GapAdvertisingData.h"
MrBedfordVan 0:b9164b348919 41 #include "ble/Gap.h"
MrBedfordVan 0:b9164b348919 42 #include "ble/GapScanningParams.h"
MrBedfordVan 0:b9164b348919 43
MrBedfordVan 0:b9164b348919 44 #include "nrf_soc.h"
MrBedfordVan 0:b9164b348919 45
MrBedfordVan 0:b9164b348919 46 extern "C" {
MrBedfordVan 0:b9164b348919 47 #include "ble_radio_notification.h"
MrBedfordVan 0:b9164b348919 48 }
MrBedfordVan 0:b9164b348919 49
MrBedfordVan 0:b9164b348919 50 #include "btle_security.h"
MrBedfordVan 0:b9164b348919 51
MrBedfordVan 0:b9164b348919 52 void radioNotificationStaticCallback(bool param);
MrBedfordVan 0:b9164b348919 53
MrBedfordVan 0:b9164b348919 54 /**************************************************************************/
MrBedfordVan 0:b9164b348919 55 /*!
MrBedfordVan 0:b9164b348919 56 \brief
MrBedfordVan 0:b9164b348919 57
MrBedfordVan 0:b9164b348919 58 */
MrBedfordVan 0:b9164b348919 59 /**************************************************************************/
MrBedfordVan 0:b9164b348919 60 class nRF5xGap : public Gap
MrBedfordVan 0:b9164b348919 61 {
MrBedfordVan 0:b9164b348919 62 public:
MrBedfordVan 0:b9164b348919 63 /* Functions that must be implemented from Gap */
MrBedfordVan 0:b9164b348919 64 virtual ble_error_t setAddress(AddressType_t type, const Address_t address);
MrBedfordVan 0:b9164b348919 65 virtual ble_error_t getAddress(AddressType_t *typeP, Address_t address);
MrBedfordVan 0:b9164b348919 66 virtual ble_error_t setAdvertisingData(const GapAdvertisingData &, const GapAdvertisingData &);
MrBedfordVan 0:b9164b348919 67
MrBedfordVan 0:b9164b348919 68 virtual uint16_t getMinAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MIN);}
MrBedfordVan 0:b9164b348919 69 virtual uint16_t getMinNonConnectableAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_NONCON_INTERVAL_MIN);}
MrBedfordVan 0:b9164b348919 70 virtual uint16_t getMaxAdvertisingInterval(void) const {return GapAdvertisingParams::ADVERTISEMENT_DURATION_UNITS_TO_MS(BLE_GAP_ADV_INTERVAL_MAX);}
MrBedfordVan 0:b9164b348919 71
MrBedfordVan 0:b9164b348919 72 virtual ble_error_t startAdvertising(const GapAdvertisingParams &);
MrBedfordVan 0:b9164b348919 73 virtual ble_error_t stopAdvertising(void);
MrBedfordVan 0:b9164b348919 74 virtual ble_error_t connect(const Address_t, BLEProtocol::AddressType_t peerAddrType, const ConnectionParams_t *connectionParams, const GapScanningParams *scanParams);
MrBedfordVan 0:b9164b348919 75 virtual ble_error_t disconnect(Handle_t connectionHandle, DisconnectionReason_t reason);
MrBedfordVan 0:b9164b348919 76 virtual ble_error_t disconnect(DisconnectionReason_t reason);
MrBedfordVan 0:b9164b348919 77
MrBedfordVan 0:b9164b348919 78 virtual ble_error_t setDeviceName(const uint8_t *deviceName);
MrBedfordVan 0:b9164b348919 79 virtual ble_error_t getDeviceName(uint8_t *deviceName, unsigned *lengthP);
MrBedfordVan 0:b9164b348919 80 virtual ble_error_t setAppearance(GapAdvertisingData::Appearance appearance);
MrBedfordVan 0:b9164b348919 81 virtual ble_error_t getAppearance(GapAdvertisingData::Appearance *appearanceP);
MrBedfordVan 0:b9164b348919 82
MrBedfordVan 0:b9164b348919 83 virtual ble_error_t setTxPower(int8_t txPower);
MrBedfordVan 0:b9164b348919 84 virtual void getPermittedTxPowerValues(const int8_t **valueArrayPP, size_t *countP);
MrBedfordVan 0:b9164b348919 85
MrBedfordVan 0:b9164b348919 86 void setConnectionHandle(uint16_t con_handle);
MrBedfordVan 0:b9164b348919 87 uint16_t getConnectionHandle(void);
MrBedfordVan 0:b9164b348919 88
MrBedfordVan 0:b9164b348919 89 virtual ble_error_t getPreferredConnectionParams(ConnectionParams_t *params);
MrBedfordVan 0:b9164b348919 90 virtual ble_error_t setPreferredConnectionParams(const ConnectionParams_t *params);
MrBedfordVan 0:b9164b348919 91 virtual ble_error_t updateConnectionParams(Handle_t handle, const ConnectionParams_t *params);
MrBedfordVan 0:b9164b348919 92
MrBedfordVan 0:b9164b348919 93 virtual ble_error_t reset(void);
MrBedfordVan 0:b9164b348919 94
MrBedfordVan 0:b9164b348919 95 /*
MrBedfordVan 0:b9164b348919 96 * The following functions are part of the whitelisting experimental API.
MrBedfordVan 0:b9164b348919 97 * Therefore, this functionality can change in the near future.
MrBedfordVan 0:b9164b348919 98 */
MrBedfordVan 0:b9164b348919 99 virtual uint8_t getMaxWhitelistSize(void) const;
MrBedfordVan 0:b9164b348919 100 virtual ble_error_t getWhitelist(Gap::Whitelist_t &whitelistOut) const;
MrBedfordVan 0:b9164b348919 101 virtual ble_error_t setWhitelist(const Gap::Whitelist_t &whitelistIn);
MrBedfordVan 0:b9164b348919 102
MrBedfordVan 0:b9164b348919 103 virtual ble_error_t setAdvertisingPolicyMode(AdvertisingPolicyMode_t mode);
MrBedfordVan 0:b9164b348919 104 virtual ble_error_t setScanningPolicyMode(ScanningPolicyMode_t mode);
MrBedfordVan 0:b9164b348919 105 virtual ble_error_t setInitiatorPolicyMode(InitiatorPolicyMode_t mode);
MrBedfordVan 0:b9164b348919 106 virtual Gap::AdvertisingPolicyMode_t getAdvertisingPolicyMode(void) const;
MrBedfordVan 0:b9164b348919 107 virtual Gap::ScanningPolicyMode_t getScanningPolicyMode(void) const;
MrBedfordVan 0:b9164b348919 108 virtual Gap::InitiatorPolicyMode_t getInitiatorPolicyMode(void) const;
MrBedfordVan 0:b9164b348919 109
MrBedfordVan 0:b9164b348919 110 virtual ble_error_t initRadioNotification(void) {
MrBedfordVan 0:b9164b348919 111 if (ble_radio_notification_init(NRF_APP_PRIORITY_HIGH, NRF_RADIO_NOTIFICATION_DISTANCE_800US, radioNotificationStaticCallback) == NRF_SUCCESS) {
MrBedfordVan 0:b9164b348919 112 return BLE_ERROR_NONE;
MrBedfordVan 0:b9164b348919 113 }
MrBedfordVan 0:b9164b348919 114
MrBedfordVan 0:b9164b348919 115 return BLE_ERROR_UNSPECIFIED;
MrBedfordVan 0:b9164b348919 116 }
MrBedfordVan 0:b9164b348919 117
MrBedfordVan 0:b9164b348919 118 /* Observer role is not supported by S110, return BLE_ERROR_NOT_IMPLEMENTED */
MrBedfordVan 0:b9164b348919 119 #if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
MrBedfordVan 0:b9164b348919 120 virtual ble_error_t startRadioScan(const GapScanningParams &scanningParams);
MrBedfordVan 0:b9164b348919 121 virtual ble_error_t stopScan(void);
MrBedfordVan 0:b9164b348919 122 #endif
MrBedfordVan 0:b9164b348919 123
MrBedfordVan 0:b9164b348919 124 private:
MrBedfordVan 0:b9164b348919 125 /*
MrBedfordVan 0:b9164b348919 126 * Whitelisting API related structures and helper functions.
MrBedfordVan 0:b9164b348919 127 */
MrBedfordVan 0:b9164b348919 128
MrBedfordVan 0:b9164b348919 129 /* Policy modes set by the user. By default these are set to ignore the whitelist */
MrBedfordVan 0:b9164b348919 130 Gap::AdvertisingPolicyMode_t advertisingPolicyMode;
MrBedfordVan 0:b9164b348919 131 Gap::ScanningPolicyMode_t scanningPolicyMode;
MrBedfordVan 0:b9164b348919 132
MrBedfordVan 0:b9164b348919 133 /* Internal representation of a whitelist */
MrBedfordVan 0:b9164b348919 134 uint8_t whitelistAddressesSize;
MrBedfordVan 0:b9164b348919 135 ble_gap_addr_t whitelistAddresses[YOTTA_CFG_WHITELIST_MAX_SIZE];
MrBedfordVan 0:b9164b348919 136
MrBedfordVan 0:b9164b348919 137 /*
MrBedfordVan 0:b9164b348919 138 * An internal function used to populate the ble_gap_whitelist_t that will be used by
MrBedfordVan 0:b9164b348919 139 * the SoftDevice for filtering requests. This function is needed because for the BLE
MrBedfordVan 0:b9164b348919 140 * API the whitelist is just a collection of keys, but for the stack it also includes
MrBedfordVan 0:b9164b348919 141 * the IRK table.
MrBedfordVan 0:b9164b348919 142 */
MrBedfordVan 0:b9164b348919 143 ble_error_t generateStackWhitelist(ble_gap_whitelist_t &whitelist);
MrBedfordVan 0:b9164b348919 144
MrBedfordVan 0:b9164b348919 145 private:
MrBedfordVan 0:b9164b348919 146 bool radioNotificationCallbackParam; /* parameter to be passed into the Timeout-generated radio notification callback. */
MrBedfordVan 0:b9164b348919 147 Timeout radioNotificationTimeout;
MrBedfordVan 0:b9164b348919 148
MrBedfordVan 0:b9164b348919 149 /*
MrBedfordVan 0:b9164b348919 150 * A helper function to post radio notification callbacks with low interrupt priority.
MrBedfordVan 0:b9164b348919 151 */
MrBedfordVan 0:b9164b348919 152 void postRadioNotificationCallback(void) {
MrBedfordVan 0:b9164b348919 153 #ifdef YOTTA_CFG_MBED_OS
MrBedfordVan 0:b9164b348919 154 /*
MrBedfordVan 0:b9164b348919 155 * In mbed OS, all user-facing BLE events (interrupts) are posted to the
MrBedfordVan 0:b9164b348919 156 * MINAR scheduler to be executed as callbacks in thread mode. MINAR guards
MrBedfordVan 0:b9164b348919 157 * its critical sections from interrupts by acquiring CriticalSectionLock,
MrBedfordVan 0:b9164b348919 158 * which results in a call to sd_nvic_critical_region_enter(). Thus, it is
MrBedfordVan 0:b9164b348919 159 * safe to invoke MINAR APIs from interrupt context as long as those
MrBedfordVan 0:b9164b348919 160 * interrupts are blocked by sd_nvic_critical_region_enter().
MrBedfordVan 0:b9164b348919 161 *
MrBedfordVan 0:b9164b348919 162 * Radio notifications are a special case for the above. The Radio
MrBedfordVan 0:b9164b348919 163 * Notification IRQ is handled at a very high priority--higher than the
MrBedfordVan 0:b9164b348919 164 * level blocked by sd_nvic_critical_region_enter(). Thus Radio Notification
MrBedfordVan 0:b9164b348919 165 * events can preempt MINAR's critical sections. Using MINAR APIs (such as
MrBedfordVan 0:b9164b348919 166 * posting an event) directly in processRadioNotification() may result in a
MrBedfordVan 0:b9164b348919 167 * race condition ending in a hard-fault.
MrBedfordVan 0:b9164b348919 168 *
MrBedfordVan 0:b9164b348919 169 * The solution is to *not* call MINAR APIs directly from the Radio
MrBedfordVan 0:b9164b348919 170 * Notification handling; i.e. to do the bulk of RadioNotification
MrBedfordVan 0:b9164b348919 171 * processing at a reduced priority which respects MINAR's critical
MrBedfordVan 0:b9164b348919 172 * sections. Unfortunately, on a cortex-M0, there is no clean way to demote
MrBedfordVan 0:b9164b348919 173 * priority for the currently executing interrupt--we wouldn't want to
MrBedfordVan 0:b9164b348919 174 * demote the radio notification handling anyway because it is sensitive to
MrBedfordVan 0:b9164b348919 175 * timing, and the system expects to finish this handling very quickly. The
MrBedfordVan 0:b9164b348919 176 * workaround is to employ a Timeout to trigger
MrBedfordVan 0:b9164b348919 177 * postRadioNotificationCallback() after a very short delay (~0 us) and post
MrBedfordVan 0:b9164b348919 178 * the MINAR callback that context.
MrBedfordVan 0:b9164b348919 179 *
MrBedfordVan 0:b9164b348919 180 * !!!WARNING!!! Radio notifications are very time critical events. The
MrBedfordVan 0:b9164b348919 181 * current solution is expected to work under the assumption that
MrBedfordVan 0:b9164b348919 182 * postRadioNotificationCalback() will be executed BEFORE the next radio
MrBedfordVan 0:b9164b348919 183 * notification event is generated.
MrBedfordVan 0:b9164b348919 184 */
MrBedfordVan 0:b9164b348919 185 minar::Scheduler::postCallback(
MrBedfordVan 0:b9164b348919 186 mbed::util::FunctionPointer1<void, bool>(&radioNotificationCallback, &FunctionPointerWithContext<bool>::call).bind(radioNotificationCallbackParam)
MrBedfordVan 0:b9164b348919 187 );
MrBedfordVan 0:b9164b348919 188 #else
MrBedfordVan 0:b9164b348919 189 /*
MrBedfordVan 0:b9164b348919 190 * In mbed classic, all user-facing BLE events execute callbacks in interrupt
MrBedfordVan 0:b9164b348919 191 * mode. Radio Notifications are a special case because its IRQ is handled at
MrBedfordVan 0:b9164b348919 192 * a very high priority. Thus Radio Notification events can preempt other
MrBedfordVan 0:b9164b348919 193 * operations that require interaction with the SoftDevice such as advertising
MrBedfordVan 0:b9164b348919 194 * payload updates and changing the Gap state. Therefore, executing a Radio
MrBedfordVan 0:b9164b348919 195 * Notification callback directly from processRadioNotification() may result
MrBedfordVan 0:b9164b348919 196 * in a race condition ending in a hard-fault.
MrBedfordVan 0:b9164b348919 197 *
MrBedfordVan 0:b9164b348919 198 * The solution is to *not* execute the Radio Notification callback directly
MrBedfordVan 0:b9164b348919 199 * from the Radio Notification handling; i.e. to do the bulk of the
MrBedfordVan 0:b9164b348919 200 * Radio Notification processing at a reduced priority. Unfortunately, on a
MrBedfordVan 0:b9164b348919 201 * cortex-M0, there is no clean way to demote priority for the currently
MrBedfordVan 0:b9164b348919 202 * executing interrupt--we wouldn't want to demote the radio notification
MrBedfordVan 0:b9164b348919 203 * handling anyway because it is sensitive to timing, and the system expects
MrBedfordVan 0:b9164b348919 204 * to finish this handling very quickly. The workaround is to employ a Timeout
MrBedfordVan 0:b9164b348919 205 * to trigger postRadioNotificationCallback() after a very short delay (~0 us)
MrBedfordVan 0:b9164b348919 206 * and execute the callback in that context.
MrBedfordVan 0:b9164b348919 207 *
MrBedfordVan 0:b9164b348919 208 * !!!WARNING!!! Radio notifications are very time critical events. The
MrBedfordVan 0:b9164b348919 209 * current solution is expected to work under the assumption that
MrBedfordVan 0:b9164b348919 210 * postRadioNotificationCalback() will be executed BEFORE the next radio
MrBedfordVan 0:b9164b348919 211 * notification event is generated.
MrBedfordVan 0:b9164b348919 212 */
MrBedfordVan 0:b9164b348919 213 radioNotificationCallback.call(radioNotificationCallbackParam);
MrBedfordVan 0:b9164b348919 214 #endif /* #ifdef YOTTA_CFG_MBED_OS */
MrBedfordVan 0:b9164b348919 215 }
MrBedfordVan 0:b9164b348919 216
MrBedfordVan 0:b9164b348919 217 /**
MrBedfordVan 0:b9164b348919 218 * A helper function to process radio-notification events; to be called internally.
MrBedfordVan 0:b9164b348919 219 * @param param [description]
MrBedfordVan 0:b9164b348919 220 */
MrBedfordVan 0:b9164b348919 221 void processRadioNotificationEvent(bool param) {
MrBedfordVan 0:b9164b348919 222 radioNotificationCallbackParam = param;
MrBedfordVan 0:b9164b348919 223 radioNotificationTimeout.attach_us(this, &nRF5xGap::postRadioNotificationCallback, 0);
MrBedfordVan 0:b9164b348919 224 }
MrBedfordVan 0:b9164b348919 225 friend void radioNotificationStaticCallback(bool param); /* allow invocations of processRadioNotificationEvent() */
MrBedfordVan 0:b9164b348919 226
MrBedfordVan 0:b9164b348919 227 private:
MrBedfordVan 0:b9164b348919 228 uint16_t m_connectionHandle;
MrBedfordVan 0:b9164b348919 229
MrBedfordVan 0:b9164b348919 230 /*
MrBedfordVan 0:b9164b348919 231 * Allow instantiation from nRF5xn when required.
MrBedfordVan 0:b9164b348919 232 */
MrBedfordVan 0:b9164b348919 233 friend class nRF5xn;
MrBedfordVan 0:b9164b348919 234
MrBedfordVan 0:b9164b348919 235 nRF5xGap() :
MrBedfordVan 0:b9164b348919 236 advertisingPolicyMode(Gap::ADV_POLICY_IGNORE_WHITELIST),
MrBedfordVan 0:b9164b348919 237 scanningPolicyMode(Gap::SCAN_POLICY_IGNORE_WHITELIST),
MrBedfordVan 0:b9164b348919 238 whitelistAddressesSize(0) {
MrBedfordVan 0:b9164b348919 239 m_connectionHandle = BLE_CONN_HANDLE_INVALID;
MrBedfordVan 0:b9164b348919 240 }
MrBedfordVan 0:b9164b348919 241
MrBedfordVan 0:b9164b348919 242 nRF5xGap(nRF5xGap const &);
MrBedfordVan 0:b9164b348919 243 void operator=(nRF5xGap const &);
MrBedfordVan 0:b9164b348919 244 };
MrBedfordVan 0:b9164b348919 245
MrBedfordVan 0:b9164b348919 246 #endif // ifndef __NRF5x_GAP_H__