1616#include "hal/ccm.h"
1717#include "hal/radio.h"
1818#include "hal/ticker.h"
19+ #include "hal/cntr.h"
1920
2021#include "util/util.h"
2122#include "util/mem.h"
@@ -1732,20 +1733,109 @@ uint8_t ull_scan_rsp_set(struct ll_adv_set *adv, uint8_t len,
17321733 return 0 ;
17331734}
17341735
1735- #if defined(CONFIG_BT_CTLR_ADV_EXT )
1736+ static uint32_t ticker_update_rand (struct ll_adv_set * adv , uint32_t ticks_delay_window ,
1737+ uint32_t ticks_delay_window_offset ,
1738+ uint32_t ticks_adjust_minus )
1739+ {
1740+ uint32_t random_delay ;
1741+ uint32_t ret ;
1742+
1743+ /* Get pseudo-random number in the range [0..ticks_delay_window].
1744+ * Please note that using modulo of 2^32 samle space has an uneven
1745+ * distribution, slightly favoring smaller values.
1746+ */
1747+ lll_rand_isr_get (& random_delay , sizeof (random_delay ));
1748+ random_delay %= ticks_delay_window ;
1749+ random_delay += (ticks_delay_window_offset + 1 );
1750+
1751+ ret = ticker_update (TICKER_INSTANCE_ID_CTLR ,
1752+ TICKER_USER_ID_ULL_HIGH ,
1753+ TICKER_ID_ADV_BASE + ull_adv_handle_get (adv ),
1754+ random_delay ,
1755+ ticks_adjust_minus , 0 , 0 , 0 , 0 ,
1756+ ticker_op_update_cb , adv );
1757+
1758+ LL_ASSERT ((ret == TICKER_STATUS_SUCCESS ) ||
1759+ (ret == TICKER_STATUS_BUSY ));
1760+
1761+ #if defined(CONFIG_BT_CTLR_JIT_SCHEDULING )
1762+ adv -> delay = random_delay ;
1763+ #endif
1764+ return random_delay ;
1765+ }
1766+
1767+ #if defined(CONFIG_BT_CTLR_ADV_EXT ) || \
1768+ defined(CONFIG_BT_CTLR_JIT_SCHEDULING )
17361769void ull_adv_done (struct node_rx_event_done * done )
17371770{
1771+ #if defined(CONFIG_BT_CTLR_ADV_EXT )
17381772 struct lll_adv_aux * lll_aux ;
17391773 struct node_rx_hdr * rx_hdr ;
1740- struct ll_adv_set * adv ;
1741- struct lll_adv * lll ;
17421774 uint8_t handle ;
17431775 uint32_t ret ;
1776+ #endif /* CONFIG_BT_CTLR_ADV_EXT */
1777+ struct ll_adv_set * adv ;
1778+ struct lll_adv * lll ;
17441779
17451780 /* Get reference to ULL context */
17461781 adv = CONTAINER_OF (done -> param , struct ll_adv_set , ull );
17471782 lll = & adv -> lll ;
17481783
1784+ #if defined(CONFIG_BT_CTLR_JIT_SCHEDULING )
1785+ if (done -> extra .result == DONE_COMPLETED ) {
1786+ /* Event completed successfully */
1787+ adv -> delay_remain = ULL_ADV_RANDOM_DELAY ;
1788+ } else {
1789+ /* Event aborted or too late - try to re-schedule */
1790+ uint32_t ticks_elapsed ;
1791+ uint32_t ticks_now ;
1792+
1793+ const uint32_t prepare_overhead =
1794+ HAL_TICKER_US_TO_TICKS (EVENT_OVERHEAD_START_US );
1795+ const uint32_t ticks_adv_airtime = adv -> ticks_at_expire +
1796+ prepare_overhead ;
1797+
1798+ ticks_elapsed = 0 ;
1799+
1800+ ticks_now = cntr_cnt_get ();
1801+ if ((int32_t )(ticks_now - ticks_adv_airtime ) > 0 ) {
1802+ ticks_elapsed = ticks_now - ticks_adv_airtime ;
1803+ }
1804+
1805+ if (adv -> delay_remain >= adv -> delay + ticks_elapsed ) {
1806+ /* The perturbation window is still open */
1807+ adv -> delay_remain -= (adv -> delay + ticks_elapsed );
1808+ } else {
1809+ adv -> delay_remain = 0 ;
1810+ }
1811+
1812+ /* Check if we have enough time to re-schedule */
1813+ if (adv -> delay_remain > prepare_overhead ) {
1814+ uint32_t ticks_adjust_minus ;
1815+
1816+ /* Get negative ticker adjustment needed to pull back ADV one
1817+ * interval plus the randomized delay. This means that the ticker
1818+ * will be updated to expire in time frame of now + start
1819+ * overhead, until 10 ms window is exhausted.
1820+ */
1821+ ticks_adjust_minus = HAL_TICKER_US_TO_TICKS (
1822+ (uint64_t )adv -> interval * ADV_INT_UNIT_US ) + adv -> delay ;
1823+
1824+ /* Apply random delay in range [prepare_overhead..delay_remain] */
1825+ ticker_update_rand (adv , adv -> delay_remain - prepare_overhead ,
1826+ prepare_overhead , ticks_adjust_minus );
1827+
1828+ /* Score of the event was increased due to the result, but since
1829+ * we're getting a another chance we'll set it back.
1830+ */
1831+ adv -> lll .hdr .score -= 1 ;
1832+ } else {
1833+ adv -> delay_remain = ULL_ADV_RANDOM_DELAY ;
1834+ }
1835+ }
1836+ #endif /* CONFIG_BT_CTLR_JIT_SCHEDULING */
1837+
1838+ #if defined(CONFIG_BT_CTLR_ADV_EXT )
17491839 if (adv -> max_events && (adv -> event_counter >= adv -> max_events )) {
17501840 adv -> max_events = 0 ;
17511841
@@ -1791,8 +1881,9 @@ void ull_adv_done(struct node_rx_event_done *done)
17911881
17921882 LL_ASSERT ((ret == TICKER_STATUS_SUCCESS ) ||
17931883 (ret == TICKER_STATUS_BUSY ));
1794- }
17951884#endif /* CONFIG_BT_CTLR_ADV_EXT */
1885+ }
1886+ #endif /* CONFIG_BT_CTLR_ADV_EXT || CONFIG_BT_CTLR_JIT_SCHEDULING */
17961887
17971888const uint8_t * ull_adv_pdu_update_addrs (struct ll_adv_set * adv ,
17981889 struct pdu_adv * pdu )
@@ -1971,6 +2062,7 @@ static void ticker_cb(uint32_t ticks_at_expire, uint32_t remainder, uint16_t laz
19712062 static struct mayfly mfy = {0 , 0 , & link , NULL , lll_adv_prepare };
19722063 static struct lll_prepare_param p ;
19732064 struct ll_adv_set * adv = param ;
2065+ uint32_t random_delay ;
19742066 struct lll_adv * lll ;
19752067 uint32_t ret ;
19762068 uint8_t ref ;
@@ -1997,29 +2089,19 @@ static void ticker_cb(uint32_t ticks_at_expire, uint32_t remainder, uint16_t laz
19972089 ret = mayfly_enqueue (TICKER_USER_ID_ULL_HIGH ,
19982090 TICKER_USER_ID_LLL , 0 , & mfy );
19992091 LL_ASSERT (!ret );
2092+
2093+ #if defined(CONFIG_BT_CTLR_JIT_SCHEDULING )
2094+ adv -> ticks_at_expire = ticks_at_expire ;
2095+ #endif /* CONFIG_BT_CTLR_JIT_SCHEDULING */
20002096 }
20012097
20022098 /* Apply adv random delay */
20032099#if defined(CONFIG_BT_PERIPHERAL )
20042100 if (!lll -> is_hdcd )
20052101#endif /* CONFIG_BT_PERIPHERAL */
20062102 {
2007- uint32_t random_delay ;
2008- uint32_t ret ;
2009-
2010- lll_rand_isr_get (& random_delay , sizeof (random_delay ));
2011- random_delay %= ULL_ADV_RANDOM_DELAY ;
2012- random_delay += 1 ;
2013-
2014- ret = ticker_update (TICKER_INSTANCE_ID_CTLR ,
2015- TICKER_USER_ID_ULL_HIGH ,
2016- (TICKER_ID_ADV_BASE +
2017- ull_adv_handle_get (adv )),
2018- random_delay ,
2019- 0 , 0 , 0 , 0 , 0 ,
2020- ticker_op_update_cb , adv );
2021- LL_ASSERT ((ret == TICKER_STATUS_SUCCESS ) ||
2022- (ret == TICKER_STATUS_BUSY ));
2103+ /* Apply random delay in range [0..ULL_ADV_RANDOM_DELAY] */
2104+ random_delay = ticker_update_rand (adv , ULL_ADV_RANDOM_DELAY , 0 , 0 );
20232105
20242106#if defined(CONFIG_BT_CTLR_ADV_EXT )
20252107 adv -> event_counter += (lazy + 1 );
@@ -2581,6 +2663,9 @@ static void init_set(struct ll_adv_set *adv)
25812663#endif /* CONFIG_BT_CTLR_PRIVACY */
25822664 adv -> lll .chan_map = BT_LE_ADV_CHAN_MAP_ALL ;
25832665 adv -> lll .filter_policy = BT_LE_ADV_FP_NO_WHITELIST ;
2666+ #if defined(CONFIG_BT_CTLR_JIT_SCHEDULING )
2667+ adv -> delay_remain = ULL_ADV_RANDOM_DELAY ;
2668+ #endif /* ONFIG_BT_CTLR_JIT_SCHEDULING */
25842669
25852670 init_pdu (lll_adv_data_peek (& ll_adv [0 ].lll ), PDU_ADV_TYPE_ADV_IND );
25862671 init_pdu (lll_adv_scan_rsp_peek (& ll_adv [0 ].lll ), PDU_ADV_TYPE_SCAN_RSP );
0 commit comments