2020-06-07 03:38:38 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020-2021 Vestas Wind Systems A/S
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/zephyr.h>
|
|
|
|
#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;
|
|
|
|
}
|