go-rpio/rpio.go

641 lines
15 KiB
Go
Raw Normal View History

/*
Package rpio provides GPIO access on the Raspberry PI without any need
2017-11-20 12:14:52 +01:00
for external c libraries (eg. WiringPi or BCM2835).
Supports simple operations such as:
2017-11-20 12:14:52 +01:00
- Pin mode/direction (input/output/clock/pwm)
- Pin write (high/low)
- Pin read (high/low)
- Pull up/down/off
And clock/pwm related oparations:
- Set Clock frequency
- Set Duty cycle
Example of use:
rpio.Open()
defer rpio.Close()
pin := rpio.Pin(4)
pin.Output()
for {
pin.Toggle()
time.Sleep(time.Second)
}
The library use the raw BCM2835 pinouts, not the ports as they are mapped
2017-11-20 12:14:52 +01:00
on the output pins for the raspberry pi, and not the wiringPi convention.
Rev 2 and 3 Raspberry Pi Rev 1 Raspberry Pi (legacy)
+-----+---------+----------+---------+-----+ +-----+--------+----------+--------+-----+
| BCM | Name | Physical | Name | BCM | | BCM | Name | Physical | Name | BCM |
+-----+---------+----++----+---------+-----+ +-----+--------+----++----+--------+-----+
| | 3.3v | 1 || 2 | 5v | | | | 3.3v | 1 || 2 | 5v | |
| 2 | SDA 1 | 3 || 4 | 5v | | | 0 | SDA | 3 || 4 | 5v | |
| 3 | SCL 1 | 5 || 6 | 0v | | | 1 | SCL | 5 || 6 | 0v | |
| 4 | GPIO 7 | 7 || 8 | TxD | 14 | | 4 | GPIO 7 | 7 || 8 | TxD | 14 |
| | 0v | 9 || 10 | RxD | 15 | | | 0v | 9 || 10 | RxD | 15 |
| 17 | GPIO 0 | 11 || 12 | GPIO 1 | 18 | | 17 | GPIO 0 | 11 || 12 | GPIO 1 | 18 |
| 27 | GPIO 2 | 13 || 14 | 0v | | | 21 | GPIO 2 | 13 || 14 | 0v | |
| 22 | GPIO 3 | 15 || 16 | GPIO 4 | 23 | | 22 | GPIO 3 | 15 || 16 | GPIO 4 | 23 |
| | 3.3v | 17 || 18 | GPIO 5 | 24 | | | 3.3v | 17 || 18 | GPIO 5 | 24 |
| 10 | MOSI | 19 || 20 | 0v | | | 10 | MOSI | 19 || 20 | 0v | |
| 9 | MISO | 21 || 22 | GPIO 6 | 25 | | 9 | MISO | 21 || 22 | GPIO 6 | 25 |
| 11 | SCLK | 23 || 24 | CE0 | 8 | | 11 | SCLK | 23 || 24 | CE0 | 8 |
| | 0v | 25 || 26 | CE1 | 7 | | | 0v | 25 || 26 | CE1 | 7 |
| 0 | SDA 0 | 27 || 28 | SCL 0 | 1 | +-----+--------+----++----+--------+-----+
| 5 | GPIO 21 | 29 || 30 | 0v | |
| 6 | GPIO 22 | 31 || 32 | GPIO 26 | 12 |
| 13 | GPIO 23 | 33 || 34 | 0v | |
| 19 | GPIO 24 | 35 || 36 | GPIO 27 | 16 |
| 26 | GPIO 25 | 37 || 38 | GPIO 28 | 20 |
| | 0v | 39 || 40 | GPIO 29 | 21 |
+-----+---------+----++----+---------+-----+
See the spec for full details of the BCM2835 controller:
2017-11-20 12:14:52 +01:00
https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/BCM2835-ARM-Peripherals.pdf
and https://elinux.org/BCM2835_datasheet_errata - for errors in that spec
2017-11-20 12:14:52 +01:00
*/
2013-07-30 16:12:13 +02:00
package rpio
import (
"bytes"
"encoding/binary"
"os"
"reflect"
"sync"
"syscall"
"time"
"unsafe"
)
2017-11-14 17:52:05 +01:00
type Mode uint8
type Pin uint8
type State uint8
type Pull uint8
2018-06-04 14:46:15 +02:00
type Edge uint8
// Memory offsets for gpio, see the spec for more details
const (
bcm2835Base = 0x20000000
2017-11-14 22:33:04 +01:00
gpioOffset = 0x200000
clkOffset = 0x101000
2017-11-17 09:37:18 +01:00
pwmOffset = 0x20C000
memLength = 4096
)
var (
gpioBase int64
2017-11-14 22:33:04 +01:00
clkBase int64
2017-11-17 09:37:18 +01:00
pwmBase int64
)
2017-11-14 22:33:04 +01:00
func init() {
base := getBase()
gpioBase = base + gpioOffset
clkBase = base + clkOffset
2017-11-17 09:37:18 +01:00
pwmBase = base + pwmOffset
}
2017-11-17 02:57:42 +01:00
// Pin mode, a pin can be set in Input or Output, Clock or Pwm mode
const (
2017-11-14 17:52:05 +01:00
Input Mode = iota
Output
2017-11-14 17:52:05 +01:00
Clock
2017-11-17 02:57:42 +01:00
Pwm
)
// State of pin, High / Low
const (
Low State = iota
High
)
// Pull Up / Down / Off
const (
PullOff Pull = iota
PullDown
PullUp
)
2018-06-04 14:46:15 +02:00
// Edge events
const (
2018-06-04 14:46:15 +02:00
NoEdge Edge = iota
RiseEdge
FallEdge
AnyEdge = RiseEdge | FallEdge
)
// Arrays for 8 / 32 bit access to memory and a semaphore for write locking
var (
2017-11-14 22:33:04 +01:00
memlock sync.Mutex
gpioMem []uint32
clkMem []uint32
2017-11-17 09:37:18 +01:00
pwmMem []uint32
2017-11-14 22:33:04 +01:00
gpioMem8 []uint8
clkMem8 []uint8
2017-11-17 09:37:18 +01:00
pwmMem8 []uint8
)
// Set pin as Input
func (pin Pin) Input() {
PinMode(pin, Input)
}
// Set pin as Output
func (pin Pin) Output() {
PinMode(pin, Output)
}
2017-11-14 23:16:39 +01:00
// Set pin as Clock
func (pin Pin) Clock() {
2017-11-14 17:52:05 +01:00
PinMode(pin, Clock)
}
2017-11-17 02:57:42 +01:00
// Set pin as Pwm
func (pin Pin) Pwm() {
PinMode(pin, Pwm)
}
// Set pin High
func (pin Pin) High() {
WritePin(pin, High)
}
// Set pin Low
func (pin Pin) Low() {
WritePin(pin, Low)
}
// Toggle pin state
func (pin Pin) Toggle() {
TogglePin(pin)
}
2017-11-20 10:22:14 +01:00
// Set frequency of Clock or Pwm pin (see doc of SetFreq)
2017-11-14 23:16:39 +01:00
func (pin Pin) Freq(freq int) {
SetFreq(pin, freq)
}
2017-11-20 10:22:14 +01:00
// Set duty cycle for Pwm pin (see doc of SetDutyCycle)
2017-11-19 17:23:48 +01:00
func (pin Pin) DutyCycle(dutyLen, cycleLen uint32) {
SetDutyCycle(pin, dutyLen, cycleLen)
}
2017-11-14 17:52:05 +01:00
// Set pin Mode
func (pin Pin) Mode(mode Mode) {
PinMode(pin, mode)
}
// Set pin state (high/low)
func (pin Pin) Write(state State) {
WritePin(pin, state)
}
// Read pin state (high/low)
func (pin Pin) Read() State {
return ReadPin(pin)
}
// Set a given pull up/down mode
func (pin Pin) Pull(pull Pull) {
PullMode(pin, pull)
}
// Pull up pin
func (pin Pin) PullUp() {
PullMode(pin, PullUp)
}
// Pull down pin
func (pin Pin) PullDown() {
PullMode(pin, PullDown)
}
// Disable pullup/down on pin
func (pin Pin) PullOff() {
PullMode(pin, PullOff)
}
2018-06-04 14:46:15 +02:00
// Enable edge event detection on pin
func (pin Pin) Detect(edge Edge) {
DetectEdge(pin, edge)
}
// Check edge event on pin
func (pin Pin) EdgeDetected() bool {
return EdgeDetected(pin)
}
2018-06-04 14:46:15 +02:00
2017-11-17 02:57:42 +01:00
// PinMode sets the mode (direction) of a given pin (Input, Output, Clock or Pwm)
2017-11-15 11:26:49 +01:00
//
2017-11-20 12:14:52 +01:00
// Clock is possible only for pins 4, 5, 6, 20, 21.
// Pwm is possible only for pins 12, 13, 18, 19.
2017-11-14 17:52:05 +01:00
func PinMode(pin Pin, mode Mode) {
// Pin fsel register, 0 or 1 depending on bank
2017-11-14 22:16:15 +01:00
fselReg := uint8(pin) / 10
shift := (uint8(pin) % 10) * 3
2017-11-14 17:52:05 +01:00
f := uint32(0)
2017-11-17 02:57:42 +01:00
const alt0 = 4 // 100
const alt5 = 2 // 010
2017-11-14 17:52:05 +01:00
switch mode {
case Input:
f = 0 // 000
case Output:
f = 1 // 001
case Clock:
switch pin {
case 4, 5, 6, 32, 34, 42, 43, 44:
2017-11-17 02:57:42 +01:00
f = alt0
2017-11-14 17:52:05 +01:00
case 20, 21:
2017-11-17 02:57:42 +01:00
f = alt5
default:
return
}
case Pwm:
switch pin {
case 12, 13, 40, 41, 45:
f = alt0
case 18, 19:
f = alt5
2017-11-14 17:52:05 +01:00
default:
return
2017-11-14 17:52:05 +01:00
}
}
2017-11-14 22:25:26 +01:00
memlock.Lock()
defer memlock.Unlock()
const pinMask = 7 // 0b111 - pinmode is 3 bits
2017-11-14 22:16:15 +01:00
gpioMem[fselReg] = (gpioMem[fselReg] &^ (pinMask << shift)) | (f << shift)
}
// WritePin sets a given pin High or Low
// by setting the clear or set registers respectively
func WritePin(pin Pin, state State) {
p := uint8(pin)
// Clear register, 10 / 11 depending on bank
// Set register, 7 / 8 depending on bank
clearReg := p/32 + 10
setReg := p/32 + 7
memlock.Lock()
2014-08-23 14:39:31 +02:00
defer memlock.Unlock()
if state == Low {
gpioMem[clearReg] = 1 << (p & 31)
} else {
gpioMem[setReg] = 1 << (p & 31)
}
}
2014-01-07 23:15:25 +01:00
// Read the state of a pin
func ReadPin(pin Pin) State {
// Input level register offset (13 / 14 depending on bank)
levelReg := uint8(pin)/32 + 13
2017-11-14 22:33:04 +01:00
if (gpioMem[levelReg] & (1 << uint8(pin&31))) != 0 {
return High
}
return Low
}
// Toggle a pin state (high -> low -> high)
// TODO: probably possible to do this much faster without read
func TogglePin(pin Pin) {
switch ReadPin(pin) {
case Low:
pin.High()
case High:
pin.Low()
}
}
2018-06-04 14:46:15 +02:00
// Enable edge event detection on pin
//
2018-06-04 14:46:15 +02:00
// Combine with pin.EdgeDetected() to check whether event occured.
//
2018-06-04 15:46:49 +02:00
// It also clears previously detected event of this pin if any.
//
// Note that call with RiseEdge will disable previously set FallEdge detection and vice versa
// You have to call with AnyEdge, to enable detection for both edges
// To disable previously enabled detection call it with NoEdge
2018-06-04 14:46:15 +02:00
func DetectEdge(pin Pin, edge Edge) {
p := uint8(pin)
// Rising edge detect enable register (19/20 depending on bank)
// Falling edge detect enable register (22/23 depending on bank)
// Event detect status register (16/17)
2018-06-04 14:46:15 +02:00
renReg := p/32 + 19
2018-06-04 16:32:46 +02:00
fenReg := p/32 + 22
edsReg := p/32 + 16
2018-06-04 14:46:15 +02:00
bit := uint32(1 << (p & 31))
if edge&RiseEdge > 0 { // set bit
gpioMem[renReg] = gpioMem[renReg] | bit
} else { // clear bit
gpioMem[renReg] = gpioMem[renReg] &^ bit
2018-06-04 14:46:15 +02:00
}
if edge&FallEdge > 0 { // set bit
gpioMem[fenReg] = gpioMem[fenReg] | bit
} else { // clear bit
gpioMem[fenReg] = gpioMem[fenReg] &^ bit
2018-06-04 14:46:15 +02:00
}
gpioMem[edsReg] = bit // to clear outdated detection
2018-06-04 14:46:15 +02:00
}
// Check whether edge event occured
//
// Event detection has to be enabled first, by pin.Detect(edge)
func EdgeDetected(pin Pin) bool {
p := uint8(pin)
// Event detect status register (16/17)
edsReg := p/32 + 16
test := gpioMem[edsReg] & (1 << (p & 31))
2018-06-04 14:46:15 +02:00
gpioMem[edsReg] = test // set bit to clear it
2018-06-04 16:32:46 +02:00
return test != 0
2018-06-04 14:46:15 +02:00
}
func PullMode(pin Pin, pull Pull) {
// Pull up/down/off register has offset 38 / 39, pull is 37
2017-11-19 17:23:48 +01:00
pullClkReg := pin/32 + 38
pullReg := 37
2017-11-19 17:23:48 +01:00
shift := pin % 32
memlock.Lock()
2014-08-23 14:39:31 +02:00
defer memlock.Unlock()
switch pull {
case PullDown, PullUp:
gpioMem[pullReg] = gpioMem[pullReg]&^3 | uint32(pull)
case PullOff:
gpioMem[pullReg] = gpioMem[pullReg] &^ 3
}
// Wait for value to clock in, this is ugly, sorry :(
time.Sleep(time.Microsecond)
gpioMem[pullClkReg] = 1 << shift
// Wait for value to clock in
time.Sleep(time.Microsecond)
gpioMem[pullReg] = gpioMem[pullReg] &^ 3
gpioMem[pullClkReg] = 0
}
2017-11-19 17:23:48 +01:00
// Set clock speed for given pin in Clock or Pwm mode
//
2017-11-20 10:22:14 +01:00
// Param freq should be in range 4688Hz - 19.2MHz to prevent unexpected behavior,
2017-11-20 12:14:52 +01:00
// however output frequency of Pwm pins can be further adjusted with SetDutyCycle.
// So for smaller frequencies use Pwm pin with large cycle range. (Or implement custom software clock using output pin and sleep.)
//
2017-11-14 22:16:15 +01:00
// Note that some pins share the same clock source, it means that
2017-11-20 12:14:52 +01:00
// changing frequency for one pin will change it also for all pins within a group.
// The groups are:
// gp_clk0: pins 4, 20, 32, 34
2018-06-04 14:46:15 +02:00
// gp_clk1: pins 5, 21, 42, 44
2017-11-20 12:14:52 +01:00
// gp_clk2: pins 6 and 43
// pwm_clk: pins 12, 13, 18, 19, 40, 41, 45
2017-11-14 23:16:39 +01:00
func SetFreq(pin Pin, freq int) {
2017-11-16 01:14:01 +01:00
// TODO: would be nice to choose best clock source depending on target frequency, oscilator is used for now
const sourceFreq = 19200000 // oscilator frequency
2017-11-19 17:23:48 +01:00
const divMask = 4095 // divi and divf have 12 bits each
2017-11-16 01:14:01 +01:00
divi := uint32(sourceFreq / freq)
divf := uint32(((sourceFreq % freq) << 12) / freq)
2017-11-19 17:23:48 +01:00
divi &= divMask
divf &= divMask
2017-11-16 01:12:13 +01:00
clkCtlReg := 28
2017-11-19 17:23:48 +01:00
clkDivReg := 28
switch pin {
2017-11-14 22:33:04 +01:00
case 4, 20, 32, 34: // clk0
clkCtlReg += 0
2017-11-19 17:23:48 +01:00
clkDivReg += 1
2017-11-14 22:33:04 +01:00
case 5, 21, 42, 44: // clk1
2017-11-16 01:12:13 +01:00
clkCtlReg += 2
2017-11-19 17:23:48 +01:00
clkDivReg += 3
2017-11-14 22:33:04 +01:00
case 6, 43: // clk2
2017-11-16 01:12:13 +01:00
clkCtlReg += 4
2017-11-19 17:23:48 +01:00
clkDivReg += 5
2017-11-20 10:22:14 +01:00
case 12, 13, 40, 41, 45, 18, 19: // pwm_clk - shared clk for both pwm channels
2017-11-19 17:23:48 +01:00
clkCtlReg += 12
clkDivReg += 13
2017-11-20 09:29:43 +01:00
StopPwm() // pwm clk busy wont go down without stopping pwm first
2017-11-19 17:23:48 +01:00
defer StartPwm()
2017-11-14 22:33:04 +01:00
default:
return
}
2017-11-16 01:14:01 +01:00
mash := uint32(1 << 9) // 1-stage MASH
if divi < 2 || divf == 0 {
mash = 0
}
memlock.Lock()
defer memlock.Unlock()
const PASSWORD = 0x5A000000
2017-11-16 01:14:01 +01:00
const busy = 1 << 7
const enab = 1 << 4
const src = 1 << 0 // oscilator
2017-11-16 01:47:08 +01:00
clkMem[clkCtlReg] = PASSWORD | (clkMem[clkCtlReg] &^ enab) // stop gpio clock (without changing src or mash)
2017-11-14 22:33:04 +01:00
for clkMem[clkCtlReg]&busy != 0 {
2017-11-16 01:14:01 +01:00
time.Sleep(time.Microsecond * 10)
} // ... and wait for not busy
2017-11-16 01:14:01 +01:00
clkMem[clkCtlReg] = PASSWORD | mash | src // set mash and source (without enabling clock)
2017-11-14 22:16:15 +01:00
clkMem[clkDivReg] = PASSWORD | (divi << 12) | divf // set dividers
2017-11-16 01:14:01 +01:00
// mash and src can not be changed in same step as enab, to prevent lock-up and glitches
time.Sleep(time.Microsecond * 10) // ... so wait for them to take effect
clkMem[clkCtlReg] = PASSWORD | mash | src | enab // finally start clock
// NOTE without root permission this changes will simply do nothing successfully
}
2017-11-20 12:14:52 +01:00
// Set cycle length (range) and duty length (data) for Pwm pin in M/S mode
2017-11-19 17:23:48 +01:00
//
2017-11-20 10:22:14 +01:00
// |<- duty ->|
// __________
// _/ \_____________/
// |<------- cycle -------->|
2017-11-19 17:23:48 +01:00
//
2017-11-20 12:14:52 +01:00
// Output frequency is computed as pwm clock frequency divided by cycle length.
// So, to set Pwm pin to freqency 38kHz with duty cycle 1/4, use this combination:
2017-11-20 10:22:14 +01:00
//
2017-11-20 12:14:52 +01:00
// pin.Pwm()
2017-11-20 10:22:14 +01:00
// pin.DutyCycle(1, 4)
// pin.Freq(38000*4)
//
// Note that some pins share common pwm channel,
2017-11-20 12:14:52 +01:00
// so calling this function will set same duty cycle for all pins belonging to channel.
// The channels are:
// channel 1 (pwm0) for pins 12, 18, 40
// channel 2 (pwm1) for pins 13, 19, 41, 45.
2017-11-19 17:23:48 +01:00
func SetDutyCycle(pin Pin, dutyLen, cycleLen uint32) {
const pwmCtlReg = 0
var (
pwmDatReg uint
pwmRngReg uint
shift uint // offset inside ctlReg
)
switch pin {
2017-11-20 10:22:14 +01:00
case 12, 18, 40: // channel pwm0
2017-11-20 09:29:43 +01:00
pwmRngReg = 4
pwmDatReg = 5
2017-11-19 17:23:48 +01:00
shift = 0
2017-11-20 10:22:14 +01:00
case 13, 19, 41, 45: // channel pwm1
2017-11-20 09:29:43 +01:00
pwmRngReg = 8
2017-11-19 17:23:48 +01:00
pwmDatReg = 9
shift = 8
default:
return
}
2017-11-20 10:22:14 +01:00
const ctlMask = 255 // ctl setting has 8 bits for each channel
2017-11-20 09:29:43 +01:00
const pwen = 1 << 0 // enable pwm
2017-11-19 17:23:48 +01:00
const msen = 1 << 7 // use M/S transition instead of pwm algorithm
// reset settings
2018-06-04 14:46:15 +02:00
pwmMem[pwmCtlReg] = pwmMem[pwmCtlReg]&^(ctlMask<<shift) | msen<<shift | pwen<<shift
2017-11-19 17:23:48 +01:00
// set duty cycle
pwmMem[pwmDatReg] = dutyLen
pwmMem[pwmRngReg] = cycleLen
time.Sleep(time.Microsecond * 10)
2017-11-20 09:29:43 +01:00
// NOTE without root permission this changes will simply do nothing successfully
2017-11-19 17:23:48 +01:00
}
2017-11-20 10:22:14 +01:00
// Stop pwm for both channels
2017-11-19 17:23:48 +01:00
func StopPwm() {
const pwmCtlReg = 0
const pwen = 1
pwmMem[pwmCtlReg] = pwmMem[pwmCtlReg] &^ (pwen<<8 | pwen)
}
2017-11-20 10:22:14 +01:00
// Start pwm for both channels
2017-11-19 17:23:48 +01:00
func StartPwm() {
const pwmCtlReg = 0
const pwen = 1
pwmMem[pwmCtlReg] = pwmMem[pwmCtlReg] | pwen<<8 | pwen
}
// Open and memory map GPIO memory range from /dev/mem .
// Some reflection magic is used to convert it to a unsafe []uint32 pointer
func Open() (err error) {
var file *os.File
// Open fd for rw mem access; try dev/mem first (need root)
file, err = os.OpenFile("/dev/mem", os.O_RDWR|os.O_SYNC, 0)
2017-11-17 09:37:18 +01:00
if os.IsPermission(err) { // try gpiomem otherwise (some extra functions like clock and pwm setting wont work)
file, err = os.OpenFile("/dev/gpiomem", os.O_RDWR|os.O_SYNC, 0)
}
if err != nil {
return
}
// FD can be closed after memory mapping
defer file.Close()
memlock.Lock()
2014-08-23 14:39:31 +02:00
defer memlock.Unlock()
// Memory map GPIO registers to slice
2017-11-14 22:16:15 +01:00
gpioMem, gpioMem8, err = memMap(file.Fd(), gpioBase)
if err != nil {
return
}
2018-01-12 15:45:34 +01:00
// Memory map clock registers to slice
2017-11-14 22:16:15 +01:00
clkMem, clkMem8, err = memMap(file.Fd(), clkBase)
if err != nil {
return
}
2018-01-12 15:45:34 +01:00
// Memory map pwm registers to slice
2017-11-20 09:29:43 +01:00
pwmMem, pwmMem8, err = memMap(file.Fd(), pwmBase)
2017-11-17 09:37:18 +01:00
if err != nil {
return
}
return nil
}
func memMap(fd uintptr, base int64) (mem []uint32, mem8 []byte, err error) {
mem8, err = syscall.Mmap(
int(fd),
base,
memLength,
syscall.PROT_READ|syscall.PROT_WRITE,
2017-11-14 17:06:00 +01:00
syscall.MAP_SHARED,
)
if err != nil {
return
}
// Convert mapped byte memory to unsafe []uint32 pointer, adjust length as needed
header := *(*reflect.SliceHeader)(unsafe.Pointer(&mem8))
header.Len /= (32 / 8) // (32 bit = 4 bytes)
header.Cap /= (32 / 8)
mem = *(*[]uint32)(unsafe.Pointer(&header))
return
}
// Close unmaps GPIO memory
2014-08-23 14:39:31 +02:00
func Close() error {
memlock.Lock()
2014-08-23 14:39:31 +02:00
defer memlock.Unlock()
2017-11-14 22:33:04 +01:00
if err := syscall.Munmap(gpioMem8); err != nil {
2017-11-14 22:25:26 +01:00
return err
}
2017-11-14 22:33:04 +01:00
if err := syscall.Munmap(clkMem8); err != nil {
2017-11-14 22:25:26 +01:00
return err
}
2018-01-12 15:45:34 +01:00
if err := syscall.Munmap(pwmMem8); err != nil {
return err
}
return nil
}
2015-03-08 03:54:44 +01:00
// Read /proc/device-tree/soc/ranges and determine the base address.
// Use the default Raspberry Pi 1 base address if this fails.
func getBase() (base int64) {
base = bcm2835Base
ranges, err := os.Open("/proc/device-tree/soc/ranges")
defer ranges.Close()
if err != nil {
return
}
b := make([]byte, 4)
n, err := ranges.ReadAt(b, 4)
if n != 4 || err != nil {
return
}
buf := bytes.NewReader(b)
2015-03-08 03:53:00 +01:00
var out uint32
err = binary.Read(buf, binary.BigEndian, &out)
if err != nil {
return
}
return int64(out)
}