Skip to content

Commit 66b892f

Browse files
nordic-krchbjarki-andreasen
authored andcommitted
[nrf fromtree] drivers: clock_control: nrf: Add API for synchronous request
Add API for synchronous request for clock attributes. Signed-off-by: Krzysztof Chruściński <[email protected]> (cherry picked from commit fe0e2db)
1 parent fc0f104 commit 66b892f

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

drivers/clock_control/clock_control_nrf2_common.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
#include "clock_control_nrf2_common.h"
7+
#include <zephyr/drivers/clock_control/nrf_clock_control.h>
78

89
#include <zephyr/logging/log.h>
910
LOG_MODULE_REGISTER(clock_control_nrf2, CONFIG_CLOCK_CONTROL_LOG_LEVEL);
@@ -24,6 +25,13 @@ LOG_MODULE_REGISTER(clock_control_nrf2, CONFIG_CLOCK_CONTROL_LOG_LEVEL);
2425
*/
2526
STRUCT_CLOCK_CONFIG(generic, ONOFF_CNT_MAX);
2627

28+
/* Structure used for synchronous clock request. */
29+
struct sync_req {
30+
struct onoff_client cli;
31+
struct k_sem sem;
32+
int res;
33+
};
34+
2735
static void update_config(struct clock_config_generic *cfg)
2836
{
2937
atomic_val_t prev_flags = atomic_or(&cfg->flags, FLAG_UPDATE_NEEDED);
@@ -159,3 +167,39 @@ int api_nosys_on_off(const struct device *dev, clock_control_subsys_t sys)
159167

160168
return -ENOSYS;
161169
}
170+
171+
static void sync_cb(struct onoff_manager *mgr, struct onoff_client *cli, uint32_t state, int res)
172+
{
173+
struct sync_req *req = CONTAINER_OF(cli, struct sync_req, cli);
174+
175+
req->res = res;
176+
k_sem_give(&req->sem);
177+
}
178+
179+
int nrf_clock_control_request_sync(const struct device *dev,
180+
const struct nrf_clock_spec *spec,
181+
k_timeout_t timeout)
182+
{
183+
struct sync_req req = {
184+
.sem = Z_SEM_INITIALIZER(req.sem, 0, 1)
185+
};
186+
int err;
187+
188+
if (k_is_in_isr()) {
189+
return -EWOULDBLOCK;
190+
}
191+
192+
sys_notify_init_callback(&req.cli.notify, sync_cb);
193+
194+
err = nrf_clock_control_request(dev, spec, &req.cli);
195+
if (err < 0) {
196+
return err;
197+
}
198+
199+
err = k_sem_take(&req.sem, timeout);
200+
if (err < 0) {
201+
return err;
202+
}
203+
204+
return req.res;
205+
}

include/zephyr/drivers/clock_control/nrf_clock_control.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,24 @@ int nrf_clock_control_request(const struct device *dev,
242242
return api->request(dev, spec, cli);
243243
}
244244

245+
/**
246+
* @brief Synchronously request a reservation to use a given clock with specified attributes.
247+
*
248+
* Function can only be called from thread context as it blocks until request is completed.
249+
* @see nrf_clock_control_request().
250+
*
251+
* @param dev pointer to the clock device structure.
252+
* @param spec See nrf_clock_control_request().
253+
* @param timeout Request timeout.
254+
*
255+
* @retval 0 if request is fulfilled.
256+
* @retval -EWOULDBLOCK if request is called from the interrupt context.
257+
* @retval negative See error codes returned by nrf_clock_control_request().
258+
*/
259+
int nrf_clock_control_request_sync(const struct device *dev,
260+
const struct nrf_clock_spec *spec,
261+
k_timeout_t timeout);
262+
245263
/**
246264
* @brief Release a reserved use of a clock.
247265
*

0 commit comments

Comments
 (0)