Skip to content

Commit

Permalink
drivers: clock_control: nrf: Add API for synchronous request
Browse files Browse the repository at this point in the history
Add API for synchronous request for clock attributes.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
  • Loading branch information
nordic-krch authored and kartben committed Dec 13, 2024
1 parent b44250a commit fe0e2db
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
44 changes: 44 additions & 0 deletions drivers/clock_control/clock_control_nrf2_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

#include "clock_control_nrf2_common.h"
#include <zephyr/drivers/clock_control/nrf_clock_control.h>

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

/* Structure used for synchronous clock request. */
struct sync_req {
struct onoff_client cli;
struct k_sem sem;
int res;
};

static void update_config(struct clock_config_generic *cfg)
{
atomic_val_t prev_flags = atomic_or(&cfg->flags, FLAG_UPDATE_NEEDED);
Expand Down Expand Up @@ -159,3 +167,39 @@ int api_nosys_on_off(const struct device *dev, clock_control_subsys_t sys)

return -ENOSYS;
}

static void sync_cb(struct onoff_manager *mgr, struct onoff_client *cli, uint32_t state, int res)
{
struct sync_req *req = CONTAINER_OF(cli, struct sync_req, cli);

req->res = res;
k_sem_give(&req->sem);
}

int nrf_clock_control_request_sync(const struct device *dev,
const struct nrf_clock_spec *spec,
k_timeout_t timeout)
{
struct sync_req req = {
.sem = Z_SEM_INITIALIZER(req.sem, 0, 1)
};
int err;

if (k_is_in_isr()) {
return -EWOULDBLOCK;
}

sys_notify_init_callback(&req.cli.notify, sync_cb);

err = nrf_clock_control_request(dev, spec, &req.cli);
if (err < 0) {
return err;
}

err = k_sem_take(&req.sem, timeout);
if (err < 0) {
return err;
}

return req.res;
}
18 changes: 18 additions & 0 deletions include/zephyr/drivers/clock_control/nrf_clock_control.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,24 @@ int nrf_clock_control_request(const struct device *dev,
return api->request(dev, spec, cli);
}

/**
* @brief Synchronously request a reservation to use a given clock with specified attributes.
*
* Function can only be called from thread context as it blocks until request is completed.
* @see nrf_clock_control_request().
*
* @param dev pointer to the clock device structure.
* @param spec See nrf_clock_control_request().
* @param timeout Request timeout.
*
* @retval 0 if request is fulfilled.
* @retval -EWOULDBLOCK if request is called from the interrupt context.
* @retval negative See error codes returned by nrf_clock_control_request().
*/
int nrf_clock_control_request_sync(const struct device *dev,
const struct nrf_clock_spec *spec,
k_timeout_t timeout);

/**
* @brief Release a reserved use of a clock.
*
Expand Down

0 comments on commit fe0e2db

Please sign in to comment.