2020-06-07 03:38:38 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020-2021 Vestas Wind Systems A/S
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 15:58:46 +08:00
|
|
|
#include <zephyr/kernel.h>
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/pwm.h>
|
|
|
|
#include <zephyr/logging/log.h>
|
2020-06-07 03:38:38 +08:00
|
|
|
|
|
|
|
LOG_MODULE_REGISTER(pwm_capture, CONFIG_PWM_LOG_LEVEL);
|
|
|
|
|
2022-04-01 16:24:14 +08:00
|
|
|
struct z_pwm_capture_cb_data {
|
2020-06-07 03:38:38 +08:00
|
|
|
uint32_t period;
|
|
|
|
uint32_t pulse;
|
|
|
|
struct k_sem sem;
|
|
|
|
int status;
|
|
|
|
};
|
|
|
|
|
2022-04-01 16:24:14 +08:00
|
|
|
static void z_pwm_capture_cycles_callback(const struct device *dev,
|
|
|
|
uint32_t channel,
|
|
|
|
uint32_t period_cycles,
|
|
|
|
uint32_t pulse_cycles, int status,
|
|
|
|
void *user_data)
|
2020-06-07 03:38:38 +08:00
|
|
|
{
|
2022-04-01 16:24:14 +08:00
|
|
|
struct z_pwm_capture_cb_data *data = user_data;
|
2020-06-07 03:38:38 +08:00
|
|
|
|
|
|
|
data->period = period_cycles;
|
|
|
|
data->pulse = pulse_cycles;
|
|
|
|
data->status = status;
|
|
|
|
|
|
|
|
k_sem_give(&data->sem);
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:24:14 +08:00
|
|
|
int z_impl_pwm_capture_cycles(const struct device *dev, uint32_t channel,
|
|
|
|
pwm_flags_t flags, uint32_t *period,
|
|
|
|
uint32_t *pulse, k_timeout_t timeout)
|
2020-06-07 03:38:38 +08:00
|
|
|
{
|
2022-04-01 16:24:14 +08:00
|
|
|
struct z_pwm_capture_cb_data data;
|
2020-06-07 03:38:38 +08:00
|
|
|
int err;
|
|
|
|
|
|
|
|
if ((flags & PWM_CAPTURE_MODE_MASK) == PWM_CAPTURE_MODE_CONTINUOUS) {
|
|
|
|
LOG_ERR("continuous capture mode only supported via callback");
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags |= PWM_CAPTURE_MODE_SINGLE;
|
|
|
|
k_sem_init(&data.sem, 0, 1);
|
|
|
|
|
2022-04-01 16:24:14 +08:00
|
|
|
err = pwm_configure_capture(dev, channel, flags,
|
|
|
|
z_pwm_capture_cycles_callback, &data);
|
2020-06-07 03:38:38 +08:00
|
|
|
if (err) {
|
|
|
|
LOG_ERR("failed to configure pwm capture");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:24:14 +08:00
|
|
|
err = pwm_enable_capture(dev, channel);
|
2020-06-07 03:38:38 +08:00
|
|
|
if (err) {
|
|
|
|
LOG_ERR("failed to enable pwm capture");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = k_sem_take(&data.sem, timeout);
|
|
|
|
if (err == -EAGAIN) {
|
2022-04-01 16:24:14 +08:00
|
|
|
(void)pwm_disable_capture(dev, channel);
|
|
|
|
(void)pwm_configure_capture(dev, channel, flags, NULL, NULL);
|
2020-06-07 03:38:38 +08:00
|
|
|
LOG_WRN("pwm capture timed out");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.status == 0) {
|
|
|
|
if (period != NULL) {
|
|
|
|
*period = data.period;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pulse != NULL) {
|
|
|
|
*pulse = data.pulse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.status;
|
|
|
|
}
|