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

24
rpio.go
View File

@ -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) {
@ -497,14 +517,14 @@ func PullMode(pin Pin, pull Pull) {
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