1414#include <zephyr/drivers/clock_control/nrf_clock_control.h>
1515#include <nrf_sys_event.h>
1616
17+ #define HFCLK_NOT_REQUESTED (0u)
18+ #define HFCLK_REQUESTED (1u)
19+
1720static bool hfclk_is_running ;
21+ static atomic_t hfclk_requested ;
1822
1923void nrf_802154_clock_init (void )
2024{
21- /* Intentionally empty. */
25+ atomic_set ( & hfclk_requested , HFCLK_NOT_REQUESTED );
2226}
2327
2428void nrf_802154_clock_deinit (void )
@@ -39,8 +43,10 @@ static void hfclk_on_callback(struct onoff_manager *mgr,
3943 uint32_t state ,
4044 int res )
4145{
42- hfclk_is_running = true;
43- nrf_802154_clock_hfclk_ready ();
46+ if (atomic_cas (& hfclk_requested , HFCLK_REQUESTED , HFCLK_NOT_REQUESTED )) {
47+ hfclk_is_running = true;
48+ nrf_802154_clock_hfclk_ready ();
49+ }
4450}
4551
4652#if defined(CONFIG_CLOCK_CONTROL_NRF )
@@ -59,72 +65,80 @@ K_TIMER_DEFINE(hfclk_started_timer, hfclk_started_timer_handler, NULL);
5965
6066void nrf_802154_clock_hfclk_start (void )
6167{
68+ if (atomic_cas (& hfclk_requested , HFCLK_NOT_REQUESTED , HFCLK_REQUESTED )) {
6269#if defined(NRF54LM20A_ENGA_XXAA )
63- /*
64- * Right now, the power_clock_irq is not available on nRF54LM20A.
65- * Since the onoff mechanism relies on the irq, the timer is used
66- * to emit the hfclk_ready callback.
67- */
68- k_timer_start (& hfclk_started_timer , K_USEC (MAX_HFXO_RAMP_UP_TIME_US ), K_NO_WAIT );
70+ /*
71+ * Right now, the power_clock_irq is not available on nRF54LM20A.
72+ * Since the onoff mechanism relies on the irq, the timer is used
73+ * to emit the hfclk_ready callback.
74+ */
75+ k_timer_start (& hfclk_started_timer , K_USEC (MAX_HFXO_RAMP_UP_TIME_US ), K_NO_WAIT );
6976#else
70- struct onoff_manager * mgr =
71- z_nrf_clock_control_get_onoff (CLOCK_CONTROL_NRF_SUBSYS_HF );
77+ struct onoff_manager * mgr =
78+ z_nrf_clock_control_get_onoff (CLOCK_CONTROL_NRF_SUBSYS_HF );
7279
73- __ASSERT_NO_MSG (mgr != NULL );
80+ __ASSERT_NO_MSG (mgr != NULL );
7481
75- sys_notify_init_callback (& hfclk_cli .notify , hfclk_on_callback );
82+ sys_notify_init_callback (& hfclk_cli .notify , hfclk_on_callback );
7683#endif // defined(NRF54LM20A_ENGA_XXAA)
77- /*
78- * todo: replace constlat request with PM policy API when
79- * controlling the event latency becomes possible.
80- */
81- if (IS_ENABLED (CONFIG_NRF_802154_CONSTLAT_CONTROL )) {
82- nrf_sys_event_request_global_constlat ();
83- }
84+ /*
85+ * todo: replace constlat request with PM policy API when
86+ * controlling the event latency becomes possible.
87+ */
88+ if (IS_ENABLED (CONFIG_NRF_802154_CONSTLAT_CONTROL )) {
89+ nrf_sys_event_request_global_constlat ();
90+ }
8491
8592#if !defined(NRF54LM20A_ENGA_XXAA )
86- int ret = onoff_request (mgr , & hfclk_cli );
87- __ASSERT_NO_MSG (ret >= 0 );
88- (void )ret ;
93+ int ret = onoff_request (mgr , & hfclk_cli );
94+ __ASSERT_NO_MSG (ret >= 0 );
95+ (void )ret ;
8996#endif
97+ }
9098}
9199
92100void nrf_802154_clock_hfclk_stop (void )
93101{
94- struct onoff_manager * mgr =
95- z_nrf_clock_control_get_onoff (CLOCK_CONTROL_NRF_SUBSYS_HF );
102+ if (atomic_cas (& hfclk_requested , HFCLK_REQUESTED , HFCLK_NOT_REQUESTED )) {
103+ struct onoff_manager * mgr =
104+ z_nrf_clock_control_get_onoff (CLOCK_CONTROL_NRF_SUBSYS_HF );
96105
97- __ASSERT_NO_MSG (mgr != NULL );
106+ __ASSERT_NO_MSG (mgr != NULL );
98107
99- int ret = onoff_cancel_or_release (mgr , & hfclk_cli );
100- __ASSERT_NO_MSG (ret >= 0 );
101- (void )ret ;
108+ int ret = onoff_cancel_or_release (mgr , & hfclk_cli );
109+ __ASSERT_NO_MSG (ret >= 0 );
110+ (void )ret ;
102111
103- if (IS_ENABLED (CONFIG_NRF_802154_CONSTLAT_CONTROL )) {
104- nrf_sys_event_release_global_constlat ();
105- }
112+ if (IS_ENABLED (CONFIG_NRF_802154_CONSTLAT_CONTROL )) {
113+ nrf_sys_event_release_global_constlat ();
114+ }
106115
107- hfclk_is_running = false;
116+ hfclk_is_running = false;
117+ }
108118}
109119
110120#elif defined(CONFIG_CLOCK_CONTROL_NRF2 )
111121
112122void nrf_802154_clock_hfclk_start (void )
113123{
114- sys_notify_init_callback (& hfclk_cli .notify , hfclk_on_callback );
115- int ret = nrf_clock_control_request (DEVICE_DT_GET (DT_NODELABEL (hfxo )), NULL , & hfclk_cli );
124+ if (atomic_cas (& hfclk_requested , HFCLK_NOT_REQUESTED , HFCLK_REQUESTED )) {
125+ sys_notify_init_callback (& hfclk_cli .notify , hfclk_on_callback );
126+ int ret = nrf_clock_control_request (DEVICE_DT_GET (DT_NODELABEL (hfxo )), NULL , & hfclk_cli );
116127
117- __ASSERT_NO_MSG (ret >= 0 );
118- (void )ret ;
128+ __ASSERT_NO_MSG (ret >= 0 );
129+ (void )ret ;
130+ }
119131}
120132
121133void nrf_802154_clock_hfclk_stop (void )
122134{
123- int ret = nrf_clock_control_cancel_or_release (DEVICE_DT_GET (DT_NODELABEL (hfxo )),
124- NULL , & hfclk_cli );
135+ if (atomic_cas (& hfclk_requested , HFCLK_NOT_REQUESTED , HFCLK_REQUESTED )) {
136+ int ret = nrf_clock_control_cancel_or_release (DEVICE_DT_GET (DT_NODELABEL (hfxo )),
137+ NULL , & hfclk_cli );
125138
126- __ASSERT_NO_MSG (ret >= 0 );
127- (void )ret ;
139+ __ASSERT_NO_MSG (ret >= 0 );
140+ (void )ret ;
141+ }
128142}
129143
130144#endif
0 commit comments