1212
1313#include "hal/ccm.h"
1414#include "hal/radio.h"
15+ #include "hal/ticker.h"
1516
1617#include "util/memq.h"
1718
2324
2425#include "lll.h"
2526
27+ #include "hal/debug.h"
28+
29+ /* Below profiling measurements using a sample with:
30+ * 1 connectable legacy advertising or 1 peripheral ACL,
31+ * plus
32+ * 3 extended advertising sets, scanning on 2M PHY, scanning on Coded PHY, and
33+ * 2 auxiliary scan set.
34+ */
35+ #if defined(CONFIG_SOC_COMPATIBLE_NRF54LX )
36+ #define LLL_PROF_RADIO_MAX_US 103 /* Max. Radio Rx/Tx ISR, O(1)*/
37+ #define LLL_PROF_LLL_MAX_US 105 /* Max. LLL prepare, O(1) */
38+ #define LLL_PROF_ULL_HIGH_MAX_US 260 /* Max. Radio + LLL + ULL High, O(1) */
39+ #define LLL_PROF_ULL_LOW_MAX_US 306 /* Max. ULL Low, O(n) n is ticker nodes */
40+ #else /* !CONFIG_SOC_COMPATIBLE_NRF54LX */
41+ #define LLL_PROF_RADIO_MAX_US 184 /* Max. Radio Rx/Tx ISR, O(1)*/
42+ #define LLL_PROF_LLL_MAX_US 245 /* Max. LLL prepare, O(1) */
43+ #define LLL_PROF_ULL_HIGH_MAX_US 458 /* Max. Radio + LLL + ULL High, O(1) */
44+ #define LLL_PROF_ULL_LOW_MAX_US 733 /* Max. ULL Low, O(n) n is ticker nodes */
45+ #endif /* !CONFIG_SOC_COMPATIBLE_NRF54LX */
46+
47+ #define LLL_PROF_ASSERT (_val , _max ) \
48+ { \
49+ LL_ASSERT_MSG(((_val) <= (_max)), \
50+ "%s: %u (%u), %u (%u), %u (%u), %u (%u)\n", __func__, \
51+ HAL_TICKER_TICKS_TO_US(cputime_ticks_radio), \
52+ LLL_PROF_RADIO_MAX_US, \
53+ HAL_TICKER_TICKS_TO_US(cputime_ticks_lll), \
54+ LLL_PROF_LLL_MAX_US, \
55+ HAL_TICKER_TICKS_TO_US(cputime_ticks_ull_high), \
56+ LLL_PROF_ULL_HIGH_MAX_US, \
57+ HAL_TICKER_TICKS_TO_US(cputime_ticks_ull_low), \
58+ LLL_PROF_ULL_LOW_MAX_US); \
59+ }
60+
2661static int send (struct node_rx_pdu * rx );
2762static uint16_t latency_get (void );
2863static inline void sample (uint32_t * timestamp );
2964static inline void sample_ticks (uint32_t * timestamp_ticks );
3065static inline void delta (uint32_t timestamp , uint16_t * cputime );
31- static inline void delta_ticks (uint32_t timestamp_ticks , uint8_t * cputime_ticks );
66+ static inline void delta_ticks (uint32_t timestamp_ticks , uint16_t * cputime_ticks );
3267
3368static uint32_t timestamp_radio ;
3469static uint32_t timestamp_lll ;
@@ -50,10 +85,10 @@ static uint32_t timestamp_ticks_radio;
5085static uint32_t timestamp_ticks_lll ;
5186static uint32_t timestamp_ticks_ull_high ;
5287static uint32_t timestamp_ticks_ull_low ;
53- static uint8_t cputime_ticks_radio ;
54- static uint8_t cputime_ticks_lll ;
55- static uint8_t cputime_ticks_ull_high ;
56- static uint8_t cputime_ticks_ull_low ;
88+ static uint16_t cputime_ticks_radio ;
89+ static uint16_t cputime_ticks_lll ;
90+ static uint16_t cputime_ticks_ull_high ;
91+ static uint16_t cputime_ticks_ull_low ;
5792
5893void lll_prof_enter_radio (void )
5994{
@@ -65,6 +100,12 @@ void lll_prof_exit_radio(void)
65100{
66101 delta (timestamp_radio , & cputime_radio );
67102 delta_ticks (timestamp_ticks_radio , & cputime_ticks_radio );
103+ LLL_PROF_ASSERT (cputime_ticks_radio , HAL_TICKER_US_TO_TICKS (LLL_PROF_RADIO_MAX_US ));
104+ }
105+
106+ uint16_t lll_prof_radio_get (void )
107+ {
108+ return HAL_TICKER_TICKS_TO_US (cputime_ticks_radio );
68109}
69110
70111void lll_prof_enter_lll (void )
@@ -77,6 +118,12 @@ void lll_prof_exit_lll(void)
77118{
78119 delta (timestamp_lll , & cputime_lll );
79120 delta_ticks (timestamp_ticks_lll , & cputime_ticks_lll );
121+ LLL_PROF_ASSERT (cputime_ticks_lll , HAL_TICKER_US_TO_TICKS (LLL_PROF_LLL_MAX_US ));
122+ }
123+
124+ uint16_t lll_prof_lll_get (void )
125+ {
126+ return HAL_TICKER_TICKS_TO_US (cputime_ticks_lll );
80127}
81128
82129void lll_prof_enter_ull_high (void )
@@ -89,6 +136,12 @@ void lll_prof_exit_ull_high(void)
89136{
90137 delta (timestamp_ull_high , & cputime_ull_high );
91138 delta_ticks (timestamp_ticks_ull_high , & cputime_ticks_ull_high );
139+ LLL_PROF_ASSERT (cputime_ticks_ull_high , HAL_TICKER_US_TO_TICKS (LLL_PROF_ULL_HIGH_MAX_US ));
140+ }
141+
142+ uint16_t lll_prof_ull_high_get (void )
143+ {
144+ return HAL_TICKER_TICKS_TO_US (cputime_ticks_ull_high );
92145}
93146
94147void lll_prof_enter_ull_low (void )
@@ -101,6 +154,12 @@ void lll_prof_exit_ull_low(void)
101154{
102155 delta (timestamp_ull_low , & cputime_ull_low );
103156 delta_ticks (timestamp_ticks_ull_low , & cputime_ticks_ull_low );
157+ LLL_PROF_ASSERT (cputime_ticks_ull_low , HAL_TICKER_US_TO_TICKS (LLL_PROF_ULL_LOW_MAX_US ));
158+ }
159+
160+ uint16_t lll_prof_ull_low_get (void )
161+ {
162+ return HAL_TICKER_TICKS_TO_US (cputime_ticks_ull_low );
104163}
105164
106165void lll_prof_latency_capture (void )
@@ -120,7 +179,7 @@ uint16_t lll_prof_latency_get(void)
120179{
121180 uint16_t latency ;
122181
123- /* We are here before lll_prof_cputime_capture was called */
182+ /* We are here before latency timestamp was fetched */
124183 if (timestamp_latency == UINT16_MAX ) {
125184 /* get the ISR latency sample */
126185 timestamp_latency = radio_tmr_sample_get ();
@@ -150,15 +209,28 @@ uint32_t lll_prof_radio_end_backup(void)
150209
151210void lll_prof_cputime_capture (void )
152211{
153- /* get the ISR latency sample */
154- timestamp_latency = radio_tmr_sample_get ();
212+ /* We are here before latency timestamp was fetched */
213+ if (timestamp_latency == UINT16_MAX ) {
214+ /* get the ISR latency sample */
215+ timestamp_latency = radio_tmr_sample_get ();
216+ }
155217
156218 /* sample the packet timer again, use it to calculate ISR execution time
157219 * and use it in profiling event
158220 */
159221 radio_tmr_sample ();
160222}
161223
224+ uint16_t lll_prof_cputime_get (void )
225+ {
226+ uint16_t cputime ;
227+
228+ /* calculate the elapsed time in us since ISR entry */
229+ cputime = radio_tmr_sample_get () - timestamp_latency ;
230+
231+ return cputime ;
232+ }
233+
162234void lll_prof_send (void )
163235{
164236 struct node_rx_pdu * rx ;
@@ -228,7 +300,7 @@ static int send(struct node_rx_pdu *rx)
228300 }
229301
230302 /* calculate the elapsed time in us since ISR entry */
231- cputime = radio_tmr_sample_get () - timestamp_latency ;
303+ cputime = lll_prof_cputime_get () ;
232304
233305 /* check changes in min, avg and max */
234306 if (cputime > cputime_max ) {
@@ -329,12 +401,12 @@ static inline void delta(uint32_t timestamp, uint16_t *cputime)
329401 }
330402}
331403
332- static inline void delta_ticks (uint32_t timestamp_ticks , uint8_t * cputime_ticks )
404+ static inline void delta_ticks (uint32_t timestamp_ticks , uint16_t * cputime_ticks )
333405{
334406 uint32_t delta ;
335407
336408 delta = ticker_ticks_now_get () - timestamp_ticks ;
337- if (delta < UINT8_MAX && delta > * cputime_ticks ) {
409+ if (delta < UINT16_MAX && delta > * cputime_ticks ) {
338410 * cputime_ticks = delta ;
339411 }
340412}
0 commit comments