add func ReadMode、ReadPinMode。

get gpio mod
This commit is contained in:
fenglei 2020-07-31 17:28:37 +08:00
parent acc952dac3
commit 842c7d503f

56
rpio.go
View File

@ -100,10 +100,10 @@ const (
// BCM 2711 has a different mechanism for pull-up/pull-down/enable // BCM 2711 has a different mechanism for pull-up/pull-down/enable
const ( const (
GPPUPPDN0 = 57 // Pin pull-up/down for pins 15:0 GPPUPPDN0 = 57 // Pin pull-up/down for pins 15:0
GPPUPPDN1 = 58 // Pin pull-up/down for pins 31:16 GPPUPPDN1 = 58 // Pin pull-up/down for pins 31:16
GPPUPPDN2 = 59 // Pin pull-up/down for pins 47:32 GPPUPPDN2 = 59 // Pin pull-up/down for pins 47:32
GPPUPPDN3 = 60 // Pin pull-up/down for pins 57:48 GPPUPPDN3 = 60 // Pin pull-up/down for pins 57:48
) )
var ( var (
@ -259,7 +259,7 @@ func (pin Pin) PullOff() {
func (pin Pin) ReadPull() Pull { func (pin Pin) ReadPull() Pull {
if !isBCM2711() { 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) reg := GPPUPPDN0 + (uint8(pin) >> 4)
@ -272,7 +272,7 @@ func (pin Pin) ReadPull() Pull {
case 2: case 2:
return PullDown return PullDown
default: default:
return PullNone // Invalid return PullNone // Invalid
} }
} }
@ -286,6 +286,11 @@ func (pin Pin) EdgeDetected() bool {
return EdgeDetected(pin) 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) // 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. // 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) 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 // WritePin sets a given pin High or Low
// by setting the clear or set registers respectively // by setting the clear or set registers respectively
func WritePin(pin Pin, state State) { func WritePin(pin Pin, state State) {
@ -481,51 +501,51 @@ func EdgeDetected(pin Pin) bool {
} }
func PullMode(pin Pin, pull Pull) { func PullMode(pin Pin, pull Pull) {
memlock.Lock() memlock.Lock()
defer memlock.Unlock() defer memlock.Unlock()
if isBCM2711() { if isBCM2711() {
pullreg := GPPUPPDN0 + (pin >> 4) pullreg := GPPUPPDN0 + (pin >> 4)
pullshift := (pin & 0xf) << 1 pullshift := (pin & 0xf) << 1
var p uint32 var p uint32
switch pull { switch pull {
case PullOff: case PullOff:
p = 0 p = 0
case PullUp: case PullUp:
p = 1 p = 1
case PullDown: case PullDown:
p = 2; p = 2
} }
// This is verbatim C code from raspi-gpio.c // This is verbatim C code from raspi-gpio.c
pullbits := gpioMem[pullreg] pullbits := gpioMem[pullreg]
pullbits &= ^(3 << pullshift) pullbits &= ^(3 << pullshift)
pullbits |= (p << pullshift) pullbits |= (p << pullshift)
gpioMem[pullreg]= pullbits gpioMem[pullreg] = pullbits
} else { } else {
// Pull up/down/off register has offset 38 / 39, pull is 37 // Pull up/down/off register has offset 38 / 39, pull is 37
pullClkReg := pin/32 + 38 pullClkReg := pin/32 + 38
pullReg := 37 pullReg := 37
shift := pin % 32 shift := pin % 32
switch pull { switch pull {
case PullDown, PullUp: case PullDown, PullUp:
gpioMem[pullReg] |= uint32(pull) gpioMem[pullReg] |= uint32(pull)
case PullOff: case PullOff:
gpioMem[pullReg] &^= 3 gpioMem[pullReg] &^= 3
} }
// Wait for value to clock in, this is ugly, sorry :( // Wait for value to clock in, this is ugly, sorry :(
time.Sleep(time.Microsecond) time.Sleep(time.Microsecond)
gpioMem[pullClkReg] = 1 << shift gpioMem[pullClkReg] = 1 << shift
// Wait for value to clock in // Wait for value to clock in
time.Sleep(time.Microsecond) time.Sleep(time.Microsecond)
gpioMem[pullReg] &^= 3 gpioMem[pullReg] &^= 3
gpioMem[pullClkReg] = 0 gpioMem[pullClkReg] = 0
} }