For scoop

Dependents:   GonioTrainer

Fork of nRF51822 by Nordic Semiconductor

Committer:
Rohit Grover
Date:
Wed Jun 04 09:19:57 2014 +0100
Revision:
17:c3e11bb6cb72
Parent:
0:eff01767de02
Child:
22:c6ee8136847e
white space diffs; uncrustify

Who changed what in which revision?

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