mirror of
https://github.com/stianeikeland/go-rpio.git
synced 2025-01-23 10:41:03 +01:00
Added support for PullUp / PullDown / PullOff
This commit is contained in:
parent
7136a1a68f
commit
e934c432f9
13
README.md
13
README.md
|
@ -39,6 +39,16 @@ pin.Mode(rpio.Output) // Alternative syntax
|
||||||
pin.Write(rpio.High) // Alternative syntax
|
pin.Write(rpio.High) // Alternative syntax
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Pull up/down/off can be set using:
|
||||||
|
|
||||||
|
```go
|
||||||
|
pin.PullUp()
|
||||||
|
pin.PullDown()
|
||||||
|
pin.PullOff()
|
||||||
|
|
||||||
|
pin.Pull(rpio.PullUp)
|
||||||
|
```
|
||||||
|
|
||||||
Unmap memory when done
|
Unmap memory when done
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
@ -53,11 +63,10 @@ Currently, it supports basic functionality such as:
|
||||||
- Pin Direction (Input / Output)
|
- Pin Direction (Input / Output)
|
||||||
- Write (High / Low)
|
- Write (High / Low)
|
||||||
- Read (High / Low)
|
- Read (High / Low)
|
||||||
|
- Pull (Up / Down / Off)
|
||||||
|
|
||||||
Would be nice to add in the future:
|
Would be nice to add in the future:
|
||||||
- PWM
|
- PWM
|
||||||
- PullUp
|
|
||||||
- PullDown
|
|
||||||
- I2C
|
- I2C
|
||||||
- SPI
|
- SPI
|
||||||
- etc...
|
- etc...
|
||||||
|
|
32
examples/pullup/pullup.go
Normal file
32
examples/pullup/pullup.go
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/stianeikeland/go-rpio"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Use mcu pin 22, corresponds to GPIO3 on the pi
|
||||||
|
pin = rpio.Pin(22)
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
// Open and map memory to access gpio, check for errors
|
||||||
|
if err := rpio.Open(); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmap gpio memory when done
|
||||||
|
defer rpio.Close()
|
||||||
|
|
||||||
|
// Pull up and read value
|
||||||
|
pin.PullUp()
|
||||||
|
fmt.Printf("PullUp: %d\n", pin.Read())
|
||||||
|
|
||||||
|
// Pull down and read value
|
||||||
|
pin.PullDown()
|
||||||
|
fmt.Printf("PullDown: %d\n", pin.Read())
|
||||||
|
|
||||||
|
}
|
60
rpio.go
60
rpio.go
|
@ -7,6 +7,7 @@ Supports simple operations such as:
|
||||||
- Pin mode/direction (input/output)
|
- Pin mode/direction (input/output)
|
||||||
- Pin write (high/low)
|
- Pin write (high/low)
|
||||||
- Pin read (high/low)
|
- Pin read (high/low)
|
||||||
|
- Pull up/down/off
|
||||||
|
|
||||||
Example of use:
|
Example of use:
|
||||||
|
|
||||||
|
@ -59,12 +60,14 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Direction uint8
|
type Direction uint8
|
||||||
type Pin uint8
|
type Pin uint8
|
||||||
type State uint8
|
type State uint8
|
||||||
|
type Pull uint8
|
||||||
|
|
||||||
// Memory offsets for gpio, see the spec for more details
|
// Memory offsets for gpio, see the spec for more details
|
||||||
const (
|
const (
|
||||||
|
@ -87,6 +90,13 @@ const (
|
||||||
High
|
High
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Pull Up / Down / Off
|
||||||
|
const (
|
||||||
|
PullOff Pull = iota
|
||||||
|
PullDown
|
||||||
|
PullUp
|
||||||
|
)
|
||||||
|
|
||||||
// Arrays for 8 / 32 bit access to memory and a semaphore for write locking
|
// Arrays for 8 / 32 bit access to memory and a semaphore for write locking
|
||||||
var (
|
var (
|
||||||
memlock sync.Mutex
|
memlock sync.Mutex
|
||||||
|
@ -134,6 +144,26 @@ func (pin Pin) Read() State {
|
||||||
return ReadPin(pin)
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
// WritePin sets the direction of a given pin (Input or Output)
|
// WritePin sets the direction of a given pin (Input or Output)
|
||||||
func PinMode(pin Pin, direction Direction) {
|
func PinMode(pin Pin, direction Direction) {
|
||||||
|
|
||||||
|
@ -197,6 +227,36 @@ func TogglePin(pin Pin) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func PullMode(pin Pin, pull Pull) {
|
||||||
|
// Pull up/down/off register has offset 38 / 39, pull is 37
|
||||||
|
pullClkReg := uint8(pin)/32 + 38
|
||||||
|
pullReg := 37
|
||||||
|
shift := (uint8(pin) % 32)
|
||||||
|
|
||||||
|
memlock.Lock()
|
||||||
|
|
||||||
|
switch pull {
|
||||||
|
case PullDown, PullUp:
|
||||||
|
mem[pullReg] = mem[pullReg]&^3 | uint32(pull)
|
||||||
|
case PullOff:
|
||||||
|
mem[pullReg] = mem[pullReg] &^ 3
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for value to clock in, this is ugly, sorry :(
|
||||||
|
time.Sleep(time.Microsecond)
|
||||||
|
|
||||||
|
mem[pullClkReg] = 1 << shift
|
||||||
|
|
||||||
|
// Wait for value to clock in
|
||||||
|
time.Sleep(time.Microsecond)
|
||||||
|
|
||||||
|
mem[pullReg] = mem[pullReg] &^ 3
|
||||||
|
mem[pullClkReg] = 0
|
||||||
|
|
||||||
|
memlock.Unlock()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// Open and memory map GPIO memory range from /dev/mem .
|
// Open and memory map GPIO memory range from /dev/mem .
|
||||||
// Some reflection magic is used to convert it to a unsafe []uint32 pointer
|
// Some reflection magic is used to convert it to a unsafe []uint32 pointer
|
||||||
func Open() (err error) {
|
func Open() (err error) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user