Files
microchip-application-bmd38…/le_gap.c
T
chain40 d99848592b bugfix: 修正掉資料的問題 (已排除)
原因: 因為連線後的 connection interval 設為 20ms, 造成 EIS 在 100sps 狀況下會掉資料

修正方式: 將連線後的 connection interval 設為 8~10ms, 即可排除 EIS 掉資料的問題

其它: 將程式碼優化調整為 -O0, 利於程式碼除錯
2023-08-15 23:30:50 +08:00

114 lines
3.1 KiB
C

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#ifdef __cplusplus
extern "C"
{
#endif
#include "app_config.h"
#include "apply_old_config.h"
#include "sdk_config.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf_ble_scan.h"
#include "nrf_sdh.h"
#include "nrf_sdh_ble.h"
#include "app_error.h"
#ifdef __cplusplus
}
#endif
bool is_connected = false;
static void le_gap_handler(ble_evt_t const *p_ble_evt, void *p_context)
{
ret_code_t err_code;
switch (p_ble_evt->header.evt_id)
{
case BLE_GAP_EVT_CONNECTED:
is_connected = true;
break;
case BLE_GAP_EVT_DISCONNECTED:
is_connected = false;
break;
default:
break;
}
}
void le_gap_init(const char *device_name, uint16_t usAppearance)
{
// Register a handler for BLE events.
NRF_SDH_BLE_OBSERVER(m_gap_observer, 3, le_gap_handler, NULL);
}
void le_gap_connet(ble_gap_addr_t *p_peer_addr)
{
if (is_connected)
{
return;
}
// If address is correct, stop scanning and initiate connection with peripheral device.
const ble_gap_conn_params_t conn_params = {
.conn_sup_timeout =
(uint16_t)MSEC_TO_UNITS(NRF_BLE_SCAN_SUPERVISION_TIMEOUT, UNIT_10_MS),
.min_conn_interval =
(uint16_t)MSEC_TO_UNITS(NRF_BLE_SCAN_MIN_CONNECTION_INTERVAL, UNIT_1_25_MS),
.max_conn_interval =
(uint16_t)MSEC_TO_UNITS(NRF_BLE_SCAN_MAX_CONNECTION_INTERVAL, UNIT_1_25_MS),
.slave_latency =
(uint16_t)NRF_BLE_SCAN_SLAVE_LATENCY,
};
extern ble_gap_scan_params_t *le_scan_params(void);
ret_code_t err_code = sd_ble_gap_connect(p_peer_addr,
le_scan_params(),
&conn_params,
APP_BLE_CONN_CFG_TAG);
APP_ERROR_CHECK(err_code);
}
void le_gap_disconnet(uint16_t conn_handle)
{
ret_code_t err_code = sd_ble_gap_disconnect(conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
}
void le_gap_phy_update(uint16_t conn_handle)
{
NRF_LOG_INFO("Send PHY update request.");
ble_gap_phys_t const phys = {
.rx_phys = BLE_GAP_PHY_AUTO,
.tx_phys = BLE_GAP_PHY_AUTO,
};
ret_code_t err_code = sd_ble_gap_phy_update(conn_handle, &phys);
APP_ERROR_CHECK(err_code);
}
void le_gap_conn_param_update(uint16_t conn_handle)
{
NRF_LOG_INFO("Central Preferred Connection Parameters.");
// Set GAP Central Preferred Connection Parameters.
ble_gap_conn_params_t const gap_conn_params = {
.min_conn_interval = MSEC_TO_UNITS(8, UNIT_1_25_MS), /**< Minimum connection interval ( 7.5 ms) */
.max_conn_interval = MSEC_TO_UNITS(10, UNIT_1_25_MS), /**< Maximum connection interval (10.0 ms). */
.slave_latency = 8, /**< Slave latency. */
.conn_sup_timeout = MSEC_TO_UNITS(10000, UNIT_10_MS) /**< Connection supervisory timeout (10s). */
};
sd_ble_gap_conn_param_update(conn_handle, &gap_conn_params);
}
bool le_gap_is_connected(void)
{
return is_connected;
}