From ed5c36b35ae946b56f7b8194324a6cc474756b00 Mon Sep 17 00:00:00 2001 From: Walt Drummond Date: Thu, 24 Oct 2019 10:28:39 -0700 Subject: [PATCH 1/4] Updated to support Raspberry Pi 4 GPIO Base Address --- rpio.go | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/rpio.go b/rpio.go index 1049edf..13157c1 100644 --- a/rpio.go +++ b/rpio.go @@ -69,6 +69,7 @@ package rpio import ( "bytes" "encoding/binary" + "errors" "os" "reflect" "sync" @@ -714,23 +715,43 @@ func Close() error { // Read /proc/device-tree/soc/ranges and determine the base address. // Use the default Raspberry Pi 1 base address if this fails. -func getBase() (base int64) { - base = bcm2835Base +func readBase(offset int64) (int64, error) { ranges, err := os.Open("/proc/device-tree/soc/ranges") defer ranges.Close() if err != nil { - return + return 0, err } b := make([]byte, 4) - n, err := ranges.ReadAt(b, 4) + n, err := ranges.ReadAt(b, offset) if n != 4 || err != nil { - return + return 0, err } buf := bytes.NewReader(b) var out uint32 err = binary.Read(buf, binary.BigEndian, &out) if err != nil { - return + return 0, err } - return int64(out) + + if out == 0 { + return 0, errors.New("rpio: GPIO base address not found") + } + return int64(out), nil +} + +func getBase() int64 { + // Pi 2 & 3 GPIO base address is at offset 4 + b, err := readBase(4) + if err == nil { + return b + } + + // Pi 4 GPIO base address is as offset 8 + b, err = readBase(8) + if err == nil { + return b + } + + // Default to Pi 1 + return int64(bcm2835Base) } From 10d3b57bb64b6c5178bc16fb15f01ecc001f7d8a Mon Sep 17 00:00:00 2001 From: Walt Drummond Date: Wed, 27 Nov 2019 12:39:38 -0800 Subject: [PATCH 2/4] Updated Pull-up/Pull-down to support BCM2711 and PRi4 --- go.mod | 2 ++ rpio.go | 99 +++++++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 80 insertions(+), 21 deletions(-) diff --git a/go.mod b/go.mod index da5784d..b49f549 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,3 @@ module github.com/stianeikeland/go-rpio/v4 + +go 1.13 diff --git a/rpio.go b/rpio.go index 13157c1..44851ab 100644 --- a/rpio.go +++ b/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,32 +459,54 @@ func EdgeDetected(pin Pin) bool { } func PullMode(pin Pin, pull Pull) { - // 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) - case PullOff: + 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 + + switch pull { + case PullDown, PullUp: + gpioMem[pullReg] |= uint32(pull) + case PullOff: + gpioMem[pullReg] &^= 3 + } + + // Wait for value to clock in, this is ugly, sorry :( + time.Sleep(time.Microsecond) + + gpioMem[pullClkReg] = 1 << shift + + // Wait for value to clock in + time.Sleep(time.Microsecond) + gpioMem[pullReg] &^= 3 + gpioMem[pullClkReg] = 0 } - - // Wait for value to clock in, this is ugly, sorry :( - time.Sleep(time.Microsecond) - - gpioMem[pullClkReg] = 1 << shift - - // Wait for value to clock in - time.Sleep(time.Microsecond) - - 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 +} From be82a08f8a94f04c2c58939587eb1a7a3c0e47cd Mon Sep 17 00:00:00 2001 From: Walt Drummond Date: Thu, 28 Nov 2019 11:39:02 -0800 Subject: [PATCH 3/4] Expand example to cover new Pi 4 ReadPull() interface --- examples/pullup/pullup.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/pullup/pullup.go b/examples/pullup/pullup.go index e992004..58fc9c7 100644 --- a/examples/pullup/pullup.go +++ b/examples/pullup/pullup.go @@ -23,10 +23,10 @@ func main() { // Pull up and read value pin.PullUp() - fmt.Printf("PullUp: %d\n", pin.Read()) + fmt.Printf("PullUp: %d, %d\n", pin.Read(), pin.ReadPull()) // Pull down and read value pin.PullDown() - fmt.Printf("PullDown: %d\n", pin.Read()) + fmt.Printf("PullDown: %d, %d\n", pin.Read(), pin.ReadPull()) } From 9f58572951e7477c6852d19b075034f1dce66368 Mon Sep 17 00:00:00 2001 From: Walt Drummond Date: Thu, 28 Nov 2019 11:39:57 -0800 Subject: [PATCH 4/4] Support Pi3 and earlier boards --- rpio.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rpio.go b/rpio.go index 44851ab..522a517 100644 --- a/rpio.go +++ b/rpio.go @@ -98,7 +98,7 @@ const ( memLength = 4096 ) -// BCM 2711 has a differnet mechanism for pin pull-up/pull-down/enable +// BCM 2711 has a different mechanism for 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 @@ -145,6 +145,7 @@ const ( PullOff Pull = iota PullDown PullUp + PullNone ) // Edge events @@ -252,7 +253,7 @@ func (pin Pin) PullOff() { func (pin Pin) ReadPull() Pull { if !isBCM2711() { - return PullOff // TODO: Can you read pull state on other Pi boards? + return PullNone // Can't read pull-up/pull-down state on other Pi boards } reg := GPPUPPDN0 + (uint8(pin) >> 4) @@ -265,7 +266,7 @@ func (pin Pin) ReadPull() Pull { case 2: return PullDown default: - return PullOff + return PullNone // Invalid } }