mirror of
https://github.com/stianeikeland/go-rpio.git
synced 2025-02-02 15:30:36 +01:00
Add conversion from physical PIN to BCM notation.
On Open() detect the board model and configure how physical PINs are mapped to GPIO pin numbers [1]. Model detection is based on content of /sys/firmware/devicetree/base/model [2] . If you are running a weird or very old system which does not have the file, GetBoardPin() won't work. [1] https://pinout.xyz/ [2] https://www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/README.md
This commit is contained in:
parent
a36b96d0b1
commit
904f4f9711
10
README.md
10
README.md
|
@ -61,6 +61,16 @@ pin.Mode(rpio.Output) // Alternative syntax
|
||||||
pin.Write(rpio.High) // Alternative syntax
|
pin.Write(rpio.High) // Alternative syntax
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If you prefer using physical pin numbers instead of BCM notation, call GetBoardPin():
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Physical pin 19 is mapped to BCM pin 10, so this two lines are quivalent
|
||||||
|
|
||||||
|
pin := rpio.GetBoardPin(19)
|
||||||
|
pin := rpio.Pin(10)
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
Pull up/down/off can be set using:
|
Pull up/down/off can be set using:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|
44
rpio.go
44
rpio.go
|
@ -75,6 +75,8 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
"io/ioutil"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Mode uint8
|
type Mode uint8
|
||||||
|
@ -625,6 +627,38 @@ func backupIRQs() {
|
||||||
irqsBackup = uint64(intrMem[irqEnable2])<<32 | uint64(intrMem[irqEnable1])
|
irqsBackup = uint64(intrMem[irqEnable2])<<32 | uint64(intrMem[irqEnable1])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var pin_to_gpio_rev1 = [26]int {-1, -1, 0, -1, 1, -1, 4, 14, -1, 15, 17, 18, 21, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7};
|
||||||
|
var pin_to_gpio_rev2 = [40]int {-1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17, 18, 27, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
|
||||||
|
var pin_to_gpio_rev3 = [40]int {-1, -1, 2, -1, 3, -1, 4, 14, -1, 15, 17, 18, 27, -1, 22, 23, -1, 24, 10, -1, 9, 25, 11, 8, -1, 7, -1, -1, 5, -1, 6, 12, 13, -1, 19, 16, 26, 20, -1, 21 };
|
||||||
|
|
||||||
|
var pin_to_gpio []int = nil;
|
||||||
|
func initGPIOconversion () (error) {
|
||||||
|
file, err := os.Open("/sys/firmware/devicetree/base/model")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
model, err := ioutil.ReadAll(file)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch string(bytes.TrimRight(model, "\000")) {
|
||||||
|
case "Raspberry Pi Model B Rev 1" :
|
||||||
|
pin_to_gpio = pin_to_gpio_rev1[0:26];
|
||||||
|
case "Raspberry Pi Model B Rev 2" :
|
||||||
|
pin_to_gpio = pin_to_gpio_rev2[0:40];
|
||||||
|
case "Raspberry Pi Model A Rev 2" :
|
||||||
|
pin_to_gpio = pin_to_gpio_rev2[0:40];
|
||||||
|
default :
|
||||||
|
pin_to_gpio = pin_to_gpio_rev3[0:40];
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Open and memory map GPIO memory range from /dev/mem .
|
// Open and memory map GPIO memory range from /dev/mem .
|
||||||
// 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) {
|
||||||
|
@ -676,6 +710,7 @@ func Open() (err error) {
|
||||||
|
|
||||||
backupIRQs() // back up enabled IRQs, to restore it later
|
backupIRQs() // back up enabled IRQs, to restore it later
|
||||||
|
|
||||||
|
initGPIOconversion()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -734,3 +769,12 @@ func getBase() (base int64) {
|
||||||
}
|
}
|
||||||
return int64(out)
|
return int64(out)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert GPIO number from physical PIN to BCM convention
|
||||||
|
func GetBoardPin(board_pin int) (Pin, error) {
|
||||||
|
if board_pin < 1 || len(pin_to_gpio) < board_pin || pin_to_gpio[board_pin - 1] == -1 {
|
||||||
|
return Pin(0xff), fmt.Errorf("Pin %d is not wired", board_pin)
|
||||||
|
}
|
||||||
|
|
||||||
|
return Pin(pin_to_gpio[board_pin - 1]), nil
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user