Updated to latest online libraries (See also mbed Apps: HelloBlue, FOTA4)

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Default_APP by Yihui Xiong

Committer:
yihui
Date:
Fri Oct 10 03:36:28 2014 +0000
Revision:
1:a607cd9655d7
use NVIC_SystemReset() to run bootloader

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 1:a607cd9655d7 1 /* mbed Microcontroller Library
yihui 1:a607cd9655d7 2 * Copyright (c) 2006-2013 ARM Limited
yihui 1:a607cd9655d7 3 *
yihui 1:a607cd9655d7 4 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 1:a607cd9655d7 5 * you may not use this file except in compliance with the License.
yihui 1:a607cd9655d7 6 * You may obtain a copy of the License at
yihui 1:a607cd9655d7 7 *
yihui 1:a607cd9655d7 8 * http://www.apache.org/licenses/LICENSE-2.0
yihui 1:a607cd9655d7 9 *
yihui 1:a607cd9655d7 10 * Unless required by applicable law or agreed to in writing, software
yihui 1:a607cd9655d7 11 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 1:a607cd9655d7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 1:a607cd9655d7 13 * See the License for the specific language governing permissions and
yihui 1:a607cd9655d7 14 * limitations under the License.
yihui 1:a607cd9655d7 15 */
yihui 1:a607cd9655d7 16 #include "common/common.h"
yihui 1:a607cd9655d7 17
yihui 1:a607cd9655d7 18 #include "app_timer.h"
yihui 1:a607cd9655d7 19 #include "ble_gap.h"
yihui 1:a607cd9655d7 20 #include "ble_conn_params.h"
yihui 1:a607cd9655d7 21
yihui 1:a607cd9655d7 22 static inline uint32_t msec_to_1_25msec(uint32_t interval_ms) ATTR_ALWAYS_INLINE ATTR_CONST;
yihui 1:a607cd9655d7 23 #if SDK_CONN_PARAMS_MODULE_ENABLE
yihui 1:a607cd9655d7 24 static void error_callback(uint32_t nrf_error);
yihui 1:a607cd9655d7 25 #endif // SDK_CONN_PARAMS_MODULE_ENABLE
yihui 1:a607cd9655d7 26
yihui 1:a607cd9655d7 27 /**************************************************************************/
yihui 1:a607cd9655d7 28 /*!
yihui 1:a607cd9655d7 29 @brief Initialise GAP in the underlying SoftDevice
yihui 1:a607cd9655d7 30
yihui 1:a607cd9655d7 31 @returns
yihui 1:a607cd9655d7 32 */
yihui 1:a607cd9655d7 33 /**************************************************************************/
yihui 1:a607cd9655d7 34 error_t btle_gap_init(void)
yihui 1:a607cd9655d7 35 {
yihui 1:a607cd9655d7 36 ble_gap_conn_params_t gap_conn_params = {0};
yihui 1:a607cd9655d7 37
yihui 1:a607cd9655d7 38 gap_conn_params.min_conn_interval = msec_to_1_25msec(CFG_GAP_CONNECTION_MIN_INTERVAL_MS); // in 1.25ms units
yihui 1:a607cd9655d7 39 gap_conn_params.max_conn_interval = msec_to_1_25msec(CFG_GAP_CONNECTION_MAX_INTERVAL_MS); // in 1.25ms unit
yihui 1:a607cd9655d7 40 gap_conn_params.slave_latency = CFG_GAP_CONNECTION_SLAVE_LATENCY;
yihui 1:a607cd9655d7 41 gap_conn_params.conn_sup_timeout = CFG_GAP_CONNECTION_SUPERVISION_TIMEOUT_MS / 10; // in 10ms unit
yihui 1:a607cd9655d7 42
yihui 1:a607cd9655d7 43 ble_gap_conn_sec_mode_t sec_mode;
yihui 1:a607cd9655d7 44 BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode); // no security is needed
yihui 1:a607cd9655d7 45
yihui 1:a607cd9655d7 46 ASSERT_STATUS( sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *) CFG_GAP_LOCAL_NAME, strlen(CFG_GAP_LOCAL_NAME)));
yihui 1:a607cd9655d7 47 ASSERT_STATUS( sd_ble_gap_appearance_set(CFG_GAP_APPEARANCE));
yihui 1:a607cd9655d7 48 ASSERT_STATUS( sd_ble_gap_ppcp_set(&gap_conn_params));
yihui 1:a607cd9655d7 49 ASSERT_STATUS( sd_ble_gap_tx_power_set(CFG_BLE_TX_POWER_LEVEL));
yihui 1:a607cd9655d7 50
yihui 1:a607cd9655d7 51 /**
yihui 1:a607cd9655d7 52 * Call to conn_params_init() is not necessary; and so is disabled by default.
yihui 1:a607cd9655d7 53 * This API should be exposed to the user to be invoked when necessary.
yihui 1:a607cd9655d7 54 */
yihui 1:a607cd9655d7 55 #if SDK_CONN_PARAMS_MODULE_ENABLE
yihui 1:a607cd9655d7 56 /* Connection Parameters */
yihui 1:a607cd9655d7 57 enum {
yihui 1:a607cd9655d7 58 FIRST_UPDATE_DELAY = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER),
yihui 1:a607cd9655d7 59 NEXT_UPDATE_DELAY = APP_TIMER_TICKS(5000, CFG_TIMER_PRESCALER),
yihui 1:a607cd9655d7 60 MAX_UPDATE_COUNT = 3
yihui 1:a607cd9655d7 61 };
yihui 1:a607cd9655d7 62
yihui 1:a607cd9655d7 63 ble_conn_params_init_t cp_init = {0};
yihui 1:a607cd9655d7 64
yihui 1:a607cd9655d7 65 cp_init.p_conn_params = NULL;
yihui 1:a607cd9655d7 66 cp_init.first_conn_params_update_delay = FIRST_UPDATE_DELAY;
yihui 1:a607cd9655d7 67 cp_init.next_conn_params_update_delay = NEXT_UPDATE_DELAY;
yihui 1:a607cd9655d7 68 cp_init.max_conn_params_update_count = MAX_UPDATE_COUNT;
yihui 1:a607cd9655d7 69 cp_init.start_on_notify_cccd_handle = BLE_GATT_HANDLE_INVALID;
yihui 1:a607cd9655d7 70 cp_init.disconnect_on_fail = true;
yihui 1:a607cd9655d7 71 cp_init.evt_handler = NULL;
yihui 1:a607cd9655d7 72 cp_init.error_handler = error_callback;
yihui 1:a607cd9655d7 73
yihui 1:a607cd9655d7 74 ASSERT_STATUS ( ble_conn_params_init(&cp_init));
yihui 1:a607cd9655d7 75 #endif // SDK_CONN_PARAMS_MODULE_ENABLE
yihui 1:a607cd9655d7 76
yihui 1:a607cd9655d7 77 return ERROR_NONE;
yihui 1:a607cd9655d7 78 }
yihui 1:a607cd9655d7 79
yihui 1:a607cd9655d7 80 /**************************************************************************/
yihui 1:a607cd9655d7 81 /*!
yihui 1:a607cd9655d7 82 @brief Converts msecs to an integer representing 1.25ms units
yihui 1:a607cd9655d7 83
yihui 1:a607cd9655d7 84 @param[in] ms
yihui 1:a607cd9655d7 85 The number of milliseconds to conver to 1.25ms units
yihui 1:a607cd9655d7 86
yihui 1:a607cd9655d7 87 @returns The number of 1.25ms units in the supplied number of ms
yihui 1:a607cd9655d7 88 */
yihui 1:a607cd9655d7 89 /**************************************************************************/
yihui 1:a607cd9655d7 90 static inline uint32_t msec_to_1_25msec(uint32_t interval_ms)
yihui 1:a607cd9655d7 91 {
yihui 1:a607cd9655d7 92 return (interval_ms * 4) / 5;
yihui 1:a607cd9655d7 93 }
yihui 1:a607cd9655d7 94
yihui 1:a607cd9655d7 95 #if SDK_CONN_PARAMS_MODULE_ENABLE
yihui 1:a607cd9655d7 96 static void error_callback(uint32_t nrf_error)
yihui 1:a607cd9655d7 97 {
yihui 1:a607cd9655d7 98 ASSERT_STATUS_RET_VOID( nrf_error );
yihui 1:a607cd9655d7 99 }
yihui 1:a607cd9655d7 100 #endif // SDK_CONN_PARAMS_MODULE_ENABLE