Skip to content

Commit c152453

Browse files
nono313kartben
authored andcommitted
drivers: pwm: implement fake-pwm driver
implement fake-pwm driver with binding using fff Signed-off-by: Nathan Olff <[email protected]>
1 parent e8a5e97 commit c152453

File tree

6 files changed

+127
-1
lines changed

6 files changed

+127
-1
lines changed

drivers/pwm/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ zephyr_library_sources_ifdef(CONFIG_PWM_NXP_S32_EMIOS pwm_nxp_s32_emios.c)
4747
zephyr_library_sources_ifdef(CONFIG_PWM_ENE_KB1200 pwm_ene_kb1200.c)
4848
zephyr_library_sources_ifdef(CONFIG_PWM_RENESAS_RA8 pwm_renesas_ra8.c)
4949
zephyr_library_sources_ifdef(CONFIG_PWM_INFINEON_CAT1 pwm_ifx_cat1.c)
50-
50+
zephyr_library_sources_ifdef(CONFIG_PWM_FAKE pwm_fake.c)
5151
zephyr_library_sources_ifdef(CONFIG_USERSPACE pwm_handlers.c)
5252
zephyr_library_sources_ifdef(CONFIG_PWM_CAPTURE pwm_capture.c)
5353
zephyr_library_sources_ifdef(CONFIG_PWM_SHELL pwm_shell.c)

drivers/pwm/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,6 @@ source "drivers/pwm/Kconfig.renesas_ra8"
114114

115115
source "drivers/pwm/Kconfig.ifx_cat1"
116116

117+
source "drivers/pwm/Kconfig.fake"
118+
117119
endif # PWM

drivers/pwm/Kconfig.fake

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Fake PWM configuration options
2+
3+
# Copyright (c) 2024 Kickmaker
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
config PWM_FAKE
7+
bool "Fake PWM driver"
8+
default y
9+
depends on DT_HAS_ZEPHYR_FAKE_PWM_ENABLED
10+
help
11+
Enable support for the FFF-based fake PWM driver.

drivers/pwm/pwm_fake.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2024 Kickmaker
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/device.h>
8+
#include <zephyr/drivers/pwm.h>
9+
#include <zephyr/drivers/pwm/pwm_fake.h>
10+
#include <zephyr/fff.h>
11+
#include <zephyr/sys/util.h>
12+
13+
#ifdef CONFIG_ZTEST
14+
#include <zephyr/ztest.h>
15+
#endif /* CONFIG_ZTEST */
16+
17+
#define DT_DRV_COMPAT zephyr_fake_pwm
18+
19+
/** Fake PWM config structure */
20+
struct fake_pwm_config {
21+
/** Frequency of the (fake) underlying timer */
22+
uint64_t frequency_hz;
23+
};
24+
25+
DEFINE_FAKE_VALUE_FUNC(int, fake_pwm_set_cycles, const struct device *, uint32_t, uint32_t,
26+
uint32_t, pwm_flags_t);
27+
28+
#ifdef CONFIG_ZTEST
29+
static void fake_pwm_reset_rule_before(const struct ztest_unit_test *test, void *fixture)
30+
{
31+
ARG_UNUSED(test);
32+
ARG_UNUSED(fixture);
33+
34+
RESET_FAKE(fake_pwm_set_cycles);
35+
}
36+
37+
ZTEST_RULE(fake_pwm_reset_rule, fake_pwm_reset_rule_before, NULL);
38+
#endif /* CONFIG_ZTEST */
39+
40+
static int fake_pwm_get_cycles_per_sec(const struct device *dev, uint32_t channel, uint64_t *cycles)
41+
{
42+
ARG_UNUSED(channel);
43+
const struct fake_pwm_config *config = dev->config;
44+
45+
*cycles = config->frequency_hz;
46+
47+
return 0;
48+
}
49+
50+
static DEVICE_API(pwm, fake_pwm_driver_api) = {
51+
.set_cycles = fake_pwm_set_cycles,
52+
.get_cycles_per_sec = fake_pwm_get_cycles_per_sec,
53+
};
54+
55+
#define FAKE_PWM_INIT(inst) \
56+
static const struct fake_pwm_config fake_pwm_config_##inst = { \
57+
.frequency_hz = DT_INST_PROP(inst, frequency), \
58+
}; \
59+
\
60+
DEVICE_DT_INST_DEFINE(inst, NULL, NULL, NULL, &fake_pwm_config_##inst, POST_KERNEL, \
61+
CONFIG_PWM_INIT_PRIORITY, &fake_pwm_driver_api);
62+
63+
DT_INST_FOREACH_STATUS_OKAY(FAKE_PWM_INIT)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright (c) 2024 Kickmaker
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: |
5+
This binding provides a fake PWM for use as either a stub or a mock in Zephyr
6+
testing.
7+
8+
compatible: "zephyr,fake-pwm"
9+
10+
include: [pwm-controller.yaml, base.yaml, pinctrl-device.yaml]
11+
12+
properties:
13+
"#pwm-cells":
14+
const: 2
15+
description: |
16+
Number of items to expect in a PWM
17+
- channel of the timer used for PWM
18+
- period to set in ns
19+
frequency:
20+
type: int
21+
description: |
22+
Frequency for the underlying timer (in Hz)
23+
24+
pwm-cells:
25+
- channel
26+
- period
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) 2024, Kickmaker
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef INCLUDE_DRIVERS_PWM_PWM_FAKE_H_
8+
#define INCLUDE_DRIVERS_PWM_PWM_FAKE_H_
9+
10+
#include <zephyr/drivers/pwm.h>
11+
#include <zephyr/fff.h>
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
DECLARE_FAKE_VALUE_FUNC(int, fake_pwm_set_cycles, const struct device *, uint32_t, uint32_t,
18+
uint32_t, pwm_flags_t);
19+
20+
#ifdef __cplusplus
21+
}
22+
#endif
23+
24+
#endif /* INCLUDE_DRIVERS_PWM_PWM_FAKE_H_ */

0 commit comments

Comments
 (0)