1 /*
2  * Copyright (c) 2018-2019 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define EVENT_OVERHEAD_XTAL_US        1500
8 #define EVENT_OVERHEAD_PREEMPT_US     0    /* if <= min, then dynamic preempt */
9 #define EVENT_OVERHEAD_PREEMPT_MIN_US 0
10 #define EVENT_OVERHEAD_PREEMPT_MAX_US EVENT_OVERHEAD_XTAL_US
11 
12 /* Measurement based on drifting roles that can overlap leading to collision
13  * resolutions that consume CPU time between radio events.
14  * Value include max end, start and scheduling CPU usage times.
15  * Measurements based on central_gatt_write and peripheral_gatt_write sample on
16  * nRF52833 SoC.
17  */
18 #if defined(CONFIG_BT_CTLR_ADV_EXT)
19 #if defined(CONFIG_BT_OBSERVER)
20 #if defined(CONFIG_BT_CTLR_PHY_CODED)
21 /* Active connection in peripheral role with extended scanning on 1M and Coded
22  * PHY, scheduling and receiving auxiliary PDUs.
23  */
24 #define EVENT_OVERHEAD_START_US       733 /* 24 RTC ticks */
25 #else /* !CONFIG_BT_CTLR_PHY_CODED */
26 /* Active connection in peripheral role with extended scanning on 1M only,
27  * scheduling and receiving auxiliary PDUs.
28  */
29 #define EVENT_OVERHEAD_START_US       428 /* 14 RTC ticks */
30 #endif /* !CONFIG_BT_CTLR_PHY_CODED */
31 #else /* !CONFIG_BT_OBSERVER */
32 /* Active connection in peripheral role with legacy scanning on 1M.
33  */
34 #define EVENT_OVERHEAD_START_US       275 /* 9 RTC ticks */
35 #endif /* !CONFIG_BT_OBSERVER */
36 #else /* !CONFIG_BT_CTLR_ADV_EXT */
37 /* Active connection in peripheral role with additional advertising state.
38  */
39 #define EVENT_OVERHEAD_START_US       275 /* 9 RTC ticks */
40 #endif /* !CONFIG_BT_CTLR_ADV_EXT */
41 
42 /* Worst-case time margin needed after event end-time in the air
43  * (done/preempt race margin + power-down/chain delay)
44  */
45 #define EVENT_OVERHEAD_END_US         40
46 
47 /* Sleep Clock Accuracy */
48 #define EVENT_JITTER_US               16
49 
50 /* Inter-Event Space (IES) */
51 #define EVENT_TIES_US                 625
52 
53 /* Ticker resolution margin
54  * Needed due to the lack of fine timing resolution in ticker_start
55  * and ticker_update. Set to 32 us, which is ~1 tick with 32768 Hz
56  * clock.
57  */
58 #define EVENT_TICKER_RES_MARGIN_US    32
59 
60 #define EVENT_RX_JITTER_US(phy) 16    /* Radio Rx timing uncertainty */
61 #define EVENT_RX_TO_US(phy) ((((((phy)&0x03) + 4)<<3)/BIT((((phy)&0x3)>>1))) + \
62 				  EVENT_RX_JITTER_US(phy))
63 
64 /* Turnaround time between RX and TX is based on CPU execution speed. It also
65  * includes radio ramp up time. The value must meet hard deadline of `150 us`
66  * imposed by BT Core spec for inter frame spacing (IFS). To include CPUs with
67  * slow clock, the conservative approach was taken to use IFS value for all
68  * cases.
69  */
70 #define EVENT_RX_TX_TURNAROUND(phy)   150
71 
72 /* Sub-microsecond conversion macros. With current timer resolution of ~30 us
73  * per tick, conversion factor is 1, and macros map 1:1 between us_frac and us.
74  * On sub-microsecond tick resolution architectures, a number of bits may be
75  * used to represent fractions of a microsecond, to allow higher precision in
76  * window widening.
77  */
78 #define EVENT_US_TO_US_FRAC(us)             (us)
79 #define EVENT_US_FRAC_TO_US(us_frac)        (us_frac)
80 #define EVENT_TICKS_TO_US_FRAC(ticks)       HAL_TICKER_TICKS_TO_US(ticks)
81 #define EVENT_US_FRAC_TO_TICKS(us_frac)     HAL_TICKER_US_TO_TICKS(us_frac)
82 #define EVENT_US_FRAC_TO_REMAINDER(us_frac) HAL_TICKER_REMAINDER(us_frac)
83 
84 /* Time needed to set up a CIS from ACL instant to prepare (incl. radio). Used
85  * for CIS_Offset_Min.
86  */
87 #define EVENT_OVERHEAD_CIS_SETUP_US         MAX(EVENT_OVERHEAD_START_US, 500U)
88