From 9401acd0135e0b0779856a16560f61185ddf68f3 Mon Sep 17 00:00:00 2001 From: alsm Date: Tue, 9 Sep 2014 15:57:27 +0100 Subject: [PATCH] embd, rpi: determine pi revision, make rev2 pinmap default added a function to determine the revision of the pi the program is running on from "Revision" field in /proc/cpuinfo, also made the rev2 gpio pinmap the default --- detect.go | 18 ++++++++++++++++++ host/rpi/rpi.go | 6 +++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/detect.go b/detect.go index f0465fa..07cf61e 100644 --- a/detect.go +++ b/detect.go @@ -4,6 +4,7 @@ package embd import ( "fmt" + "io/ioutil" "os/exec" "strconv" "strings" @@ -76,6 +77,22 @@ func kernelVersion() (major, minor, patch int, err error) { return parseVersion(output) } +func getPiRevision() (int, error) { + //default return code of a rev2 board + cpuinfo, err := ioutil.ReadFile("/proc/cpuinfo") + if err != nil { + return 4, err + } + for _, line := range strings.Split(string(cpuinfo), "\n") { + fields := strings.Fields(line) + if len(fields) > 0 && fields[0] == "Revision" { + rev, err := strconv.ParseInt(fields[2], 16, 8) + return int(rev), err + } + } + return 4, nil +} + // DetectHost returns the detected host and its revision number. func DetectHost() (Host, int, error) { major, minor, patch, err := kernelVersion() @@ -98,6 +115,7 @@ func DetectHost() (Host, int, error) { switch node { case "raspberrypi": host = HostRPi + rev, _ = getPiRevision() case "beaglebone": host = HostBBB default: diff --git a/host/rpi/rpi.go b/host/rpi/rpi.go index 41db588..372835b 100644 --- a/host/rpi/rpi.go +++ b/host/rpi/rpi.go @@ -61,9 +61,9 @@ var ledMap = embd.LEDMap{ func init() { embd.Register(embd.HostRPi, func(rev int) *embd.Descriptor { - var pins = rev1Pins - if rev > 1 { - pins = rev2Pins + var pins = rev2Pins + if rev < 4 { + pins = rev1Pins } return &embd.Descriptor{