Skip to content

Commit

Permalink
[RSDK-4296] sleep to avoid race condition in odroid's sysfs implement…
Browse files Browse the repository at this point in the history
…ation (viamrobotics#2717)

Tried on an Odroid C4: without this, the kernel spews a bunch of errors and the whole process locks up (in a way that cannot be quit with control-C or backgrounded with control-Z, since it's stuck in kernelspace). With this, hardware PWM works normally.

I strongly dislike sleeping to avoid race conditions, but this one isn't easily fixable without a sleep, since it has to do with how quickly things are happening in kernel space and we don't have control over which kernel our users are running.
  • Loading branch information
penguinland authored Aug 1, 2023
1 parent b541721 commit fdff22e
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions components/board/genericlinux/hw_pwm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"os"
"sync"
"time"

"github.com/edaniels/golog"
"github.com/pkg/errors"
Expand Down Expand Up @@ -87,6 +88,11 @@ func (pwm *pwmDevice) unexport() error {
// device results in an error. We don't care if there's an error: it should be disabled no
// matter what.
goutils.UncheckedError(pwm.disable())

// On boards like the Odroid C4, there is a race condition in the kernel where, if you unexport
// the pin too quickly after changing something else about it (e.g., disabling it), the whole
// PWM system gets corrupted. Sleep for a small amount of time to avoid this.
time.Sleep(time.Microsecond)
if err := pwm.writeChip("unexport", uint64(pwm.line)); err != nil {
return err
}
Expand Down

0 comments on commit fdff22e

Please sign in to comment.