From 842c7d503f02a30cc3f96ec3c396965bd9574ab3 Mon Sep 17 00:00:00 2001 From: fenglei Date: Fri, 31 Jul 2020 17:28:37 +0800 Subject: [PATCH] =?UTF-8?q?add=20func=20ReadMode=E3=80=81ReadPinMode?= =?UTF-8?q?=E3=80=82=20get=20gpio=20mod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rpio.go | 56 ++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/rpio.go b/rpio.go index ea3962b..944dac1 100644 --- a/rpio.go +++ b/rpio.go @@ -100,10 +100,10 @@ const ( // BCM 2711 has a different mechanism for pull-up/pull-down/enable const ( - GPPUPPDN0 = 57 // Pin pull-up/down for pins 15:0 - GPPUPPDN1 = 58 // Pin pull-up/down for pins 31:16 - GPPUPPDN2 = 59 // Pin pull-up/down for pins 47:32 - GPPUPPDN3 = 60 // Pin pull-up/down for pins 57:48 + GPPUPPDN0 = 57 // Pin pull-up/down for pins 15:0 + GPPUPPDN1 = 58 // Pin pull-up/down for pins 31:16 + GPPUPPDN2 = 59 // Pin pull-up/down for pins 47:32 + GPPUPPDN3 = 60 // Pin pull-up/down for pins 57:48 ) var ( @@ -259,7 +259,7 @@ func (pin Pin) PullOff() { func (pin Pin) ReadPull() Pull { if !isBCM2711() { - return PullNone // Can't read pull-up/pull-down state on other Pi boards + return PullNone // Can't read pull-up/pull-down state on other Pi boards } reg := GPPUPPDN0 + (uint8(pin) >> 4) @@ -272,7 +272,7 @@ func (pin Pin) ReadPull() Pull { case 2: return PullDown default: - return PullNone // Invalid + return PullNone // Invalid } } @@ -286,6 +286,11 @@ func (pin Pin) EdgeDetected() bool { return EdgeDetected(pin) } +// Get gpio mode +func (pin Pin) ReadMode() uint32 { + return ReadPinMode(pin) +} + // PinMode sets the mode of a given pin (Input, Output, Clock, Pwm or Spi) // // Clock is possible only for pins 4, 5, 6, 20, 21. @@ -366,6 +371,21 @@ func PinMode(pin Pin, mode Mode) { gpioMem[fselReg] = (gpioMem[fselReg] &^ (pinMask << shift)) | (f << shift) } +// Read pin Mod status。(gpio readall) +// const in = 0 // 000 +// const out = 1 // 001 +// const alt0 = 4 // 100 +// const alt1 = 5 // 101 +// const alt2 = 6 // 110 +// const alt3 = 7 // 111 +// const alt4 = 3 // 011 +// const alt5 = 2 // 010 +func ReadPinMode(pin Pin) uint32 { + fselReg := uint8(pin) / 10 + shift := (uint8(pin) % 10) * 3 + return gpioMem[fselReg] >> shift & 7 +} + // WritePin sets a given pin High or Low // by setting the clear or set registers respectively func WritePin(pin Pin, state State) { @@ -481,51 +501,51 @@ func EdgeDetected(pin Pin) bool { } func PullMode(pin Pin, pull Pull) { - + memlock.Lock() defer memlock.Unlock() if isBCM2711() { pullreg := GPPUPPDN0 + (pin >> 4) pullshift := (pin & 0xf) << 1 - - var p uint32 - + + var p uint32 + switch pull { case PullOff: p = 0 case PullUp: p = 1 case PullDown: - p = 2; + p = 2 } - + // This is verbatim C code from raspi-gpio.c pullbits := gpioMem[pullreg] pullbits &= ^(3 << pullshift) pullbits |= (p << pullshift) - gpioMem[pullreg]= pullbits + gpioMem[pullreg] = pullbits } else { // Pull up/down/off register has offset 38 / 39, pull is 37 pullClkReg := pin/32 + 38 pullReg := 37 shift := pin % 32 - + switch pull { case PullDown, PullUp: gpioMem[pullReg] |= uint32(pull) case PullOff: 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] &^= 3 gpioMem[pullClkReg] = 0 }