Skip to content

Commit 0604236

Browse files
committed
nimble/ll: Add packets durations to CS scheduling
1 parent e9151d4 commit 0604236

File tree

3 files changed

+198
-2
lines changed

3 files changed

+198
-2
lines changed

nimble/controller/src/ble_ll_cs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "ble_ll_conn_priv.h"
3131
#include "ble_ll_cs_priv.h"
3232

33-
static struct ble_ll_cs_supp_cap g_ble_ll_cs_local_cap;
33+
struct ble_ll_cs_supp_cap g_ble_ll_cs_local_cap;
3434
static struct ble_ll_cs_sm g_ble_ll_cs_sm[MYNEWT_VAL(BLE_MAX_CONNECTIONS)];
3535
static const uint8_t t_ip1[] = {10, 20, 30, 40, 50, 60, 80, 145};
3636
static const uint8_t t_ip2[] = {10, 20, 30, 40, 50, 60, 80, 145};

nimble/controller/src/ble_ll_cs_priv.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ extern "C" {
5252
#define BLE_LL_CS_STEPS_PER_SUBEVENT_MAX (160)
5353
#define BLE_LL_CS_STEPS_PER_PROCEDURE_MAX (256)
5454

55+
#define BLE_LL_CS_SYNC_PHY_1M (0x01)
56+
#define BLE_LL_CS_SYNC_PHY_2M (0x02)
57+
/* The duration of the CS_SYNC (T_SY) without sequence in usec */
58+
#define BLE_LL_CS_SYNC_TIME_1M (44)
59+
#define BLE_LL_CS_SYNC_TIME_2M (26)
60+
5561
struct ble_ll_cs_aci {
5662
uint8_t n_ap;
5763
uint8_t n_a_antennas;
@@ -232,6 +238,16 @@ struct ble_ll_cs_sm {
232238
uint32_t step_anchor_usecs;
233239
/* Estimated time of the step (ToF not included) */
234240
uint32_t step_duration_usecs;
241+
uint32_t mode0_step_duration_usecs;
242+
uint32_t mode1_step_duration_usecs;
243+
uint32_t mode2_step_duration_usecs;
244+
uint32_t mode3_step_duration_usecs;
245+
/* Time of antenna swith */
246+
uint8_t t_sw;
247+
/* Time of CS_SYNC packet without sequence */
248+
uint8_t t_sy;
249+
/* Time of CS_SYNC sequence only */
250+
uint8_t t_sy_seq;
235251

236252
/* CS Access Addresses to use in current step */
237253
uint32_t initiator_aa;

nimble/controller/src/ble_ll_cs_proc.c

Lines changed: 181 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@
2020
#include <syscfg/syscfg.h>
2121
#if MYNEWT_VAL(BLE_LL_CHANNEL_SOUNDING)
2222
#include <stdint.h>
23+
#include "controller/ble_ll_utils.h"
2324
#include "controller/ble_ll_conn.h"
2425
#include "controller/ble_ll_sched.h"
2526
#include "controller/ble_ll_tmr.h"
2627
#include "ble_ll_cs_priv.h"
2728

29+
extern struct ble_ll_cs_supp_cap g_ble_ll_cs_local_cap;
2830
extern uint8_t g_ble_ll_cs_chan_count;
2931
extern uint8_t g_ble_ll_cs_chan_indices[72];
3032
struct ble_ll_cs_sm *g_ble_ll_cs_sm_current;
@@ -54,10 +56,18 @@ struct ble_ll_cs_sm *g_ble_ll_cs_sm_current;
5456

5557
#define BLE_LL_CONN_ITVL_USECS (1250)
5658

59+
/* The ramp-down window in µs */
60+
#define T_RD (5)
61+
/* The guard time duration in µs */
62+
#define T_GD (10)
63+
/* The requency measurement period in µs */
64+
#define T_FM (80)
65+
5766
static struct ble_ll_cs_aci aci_table[] = {
5867
{1, 1, 1}, {2, 2, 1}, {3, 3, 1}, {4, 4, 1},
5968
{2, 1, 2}, {3, 1, 3}, {4, 1, 4}, {4, 2, 2}
6069
};
70+
static const uint8_t rtt_seq_len[] = {0, 4, 12, 4, 8, 12, 16};
6171

6272
static int
6373
ble_ll_cs_generate_channel(struct ble_ll_cs_sm *cssm)
@@ -113,10 +123,110 @@ ble_ll_cs_proc_rm_from_sched(void *cb_args)
113123
{
114124
}
115125

126+
static uint8_t
127+
ble_ll_cs_proc_set_t_sw(struct ble_ll_cs_sm *cssm)
128+
{
129+
uint8_t t_sw;
130+
uint8_t t_sw_i;
131+
uint8_t t_sw_r;
132+
uint8_t aci = cssm->active_config->proc_params.aci;
133+
134+
if (cssm->active_config->role == BLE_LL_CS_ROLE_INITIATOR) {
135+
t_sw_i = g_ble_ll_cs_local_cap.t_sw;
136+
t_sw_r = cssm->remote_cap.t_sw;
137+
} else { /* BLE_LL_CS_ROLE_REFLECTOR */
138+
t_sw_i = cssm->remote_cap.t_sw;
139+
t_sw_r = g_ble_ll_cs_local_cap.t_sw;
140+
}
141+
142+
if (aci == 0) {
143+
t_sw = 0;
144+
} else if (IN_RANGE(aci, 1, 3)) {
145+
t_sw = t_sw_i;
146+
} else if (IN_RANGE(aci, 4, 6)) {
147+
t_sw = t_sw_r;
148+
} else { /* ACI == 7 */
149+
if (g_ble_ll_cs_local_cap.t_sw > cssm->remote_cap.t_sw) {
150+
t_sw = g_ble_ll_cs_local_cap.t_sw;
151+
} else {
152+
t_sw = cssm->remote_cap.t_sw;
153+
}
154+
}
155+
156+
cssm->t_sw = t_sw;
157+
158+
return t_sw;
159+
}
160+
161+
static int
162+
ble_ll_cs_proc_calculate_timing(struct ble_ll_cs_sm *cssm)
163+
{
164+
struct ble_ll_cs_config *conf = cssm->active_config;
165+
uint8_t t_ip1 = conf->t_ip1;
166+
uint8_t t_ip2 = conf->t_ip2;
167+
uint8_t t_pm = conf->t_pm;
168+
uint8_t t_sw;
169+
uint8_t n_ap = cssm->n_ap;
170+
uint8_t t_sy;
171+
uint8_t t_sy_seq;
172+
uint8_t sequence_len;
173+
174+
t_sw = ble_ll_cs_proc_set_t_sw(cssm);
175+
176+
/* CS packets with no Sounding Sequence or Random Sequence fields take 44 µs
177+
* to transmit when sent using the LE 1M PHY and 26 µs to transmit when using
178+
* the LE 2M and the LE 2M 2BT PHYs. CS packets that include a Sounding Sequence
179+
* or Random Sequence field take proportionally longer to transmit based on
180+
* the length of the field and the PHY selection.
181+
*/
182+
183+
sequence_len = rtt_seq_len[conf->rtt_type];
184+
185+
switch (conf->cs_sync_phy) {
186+
case BLE_LL_CS_SYNC_PHY_1M:
187+
t_sy = BLE_LL_CS_SYNC_TIME_1M;
188+
t_sy_seq = sequence_len;
189+
break;
190+
case BLE_LL_CS_SYNC_PHY_2M:
191+
t_sy = BLE_LL_CS_SYNC_TIME_2M;
192+
t_sy_seq = sequence_len / 2;
193+
break;
194+
}
195+
196+
cssm->mode0_step_duration_usecs = t_ip1 + T_GD + T_FM + 2 * (t_sy + T_RD);
197+
198+
cssm->mode1_step_duration_usecs = t_ip1 + 2 * (t_sy + t_sy_seq + T_RD);
199+
200+
cssm->mode2_step_duration_usecs = t_ip2 + 2 * ((t_sw + t_pm) * (n_ap + 1) + T_RD);
201+
202+
cssm->mode3_step_duration_usecs = t_ip2 + 2 * ((t_sy + t_sy_seq + T_GD + T_RD) +
203+
(t_sw + t_pm) * (n_ap + 1));
204+
205+
cssm->t_sy = t_sy;
206+
cssm->t_sy_seq = t_sy_seq;
207+
208+
return 0;
209+
}
210+
116211
static void
117212
ble_ll_cs_set_step_duration(struct ble_ll_cs_sm *cssm)
118213
{
119-
/* TODO: cssm->step_duration_usecs = ? */
214+
switch (cssm->step_mode) {
215+
case BLE_LL_CS_MODE0:
216+
cssm->step_duration_usecs = cssm->mode0_step_duration_usecs;
217+
break;
218+
case BLE_LL_CS_MODE1:
219+
cssm->step_duration_usecs = cssm->mode1_step_duration_usecs;
220+
break;
221+
case BLE_LL_CS_MODE2:
222+
cssm->step_duration_usecs = cssm->mode2_step_duration_usecs;
223+
break;
224+
case BLE_LL_CS_MODE3:
225+
cssm->step_duration_usecs = cssm->mode3_step_duration_usecs;
226+
break;
227+
default:
228+
assert(0);
229+
}
120230
}
121231

122232
static int
@@ -324,6 +434,7 @@ ble_ll_cs_setup_next_step(struct ble_ll_cs_sm *cssm)
324434
int rc;
325435
const struct ble_ll_cs_config *conf = cssm->active_config;
326436

437+
cssm->step_anchor_usecs = cssm->anchor_usecs;
327438
++cssm->steps_in_procedure_count;
328439
++cssm->steps_in_subevent_count;
329440

@@ -367,123 +478,190 @@ ble_ll_cs_setup_next_step(struct ble_ll_cs_sm *cssm)
367478
}
368479

369480
cssm->antenna_path_count = cssm->n_ap;
481+
cssm->anchor_usecs = cssm->step_anchor_usecs;
482+
370483
return 0;
371484
}
372485

373486
static void
374487
ble_ll_cs_proc_mode0_next_state(struct ble_ll_cs_sm *cssm)
375488
{
489+
uint32_t duration = 0;
490+
uint32_t delay = 0;
376491
uint8_t state = cssm->step_state;
492+
uint8_t t_ip1 = cssm->active_config->t_ip1;
493+
uint8_t t_fcs = cssm->active_config->t_fcs;
494+
uint8_t t_sy = cssm->t_sy;
377495

378496
switch (state) {
379497
case STEP_STATE_INIT:
380498
state = STEP_STATE_CS_SYNC_I;
499+
duration = t_sy;
500+
delay = 0;
381501
break;
382502
case STEP_STATE_CS_SYNC_I:
383503
state = STEP_STATE_CS_SYNC_R;
504+
duration = t_sy;
505+
delay = T_RD + t_ip1;
384506
break;
385507
case STEP_STATE_CS_SYNC_R:
386508
state = STEP_STATE_CS_TONE_R;
509+
duration = T_FM;
510+
delay = T_GD;
387511
break;
388512
case STEP_STATE_CS_TONE_R:
389513
state = STEP_STATE_COMPLETE;
514+
duration = 0;
515+
delay = T_RD + t_fcs;
390516
break;
391517
default:
392518
assert(0);
393519
}
394520

521+
cssm->duration_usecs = duration;
522+
cssm->anchor_usecs += delay;
395523
cssm->step_state = state;
396524
}
397525

398526
static void
399527
ble_ll_cs_proc_mode1_next_state(struct ble_ll_cs_sm *cssm)
400528
{
529+
uint32_t duration = 0;
530+
uint32_t delay = 0;
401531
uint8_t state = cssm->step_state;
532+
uint8_t t_ip1 = cssm->active_config->t_ip1;
533+
uint8_t t_fcs = cssm->active_config->t_fcs;
534+
uint8_t t_sy = cssm->t_sy + cssm->t_sy_seq;
402535

403536
switch (state) {
404537
case STEP_STATE_INIT:
405538
state = STEP_STATE_CS_SYNC_I;
539+
duration = t_sy;
540+
delay = 0;
406541
break;
407542
case STEP_STATE_CS_SYNC_I:
408543
state = STEP_STATE_CS_SYNC_R;
544+
duration = t_sy;
545+
delay = T_RD + t_ip1;
409546
break;
410547
case STEP_STATE_CS_SYNC_R:
411548
state = STEP_STATE_COMPLETE;
549+
duration = 0;
550+
delay = T_RD + t_fcs;
412551
break;
413552
default:
414553
assert(0);
415554
}
416555

556+
cssm->duration_usecs = duration;
557+
cssm->anchor_usecs += delay;
417558
cssm->step_state = state;
418559
}
419560

420561
static void
421562
ble_ll_cs_proc_mode2_next_state(struct ble_ll_cs_sm *cssm)
422563
{
564+
uint32_t duration = 0;
565+
uint32_t delay = 0;
423566
uint8_t state = cssm->step_state;
567+
uint8_t t_ip2 = cssm->active_config->t_ip2;
568+
uint8_t t_fcs = cssm->active_config->t_fcs;
569+
uint8_t t_pm = cssm->active_config->t_pm;
424570

425571
switch (state) {
426572
case STEP_STATE_INIT:
427573
state = STEP_STATE_CS_TONE_I;
574+
duration = t_pm;
575+
delay = 0;
428576
break;
429577
case STEP_STATE_CS_TONE_I:
578+
duration = t_pm;
430579
if (cssm->antenna_path_count != 0) {
431580
--cssm->antenna_path_count;
581+
delay = cssm->t_sw;
432582
} else {
433583
state = STEP_STATE_CS_TONE_R;
434584
cssm->antenna_path_count = cssm->n_ap;
585+
delay = T_RD + t_ip2;
435586
}
436587
break;
437588
case STEP_STATE_CS_TONE_R:
438589
if (cssm->antenna_path_count != 0) {
439590
--cssm->antenna_path_count;
591+
duration = t_pm;
592+
delay = cssm->t_sw;
440593
} else {
441594
state = STEP_STATE_COMPLETE;
442595
cssm->antenna_path_count = cssm->n_ap;
596+
duration = 0;
597+
delay = T_RD + t_fcs;
443598
}
444599
break;
445600
default:
446601
assert(0);
447602
}
448603

604+
cssm->duration_usecs = duration;
605+
cssm->anchor_usecs += delay;
449606
cssm->step_state = state;
450607
}
451608

452609
static void
453610
ble_ll_cs_proc_mode3_next_state(struct ble_ll_cs_sm *cssm)
454611
{
612+
uint32_t duration = 0;
613+
uint32_t delay = 0;
455614
uint8_t state = cssm->step_state;
615+
uint8_t t_ip2 = cssm->active_config->t_ip2;
616+
uint8_t t_fcs = cssm->active_config->t_fcs;
617+
uint8_t t_sy = cssm->t_sy + cssm->t_sy_seq;
618+
uint8_t t_pm = cssm->active_config->t_pm;
456619

457620
switch (state) {
458621
case STEP_STATE_INIT:
459622
state = STEP_STATE_CS_SYNC_I;
623+
duration = t_sy;
624+
delay = 0;
460625
break;
461626
case STEP_STATE_CS_SYNC_I:
462627
state = STEP_STATE_CS_TONE_I;
628+
duration = t_pm;
629+
delay = T_GD;
463630
break;
464631
case STEP_STATE_CS_TONE_I:
632+
duration = t_pm;
465633
if (cssm->antenna_path_count != 0) {
466634
--cssm->antenna_path_count;
635+
delay = cssm->t_sw;
467636
} else {
468637
state = STEP_STATE_CS_TONE_R;
469638
cssm->antenna_path_count = cssm->n_ap;
639+
delay = T_RD + t_ip2;
470640
}
471641
break;
472642
case STEP_STATE_CS_TONE_R:
473643
if (cssm->antenna_path_count != 0) {
474644
--cssm->antenna_path_count;
645+
duration = t_pm;
646+
delay = cssm->t_sw;
475647
} else {
476648
state = STEP_STATE_CS_SYNC_R;
477649
cssm->antenna_path_count = cssm->n_ap;
650+
duration = t_sy;
651+
delay = T_RD;
478652
}
479653
break;
480654
case STEP_STATE_CS_SYNC_R:
481655
state = STEP_STATE_COMPLETE;
656+
duration = 0;
657+
delay = T_RD + t_fcs;
482658
break;
483659
default:
484660
assert(0);
485661
}
486662

663+
cssm->duration_usecs = duration;
664+
cssm->anchor_usecs += delay;
487665
cssm->step_state = state;
488666
}
489667

@@ -647,6 +825,8 @@ ble_ll_cs_proc_scheduling_start(struct ble_ll_conn_sm *connsm,
647825
cssm->mode0_next_chan_id = 0xFF;
648826
cssm->non_mode0_next_chan_id = 0xFF;
649827

828+
ble_ll_cs_proc_calculate_timing(cssm);
829+
650830
rc = ble_ll_cs_proc_schedule_next_tx_or_rx(cssm);
651831
if (rc) {
652832
return BLE_ERR_UNSPECIFIED;

0 commit comments

Comments
 (0)