@@ -34,64 +34,6 @@ static nrfx_timer_t timer =
3434static bool ready ;
3535static struct k_work_delayable cpu_load_log ;
3636static uint32_t cycle_ref ;
37- static uint32_t shared_ch_mask ;
38-
39- #define IS_CH_SHARED (ch ) \
40- (IS_ENABLED(CONFIG_NRF_CPU_LOAD_USE_SHARED_DPPI_CHANNELS) && \
41- (BIT(ch) & shared_ch_mask))
42-
43-
44- /** @brief Allocate (D)PPI channel. */
45- static nrfx_err_t ppi_alloc (uint8_t * ch , uint32_t evt )
46- {
47- #ifdef DPPI_PRESENT
48- if (* PUBLISH_ADDR (evt ) != 0 ) {
49- if (!IS_ENABLED (CONFIG_NRF_CPU_LOAD_USE_SHARED_DPPI_CHANNELS )) {
50- return NRFX_ERROR_BUSY ;
51- }
52- /* Use mask of one of subscribe registers in the system,
53- * assuming that all subscribe registers has the same mask for
54- * channel id.
55- */
56- * ch = * PUBLISH_ADDR (evt ) & DPPIC_SUBSCRIBE_CHG_EN_CHIDX_Msk ;
57- shared_ch_mask |= BIT (* ch );
58- return NRFX_SUCCESS ;
59- }
60- #endif
61- return nrfx_gppi_channel_alloc (ch );
62- }
63-
64- static nrfx_err_t ppi_free (uint8_t ch )
65- {
66- #ifdef DPPI_PRESENT
67- if (IS_CH_SHARED (ch )) {
68- shared_ch_mask &= ~BIT (ch );
69- return NRFX_SUCCESS ;
70- }
71- #endif
72- return nrfx_gppi_channel_free (ch );
73- }
74-
75- static void ppi_cleanup (uint8_t ch_tick , uint8_t ch_sleep , uint8_t ch_wakeup )
76- {
77- nrfx_err_t err = NRFX_SUCCESS ;
78-
79- if (IS_ENABLED (CONFIG_NRF_CPU_LOAD_ALIGNED_CLOCKS )) {
80- err = ppi_free (ch_tick );
81- }
82-
83- if ((err == NRFX_SUCCESS ) && (ch_sleep != CH_INVALID )) {
84- err = ppi_free (ch_sleep );
85- }
86-
87- if ((err == NRFX_SUCCESS ) && (ch_wakeup != CH_INVALID )) {
88- err = ppi_free (ch_wakeup );
89- }
90-
91- if (err != NRFX_SUCCESS ) {
92- LOG_ERR ("PPI channel freeing failed (err:%d)" , err );
93- }
94- }
9537
9638static void cpu_load_log_fn (struct k_work * item )
9739{
@@ -120,12 +62,43 @@ static void timer_handler(nrf_timer_event_t event_type, void *context)
12062 /*empty*/
12163}
12264
65+ static int ppi_handle_get (uint32_t evt )
66+ {
67+ #ifdef DPPI_PRESENT
68+ if (* PUBLISH_ADDR (evt ) != 0 ) {
69+ if (!IS_ENABLED (CONFIG_NRF_CPU_LOAD_USE_SHARED_DPPI_CHANNELS )) {
70+ return - ENOTSUP ;
71+ }
72+ return * PUBLISH_ADDR (evt ) & DPPIC_SUBSCRIBE_CHG_EN_CHIDX_Msk ;
73+ }
74+ #endif
75+ return - ENOTSUP ;
76+ }
77+ static int ppi_setup (uint32_t eep , uint32_t tep )
78+ {
79+ nrfx_gppi_handle_t handle ;
80+ int err ;
81+
82+ err = ppi_handle_get (eep );
83+ if (err >= 0 ) {
84+ /* It works only on single domain DPPI. */
85+ handle = (nrfx_gppi_handle_t )err ;
86+ nrfx_gppi_ep_attach (handle , tep );
87+ return 0 ;
88+ }
89+
90+ err = nrfx_gppi_conn_alloc (eep , tep , & handle );
91+ if (err < 0 ) {
92+ LOG_ERR ("Failed to allocate PPI resources" );
93+ return err ;
94+ }
95+
96+ nrfx_gppi_conn_enable (handle );
97+ return 0 ;
98+ }
12399
124100int cpu_load_init_internal (void )
125101{
126- uint8_t ch_sleep ;
127- uint8_t ch_wakeup ;
128- uint8_t ch_tick = 0 ;
129102 nrfx_err_t err ;
130103 uint32_t base_frequency = NRF_TIMER_BASE_FREQUENCY_GET (timer .p_reg );
131104 nrfx_timer_config_t config = NRFX_TIMER_DEFAULT_CONFIG (base_frequency );
@@ -141,57 +114,31 @@ int cpu_load_init_internal(void)
141114#ifdef CONFIG_NRF_CPU_LOAD_ALIGNED_CLOCKS
142115 /* It's assumed that RTC1 is driving system clock. */
143116 config .mode = NRF_TIMER_MODE_COUNTER ;
144- err = ppi_alloc ( & ch_tick ,
145- nrf_rtc_event_address_get ( NRF_RTC1 , NRF_RTC_EVENT_TICK ));
146- if (err != NRFX_SUCCESS ) {
147- return - ENODEV ;
117+ ret = ppi_setup ( nrf_rtc_event_address_get ( NRF_RTC1 , NRF_RTC_EVENT_TICK ) ,
118+ nrfx_timer_task_address_get ( & timer , NRF_TIMER_TASK_COUNT ));
119+ if (ret < 0 ) {
120+ return ret ;
148121 }
149- nrfx_gppi_channel_endpoints_setup (ch_tick ,
150- nrf_rtc_event_address_get (NRF_RTC1 , NRF_RTC_EVENT_TICK ),
151- nrfx_timer_task_address_get (& timer , NRF_TIMER_TASK_COUNT ));
152122 nrf_rtc_event_enable (NRF_RTC1 , NRF_RTC_INT_TICK_MASK );
153123#endif
154124
155- err = ppi_alloc (& ch_sleep ,
156- nrf_power_event_address_get (NRF_POWER ,
157- NRF_POWER_EVENT_SLEEPENTER ));
158- if (err != NRFX_SUCCESS ) {
159- ppi_cleanup (ch_tick , CH_INVALID , CH_INVALID );
160- return - ENODEV ;
125+ ret = ppi_setup (nrf_power_event_address_get (NRF_POWER , NRF_POWER_EVENT_SLEEPENTER ),
126+ nrfx_timer_task_address_get (& timer , NRF_TIMER_TASK_START ));
127+ if (ret < 0 ) {
128+ return ret ;
161129 }
162130
163- err = ppi_alloc (& ch_wakeup ,
164- nrf_power_event_address_get (NRF_POWER ,
165- NRF_POWER_EVENT_SLEEPEXIT ));
166- if (err != NRFX_SUCCESS ) {
167- ppi_cleanup (ch_tick , ch_sleep , CH_INVALID );
168- return - ENODEV ;
131+ ret = ppi_setup (nrf_power_event_address_get (NRF_POWER , NRF_POWER_EVENT_SLEEPEXIT ),
132+ nrfx_timer_task_address_get (& timer , NRF_TIMER_TASK_STOP ));
133+ if (ret < 0 ) {
134+ return ret ;
169135 }
170136
171137 err = nrfx_timer_init (& timer , & config , timer_handler );
172138 if (err != NRFX_SUCCESS ) {
173- ppi_cleanup (ch_tick , ch_sleep , ch_wakeup );
174139 return - EBUSY ;
175140 }
176141
177- nrfx_gppi_channel_endpoints_setup (ch_sleep ,
178- nrf_power_event_address_get (NRF_POWER ,
179- NRF_POWER_EVENT_SLEEPENTER ),
180- nrfx_timer_task_address_get (& timer , NRF_TIMER_TASK_START ));
181- nrfx_gppi_channel_endpoints_setup (ch_wakeup ,
182- nrf_power_event_address_get (NRF_POWER ,
183- NRF_POWER_EVENT_SLEEPEXIT ),
184- nrfx_timer_task_address_get (& timer , NRF_TIMER_TASK_STOP ));
185-
186- /* In case of DPPI event can only be assigned to a single channel. In
187- * that case, cpu load can still subscribe to the channel but should
188- * not control it. It may result in cpu load not working. User must
189- * take care of that.
190- */
191- nrfx_gppi_channels_enable ((IS_CH_SHARED (ch_sleep ) ? 0 : BIT (ch_sleep )) |
192- (IS_CH_SHARED (ch_wakeup ) ? 0 : BIT (ch_wakeup )) |
193- (IS_CH_SHARED (ch_tick ) ? 0 : BIT (ch_tick )));
194-
195142 cpu_load_reset ();
196143
197144 if (IS_ENABLED (CONFIG_NRF_CPU_LOAD_LOG_PERIODIC )) {
0 commit comments