mirror of
https://github.com/stianeikeland/go-rpio.git
synced 2025-01-23 02:31:05 +01:00
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:
parent
ab68f61e91
commit
f2af793fd7
17
rpio.go
17
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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user