mirror of
https://github.com/stianeikeland/go-rpio.git
synced 2025-01-23 02:31:05 +01:00
Updated Pull-up/Pull-down to support BCM2711 and PRi4
This commit is contained in:
parent
ed5c36b35a
commit
10d3b57bb6
65
rpio.go
65
rpio.go
|
@ -63,6 +63,8 @@ See the spec for full details of the BCM2835 controller:
|
|||
https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/BCM2835-ARM-Peripherals.pdf
|
||||
and https://elinux.org/BCM2835_datasheet_errata - for errors in that spec
|
||||
|
||||
Changes to support the BCM2711, used on the Raspberry Pi 4, were cribbed from https://github.com/RPi-Distro/raspi-gpio/
|
||||
|
||||
*/
|
||||
package rpio
|
||||
|
||||
|
@ -96,6 +98,14 @@ const (
|
|||
memLength = 4096
|
||||
)
|
||||
|
||||
// BCM 2711 has a differnet mechanism for pin pull-up/pull-down/enable
|
||||
const (
|
||||
GPPUPPDN0 = 57 // Pin pull-up/down for pins 15:0
|
||||
GPPUPPDN1 = 58 // Pin pull-up/down for pins 31:16
|
||||
GPPUPPDN2 = 59 // Pin pull-up/down for pins 47:32
|
||||
GPPUPPDN3 = 60 // Pin pull-up/down for pins 57:48
|
||||
)
|
||||
|
||||
var (
|
||||
gpioBase int64
|
||||
clkBase int64
|
||||
|
@ -240,6 +250,25 @@ func (pin Pin) PullOff() {
|
|||
PullMode(pin, PullOff)
|
||||
}
|
||||
|
||||
func (pin Pin) ReadPull() Pull {
|
||||
if !isBCM2711() {
|
||||
return PullOff // TODO: Can you read pull state on other Pi boards?
|
||||
}
|
||||
|
||||
reg := GPPUPPDN0 + (uint8(pin) >> 4)
|
||||
bits := gpioMem[reg] >> ((uint8(pin) & 0xf) << 1) & 0x3
|
||||
switch bits {
|
||||
case 0:
|
||||
return PullOff
|
||||
case 1:
|
||||
return PullUp
|
||||
case 2:
|
||||
return PullDown
|
||||
default:
|
||||
return PullOff
|
||||
}
|
||||
}
|
||||
|
||||
// Detect: Enable edge event detection on pin
|
||||
func (pin Pin) Detect(edge Edge) {
|
||||
DetectEdge(pin, edge)
|
||||
|
@ -430,14 +459,36 @@ func EdgeDetected(pin Pin) bool {
|
|||
}
|
||||
|
||||
func PullMode(pin Pin, pull Pull) {
|
||||
|
||||
memlock.Lock()
|
||||
defer memlock.Unlock()
|
||||
|
||||
if isBCM2711() {
|
||||
pullreg := GPPUPPDN0 + (pin >> 4)
|
||||
pullshift := (pin & 0xf) << 1
|
||||
|
||||
var p uint32
|
||||
|
||||
switch pull {
|
||||
case PullOff:
|
||||
p = 0
|
||||
case PullUp:
|
||||
p = 1
|
||||
case PullDown:
|
||||
p = 2;
|
||||
}
|
||||
|
||||
// This is verbatim C code from raspi-gpio.c
|
||||
pullbits := gpioMem[pullreg]
|
||||
pullbits &= ^(3 << pullshift)
|
||||
pullbits |= (p << pullshift)
|
||||
gpioMem[pullreg]= pullbits
|
||||
} else {
|
||||
// Pull up/down/off register has offset 38 / 39, pull is 37
|
||||
pullClkReg := pin/32 + 38
|
||||
pullReg := 37
|
||||
shift := pin % 32
|
||||
|
||||
memlock.Lock()
|
||||
defer memlock.Unlock()
|
||||
|
||||
switch pull {
|
||||
case PullDown, PullUp:
|
||||
gpioMem[pullReg] |= uint32(pull)
|
||||
|
@ -455,7 +506,7 @@ func PullMode(pin Pin, pull Pull) {
|
|||
|
||||
gpioMem[pullReg] &^= 3
|
||||
gpioMem[pullClkReg] = 0
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// SetFreq: Set clock speed for given pin in Clock or Pwm mode
|
||||
|
@ -755,3 +806,9 @@ func getBase() int64 {
|
|||
// Default to Pi 1
|
||||
return int64(bcm2835Base)
|
||||
}
|
||||
|
||||
// The Pi 4 uses a BCM 2711, which has different register offsets and base addresses than the rest of the Pi family (so far). This
|
||||
// helper function checks if we're on a 2711 and hence a Pi 4
|
||||
func isBCM2711() bool {
|
||||
return gpioMem[GPPUPPDN3] != 0x6770696f
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user