Created
January 2, 2022 04:09
-
-
Save bryanjhv/118e057089d8b82be7c565ec28b43d25 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"os" | |
"strconv" | |
"syscall" | |
"unsafe" | |
) | |
var mm []uint32 | |
// https://git.io/JSLoq | |
var defaultPull = [54]byte{ | |
/* 00-08 UP */ 2, 2, 2, 2, 2, 2, 2, 2, 2, | |
/* 09-27 DOWN */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, | |
/* 28-29 NONE */ 0, 0, | |
/* 30-33 DOWN */ 1, 1, 1, 1, | |
/* 34-36 UP */ 2, 2, 2, | |
/* 37-43 DOWN */ 1, 1, 1, 1, 1, 1, 1, | |
/* 44-45 NONE */ 0, 0, | |
/* 46-53 UP */ 2, 2, 2, 2, 2, 2, 2, 2, | |
} | |
// https://git.io/JSLi3 | |
//go:noinline | |
func sleep150cycles() uint32 { | |
var out uint32 | |
for range [150]struct{}{} { | |
out += mm[0] | |
} | |
return out | |
} | |
func main() { | |
// https://git.io/JSLo2 | |
fd, err := syscall.Open("/dev/gpiomem", syscall.O_RDWR|syscall.O_SYNC|syscall.O_CLOEXEC, 0600) | |
if err != nil { | |
panic(err) | |
} | |
defer syscall.Close(fd) | |
// https://git.io/JSLoP | |
const l = 0x1000 | |
mb, err := syscall.Mmap(fd, 0, l, syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED) | |
if err != nil { | |
panic(err) | |
} | |
defer syscall.Munmap(mb) | |
mm = (*[l]uint32)(unsafe.Pointer(&mb[0]))[:] // this is what you came for | |
args := os.Args[1:] | |
if len(args) < 2 { | |
panic("no enough arguments") | |
} | |
pin, _ := strconv.Atoi(args[0]) | |
pull, _ := strconv.Atoi(args[1]) | |
if pin < 0 || pin > 53 { | |
panic("invalid pin given") | |
} | |
if pull < -1 || pull > 2 { | |
panic("invalid pull given") | |
} | |
// https://git.io/JSLod | |
const ( | |
GPPUD = 0x0094 / 4 | |
GPPUDCLK0 = 0x0098 / 4 | |
) | |
clkreg := GPPUDCLK0 + (pin / 32) | |
clkbit := 1 << (pin % 32) | |
if pull < 0 { | |
pull = int(defaultPull[pin-1]) | |
} | |
// https://git.io/JSLiD | |
mm[GPPUD] = uint32(pull) | |
sleep150cycles() | |
mm[clkreg] = uint32(clkbit) | |
sleep150cycles() | |
// datasheet doesn't sleep here: | |
mm[GPPUD] = 0 | |
mm[clkreg] = 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment