Skip to content

Commit

Permalink
Move the setting of alternate functions for PWM pins to the PWM code …
Browse files Browse the repository at this point in the history
…where it belongs, rather than GPIO code.
  • Loading branch information
Jon-Bright committed Feb 21, 2021
1 parent a3350ab commit 3e49afc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions pixarray/ws281x.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ func NewWS281x(numPixels int, numColors int, order int, freq uint, dma int, pins
rp.FreeDMABuf(wa.pixDMA) // Ignore error
return nil, fmt.Errorf("couldn't init registers: %v", err)
}
err = rp.InitGPIO(pins)
err = rp.InitGPIO()
if err != nil {
rp.FreeDMABuf(wa.pixDMA) // Ignore error
return nil, fmt.Errorf("couldn't init GPIO: %v", err)
}
err = rp.InitPWM(freq, wa.pixDMA, bytes)
err = rp.InitPWM(freq, wa.pixDMA, bytes, pins)
if err != nil {
rp.FreeDMABuf(wa.pixDMA) // Ignore error
return nil, fmt.Errorf("couldn't init PWM: %v", err)
Expand Down
10 changes: 1 addition & 9 deletions rpi/gpio.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (rp *RPi) gpioFunctionSet(pin int, alt int) error {
return nil
}

func (rp *RPi) InitGPIO(pins []int) error {
func (rp *RPi) InitGPIO() error {
var (
bufOffs uintptr
err error
Expand All @@ -59,13 +59,5 @@ func (rp *RPi) InitGPIO(pins []int) error {
}
log.Printf("Got gpioBuf[%d], offset %d\n", len(rp.gpioBuf), bufOffs)
rp.gpio = (*gpioT)(unsafe.Pointer(&rp.gpioBuf[bufOffs]))

for channel, pin := range pins {
alt, ok := pwmPinToAlt[pwmPin{channel, pin}]
if !ok {
return fmt.Errorf("invalid pin %d for PWM channel %d", pin, channel)
}
rp.gpioFunctionSet(pin, alt)
}
return nil
}
10 changes: 9 additions & 1 deletion rpi/pwm.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,20 @@ func rpiDmaTiPerMap(val uint32) uint32 {
return (val & 0x1f) << 16
}

func (rp *RPi) InitPWM(freq uint, buf *DMABuf, bytes uint) error {
func (rp *RPi) InitPWM(freq uint, buf *DMABuf, bytes uint, pins []int) error {
oscFreq := uint32(OSC_FREQ)
if rp.hw.hwType == RPI_HWVER_TYPE_PI4 {
oscFreq = OSC_FREQ_PI4
}

for channel, pin := range pins {
alt, ok := pwmPinToAlt[pwmPin{channel, pin}]
if !ok {
return fmt.Errorf("invalid pin %d for PWM channel %d", pin, channel)
}
rp.gpioFunctionSet(pin, alt)
}

if rp.pwmBuf == nil {
var (
bufOffs uintptr
Expand Down

0 comments on commit 3e49afc

Please sign in to comment.