@@ -33,64 +33,6 @@ static nrfx_timer_t timer = NRFX_TIMER_INSTANCE(CONFIG_NRF_CPU_LOAD_TIMER_INSTAN
3333static bool ready ;
3434static struct k_work_delayable cpu_load_log ;
3535static uint32_t cycle_ref ;
36- static uint32_t shared_ch_mask ;
37-
38- #define IS_CH_SHARED (ch ) \
39- (IS_ENABLED(CONFIG_NRF_CPU_LOAD_USE_SHARED_DPPI_CHANNELS) && \
40- (BIT(ch) & shared_ch_mask))
41-
42-
43- /** @brief Allocate (D)PPI channel. */
44- static nrfx_err_t ppi_alloc (uint8_t * ch , uint32_t evt )
45- {
46- #ifdef DPPI_PRESENT
47- if (* PUBLISH_ADDR (evt ) != 0 ) {
48- if (!IS_ENABLED (CONFIG_NRF_CPU_LOAD_USE_SHARED_DPPI_CHANNELS )) {
49- return NRFX_ERROR_BUSY ;
50- }
51- /* Use mask of one of subscribe registers in the system,
52- * assuming that all subscribe registers has the same mask for
53- * channel id.
54- */
55- * ch = * PUBLISH_ADDR (evt ) & DPPIC_SUBSCRIBE_CHG_EN_CHIDX_Msk ;
56- shared_ch_mask |= BIT (* ch );
57- return NRFX_SUCCESS ;
58- }
59- #endif
60- return nrfx_gppi_channel_alloc (ch );
61- }
62-
63- static nrfx_err_t ppi_free (uint8_t ch )
64- {
65- #ifdef DPPI_PRESENT
66- if (IS_CH_SHARED (ch )) {
67- shared_ch_mask &= ~BIT (ch );
68- return NRFX_SUCCESS ;
69- }
70- #endif
71- return nrfx_gppi_channel_free (ch );
72- }
73-
74- static void ppi_cleanup (uint8_t ch_tick , uint8_t ch_sleep , uint8_t ch_wakeup )
75- {
76- nrfx_err_t err = NRFX_SUCCESS ;
77-
78- if (IS_ENABLED (CONFIG_NRF_CPU_LOAD_ALIGNED_CLOCKS )) {
79- err = ppi_free (ch_tick );
80- }
81-
82- if ((err == NRFX_SUCCESS ) && (ch_sleep != CH_INVALID )) {
83- err = ppi_free (ch_sleep );
84- }
85-
86- if ((err == NRFX_SUCCESS ) && (ch_wakeup != CH_INVALID )) {
87- err = ppi_free (ch_wakeup );
88- }
89-
90- if (err != NRFX_SUCCESS ) {
91- LOG_ERR ("PPI channel freeing failed (err:%d)" , err );
92- }
93- }
9436
9537static void cpu_load_log_fn (struct k_work * item )
9638{
@@ -119,6 +61,39 @@ static void timer_handler(nrf_timer_event_t event_type, void *context)
11961 /*empty*/
12062}
12163
64+ static int ppi_handle_get (uint32_t evt )
65+ {
66+ #ifdef DPPI_PRESENT
67+ if (* PUBLISH_ADDR (evt ) != 0 ) {
68+ if (!IS_ENABLED (CONFIG_NRF_CPU_LOAD_USE_SHARED_DPPI_CHANNELS )) {
69+ return - ENOTSUP ;
70+ }
71+ return = * PUBLISH_ADDR (evt ) & DPPIC_SUBSCRIBE_CHG_EN_CHIDX_Msk ;
72+ }
73+ #endif
74+ return - ENOTSUP ;
75+ }
76+ static int ppi_setup (uint32_t eep , uint32_t tep )
77+ {
78+ nrfx_gppi_handle_t handle ;
79+ int err ;
80+
81+ err = ppi_handle_get (eep );
82+ if (err >= 0 ) {
83+ handle = (nrfx_gppi_handle_t )err ;
84+ nrfx_gppi_ep_attach (handle , tep );
85+ return 0 ;
86+ }
87+
88+ err = nrfx_gppi_conn_alloc (eep , tep , & handle );
89+ if (err < 0 ) {
90+ LOG_ERR ("Failed to allocate PPI resources" );
91+ return err ;
92+ }
93+
94+ nrfx_gppi_conn_enable (handle );
95+ return 0 ;
96+ }
12297
12398int cpu_load_init_internal (void )
12499{
@@ -140,57 +115,31 @@ int cpu_load_init_internal(void)
140115#ifdef CONFIG_NRF_CPU_LOAD_ALIGNED_CLOCKS
141116 /* It's assumed that RTC1 is driving system clock. */
142117 config .mode = NRF_TIMER_MODE_COUNTER ;
143- err = ppi_alloc ( & ch_tick ,
144- nrf_rtc_event_address_get ( NRF_RTC1 , NRF_RTC_EVENT_TICK ));
145- if (err != NRFX_SUCCESS ) {
146- return - ENODEV ;
118+ ret = ppi_setup ( nrf_rtc_event_address_get ( NRF_RTC1 , NRF_RTC_EVENT_TICK ) ,
119+ nrfx_timer_task_address_get ( & timer , NRF_TIMER_TASK_COUNT ));
120+ if (ret < 0 ) {
121+ return ret ;
147122 }
148- nrfx_gppi_channel_endpoints_setup (ch_tick ,
149- nrf_rtc_event_address_get (NRF_RTC1 , NRF_RTC_EVENT_TICK ),
150- nrfx_timer_task_address_get (& timer , NRF_TIMER_TASK_COUNT ));
151123 nrf_rtc_event_enable (NRF_RTC1 , NRF_RTC_INT_TICK_MASK );
152124#endif
153125
154- err = ppi_alloc (& ch_sleep ,
155- nrf_power_event_address_get (NRF_POWER ,
156- NRF_POWER_EVENT_SLEEPENTER ));
157- if (err != NRFX_SUCCESS ) {
158- ppi_cleanup (ch_tick , CH_INVALID , CH_INVALID );
159- return - ENODEV ;
126+ ret = ppi_setup (nrf_power_event_address_get (NRF_POWER , NRF_POWER_EVENT_SLEEPENTER ),
127+ nrfx_timer_task_address_get (& timer , NRF_TIMER_TASK_START ));
128+ if (ret < 0 ) {
129+ return ret ;
160130 }
161131
162- err = ppi_alloc (& ch_wakeup ,
163- nrf_power_event_address_get (NRF_POWER ,
164- NRF_POWER_EVENT_SLEEPEXIT ));
165- if (err != NRFX_SUCCESS ) {
166- ppi_cleanup (ch_tick , ch_sleep , CH_INVALID );
167- return - ENODEV ;
132+ ret = ppi_setup (nrf_power_event_address_get (NRF_POWER , NRF_POWER_EVENT_SLEEPEXIT ),
133+ nrfx_timer_task_address_get (& timer , NRF_TIMER_TASK_STOP ));
134+ if (ret < 0 ) {
135+ return ret ;
168136 }
169137
170138 err = nrfx_timer_init (& timer , & config , timer_handler );
171139 if (err != NRFX_SUCCESS ) {
172- ppi_cleanup (ch_tick , ch_sleep , ch_wakeup );
173140 return - EBUSY ;
174141 }
175142
176- nrfx_gppi_channel_endpoints_setup (ch_sleep ,
177- nrf_power_event_address_get (NRF_POWER ,
178- NRF_POWER_EVENT_SLEEPENTER ),
179- nrfx_timer_task_address_get (& timer , NRF_TIMER_TASK_START ));
180- nrfx_gppi_channel_endpoints_setup (ch_wakeup ,
181- nrf_power_event_address_get (NRF_POWER ,
182- NRF_POWER_EVENT_SLEEPEXIT ),
183- nrfx_timer_task_address_get (& timer , NRF_TIMER_TASK_STOP ));
184-
185- /* In case of DPPI event can only be assigned to a single channel. In
186- * that case, cpu load can still subscribe to the channel but should
187- * not control it. It may result in cpu load not working. User must
188- * take care of that.
189- */
190- nrfx_gppi_channels_enable ((IS_CH_SHARED (ch_sleep ) ? 0 : BIT (ch_sleep )) |
191- (IS_CH_SHARED (ch_wakeup ) ? 0 : BIT (ch_wakeup )) |
192- (IS_CH_SHARED (ch_tick ) ? 0 : BIT (ch_tick )));
193-
194143 cpu_load_reset ();
195144
196145 if (IS_ENABLED (CONFIG_NRF_CPU_LOAD_LOG_PERIODIC )) {
0 commit comments