mirror of
https://github.com/stianeikeland/go-rpio.git
synced 2025-01-23 02:31:05 +01:00
Merge pull request #50 from wfd3/master
Updated to support Raspberry Pi 4 GPIO Base Address
This commit is contained in:
commit
b62d4bed37
|
@ -23,10 +23,10 @@ func main() {
|
||||||
|
|
||||||
// Pull up and read value
|
// Pull up and read value
|
||||||
pin.PullUp()
|
pin.PullUp()
|
||||||
fmt.Printf("PullUp: %d\n", pin.Read())
|
fmt.Printf("PullUp: %d, %d\n", pin.Read(), pin.ReadPull())
|
||||||
|
|
||||||
// Pull down and read value
|
// Pull down and read value
|
||||||
pin.PullDown()
|
pin.PullDown()
|
||||||
fmt.Printf("PullDown: %d\n", pin.Read())
|
fmt.Printf("PullDown: %d, %d\n", pin.Read(), pin.ReadPull())
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1 +1,3 @@
|
||||||
module github.com/stianeikeland/go-rpio/v4
|
module github.com/stianeikeland/go-rpio/v4
|
||||||
|
|
||||||
|
go 1.13
|
||||||
|
|
135
rpio.go
135
rpio.go
|
@ -63,12 +63,15 @@ See the spec for full details of the BCM2835 controller:
|
||||||
https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/BCM2835-ARM-Peripherals.pdf
|
https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/BCM2835-ARM-Peripherals.pdf
|
||||||
and https://elinux.org/BCM2835_datasheet_errata - for errors in that spec
|
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
|
package rpio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -95,6 +98,14 @@ const (
|
||||||
memLength = 4096
|
memLength = 4096
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 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
|
||||||
|
GPPUPPDN2 = 59 // Pin pull-up/down for pins 47:32
|
||||||
|
GPPUPPDN3 = 60 // Pin pull-up/down for pins 57:48
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
gpioBase int64
|
gpioBase int64
|
||||||
clkBase int64
|
clkBase int64
|
||||||
|
@ -134,6 +145,7 @@ const (
|
||||||
PullOff Pull = iota
|
PullOff Pull = iota
|
||||||
PullDown
|
PullDown
|
||||||
PullUp
|
PullUp
|
||||||
|
PullNone
|
||||||
)
|
)
|
||||||
|
|
||||||
// Edge events
|
// Edge events
|
||||||
|
@ -239,6 +251,25 @@ func (pin Pin) PullOff() {
|
||||||
PullMode(pin, PullOff)
|
PullMode(pin, PullOff)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pin Pin) ReadPull() Pull {
|
||||||
|
if !isBCM2711() {
|
||||||
|
return PullNone // Can't read pull-up/pull-down 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 PullNone // Invalid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Detect: Enable edge event detection on pin
|
// Detect: Enable edge event detection on pin
|
||||||
func (pin Pin) Detect(edge Edge) {
|
func (pin Pin) Detect(edge Edge) {
|
||||||
DetectEdge(pin, edge)
|
DetectEdge(pin, edge)
|
||||||
|
@ -429,32 +460,54 @@ func EdgeDetected(pin Pin) bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func PullMode(pin Pin, pull Pull) {
|
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()
|
memlock.Lock()
|
||||||
defer memlock.Unlock()
|
defer memlock.Unlock()
|
||||||
|
|
||||||
switch pull {
|
if isBCM2711() {
|
||||||
case PullDown, PullUp:
|
pullreg := GPPUPPDN0 + (pin >> 4)
|
||||||
gpioMem[pullReg] |= uint32(pull)
|
pullshift := (pin & 0xf) << 1
|
||||||
case PullOff:
|
|
||||||
|
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[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
|
// SetFreq: Set clock speed for given pin in Clock or Pwm mode
|
||||||
|
@ -714,23 +767,49 @@ func Close() error {
|
||||||
|
|
||||||
// Read /proc/device-tree/soc/ranges and determine the base address.
|
// Read /proc/device-tree/soc/ranges and determine the base address.
|
||||||
// Use the default Raspberry Pi 1 base address if this fails.
|
// Use the default Raspberry Pi 1 base address if this fails.
|
||||||
func getBase() (base int64) {
|
func readBase(offset int64) (int64, error) {
|
||||||
base = bcm2835Base
|
|
||||||
ranges, err := os.Open("/proc/device-tree/soc/ranges")
|
ranges, err := os.Open("/proc/device-tree/soc/ranges")
|
||||||
defer ranges.Close()
|
defer ranges.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return 0, err
|
||||||
}
|
}
|
||||||
b := make([]byte, 4)
|
b := make([]byte, 4)
|
||||||
n, err := ranges.ReadAt(b, 4)
|
n, err := ranges.ReadAt(b, offset)
|
||||||
if n != 4 || err != nil {
|
if n != 4 || err != nil {
|
||||||
return
|
return 0, err
|
||||||
}
|
}
|
||||||
buf := bytes.NewReader(b)
|
buf := bytes.NewReader(b)
|
||||||
var out uint32
|
var out uint32
|
||||||
err = binary.Read(buf, binary.BigEndian, &out)
|
err = binary.Read(buf, binary.BigEndian, &out)
|
||||||
if err != nil {
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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