From f2af793fd715dbe2b267b72f23a0bde06c77920e Mon Sep 17 00:00:00 2001 From: Artem Sheremet Date: Mon, 7 Dec 2015 13:31:10 +0100 Subject: [PATCH] Use /dev/gpiomem (with offset=0), if available /dev/gpiomem is a map of GPIO-related memory region. No longer need root for user GPIO access, but without relaxing permissions on /dev/mem. See https://github.com/raspberrypi/linux/pull/1112/files --- rpio.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/rpio.go b/rpio.go index 12cf978..5fdc5dd 100644 --- a/rpio.go +++ b/rpio.go @@ -262,12 +262,19 @@ func PullMode(pin Pin, pull Pull) { // Some reflection magic is used to convert it to a unsafe []uint32 pointer func Open() (err error) { var file *os.File + var base int64 - // Open fd for rw mem access - file, err = os.OpenFile( - "/dev/mem", + // Open fd for rw mem access; try gpiomem first + if file, err = os.OpenFile( + "/dev/gpiomem", os.O_RDWR|os.O_SYNC, - 0) + 0); os.IsNotExist(err) { + file, err = os.OpenFile( + "/dev/mem", + os.O_RDWR|os.O_SYNC, + 0) + base = getGPIOBase() + } if err != nil { return @@ -282,7 +289,7 @@ func Open() (err error) { // Memory map GPIO registers to byte array mem8, err = syscall.Mmap( int(file.Fd()), - getGPIOBase(), + base, memLength, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED)