forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
drivers: pwm: implement fake-pwm driver
implement fake-pwm driver with binding using fff Signed-off-by: Nathan Olff <[email protected]>
- Loading branch information
Showing
6 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |