Skip to content
This repository was archived by the owner on Feb 18, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion rpi/gpio.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package rpi

import (
"errors"
"log"
"syscall"
"time"
"unsafe"

"github.com/davecheney/gpio"
)

var (
gpfsel, gpset, gpclr, gplev []*uint32
gpfsel, gpset, gpclr, gplev, gppupdnclk []*uint32
gppupdn *uint32
)

func initGPIO(memfd int) {
Expand Down Expand Up @@ -37,6 +40,11 @@ func initGPIO(memfd int) {
(*uint32)(unsafe.Pointer(&buf[BCM2835_GPLEV0])),
(*uint32)(unsafe.Pointer(&buf[BCM2835_GPLEV1])),
}
gppupdnclk = []*uint32{
(*uint32)(unsafe.Pointer(&buf[BCM2835_GPPUDCLK0])),
(*uint32)(unsafe.Pointer(&buf[BCM2835_GPPUDCLK1])),
}
gppupdn = (*uint32)(unsafe.Pointer(&buf[BCM2835_GPPUD]))
}

// pin represents a specalised RPi GPIO pin with fast paths for
Expand Down Expand Up @@ -80,3 +88,21 @@ func GPIOFSel(pin, mode uint8) {
value |= uint32(mode) << shift
*gpfsel[offset] = value & mask
}

func GPIOSetPullUpDown(pin uint8, dir PullDirection) error {
if int(dir) > 2 {
return errors.New("pull direction must be PullDown, PullUp, or PullOff")
}

offset := pin / 32
shift := pin % 32

*gppupdn = (*gppupdn & ^uint32(3)) | uint32(dir)
time.Sleep(time.Microsecond*5)
*gppupdnclk[offset] = uint32(1 << shift)
time.Sleep(time.Microsecond*5)
*gppupdn &^= 3
*gppupdnclk[offset] = 0

return nil
}
6 changes: 6 additions & 0 deletions rpi/rpi.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"sync"
)

type PullDirection uint8

const (
// Physical addresses for various peripheral register sets

Expand Down Expand Up @@ -98,6 +100,10 @@ const (
GPIO24 = GPIO_P1_18
GPIO27 = GPIO_P1_13
GPIO17 = GPIO_P1_11

PullNone PullDirection = 0
PullDown PullDirection = 1
PullUp PullDirection = 2
)

func initRPi() {
Expand Down