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_nrf5_sw: Fix the driver and make it support nRF9x SoCs #15287

Closed

Conversation

anangl
Copy link
Member

@anangl anangl commented Apr 9, 2019

This PR makes the blink_led sample working (again) on boards with nRF52 series SoCs. The SW PWM driver is selected for them instead of the default HW one, as the latter cannot handle periods longer than ~262 ms (this is a hardware limitation).
It also fixes a couple of issues (details below) found in the pwm_nrf5_sw driver and makes it support nRF9x SoCs as well, so that the blink_led sample can be run on boards equipped with those.

drivers: pwm_nrf5_sw: Use dynamically adjusted timer prescaler

Adjust the timer prescaler used by the pwm_nrf5_sw driver according to
the requested period length, so that it does not need to be specified
at the build time. Remove the incorrect "scaling" that was done to the
requested pulse and period cycles to fit them into 16 bits.

samples: blink_led: Use nRF SW PWM driver for nRF52 boards

The nrfx Hardware PWM driver cannot be used in this sample
since the driver does not allow to set a period longer than ~262 ms.

drivers: pwm_nrf5_sw: Correct the use of GPIOTE channels and TIMER

GPIOTE base channel number taken from DeviceTree was not used
consistenly, hence the driver could properly work only when this
number had the value of 0.
TIMER was not stopped during the reconfiguration of the channel
and this could lead to the inversion of the PWM polarity.
The config structure was removed on the occasion as it unnecessarily
complicated the code of this currently single-instance only driver.

drivers: pwm_nrf5_sw: Use nrfx HALs instead of direct register accesses

Now instead of accessing peripheral registers directly, the driver
uses the nrfx HAL functions. This makes the code easier to maintain
and also gives the possibility to use pins from GPIO port P1 on
nRF52840 (absolute numbers 32-47) as the GPIO HAL provides the needed
translation.

drivers: pwm_nrf5_sw: Refactor the code

Move all channel initialization code that does not need to be executed
on every pin reconfiguration (like initialization of the PPI channels)
to the driver initialization function. Simplify the routines that check
the requested period and find a suitable channel for driving the pin.

drivers: pwm_nrf5_sw: Make the driver support nRF9x series SoCs as well

Now the driver uses DPPI or PPI depending on the presence of one of
these interfaces in a given SoC, hence it can be used on nRF9x SoCs
as well. Remove the '5' from its name then, and make the blink_led
sampe use it for the nrf9160_pca10090 board, thus making the sample
working properly on this board.

Fixes #15323.

@anangl anangl added bug The issue is a bug, or the PR is fixing a bug area: Drivers area: Boards platform: nRF Nordic nRFx labels Apr 9, 2019
@anangl anangl added this to the v1.14.0 milestone Apr 9, 2019
@zephyrbot
Copy link
Collaborator

zephyrbot commented Apr 9, 2019

All checks are passing now.

Review history of this comment for details about previous failed status.
Note that some checks might have not completed yet.

@anangl
Copy link
Member Author

anangl commented Apr 9, 2019

Fixed checkpatch and codeowners issues.

@ioannisg
Copy link
Member

ioannisg commented Apr 9, 2019

Is there a filed bug for this PR? Otherwise, it shouldn't go to 1.14 - we are close to release, AFAIK

@anangl
Copy link
Member Author

anangl commented Apr 9, 2019

Is there a filed bug for this PR? Otherwise, it shouldn't go to 1.14 - we are close to release, AFAIK

I am not aware of any. The bug was reported on slack:

Aaron Tsui 9:15 AM, March 22nd
BTW, As the PWM_CLK lowest is 125khz(16M/128) and COUNTERTOP max is 0x7fff on nrf52, my understanding the nrf52 HW PWM longest period is about 262ms, cannot reach period as 1000ms. right?
@gaute this will cause failure on sample/blink_led, which need a 1000ms period.

Sure. Could you file an issue in GH with "bug" and 1.14" that relates to the slack report?

@anangl
Copy link
Member Author

anangl commented Apr 9, 2019

cc @overheat

@overheat
Copy link
Contributor

overheat commented Apr 9, 2019

@anangl @ioannisg I didn't raise an issue specific for this topic. But I think this is related to #14749, which is verifying samples. Since this is a basic sample bug, I think it's better to be solved before the LTS version released.

@anangl
Copy link
Member Author

anangl commented Apr 10, 2019

Added one line that I somehow lost during splitting the changes into several logical commits. Sorry about that.

@anangl
Copy link
Member Author

anangl commented Apr 10, 2019

@ioannisg

Is there a filed bug for this PR? Otherwise, it shouldn't go to 1.14 - we are close to release, AFAIK

I am not aware of any. The bug was reported on slack:

[..]

Sure. Could you file an issue in GH with "bug" and 1.14" that relates to the slack report?

Created #15323.

@anangl
Copy link
Member Author

anangl commented Apr 11, 2019

Removed a no longer needed overlay file that was changing the clock-prescaler property eliminated by this PR.

@carlescufi
Copy link
Member

@cvinayak @kl-cruz @nordic-krch can you please take a look?

drivers/pwm/pwm_nrf_sw.c Outdated Show resolved Hide resolved
drivers/pwm/pwm_nrf_sw.c Show resolved Hide resolved
@henrikbrixandersen henrikbrixandersen removed their request for review June 21, 2019 18:04
@joerchan joerchan removed their request for review July 8, 2019 10:22
Adjust the timer prescaler used by the pwm_nrf5_sw driver according to
the requested period length, so that it does not need to be specified
at the build time. Remove the incorrect "scaling" that was done to the
requested pulse and period cycles to fit them into 16 bits.

Signed-off-by: Andrzej Głąbek <[email protected]>
Remove the no longer used "clock-prescaler" property from the bindings
for "nordic,nrf-sw-pwm" compatible nodes and all assignments to this
property. This means also the removal of the bbc_microbit.overlay file
from the servo_motor sample (as the assignment is the only purpose
of this file) together with the entry covering this file in CODEOWNERS.

Signed-off-by: Andrzej Głąbek <[email protected]>
GPIOTE base channel number taken from DeviceTree was not used
consistenly, hence the driver could properly work only when this
number had the value of 0.
TIMER was not stopped during the reconfiguration of the channel
and this could lead to the inversion of the PWM polarity.
The config structure was removed on the occasion as it unnecessarily
complicated the code of this currently single-instance only driver.

Signed-off-by: Andrzej Głąbek <[email protected]>
@zephyrbot zephyrbot added area: PWM Pulse Width Modulation area: Devicetree area: Bluetooth area: Samples Samples EXT Has change or related to ext/ (obsolete) labels Jul 12, 2019
@anangl
Copy link
Member Author

anangl commented Jul 12, 2019

Rebased on master.

@anangl anangl added the In progress For PRs: is work in progress and should not be merged yet. For issues: Is being worked on label Jul 12, 2019
@nordic-krch
Copy link
Contributor

@anangl is there any way to detect if there is conflict in TIMER instance usage?

Now instead of accessing peripheral registers directly, the driver
uses the nrfx HAL functions. This makes the code easier to maintain
and also gives the possibility to use pins from GPIO port P1 on
nRF52840 (absolute numbers 32-47) as the GPIO HAL provides the needed
translation.

Signed-off-by: Andrzej Głąbek <[email protected]>
Move all channel initialization code that does not need to be executed
on every pin reconfiguration (like initialization of the PPI channels)
to the driver initialization function. Simplify the routine that checks
the requested period and the one that finds a suitable channel for
driving the pin.

Signed-off-by: Andrzej Głąbek <[email protected]>
The nrfx Hardware PWM driver cannot be used in this sample
since the driver does not allow to set a period longer than ~262 ms.

The sw_pwm node is made enabled by default in dts for all nRF52 series
SoCs (just like it is already done for nRF51 series) so that there is
no need to do it for particular boards to make the SW PWM driver
available for them (this will only cause that the corresponding DT_
macros will be created, the driver will be compiled and used after
it is selected in Kconfig).

Signed-off-by: Andrzej Głąbek <[email protected]>
Now the driver uses DPPI or PPI depending on the presence of one of
these interfaces in a given SoC, hence it can be used on nRF9x SoCs
as well. Remove the '5' from its name then, and make the blink_led
sample using it for the nrf9160_pca10090 board, thus making the sample
working properly on this board.

Signed-off-by: Andrzej Głąbek <[email protected]>
This approach provides more flexibility (the PPI channels utilized by
this driver no longer need to have consecutive numbers) and eliminates
the need of certain hacks in nrfx_glue.h and radio_nrf5_ppi.h.

Signed-off-by: Andrzej Głąbek <[email protected]>
@anangl
Copy link
Member Author

anangl commented Nov 6, 2019

Replaced (as a fix for #15323) by #20369.

@anangl anangl closed this Nov 6, 2019
@anangl anangl deleted the fix_pwm_nrf_sw_driver branch March 11, 2022 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: API Changes to public APIs area: Bluetooth area: Boards area: Devicetree area: Drivers area: PWM Pulse Width Modulation area: Samples Samples bug The issue is a bug, or the PR is fixing a bug EXT Has change or related to ext/ (obsolete) In progress For PRs: is work in progress and should not be merged yet. For issues: Is being worked on platform: nRF Nordic nRFx
Projects
None yet
Development

Successfully merging this pull request may close these issues.

blink_led sample does not work on most of the nRF boards
9 participants