Nordic stack and drivers for the mbed BLE API

Dependents:   idd_hw5_bleFanProto

Fork of nRF51822 by Nordic Semiconductor

Committer:
Rohit Grover
Date:
Fri Jul 11 13:52:12 2014 +0100
Revision:
42:120a96d10f90
Parent:
22:c6ee8136847e
Child:
56:a1071b629aa3
white space diffs

Who changed what in which revision?

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