drivers: pwm: implement fake-pwm driver

implement fake-pwm driver with binding using fff

Signed-off-by: Nathan Olff <nathan@kickmaker.net>
This commit is contained in:
Nathan Olff 2024-10-15 13:41:46 +02:00 committed by Benjamin Cabé
commit c152453a72
6 changed files with 127 additions and 1 deletions

View file

@ -47,7 +47,7 @@ zephyr_library_sources_ifdef(CONFIG_PWM_NXP_S32_EMIOS pwm_nxp_s32_emios.c)
zephyr_library_sources_ifdef(CONFIG_PWM_ENE_KB1200 pwm_ene_kb1200.c) zephyr_library_sources_ifdef(CONFIG_PWM_ENE_KB1200 pwm_ene_kb1200.c)
zephyr_library_sources_ifdef(CONFIG_PWM_RENESAS_RA8 pwm_renesas_ra8.c) zephyr_library_sources_ifdef(CONFIG_PWM_RENESAS_RA8 pwm_renesas_ra8.c)
zephyr_library_sources_ifdef(CONFIG_PWM_INFINEON_CAT1 pwm_ifx_cat1.c) zephyr_library_sources_ifdef(CONFIG_PWM_INFINEON_CAT1 pwm_ifx_cat1.c)
zephyr_library_sources_ifdef(CONFIG_PWM_FAKE pwm_fake.c)
zephyr_library_sources_ifdef(CONFIG_USERSPACE pwm_handlers.c) zephyr_library_sources_ifdef(CONFIG_USERSPACE pwm_handlers.c)
zephyr_library_sources_ifdef(CONFIG_PWM_CAPTURE pwm_capture.c) zephyr_library_sources_ifdef(CONFIG_PWM_CAPTURE pwm_capture.c)
zephyr_library_sources_ifdef(CONFIG_PWM_SHELL pwm_shell.c) zephyr_library_sources_ifdef(CONFIG_PWM_SHELL pwm_shell.c)

View file

@ -114,4 +114,6 @@ source "drivers/pwm/Kconfig.renesas_ra8"
source "drivers/pwm/Kconfig.ifx_cat1" source "drivers/pwm/Kconfig.ifx_cat1"
source "drivers/pwm/Kconfig.fake"
endif # PWM endif # PWM

11
drivers/pwm/Kconfig.fake Normal file
View file

@ -0,0 +1,11 @@
# Fake PWM configuration options
# Copyright (c) 2024 Kickmaker
# SPDX-License-Identifier: Apache-2.0
config PWM_FAKE
bool "Fake PWM driver"
default y
depends on DT_HAS_ZEPHYR_FAKE_PWM_ENABLED
help
Enable support for the FFF-based fake PWM driver.

63
drivers/pwm/pwm_fake.c Normal file
View file

@ -0,0 +1,63 @@
/*
* Copyright (c) 2024 Kickmaker
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/device.h>
#include <zephyr/drivers/pwm.h>
#include <zephyr/drivers/pwm/pwm_fake.h>
#include <zephyr/fff.h>
#include <zephyr/sys/util.h>
#ifdef CONFIG_ZTEST
#include <zephyr/ztest.h>
#endif /* CONFIG_ZTEST */
#define DT_DRV_COMPAT zephyr_fake_pwm
/** Fake PWM config structure */
struct fake_pwm_config {
/** Frequency of the (fake) underlying timer */
uint64_t frequency_hz;
};
DEFINE_FAKE_VALUE_FUNC(int, fake_pwm_set_cycles, const struct device *, uint32_t, uint32_t,
uint32_t, pwm_flags_t);
#ifdef CONFIG_ZTEST
static void fake_pwm_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
{
ARG_UNUSED(test);
ARG_UNUSED(fixture);
RESET_FAKE(fake_pwm_set_cycles);
}
ZTEST_RULE(fake_pwm_reset_rule, fake_pwm_reset_rule_before, NULL);
#endif /* CONFIG_ZTEST */
static int fake_pwm_get_cycles_per_sec(const struct device *dev, uint32_t channel, uint64_t *cycles)
{
ARG_UNUSED(channel);
const struct fake_pwm_config *config = dev->config;
*cycles = config->frequency_hz;
return 0;
}
static DEVICE_API(pwm, fake_pwm_driver_api) = {
.set_cycles = fake_pwm_set_cycles,
.get_cycles_per_sec = fake_pwm_get_cycles_per_sec,
};
#define FAKE_PWM_INIT(inst) \
static const struct fake_pwm_config fake_pwm_config_##inst = { \
.frequency_hz = DT_INST_PROP(inst, frequency), \
}; \
\
DEVICE_DT_INST_DEFINE(inst, NULL, NULL, NULL, &fake_pwm_config_##inst, POST_KERNEL, \
CONFIG_PWM_INIT_PRIORITY, &fake_pwm_driver_api);
DT_INST_FOREACH_STATUS_OKAY(FAKE_PWM_INIT)

View file

@ -0,0 +1,26 @@
# Copyright (c) 2024 Kickmaker
# SPDX-License-Identifier: Apache-2.0
description: |
This binding provides a fake PWM for use as either a stub or a mock in Zephyr
testing.
compatible: "zephyr,fake-pwm"
include: [pwm-controller.yaml, base.yaml, pinctrl-device.yaml]
properties:
"#pwm-cells":
const: 2
description: |
Number of items to expect in a PWM
- channel of the timer used for PWM
- period to set in ns
frequency:
type: int
description: |
Frequency for the underlying timer (in Hz)
pwm-cells:
- channel
- period

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2024, Kickmaker
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef INCLUDE_DRIVERS_PWM_PWM_FAKE_H_
#define INCLUDE_DRIVERS_PWM_PWM_FAKE_H_
#include <zephyr/drivers/pwm.h>
#include <zephyr/fff.h>
#ifdef __cplusplus
extern "C" {
#endif
DECLARE_FAKE_VALUE_FUNC(int, fake_pwm_set_cycles, const struct device *, uint32_t, uint32_t,
uint32_t, pwm_flags_t);
#ifdef __cplusplus
}
#endif
#endif /* INCLUDE_DRIVERS_PWM_PWM_FAKE_H_ */