Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: pwm: implement fake-pwm driver #79861

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

nono313
Copy link
Contributor

@nono313 nono313 commented Oct 15, 2024

Based on fake can and fake gpio driver, this PR implements a fake pwm driver to be used with native_sim target for unit testing purposes

It only implements basic PWM functionality (no capture)

@nono313 nono313 marked this pull request as draft October 15, 2024 11:43
@zephyrbot zephyrbot added the area: PWM Pulse Width Modulation label Oct 15, 2024
@nono313 nono313 force-pushed the implement-fake-pwm-driver branch 3 times, most recently from 9d3d339 to 1aecbfb Compare October 15, 2024 14:26
@nono313 nono313 marked this pull request as ready for review October 15, 2024 15:06
This binding provides a fake PWM for use as either a stub or a mock in Zephyr
testing.
compatible: "fake-pwm"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefix with zephyr,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the compatible and renamed the bindings

decsny
decsny previously approved these changes Dec 6, 2024
@kartben
Copy link
Collaborator

kartben commented Dec 12, 2024

this looks like a great addition, @anangl can you please help review/approve?

Copy link
Member

@anangl anangl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you actually tried to build it for native_sim? Apparently CI did not do it as no build errors were reported.


#include <zephyr/device.h>
#include <zephyr/drivers/pwm.h>
#include <drivers/pwm/pwm_fake.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <drivers/pwm/pwm_fake.h>
#include <zephyr/drivers/pwm/pwm_fake.h>

.frequency_hz = DT_INST_PROP(inst, frequency), \
}; \
\
DEVICE_DT_INST_DEFINE(inst, NULL, NULL, &fake_pwm_data_##inst, &fake_pwm_config_##inst, \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DEVICE_DT_INST_DEFINE(inst, NULL, NULL, &fake_pwm_data_##inst, &fake_pwm_config_##inst, \
DEVICE_DT_INST_DEFINE(inst, NULL, NULL, NULL, &fake_pwm_config_##inst, \

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I updated the code.

};

aliases {
pwm-0 = &pwm;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pwm-0 = &pwm;
pwm-0 = &pwm0;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, overlay fixed.


/{
pwm0: pwm0 {
compatible = "zephyr,fake-pwm";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it needed to add DEFINE_FFF_GLOBALS; in the test code so that this fake driver can be actually used with it?

Copy link
Contributor Author

@nono313 nono313 Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the test is used for both native_sim and others, how should I proceed to add the DEFINE in the test ? I have not found a twister test that is run both on hardware and on native_sim using FFF.

I tried adding the DEFINE in the test_pwm file under a ifdef HAS_DT

@nono313
Copy link
Contributor Author

nono313 commented Dec 12, 2024

Have you actually tried to build it for native_sim? Apparently CI did not do it as no build errors were reported.

I am actually using the fake_pwm module in a project I have.

When I try running the test I indeed see that it is not run. It seems to be filtered out.

I added a new test in the testcase. If this is not the right way to do it please tell. I'm not very familiar with testcase filters.

I also took the liberty to rebase from latest main branch

@nono313 nono313 dismissed stale reviews from decsny and henrikbrixandersen via 13e4d8a December 12, 2024 13:02
@nono313 nono313 force-pushed the implement-fake-pwm-driver branch from 8c30a5b to 13e4d8a Compare December 12, 2024 13:02
@zephyrbot zephyrbot requested a review from rruuaanng December 12, 2024 13:03
@nono313 nono313 force-pushed the implement-fake-pwm-driver branch from 13e4d8a to 46969b7 Compare December 12, 2024 13:07
drivers/pwm/pwm_fake.c Outdated Show resolved Hide resolved
@nono313 nono313 force-pushed the implement-fake-pwm-driver branch from 46969b7 to 099f052 Compare December 12, 2024 14:59
pdgendt
pdgendt previously approved these changes Dec 12, 2024
anangl
anangl previously approved these changes Dec 12, 2024
drivers/pwm/pwm_fake.c Outdated Show resolved Hide resolved
@kartben
Copy link
Collaborator

kartben commented Dec 13, 2024

@nono313 looks like you now have CI errors :)

@nono313 nono313 dismissed stale reviews from anangl and pdgendt via 78d610e December 13, 2024 08:21
@nono313 nono313 force-pushed the implement-fake-pwm-driver branch from 099f052 to 78d610e Compare December 13, 2024 08:21
@nono313
Copy link
Contributor Author

nono313 commented Dec 13, 2024

@nono313 looks like you now have CI errors :)

Thanks for the notice, I started a discussion about it on Discord in #twister

implement fake-pwm driver with binding using fff

Signed-off-by: Nathan Olff <[email protected]>
create native_sim overlay to use fake pwm for test of pwm_api

Signed-off-by: Nathan Olff <[email protected]>
@nono313 nono313 force-pushed the implement-fake-pwm-driver branch from 78d610e to 8233d31 Compare December 13, 2024 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Devicetree area: PWM Pulse Width Modulation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants