Skip to content

Commit

Permalink
linuxfs-pwm provider fixes for Pi5 operation. Default now pwmchip2, w…
Browse files Browse the repository at this point in the history
…as chip 0. After initial export of a channel, sleep quarter second so SSD card compllets directory and individual device file creation. Bug fix when user calls getActualFreq prior to setting PWM channel ON.
  • Loading branch information
taartspi committed Apr 1, 2024
1 parent 3cc68bc commit 7c2b149
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
4 changes: 3 additions & 1 deletion pi4j-core/src/main/java/com/pi4j/io/pwm/PwmBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ public Pwm initialize(Context context) throws InitializeException {
if(this.config.initialValue() != null){
try {
if(this.config.initialValue() <= 0){
this.off();
if(this.isOn()) {
this.off();
}
} else {
this.on(this.config.initialValue());
}
Expand Down
3 changes: 3 additions & 0 deletions pi4j-core/src/main/java/com/pi4j/util/Frequency.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public static float milliseconds(Number frequency){
*/
public static int getFrequencyFromNanos(Number nanoseconds){
int frequency;
if(nanoseconds.longValue() <= 0){
return(0);
}
long period = 1000000000; // NANOSECONDS PER SECOND;
frequency = Math.round(1000000000/nanoseconds.longValue());
return frequency;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public class LinuxPwm {
public static String DEFAULT_SYSTEM_PATH = "/sys/class/pwm";

/** Constant <code>DEFAULT_PWM_CHIP=0</code> */
public static int DEFAULT_PWM_CHIP = 0;
/** In Pi5 the chip is number 2 */
public static int DEFAULT_PWM_CHIP = 2;

protected final String systemPath;
protected final int chip;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,18 @@ public Pwm initialize(Context context) throws InitializeException {
if(!pwm.isExported()) {
logger.trace("exporting PWM [" + this.config.address() + "]; " + pwm.getPwmPath());
pwm.export();
// Delay to allow the SSD to persist the new directory and device partitions
Thread.sleep(250);
} else{
logger.trace("PWM [" + this.config.address() + "] is already exported; " + pwm.getPwmPath());
}
} catch (java.io.IOException e) {
logger.error(e.getMessage(), e);
throw new InitializeException("Unable to export PWM [" + config.address() + "] @ <" + pwm.systemPath() + ">; " + e.getMessage(), e);
}
} catch (InterruptedException e) {
logger.error(e.getMessage(), e);
throw new InitializeException("Programmed delay failure, unable to export PWM [" + config.address() + "] @ <" + pwm.systemPath() + ">; " + e.getMessage(), e);
}

// [INITIALIZE STATE] initialize PWM pin state (via superclass impl)
super.initialize(context);
Expand Down

0 comments on commit 7c2b149

Please sign in to comment.