Skip to content

Commit 8435a2b

Browse files
authored
disable fido service && optimize ble connection interval parameters. (#59)
* disable FIDO service. * adjust connection interval parameters. * optimize SPI data transmission. * disable NRF_SDH_BLE_SERVICE_CHANGED. * resolve data forwarding bug. * optimize data read/write handling with ST. * add command to query connection status.
1 parent 0e881a7 commit 8435a2b

File tree

3 files changed

+91
-59
lines changed

3 files changed

+91
-59
lines changed

app/data_transmission.c

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void usr_spim_init(void)
4545
driver_spi_config.mosi_pin = STM32_SPI2_MOSI_IO;
4646
driver_spi_config.sck_pin = STM32_SPI2_CLK_IO;
4747
driver_spi_config.frequency = NRF_SPIM_FREQ_4M;
48-
err_code = nrfx_spim_init(&m_spim_master, &driver_spi_config, spi_event_handler, NULL);
48+
err_code = nrfx_spim_init(&m_spim_master, &driver_spi_config, NULL, NULL);
4949
APP_ERROR_CHECK(err_code);
5050

5151
nrf_gpio_cfg_output(STM32_SPI2_CSN_IO);
@@ -68,6 +68,9 @@ void usr_spi_write(uint8_t* p_buffer, uint32_t size)
6868

6969
nrf_gpio_pin_clear(STM32_SPI2_CSN_IO);
7070

71+
uint32_t event_end = nrfx_spim_end_event_get(&m_spim_master);
72+
volatile uint32_t* event_end_ptr = (uint32_t*)event_end;
73+
7174
while ( size > 0 )
7275
{
7376
uint32_t send_len = size > 255 ? 255 : size;
@@ -79,55 +82,71 @@ void usr_spi_write(uint8_t* p_buffer, uint32_t size)
7982
}
8083

8184
spi_xfer_done = false;
85+
*event_end_ptr = 0;
8286

8387
driver_spim_xfer.tx_length = send_len;
8488
driver_spim_xfer.p_tx_buffer = buffer;
8589
driver_spim_xfer.rx_length = 0;
8690
driver_spim_xfer.p_rx_buffer = NULL;
87-
ret_code_t err_code = nrfx_spim_xfer(&m_spim_master, &driver_spim_xfer, 0);
91+
ret_code_t err_code = nrfx_spim_xfer(&m_spim_master, &driver_spim_xfer, NRFX_SPIM_FLAG_NO_XFER_EVT_HANDLER);
8892
APP_ERROR_CHECK(err_code);
8993
p_buffer += send_len;
9094
size -= send_len;
91-
while ( !spi_xfer_done )
95+
// while ( !spi_xfer_done )
96+
// {
97+
// nrf_delay_us(1);
98+
// }
99+
uint32_t timeout = 5000;
100+
101+
while ( *event_end_ptr == 0 )
92102
{
93103
nrf_delay_us(1);
104+
timeout--;
105+
if ( timeout == 0 )
106+
{
107+
break;
108+
}
94109
}
110+
*event_end_ptr = 0;
95111
}
96112
nrf_gpio_pin_set(STM32_SPI2_CSN_IO);
97-
98-
// uint32_t timeout = 5000;
99-
// while ( spi_dir_out )
100-
// {
101-
// timeout--;
102-
// if ( timeout == 0 )
103-
// {
104-
// break;
105-
// }
106-
// nrf_delay_us(1);
107-
// }
108-
// spi_dir_out = false;
109113
}
110114

111115
bool usr_spi_read(uint8_t* p_buffer, uint32_t size)
112116
{
113117

114118
nrf_gpio_pin_clear(STM32_SPI2_CSN_IO);
115119

120+
uint32_t event_end = nrfx_spim_end_event_get(&m_spim_master);
121+
volatile uint32_t* event_end_ptr = (uint32_t*)event_end;
122+
116123
while ( size > 0 )
117124
{
118125
spi_xfer_done = false;
126+
*event_end_ptr = 0;
119127
uint32_t read_len = size > 255 ? 255 : size;
120128
driver_spim_xfer.tx_length = 0;
121129
driver_spim_xfer.p_tx_buffer = NULL;
122130
driver_spim_xfer.rx_length = read_len;
123131
driver_spim_xfer.p_rx_buffer = p_buffer;
124-
APP_ERROR_CHECK(nrfx_spim_xfer(&m_spim_master, &driver_spim_xfer, 0));
132+
APP_ERROR_CHECK(nrfx_spim_xfer(&m_spim_master, &driver_spim_xfer, NRFX_SPIM_FLAG_NO_XFER_EVT_HANDLER));
125133
p_buffer += read_len;
126134
size -= read_len;
127-
while ( !spi_xfer_done )
135+
// while ( !spi_xfer_done )
136+
// {
137+
// nrf_delay_us(1);
138+
// }
139+
uint32_t timeout = 5000;
140+
while ( *event_end_ptr == 0 )
128141
{
129142
nrf_delay_us(1);
143+
timeout--;
144+
if ( timeout == 0 )
145+
{
146+
break;
147+
}
130148
}
149+
*event_end_ptr = 0;
131150
}
132151
nrf_gpio_pin_set(STM32_SPI2_CSN_IO);
133152

@@ -266,22 +285,8 @@ static bool spi_read_data(void)
266285

267286
void spi_write_st_data(void* data, uint16_t len)
268287
{
269-
uint16_t send_spi_offset = 0;
270-
271-
while ( len > 0 )
272-
{
273-
uint32_t send_len = len > 64 ? 64 : len;
274-
if ( send_len < 64 )
275-
{
276-
usr_spi_write((uint8_t*)(data + send_spi_offset), 64);
277-
}
278-
else
279-
{
280-
usr_spi_write((uint8_t*)(data + send_spi_offset), send_len);
281-
}
282-
send_spi_offset += send_len;
283-
len -= send_len;
284-
}
288+
uint32_t send_len = (len + 63) / 64 * 64;
289+
usr_spi_write((uint8_t*)(data), send_len);
285290
}
286291

287292
extern void ble_fido_send(uint8_t* data, uint16_t data_len);
@@ -294,10 +299,12 @@ void spi_read_st_data(void* data, uint16_t len)
294299
{
295300
ble_nus_send(data_recived_buf, data_recived_len);
296301
}
302+
#if BLE_FIDO_ENABLED
297303
else if ( spi_data_type == DATA_TYPE_FIDO )
298304
{
299305
ble_fido_send(data_recived_buf, data_recived_len);
300306
}
307+
#endif
301308
}
302309
}
303310

@@ -308,11 +315,11 @@ void spi_state_reset(void)
308315

309316
void spi_state_update(void)
310317
{
311-
if (read_state == READSTATE_READ_DATA)
318+
if ( read_state == READSTATE_READ_DATA )
312319
{
313320
read_state = READSTATE_READ_DATA_WAIT;
314321
}
315-
else if (read_state == READSTATE_READ_DATA_WAIT)
322+
else if ( read_state == READSTATE_READ_DATA_WAIT )
316323
{
317324
read_state = READSTATE_IDLE;
318325
}

app/main.c

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
#define BLE_DISCON 3
126126
#define BLE_CON 4
127127
#define BLE_PAIR 5
128+
#define BLE_CONN_STATUS 6
128129

129130
#define PWR_DEF 0
130131
#define PWR_SHUTDOWN_SYS 1
@@ -163,8 +164,8 @@
163164
#define BATTERY_LEVEL_MEAS_INTERVAL APP_TIMER_TICKS(1000) /**< Battery level measurement interval (ticks). */
164165
#define BATTERY_MEAS_LONG_INTERVAL APP_TIMER_TICKS(5000)
165166

166-
#define MIN_CONN_INTERVAL MSEC_TO_UNITS(30, UNIT_1_25_MS) /**< Minimum acceptable connection interval (10 ms). */
167-
#define MAX_CONN_INTERVAL MSEC_TO_UNITS(30, UNIT_1_25_MS) /**< Maximum acceptable connection interval (100 ms) */
167+
#define MIN_CONN_INTERVAL MSEC_TO_UNITS(15, UNIT_1_25_MS) /**< Minimum acceptable connection interval (10 ms). */
168+
#define MAX_CONN_INTERVAL MSEC_TO_UNITS(15, UNIT_1_25_MS) /**< Maximum acceptable connection interval (100 ms) */
168169
#define SLAVE_LATENCY 0 /**< Slave latency. */
169170
#define CONN_SUP_TIMEOUT MSEC_TO_UNITS(300, UNIT_10_MS) /**< Connection supervisory timeout (4 seconds). */
170171
#define FIRST_CONN_PARAMS_UPDATE_DELAY \
@@ -271,6 +272,7 @@
271272
#define ST_SEND_CLOSE_BLE 0x02
272273
#define ST_SEND_DISCON_BLE 0x03
273274
#define ST_GET_BLE_SWITCH_STATUS 0x04
275+
#define ST_GET_BLE_CONN_STATUS 0x05
274276
//
275277
#define ST_CMD_POWER 0x82
276278
#define ST_SEND_CLOSE_SYS_PWR 0x01
@@ -367,7 +369,9 @@
367369

368370
BLE_NUS_DEF(m_nus, NRF_SDH_BLE_TOTAL_LINK_COUNT); /**< BLE NUS service instance. */
369371
BLE_BAS_DEF(m_bas);
372+
#if BLE_FIDO_ENABLED
370373
BLE_FIDO_DEF(m_fido, NRF_SDH_BLE_TOTAL_LINK_COUNT);
374+
#endif
371375
NRF_BLE_GATT_DEF(m_gatt); /**< GATT module instance. */
372376
NRF_BLE_QWR_DEF(m_qwr); /**< Context for the Queued Write module.*/
373377
BLE_ADVERTISING_DEF(m_advertising); /**< Advertising module instance. */
@@ -408,7 +412,9 @@ static ble_uuid_t m_adv_uuids[] = /**< Universally unique
408412
{BLE_UUID_DEVICE_INFORMATION_SERVICE, BLE_UUID_TYPE_BLE},
409413
#endif
410414
{BLE_UUID_BATTERY_SERVICE, BLE_UUID_TYPE_BLE},
415+
#if BLE_FIDO_ENABLED
411416
{BLE_UUID_FIDO_SERVICE, BLE_UUID_TYPE_BLE},
417+
#endif
412418
{BLE_UUID_NUS_SERVICE, BLE_UUID_TYPE_BLE}};
413419

414420
static volatile uint8_t flag_uart_trans = 1;
@@ -668,7 +674,7 @@ static void pm_evt_handler(const pm_evt_t* p_evt)
668674

669675
case PM_EVT_BONDED_PEER_CONNECTED:
670676
NRF_LOG_INFO("%s ---> PM_EVT_BONDED_PEER_CONNECTED", __func__);
671-
request_service_changed = true;
677+
// request_service_changed = true;
672678
break;
673679

674680
case PM_EVT_CONN_SEC_CONFIG_REQ:
@@ -943,7 +949,7 @@ static void nus_data_handler(ble_nus_evt_t* p_evt)
943949
if ( nus_data_buf[0] == '?' )
944950
{
945951
pad = (nus_data_len + 63) / 64;
946-
if ( nus_data_len - pad > msg_len )
952+
if ( nus_data_len - pad >= msg_len )
947953
{
948954
rcv_head_flag = DATA_INIT;
949955
nus_data_len = msg_len + (msg_len + 62) / 63;
@@ -960,8 +966,8 @@ static void nus_data_handler(ble_nus_evt_t* p_evt)
960966
rcv_head_flag = DATA_INIT;
961967
}
962968
}
963-
// spi_write_st_data(nus_data_buf, nus_data_len);
964-
app_sched_event_put(nus_data_buf, nus_data_len, spi_write_st_data);
969+
spi_write_st_data(nus_data_buf, nus_data_len);
970+
// app_sched_event_put(nus_data_buf, nus_data_len, spi_write_st_data);
965971
}
966972
else if ( p_evt->type == BLE_NUS_EVT_TX_RDY )
967973
{
@@ -982,7 +988,9 @@ static void nus_data_handler(ble_nus_evt_t* p_evt)
982988
}
983989
}
984990

991+
#if BLE_FIDO_ENABLED
985992
#include "fido.h"
993+
#endif
986994

987995
/**@brief Function for initializing services that will be used by the application.
988996
*
@@ -994,7 +1002,6 @@ static void services_init(void)
9941002
nrf_ble_qwr_init_t qwr_init = {0};
9951003
ble_dis_init_t dis_init;
9961004
ble_nus_init_t nus_init;
997-
ble_fido_init_t fido_init = {0};
9981005

9991006
// Initialize Queued Write Module.
10001007
qwr_init.error_handler = nrf_qwr_error_handler;
@@ -1010,7 +1017,9 @@ static void services_init(void)
10101017
memset(&dis_init, 0, sizeof(dis_init));
10111018

10121019
ble_srv_ascii_to_utf8(&dis_init.manufact_name_str, MANUFACTURER_NAME);
1020+
#if BLE_FIDO_ENABLED
10131021
ble_srv_ascii_to_utf8(&dis_init.model_num_str, ble_adv_name);
1022+
#endif
10141023
ble_srv_ascii_to_utf8(&dis_init.serial_num_str, MODEL_NUMBER);
10151024
ble_srv_ascii_to_utf8(&dis_init.hw_rev_str, HW_REVISION);
10161025
ble_srv_ascii_to_utf8(&dis_init.fw_rev_str, FW_REVISION);
@@ -1033,11 +1042,14 @@ static void services_init(void)
10331042
err_code = ble_nus_init(&m_nus, &nus_init);
10341043
APP_ERROR_CHECK(err_code);
10351044

1045+
#if BLE_FIDO_ENABLED
1046+
ble_fido_init_t fido_init = {0};
1047+
10361048
// Initialize FIDO.
1037-
memset(&fido_init, 0, sizeof(fido_init));
10381049
fido_init.data_handler = fido_data_handler;
10391050
err_code = ble_fido_init(&m_fido, &fido_init);
1040-
APP_ERROR_CHECK(err_code);
1051+
APP_ERROR_CHECK(err_code);
1052+
#endif
10411053
}
10421054

10431055
/**@brief Function for the Timer initialization.
@@ -1173,24 +1185,27 @@ void send_service_changed(void* p_event_data, uint16_t event_size)
11731185

11741186
err_code = sd_ble_gatts_initial_user_handle_get(&start_handle);
11751187

1176-
if(err_code != NRF_SUCCESS)
1188+
if ( err_code != NRF_SUCCESS )
11771189
{
1178-
NRF_LOG_ERROR("sd_ble_gatts_initial_user_handle_get() returned %s which should not happen.",
1179-
nrf_strerror_get(err_code));
1190+
NRF_LOG_ERROR(
1191+
"sd_ble_gatts_initial_user_handle_get() returned %s which should not happen.", nrf_strerror_get(err_code)
1192+
);
11801193
return;
11811194
}
11821195

11831196
NRF_LOG_INFO("m_conn_handle: 0x%04x, start_handle: 0x%04x", m_conn_handle, start_handle);
11841197
err_code = sd_ble_gatts_service_changed(m_conn_handle, start_handle, 0xFFFF);
1185-
if((err_code == BLE_ERROR_INVALID_CONN_HANDLE) || (err_code == NRF_ERROR_INVALID_STATE) || (err_code == NRF_ERROR_BUSY))
1198+
if ( (err_code == BLE_ERROR_INVALID_CONN_HANDLE) || (err_code == NRF_ERROR_INVALID_STATE) ||
1199+
(err_code == NRF_ERROR_BUSY) )
11861200
{
11871201
/* These errors can be expected when trying to send a Service Changed indication */
11881202
/* if the CCCD is not set to indicate. Thus, set the returning error code to success. */
1189-
NRF_LOG_WARNING("Client did not have the Service Changed indication set to enabled."
1190-
"Error: 0x%08x",
1191-
err_code);
1203+
NRF_LOG_WARNING(
1204+
"Client did not have the Service Changed indication set to enabled."
1205+
"Error: 0x%08x",
1206+
err_code
1207+
);
11921208
err_code = NRF_SUCCESS;
1193-
11941209
}
11951210
APP_ERROR_CHECK(err_code);
11961211
}
@@ -1374,7 +1389,7 @@ static void ble_evt_handler(const ble_evt_t* p_ble_evt, void* p_context)
13741389

13751390
case BLE_GATTS_EVT_SYS_ATTR_MISSING:
13761391
case BLE_GAP_EVT_CONN_SEC_UPDATE:
1377-
NRF_LOG_INFO("BLE_GAP_EVT_CONN_SEC_UPDATE");
1392+
NRF_LOG_INFO("BLE_GAP_EVT_CONN_SEC_UPDATE");
13781393
break;
13791394

13801395
default:
@@ -1579,6 +1594,9 @@ void uart_event_handle(app_uart_evt_t* p_event)
15791594
case ST_GET_BLE_SWITCH_STATUS:
15801595
ble_conn_flag = BLE_CON;
15811596
break;
1597+
case ST_GET_BLE_CONN_STATUS:
1598+
ble_conn_flag = BLE_CONN_STATUS;
1599+
break;
15821600
default:
15831601
break;
15841602
}
@@ -1858,8 +1876,8 @@ void in_gpiote_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
18581876
}
18591877
else if ( nrf_gpio_pin_read(SLAVE_SPI_RSP_IO) == 0 && !spi_dir_out )
18601878
{
1861-
// spi_read_st_data(NULL, 0);
1862-
app_sched_event_put(NULL, 0, spi_read_st_data);
1879+
spi_read_st_data(NULL, 0);
1880+
// app_sched_event_put(NULL, 0, spi_read_st_data);
18631881
}
18641882
break;
18651883
default:
@@ -2142,13 +2160,20 @@ static void ble_ctl_process(void* p_event_data, uint16_t event_size)
21422160

21432161
bt_disconnect();
21442162
}
2145-
if ( BLE_CON == ble_conn_flag )
2163+
else if ( BLE_CON == ble_conn_flag )
21462164
{
21472165
ble_conn_flag = BLE_DEF;
21482166
bak_buff[0] = BLE_CMD_CON_STA;
21492167
bak_buff[1] = ble_status_flag + 2;
21502168
send_stm_data(bak_buff, 2);
21512169
}
2170+
else if ( BLE_CONN_STATUS == ble_conn_flag )
2171+
{
2172+
ble_conn_flag = BLE_DEF;
2173+
bak_buff[0] = BLE_CMD_CON_STA;
2174+
bak_buff[1] = m_conn_handle == BLE_CONN_HANDLE_INVALID ? BLE_DISCON_STATUS : BLE_CON_STATUS;
2175+
send_stm_data(bak_buff, 2);
2176+
}
21522177

21532178
switch ( pwr_status_flag )
21542179
{

app/sdk_config.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@
544544
// <e> BLE_FIDO_ENABLED - ble_fido - FIDO Service
545545
//==========================================================
546546
#ifndef BLE_FIDO_ENABLED
547-
#define BLE_FIDO_ENABLED 1
547+
#define BLE_FIDO_ENABLED 0
548548
#endif
549549
// <e> BLE_FIDO_CONFIG_LOG_ENABLED - Enables logging in the module.
550550
//==========================================================
@@ -9868,7 +9868,7 @@
98689868
// <i> The time set aside for this connection on every connection interval in 1.25 ms units.
98699869

98709870
#ifndef NRF_SDH_BLE_GAP_EVENT_LENGTH
9871-
#define NRF_SDH_BLE_GAP_EVENT_LENGTH 6
9871+
#define NRF_SDH_BLE_GAP_EVENT_LENGTH 12
98729872
#endif
98739873

98749874
// <o> NRF_SDH_BLE_GATT_MAX_MTU_SIZE - Static maximum MTU size.
@@ -9890,7 +9890,7 @@
98909890

98919891

98929892
#ifndef NRF_SDH_BLE_SERVICE_CHANGED
9893-
#define NRF_SDH_BLE_SERVICE_CHANGED 1
9893+
#define NRF_SDH_BLE_SERVICE_CHANGED 0
98949894
#endif
98959895

98969896
// </h>

0 commit comments

Comments
 (0)