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
This commit is contained in:
Artem Sheremet 2015-12-07 13:31:10 +01:00
parent ab68f61e91
commit f2af793fd7

17
rpio.go
View File

@ -262,12 +262,19 @@ func PullMode(pin Pin, pull Pull) {
// Some reflection magic is used to convert it to a unsafe []uint32 pointer // Some reflection magic is used to convert it to a unsafe []uint32 pointer
func Open() (err error) { func Open() (err error) {
var file *os.File var file *os.File
var base int64
// Open fd for rw mem access // Open fd for rw mem access; try gpiomem first
file, err = os.OpenFile( if file, err = os.OpenFile(
"/dev/mem", "/dev/gpiomem",
os.O_RDWR|os.O_SYNC, 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 { if err != nil {
return return
@ -282,7 +289,7 @@ func Open() (err error) {
// Memory map GPIO registers to byte array // Memory map GPIO registers to byte array
mem8, err = syscall.Mmap( mem8, err = syscall.Mmap(
int(file.Fd()), int(file.Fd()),
getGPIOBase(), base,
memLength, memLength,
syscall.PROT_READ|syscall.PROT_WRITE, syscall.PROT_READ|syscall.PROT_WRITE,
syscall.MAP_SHARED) syscall.MAP_SHARED)