Skip to content

Add nRf52840 target #3841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Mar 23, 2017
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
cd44b58
Initial commit of NRF52840 target as the copy of NRF52832 and NRF52_DK.
nvlsianpu Dec 9, 2016
ad5b64b
merge nRF528xx sdk sources into one directory
nvlsianpu Dec 9, 2016
9fa7a61
Update nRF5 SDK sources from version v11 to the version nRF5_SDK_13.0…
nvlsianpu Dec 14, 2016
decf835
move s140 headers to headers directory
nvlsianpu Dec 14, 2016
5ebcb3e
fix- sdk updste script bug causing losing of nested directory when co…
nvlsianpu Dec 15, 2016
9bf209e
fix: align nRF5 v13 sdk struct to origin
nvlsianpu Dec 15, 2016
a175e2c
+ description of the NRF52840_DK target
nvlsianpu Dec 19, 2016
aef4121
ported-fixed for nRF52840 port
nvlsianpu Dec 23, 2016
f0b3cd2
to be commented
nvlsianpu Jan 3, 2017
46f7277
make nRF52840 target compilable for ARM compiler
nvlsianpu Jan 3, 2017
5649eef
Enabled nRF52840_DK's compilation for IAR IDE:
nvlsianpu Jan 4, 2017
f1c3d75
initial creation of nRF52840 BLE target implementation "NRF5_SDK13" a…
nvlsianpu Jan 4, 2017
5b36de9
nrf52840: add workaround for RTC errata 20
nvlsianpu Jan 11, 2017
689d12d
NRF5_SDK13: Make BLE sources compilable - this is not redy to use!
nvlsianpu Jan 16, 2017
8aa71fa
add mising fds configuration
nvlsianpu Jan 16, 2017
42fcae4
nRF5: whitelisting update fo SD API >-3.x.x
nvlsianpu Jan 25, 2017
1366dfb
nrf5_SDK13 update copyright clause
nvlsianpu Jan 25, 2017
9d8c05b
introduce API update to (v3.x.x and further)in BLE_GAP_EVT_CONNECTED …
nvlsianpu Jan 31, 2017
4e188af
upgrade implementationof gap::connect in case of private address and …
nvlsianpu Feb 1, 2017
113ae4e
[NRF5_SDK13] fix:
nvlsianpu Feb 2, 2017
3a81e23
[nRF5_SDK13] Changes from CR.
nvlsianpu Feb 6, 2017
f32da7a
[nRF5_SDK13] fix:
nvlsianpu Feb 8, 2017
9f0d754
[NRF5_SDK13] fix:
nvlsianpu Feb 9, 2017
b1e5509
[NRF52480] fix bugs:
nvlsianpu Feb 22, 2017
807cd23
[nRF52840] Removed nRF52840 from memory constrained device list.
nvlsianpu Feb 23, 2017
e798edc
[NRF52840]: fix mbed-os-rtos-rtx-target_cortex_m-tests-memory-heap_an…
nvlsianpu Feb 27, 2017
b1b802f
i2c_api implementation has been changed in order to support single by…
nvlsianpu Mar 1, 2017
f6da6c0
[NRF52840] code formating, CR changes
nvlsianpu Mar 2, 2017
71f3022
[NRF52840] Removed nrf52 targets files form nrf5_sdk13 sources.
nvlsianpu Mar 3, 2017
c78f73c
Fix compilation errors on linux machine
Mar 13, 2017
680d086
[nRF52840] use core_util_critical_section_enter/exit instead of __ena…
nvlsianpu Mar 22, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2015 ARM Limited

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* S110/S120/S130 License Agreement
*
* Copyright (c) 2015, Nordic Semiconductor ASA, All rights reserved.
*
* Redistribution. Redistribution and use in binary form, without modification,
* are permitted provided that the following conditions are met:
*
* • Redistributions must reproduce the above copyright notice and the following
* disclaimer in the documentation and/or other materials provided with the
* distribution.
* • Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* • No reverse engineering, decompilation, or disassembly of this software is
* permitted.
*
* DISCLAIMER.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* /
Original file line number Diff line number Diff line change
@@ -0,0 +1,336 @@
/* mbed Microcontroller Library
* Copyright (c) 2006-2013 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "common/common.h"
#include "nordic_common.h"

#include "btle.h"
#include "btle_clock.h"

#include "ble_flash.h"
#include "ble_conn_params.h"

#include "btle_gap.h"
//#include "btle_advertising.h"
#include "custom/custom_helper.h"

#include "ble/GapEvents.h"
#include "nRF5xn.h"

#ifdef S110
#define IS_LEGACY_DEVICE_MANAGER_ENABLED 1
#elif defined(S130) || defined(S132)
#define IS_LEGACY_DEVICE_MANAGER_ENABLED 0
#endif

extern "C" {
#if (IS_LEGACY_DEVICE_MANAGER_ENABLED)
#include "pstorage.h"
#include "device_manager.h"
#else
#include "fstorage.h"
#include "fds.h"
#include "peer_manager.h"
#include "ble_conn_state.h"
#endif

#include "softdevice_handler.h"
#include "ble_stack_handler_types.h"
}

#include "ble_hci.h"
#include "btle_discovery.h"

#include "nRF5xGattClient.h"
#include "nRF5xServiceDiscovery.h"
#include "nRF5xCharacteristicDescriptorDiscoverer.h"


bool isEventsSignaled = false;

extern "C" void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name);
void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name);
extern "C" void SD_EVT_IRQHandler(void); // export the softdevice event handler for registration by nvic-set-vector.


static void btle_handler(ble_evt_t *p_ble_evt);

#if 0
#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 */
/** If value for YOTTA_CFG_NORDIC_BLE_PERIPHERAL_LINKS was used, ram settings are adjusted by the yotta target module. */
#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*/
/** If value for YOTTA_CFG_NORDIC_BLE_CENTRAL_LINKS was used, ram settings are adjusted by the yotta target module. */
#define GATTS_ATTR_TAB_SIZE (YOTTA_CFG_NORDIC_BLE_GATTS_ATTR_TAB_SIZE) /**< GATTS attribite table size. */
/** If value for YOTTA_CFG_NORDIC_BLE_GATTS_ATTR_TAB_SIZE was used, ram settings are adjusted by the yotta target module. */
#else
#define CENTRAL_LINK_COUNT 3 /**<number of central links used by the application. When changing this number remember to adjust the RAM settings */
/** If value for YOTTA_CFG_NORDIC_BLE_PERIPHERAL_LINKS was used, ram settings are adjusted by the yotta target module. */
#define PERIPHERAL_LINK_COUNT 1 /**<number of peripheral links used by the application. When changing this number remember to adjust the RAM settings*/
/** If value for YOTTA_CFG_NORDIC_BLE_CENTRAL_LINKS was used, ram settings are adjusted by the yotta target module. */
#define GATTS_ATTR_TAB_SIZE 0x600 /**< GATTS attribite table size. */
/** If value for YOTTA_CFG_NORDIC_BLE_GATTS_ATTR_TAB_SIZE was used, ram settings are adjusted by the yotta target module. */

#endif

static void sys_evt_dispatch(uint32_t sys_evt)
{
#if (IS_LEGACY_DEVICE_MANAGER_ENABLED)
pstorage_sys_event_handler(sys_evt);
#else
// Forward Softdevice events to the fstorage module
fs_sys_event_handler(sys_evt);
#endif
}

/**
* This function is called in interrupt context to handle BLE events; i.e. pull
* system and user events out of the pending events-queue of the BLE stack. The
* BLE stack signals the availability of events by the triggering the SWI2
* interrupt, which forwards the handling to this function.
*
* The event processing loop is implemented in intern_softdevice_events_execute().
*
* This function will signal to the user code by calling signalEventsToProcess
* that their is events to process and BLE::processEvents should be called.
*/
static uint32_t signalEvent()
{
if(isEventsSignaled == false) {
isEventsSignaled = true;
nRF5xn::Instance(BLE::DEFAULT_INSTANCE).signalEventsToProcess(BLE::DEFAULT_INSTANCE);
}
return NRF_SUCCESS;
}


error_t btle_init(void)
{
nrf_clock_lf_cfg_t clockConfiguration;

// register softdevice handler vector
NVIC_SetVector(SD_EVT_IRQn, (uint32_t) SD_EVT_IRQHandler);

// Configure the LF clock according to values provided by btle_clock.h.
// It is input from the chain of the yotta configuration system.
clockConfiguration.source = LFCLK_CONF_SOURCE;
clockConfiguration.xtal_accuracy = LFCLK_CONF_ACCURACY;
clockConfiguration.rc_ctiv = LFCLK_CONF_RC_CTIV;
clockConfiguration.rc_temp_ctiv = LFCLK_CONF_RC_TEMP_CTIV;

SOFTDEVICE_HANDLER_INIT(&clockConfiguration, signalEvent);

// Enable BLE stack
/**
* Using this call, the application can select whether to include the
* Service Changed characteristic in the GATT Server. The default in all
* previous releases has been to include the Service Changed characteristic,
* but this affects how GATT clients behave. Specifically, it requires
* clients to subscribe to this attribute and not to cache attribute handles
* between connections unless the devices are bonded. If the application
* does not need to change the structure of the GATT server attributes at
* runtime this adds unnecessary complexity to the interaction with peer
* clients. If the SoftDevice is enabled with the Service Changed
* Characteristics turned off, then clients are allowed to cache attribute
* handles making applications simpler on both sides.
*/
static const bool IS_SRVC_CHANGED_CHARACT_PRESENT = true;

ble_enable_params_t ble_enable_params;
uint32_t err_code = softdevice_enable_get_default_config(CENTRAL_LINK_COUNT,
PERIPHERAL_LINK_COUNT,
&ble_enable_params);

ble_enable_params.gatts_enable_params.attr_tab_size = GATTS_ATTR_TAB_SIZE;
ble_enable_params.gatts_enable_params.service_changed = IS_SRVC_CHANGED_CHARACT_PRESENT;
ble_enable_params.common_enable_params.vs_uuid_count = UUID_TABLE_MAX_ENTRIES;

if(err_code != NRF_SUCCESS) {
return ERROR_INVALID_PARAM;
}

if (softdevice_enable(&ble_enable_params) != NRF_SUCCESS) {
return ERROR_INVALID_PARAM;
}

// Peer Manger must been initialised prior any other call to its API (this file and btle_security_pm.cpp)
pm_init();

#if (NRF_SD_BLE_API_VERSION <= 2)
ble_gap_addr_t addr;
if (sd_ble_gap_address_get(&addr) != NRF_SUCCESS) {
return ERROR_INVALID_PARAM;
}
if (sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &addr) != NRF_SUCCESS) {
return ERROR_INVALID_PARAM;
}
#else
ble_gap_privacy_params_t privacy_params = {0};
privacy_params.privacy_mode = BLE_GAP_PRIVACY_MODE_OFF;
pm_privacy_set(&privacy_params);
#endif

ASSERT_STATUS( softdevice_ble_evt_handler_set(btle_handler));
ASSERT_STATUS( softdevice_sys_evt_handler_set(sys_evt_dispatch));

return btle_gap_init();
}

static void btle_handler(ble_evt_t *p_ble_evt)
{
/* Library service handlers */
#if SDK_CONN_PARAMS_MODULE_ENABLE
ble_conn_params_on_ble_evt(p_ble_evt);
#endif

#if (IS_LEGACY_DEVICE_MANAGER_ENABLED)
dm_ble_evt_handler(p_ble_evt);
#else
// Forward BLE events to the Connection State module.
// This must be called before any event handler that uses this module.
ble_conn_state_on_ble_evt(p_ble_evt);

// Forward BLE events to the Peer Manager
pm_on_ble_evt(p_ble_evt);
#endif

#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
bleGattcEventHandler(p_ble_evt);
#endif

nRF5xn &ble = nRF5xn::Instance(BLE::DEFAULT_INSTANCE);
nRF5xGap &gap = (nRF5xGap &) ble.getGap();
nRF5xGattServer &gattServer = (nRF5xGattServer &) ble.getGattServer();
nRF5xSecurityManager &securityManager = (nRF5xSecurityManager &) ble.getSecurityManager();

/* Custom event handler */
switch (p_ble_evt->header.evt_id) {
case BLE_GAP_EVT_CONNECTED: {
Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
#if defined(TARGET_MCU_NRF51_16K_S110) || defined(TARGET_MCU_NRF51_32K_S110)
/* Only peripheral role is supported by S110 */
Gap::Role_t role = Gap::PERIPHERAL;
#else
Gap::Role_t role = static_cast<Gap::Role_t>(p_ble_evt->evt.gap_evt.params.connected.role);
#endif
gap.setConnectionHandle(handle);
const Gap::ConnectionParams_t *params = reinterpret_cast<Gap::ConnectionParams_t *>(&(p_ble_evt->evt.gap_evt.params.connected.conn_params));

const ble_gap_addr_t *peer = &p_ble_evt->evt.gap_evt.params.connected.peer_addr;
#if (NRF_SD_BLE_API_VERSION <= 2)
const ble_gap_addr_t *own = &p_ble_evt->evt.gap_evt.params.connected.own_addr;

gap.processConnectionEvent(handle,
role,
static_cast<BLEProtocol::AddressType_t>(peer->addr_type), peer->addr,
static_cast<BLEProtocol::AddressType_t>(own->addr_type), own->addr,
params);
#else
Gap::AddressType_t addr_type;
Gap::Address_t own_address;
gap.getAddress(&addr_type, own_address);

gap.processConnectionEvent(handle,
role,
static_cast<BLEProtocol::AddressType_t>(peer->addr_type), peer->addr,
addr_type, own_address,
params);
#endif
break;
}

case BLE_GAP_EVT_DISCONNECTED: {
Gap::Handle_t handle = p_ble_evt->evt.gap_evt.conn_handle;
// Since we are not in a connection and have not started advertising,
// store bonds
gap.setConnectionHandle (BLE_CONN_HANDLE_INVALID);

Gap::DisconnectionReason_t reason;
switch (p_ble_evt->evt.gap_evt.params.disconnected.reason) {
case BLE_HCI_LOCAL_HOST_TERMINATED_CONNECTION:
reason = Gap::LOCAL_HOST_TERMINATED_CONNECTION;
break;
case BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION:
reason = Gap::REMOTE_USER_TERMINATED_CONNECTION;
break;
case BLE_HCI_CONN_INTERVAL_UNACCEPTABLE:
reason = Gap::CONN_INTERVAL_UNACCEPTABLE;
break;
default:
/* Please refer to the underlying transport library for an
* interpretion of this reason's value. */
reason = static_cast<Gap::DisconnectionReason_t>(p_ble_evt->evt.gap_evt.params.disconnected.reason);
break;
}

#if !defined(TARGET_MCU_NRF51_16K_S110) && !defined(TARGET_MCU_NRF51_32K_S110)
// Close all pending discoveries for this connection
nRF5xGattClient& gattClient = ble.getGattClient();
gattClient.characteristicDescriptorDiscoverer().terminate(handle, BLE_ERROR_INVALID_STATE);
gattClient.discovery().terminate(handle);
#endif

gap.processDisconnectionEvent(handle, reason);
break;
}

case BLE_GAP_EVT_PASSKEY_DISPLAY:
securityManager.processPasskeyDisplayEvent(p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->evt.gap_evt.params.passkey_display.passkey);
break;

case BLE_GAP_EVT_TIMEOUT:
gap.processTimeoutEvent(static_cast<Gap::TimeoutSource_t>(p_ble_evt->evt.gap_evt.params.timeout.src));
break;

case BLE_GATTC_EVT_TIMEOUT:
case BLE_GATTS_EVT_TIMEOUT:
// Disconnect on GATT Server and Client timeout events.
// ASSERT_STATUS_RET_VOID (sd_ble_gap_disconnect(m_conn_handle,
// BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION));
break;

case BLE_GAP_EVT_ADV_REPORT: {
const ble_gap_evt_adv_report_t *advReport = &p_ble_evt->evt.gap_evt.params.adv_report;
gap.processAdvertisementReport(advReport->peer_addr.addr,
advReport->rssi,
advReport->scan_rsp,
static_cast<GapAdvertisingParams::AdvertisingType_t>(advReport->type),
advReport->dlen,
advReport->data);
break;
}

default:
break;
}

gattServer.hwCallback(p_ble_evt);
}

/*! @brief Callback when an error occurs inside the SoftDevice */
void assert_nrf_callback(uint16_t line_num, const uint8_t *p_file_name)
{
ASSERT_TRUE(false, (void) 0);
}

/*!
@brief Handler for general errors above the SoftDevice layer.
Typically we can' recover from this so we do a reset.
*/
void app_error_handler(uint32_t error_code, uint32_t line_num, const uint8_t *p_file_name)
{
ASSERT_STATUS_RET_VOID( error_code );
NVIC_SystemReset();
}
Loading