Skip to content

Commit f1c3d75

Browse files
committed
initial creation of nRF52840 BLE target implementation "NRF5_SDK13" as a copy of "NRF5"
1 parent 5649eef commit f1c3d75

39 files changed

+7953
-0
lines changed

features/FEATURE_BLE/targets/TARGET_NORDIC/TARGET_NRF5_SDK13/CHANGELOG.md

Lines changed: 344 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (c) 2015 ARM Limited
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* S110/S120/S130 License Agreement
3+
*
4+
* Copyright (c) 2015, Nordic Semiconductor ASA, All rights reserved.
5+
*
6+
* Redistribution. Redistribution and use in binary form, without modification,
7+
* are permitted provided that the following conditions are met:
8+
*
9+
* • Redistributions must reproduce the above copyright notice and the following
10+
* disclaimer in the documentation and/or other materials provided with the
11+
* distribution.
12+
* • Neither the name of the copyright holder nor the names of its contributors
13+
* may be used to endorse or promote products derived from this software
14+
* without specific prior written permission.
15+
* • No reverse engineering, decompilation, or disassembly of this software is
16+
* permitted.
17+
*
18+
* DISCLAIMER.
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*
30+
* /
Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2013 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "common/common.h"
18+
#include "nordic_common.h"
19+
20+
#include "btle.h"
21+
#include "btle_clock.h"
22+
23+
#include "ble_flash.h"
24+
#include "ble_conn_params.h"
25+
26+
#include "btle_gap.h"
27+
#include "btle_advertising.h"
28+
#include "custom/custom_helper.h"
29+
30+
#include "ble/GapEvents.h"
31+
#include "nRF5xn.h"
32+
33+
#ifdef S110
34+
#define IS_LEGACY_DEVICE_MANAGER_ENABLED 1
35+
#elif defined(S130) || defined(S132)
36+
#define IS_LEGACY_DEVICE_MANAGER_ENABLED 0
37+
#endif
38+
39+
extern "C" {
40+
#if (IS_LEGACY_DEVICE_MANAGER_ENABLED)
41+
#include "pstorage.h"
42+
#include "device_manager.h"
43+
#else
44+
#include "fstorage.h"
45+
#include "fds.h"
46+
#include "peer_manager.h"
47+
#include "ble_conn_state.h"
48+
#endif
49+
50+
#include "softdevice_handler.h"
51+
#include "ble_stack_handler_types.h"
52+
}
53+
54+
#include "ble_hci.h"
55+
#include "btle_discovery.h"
56+
57+
#include "nRF5xGattClient.h"
58+
#include "nRF5xServiceDiscovery.h"
59+
#include "nRF5xCharacteristicDescriptorDiscoverer.h"
60+
61+
62+
bool isEventsSignaled = false;
63+
64+
extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
65+
void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name);
66+
extern "C" void SD_EVT_IRQHandler(void); // export the softdevice event handler for registration by nvic-set-vector.
67+
68+
69+
static void btle_handler(ble_evt_t *p_ble_evt);
70+
71+
#if 0
72+
#define CENTRAL_LINK_COUNT (YOTTA_CFG_NORDIC_BLE_CENTRAL_LINKS) /**<number of central links used by the application. When changing this number remember to adjust the RAM settings */
73+
/** If value for YOTTA_CFG_NORDIC_BLE_PERIPHERAL_LINKS was used, ram settings are adjusted by the yotta target module. */
74+
#define PERIPHERAL_LINK_COUNT (YOTTA_CFG_NORDIC_BLE_PERIPHERAL_LINKS) /**<number of peripheral links used by the application. When changing this number remember to adjust the RAM settings*/
75+
/** If value for YOTTA_CFG_NORDIC_BLE_CENTRAL_LINKS was used, ram settings are adjusted by the yotta target module. */
76+
#define GATTS_ATTR_TAB_SIZE (YOTTA_CFG_NORDIC_BLE_GATTS_ATTR_TAB_SIZE) /**< GATTS attribite table size. */
77+
/** If value for YOTTA_CFG_NORDIC_BLE_GATTS_ATTR_TAB_SIZE was used, ram settings are adjusted by the yotta target module. */
78+
#else
79+
#define CENTRAL_LINK_COUNT 3 /**<number of central links used by the application. When changing this number remember to adjust the RAM settings */
80+
/** If value for YOTTA_CFG_NORDIC_BLE_PERIPHERAL_LINKS was used, ram settings are adjusted by the yotta target module. */
81+
#define PERIPHERAL_LINK_COUNT 1 /**<number of peripheral links used by the application. When changing this number remember to adjust the RAM settings*/
82+
/** If value for YOTTA_CFG_NORDIC_BLE_CENTRAL_LINKS was used, ram settings are adjusted by the yotta target module. */
83+
#define GATTS_ATTR_TAB_SIZE 0x600 /**< GATTS attribite table size. */
84+
/** If value for YOTTA_CFG_NORDIC_BLE_GATTS_ATTR_TAB_SIZE was used, ram settings are adjusted by the yotta target module. */
85+
86+
#endif
87+
88+
static void sys_evt_dispatch(uint32_t sys_evt)
89+
{
90+
#if (IS_LEGACY_DEVICE_MANAGER_ENABLED)
91+
pstorage_sys_event_handler(sys_evt);
92+
#else
93+
// Forward Softdevice events to the fstorage module
94+
fs_sys_event_handler(sys_evt);
95+
#endif
96+
}
97+
98+
/**
99+
* This function is called in interrupt context to handle BLE events; i.e. pull
100+
* system and user events out of the pending events-queue of the BLE stack. The
101+
* BLE stack signals the availability of events by the triggering the SWI2
102+
* interrupt, which forwards the handling to this function.
103+
*
104+
* The event processing loop is implemented in intern_softdevice_events_execute().
105+
*
106+
* This function will signal to the user code by calling signalEventsToProcess
107+
* that their is events to process and BLE::processEvents should be called.
108+
*/
109+
static uint32_t signalEvent()
110+
{
111+
if(isEventsSignaled == false) {
112+
isEventsSignaled = true;
113+
nRF5xn::Instance(BLE::DEFAULT_INSTANCE).signalEventsToProcess(BLE::DEFAULT_INSTANCE);
114+
}
115+
return NRF_SUCCESS;
116+
}
117+
118+
119+
error_t btle_init(void)
120+
{
121+
nrf_clock_lf_cfg_t clockConfiguration;
122+
123+
// register softdevice handler vector
124+
NVIC_SetVector(SD_EVT_IRQn, (uint32_t) SD_EVT_IRQHandler);
125+
126+
// Configure the LF clock according to values provided by btle_clock.h.
127+
// It is input from the chain of the yotta configuration system.
128+
clockConfiguration.source = LFCLK_CONF_SOURCE;
129+
clockConfiguration.xtal_accuracy = LFCLK_CONF_ACCURACY;
130+
clockConfiguration.rc_ctiv = LFCLK_CONF_RC_CTIV;
131+
clockConfiguration.rc_temp_ctiv = LFCLK_CONF_RC_TEMP_CTIV;
132+
133+
SOFTDEVICE_HANDLER_INIT(&clockConfiguration, signalEvent);
134+
135+
// Enable BLE stack
136+
/**
137+
* Using this call, the application can select whether to include the
138+
* Service Changed characteristic in the GATT Server. The default in all
139+
* previous releases has been to include the Service Changed characteristic,
140+
* but this affects how GATT clients behave. Specifically, it requires
141+
* clients to subscribe to this attribute and not to cache attribute handles
142+
* between connections unless the devices are bonded. If the application
143+
* does not need to change the structure of the GATT server attributes at
144+
* runtime this adds unnecessary complexity to the interaction with peer
145+
* clients. If the SoftDevice is enabled with the Service Changed
146+
* Characteristics turned off, then clients are allowed to cache attribute
147+
* handles making applications simpler on both sides.
148+
*/
149+
static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = true;
150+
151+
ble_enable_params_t ble_enable_params;
152+
uint32_t err_code = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT,
153+
PERIPHERAL_LINK_COUNT,
154+
&ble_enable_params);
155+
156+
ble_enable_params.gatts_enable_params.attr_tab_size = GATTS_ATTR_TAB_SIZE;
157+
ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT;
158+
ble_enable_params.common_enable_params.vs_uuid_count = UUID_TABLE_MAX_ENTRIES;
159+
160+
if(err_code != NRF_SUCCESS) {
161+
return ERROR_INVALID_PARAM;
162+
}
163+
164+
if (softdevice_enable(&ble_enable_params) != NRF_SUCCESS) {
165+
return ERROR_INVALID_PARAM;
166+
}
167+
168+
ble_gap_addr_t addr;
169+
if (sd_ble_gap_address_get(&addr) != NRF_SUCCESS) {
170+
return ERROR_INVALID_PARAM;
171+
}
172+
if (sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr) != NRF_SUCCESS) {
173+
return ERROR_INVALID_PARAM;
174+
}
175+
176+
ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler));
177+
ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch));
178+
179+
return btle_gap_init();
180+
}
181+
182+
static void btle_handler(ble_evt_t *p_ble_evt)
183+
{
184+
/* Library service handlers */
185+
#if SDK_CONN_PARAMS_MODULE_ENABLE
186+
ble_conn_params_on_ble_evt(p_ble_evt);
187+
#endif
188+
189+
#if (IS_LEGACY_DEVICE_MANAGER_ENABLED)
190+
dm_ble_evt_handler(p_ble_evt);
191+
#else
192+
// Forward BLE events to the Connection State module.
193+
// This must be called before any event handler that uses this module.
194+
ble_conn_state_on_ble_evt(p_ble_evt);
195+
196+
// Forward BLE events to the Peer Manager
197+
pm_on_ble_evt(p_ble_evt);
198+
#endif
199+
200+
#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
201+
bleGattcEventHandler(p_ble_evt);
202+
#endif
203+
204+
nRF5xn &ble = nRF5xn::Instance(BLE::DEFAULT_INSTANCE);
205+
nRF5xGap &gap = (nRF5xGap &) ble.getGap();
206+
nRF5xGattServer &gattServer = (nRF5xGattServer &) ble.getGattServer();
207+
nRF5xSecurityManager &securityManager = (nRF5xSecurityManager &) ble.getSecurityManager();
208+
209+
/* Custom event handler */
210+
switch (p_ble_evt->header.evt_id) {
211+
case BLE_GAP_EVT_CONNECTED: {
212+
Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
213+
#if defined(TARGET_MCU_NRF51_16K_S110) || defined(TARGET_MCU_NRF51_32K_S110)
214+
/* Only peripheral role is supported by S110 */
215+
Gap::Role_t role = Gap::PERIPHERAL;
216+
#else
217+
Gap::Role_t role = static_cast<Gap::Role_t>(p_ble_evt->evt.gap_evt.params.connected.role);
218+
#endif
219+
gap.setConnectionHandle(handle);
220+
const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params));
221+
const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr;
222+
const ble_gap_addr_t *own = &p_ble_evt->evt.gap_evt.params.connected.own_addr;
223+
gap.processConnectionEvent(handle,
224+
role,
225+
static_cast<BLEProtocol::AddressType_t>(peer->addr_type), peer->addr,
226+
static_cast<BLEProtocol::AddressType_t>(own->addr_type), own->addr,
227+
params);
228+
break;
229+
}
230+
231+
case BLE_GAP_EVT_DISCONNECTED: {
232+
Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
233+
// Since we are not in a connection and have not started advertising,
234+
// store bonds
235+
gap.setConnectionHandle (BLE_CONN_HANDLE_INVALID);
236+
237+
Gap::DisconnectionReason_t reason;
238+
switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) {
239+
case BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION:
240+
reason = Gap::LOCAL_HOST_TERMINATED_CONNECTION;
241+
break;
242+
case BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION:
243+
reason = Gap::REMOTE_USER_TERMINATED_CONNECTION;
244+
break;
245+
case BLE_HCI_CONN_INTERVAL_UNACCEPTABLE:
246+
reason = Gap::CONN_INTERVAL_UNACCEPTABLE;
247+
break;
248+
default:
249+
/* Please refer to the underlying transport library for an
250+
* interpretion of this reason's value. */
251+
reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason);
252+
break;
253+
}
254+
255+
#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
256+
// Close all pending discoveries for this connection
257+
nRF5xGattClient& gattClient = ble.getGattClient();
258+
gattClient.characteristicDescriptorDiscoverer().terminate(handle, BLE_ERROR_INVALID_STATE);
259+
gattClient.discovery().terminate(handle);
260+
#endif
261+
262+
gap.processDisconnectionEvent(handle, reason);
263+
break;
264+
}
265+
266+
case BLE_GAP_EVT_PASSKEY_DISPLAY:
267+
securityManager.processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey);
268+
break;
269+
270+
case BLE_GAP_EVT_TIMEOUT:
271+
gap.processTimeoutEvent(static_cast<Gap::TimeoutSource_t>(p_ble_evt->evt.gap_evt.params.timeout.src));
272+
break;
273+
274+
case BLE_GATTC_EVT_TIMEOUT:
275+
case BLE_GATTS_EVT_TIMEOUT:
276+
// Disconnect on GATT Server and Client timeout events.
277+
// ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle,
278+
// BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
279+
break;
280+
281+
case BLE_GAP_EVT_ADV_REPORT: {
282+
const ble_gap_evt_adv_report_t *advReport = &p_ble_evt->evt.gap_evt.params.adv_report;
283+
gap.processAdvertisementReport(advReport->peer_addr.addr,
284+
advReport->rssi,
285+
advReport->scan_rsp,
286+
static_cast<GapAdvertisingParams::AdvertisingType_t>(advReport->type),
287+
advReport->dlen,
288+
advReport->data);
289+
break;
290+
}
291+
292+
default:
293+
break;
294+
}
295+
296+
gattServer.hwCallback(p_ble_evt);
297+
}
298+
299+
/*! @brief Callback when an error occurs inside the SoftDevice */
300+
void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
301+
{
302+
ASSERT_TRUE(false, (void) 0);
303+
}
304+
305+
/*!
306+
@brief Handler for general errors above the SoftDevice layer.
307+
Typically we can' recover from this so we do a reset.
308+
*/
309+
void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name)
310+
{
311+
ASSERT_STATUS_RET_VOID( error_code );
312+
NVIC_SystemReset();
313+
}

0 commit comments

Comments
 (0)