Skip to content

Bring clear instructions and parameters for PWM usageΒ #551

@FDelporte

Description

@FDelporte

It's probably me, but I find PWM the hardest IO Type to use... It's unclear how to map GPIO pin numbers to chip/channel parameters for hardware PWM, how to configure on different types of RPi's, etc.

Current documentation: https://www.pi4j.com/documentation/io-types/pwm/

I guess this can be fixed by extending the documentation and parameters in the following way:

Hardware PWM

Only a limited number of hardware-accelerated PWMs are available. These can be checked with:

$ ls -l /sys/class/pwm/
pwmchip0

$ pinctrl | grep PWM
18: a3    pd | lo // GPIO18 = PWM0_CHAN2
19: a3    pd | lo // GPIO19 = PWM0_CHAN3
45: op dh pd | hi // FAN_PWM/GPIO45 = output

Implementation

  • Parameters should be chip and channel
  • The plugin PWM provider must throw an error if chip and channel are not provided with PwmType.HARDWARE
  • The plugin PWM provider should log a warning if a bcm is provided with PwmType.HARDWARE
  • Implementation changes needed in the plugins to allow this usage
  • @DigitalSmile can we add com.pi4j.plugin.ffm.providers.pwm.PwmFFMSoftware ?
# Chip can be retrieved with a helper method
# This returns 0 for non-RP1 (pre-RPi 5) and correct number for RP1 (RPi 5)
var chip = PwmChipUtil.getPWMChip();

# Example based on the output above for `GPIO18 = PWM0_CHAN2`
var pwmConfig = Pwm.newConfigBuilder(pi4j)
    .pwmType(PwmType.HARDWARE)
    .chip(chip)
    .channel(2)
    .build();

Documentation

  • Describe how to enable, configure, and use HARDWARE PWM with chip and channel on different RPi models
  • Create JBang example
  • Document what happens if you try to use hardware PWM on a non-hardware PWM pin
  • Document frequency/duty cycle usage and limitations

Software PWM

Software PWM is implemented entirely in code, and it can generate PWM signals on any pin that supports general-purpose output. But it has less accurate timing due to CPU load and OS scheduling.

Implementation

  • Parameters should be bcm
  • The plugin PWM provider throw an error if ibcm is not provided with PwmType.SOFTWARE
  • The plugin PWM provider should log a warning if anything else than bcm is provided with PwmType.SOFTWARE
  • Implementation changes needed in the plugins to allow this usage
# Example based on the output above for `GPIO18 = PWM0_CHAN2`
var pwmConfig = Pwm.newConfigBuilder(pi4j)
    .pwmType(PwmType.SOFTWARE)
    .bcm(18)
    .build();

Documentation

  • Describe how to use SOFTWARE PWM with bcm
  • Create JBang example

Metadata

Metadata

Assignees

Labels

ffmFFM API Plugin

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions