Skip to content

Commit

Permalink
drivers: pwm: implement fake-pwm driver
Browse files Browse the repository at this point in the history
implement fake-pwm driver with binding using fff

Signed-off-by: Nathan Olff <[email protected]>
  • Loading branch information
nono313 authored and kartben committed Dec 14, 2024
1 parent e8a5e97 commit c152453
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 1 deletion.
2 changes: 1 addition & 1 deletion drivers/pwm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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_RENESAS_RA8 pwm_renesas_ra8.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_PWM_CAPTURE pwm_capture.c)
zephyr_library_sources_ifdef(CONFIG_PWM_SHELL pwm_shell.c)
2 changes: 2 additions & 0 deletions drivers/pwm/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,6 @@ source "drivers/pwm/Kconfig.renesas_ra8"

source "drivers/pwm/Kconfig.ifx_cat1"

source "drivers/pwm/Kconfig.fake"

endif # PWM
11 changes: 11 additions & 0 deletions drivers/pwm/Kconfig.fake
Original file line number Diff line number Diff line change
@@ -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 changes: 63 additions & 0 deletions drivers/pwm/pwm_fake.c
Original file line number Diff line number Diff line change
@@ -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)
26 changes: 26 additions & 0 deletions dts/bindings/pwm/zephyr,fake-pwm.yaml
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions include/zephyr/drivers/pwm/pwm_fake.h
Original file line number Diff line number Diff line change
@@ -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_ */

0 comments on commit c152453

Please sign in to comment.