To get started with Seeed Tiny BLE, include detecting motion, button and battery level.

Dependencies:   BLE_API eMPL_MPU6050 mbed nRF51822

Committer:
yihui
Date:
Wed Apr 22 07:47:17 2015 +0000
Revision:
1:fc2f9d636751
update libraries; ; delete nRF51822/nordic-sdk/components/gpiote/app_gpiote.c to solve GPIOTE_IRQHandler multiply defined issue. temperarily change nRF51822 library to folder

Who changed what in which revision?

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