mirror of
https://github.com/stianeikeland/go-rpio.git
synced 2025-03-15 17:38:44 +01:00
Basic functionality working
Added support for: - pin mode - write - read
This commit is contained in:
parent
942b04b4d0
commit
cc2855b1d8
248
rpio.go
Normal file
248
rpio.go
Normal file
|
@ -0,0 +1,248 @@
|
|||
/*
|
||||
|
||||
Package rpio provides GPIO access on the Raspberry PI without any need
|
||||
for external c libraries (ex: WiringPI or BCM2835).
|
||||
|
||||
Supports simple operations such as:
|
||||
- Pin mode/direction (input/output)
|
||||
- Pin write (high/low)
|
||||
- Pin read (high/low)
|
||||
|
||||
Example of use:
|
||||
|
||||
rpio.Open()
|
||||
defer rpio.Close()
|
||||
|
||||
pin := rpio.Pin(4)
|
||||
pin.Output()
|
||||
|
||||
for {
|
||||
pin.Toggle()
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
The library use the raw BCM2835 pinouts, not the ports as they are mapped
|
||||
on the output pins for the raspberry pi
|
||||
|
||||
Rev 1 Raspberry Pi
|
||||
+------+------+--------+
|
||||
| GPIO | Phys | Name |
|
||||
+------+------+--------+
|
||||
| 0 | 3 | SDA |
|
||||
| 1 | 5 | SCL |
|
||||
| 4 | 7 | GPIO 7 |
|
||||
| 7 | 26 | CE1 |
|
||||
| 8 | 24 | CE0 |
|
||||
| 9 | 21 | MISO |
|
||||
| 10 | 19 | MOSI |
|
||||
| 11 | 23 | SCLK |
|
||||
| 14 | 8 | TxD |
|
||||
| 15 | 10 | RxD |
|
||||
| 17 | 11 | GPIO 0 |
|
||||
| 18 | 12 | GPIO 1 |
|
||||
| 21 | 13 | GPIO 2 |
|
||||
| 22 | 15 | GPIO 3 |
|
||||
| 23 | 16 | GPIO 4 |
|
||||
| 24 | 18 | GPIO 5 |
|
||||
| 25 | 22 | GPIO 6 |
|
||||
+------+------+--------+
|
||||
|
||||
See the spec for full details of the BCM2835 controller:
|
||||
http://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
|
||||
|
||||
*/
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"reflect"
|
||||
"sync"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type Direction uint8
|
||||
type Pin uint8
|
||||
type State uint8
|
||||
|
||||
// Memory offsets for gpio, see the spec for more details
|
||||
const (
|
||||
bcm2835Base = 0x20000000
|
||||
gpioBase = bcm2835Base + 0x200000
|
||||
memLength = 4096
|
||||
|
||||
pinMask uint32 = 7 // 0b111 - pinmode is 3 bits
|
||||
)
|
||||
|
||||
// Pin direction, a pin can be set in Input or Output mode
|
||||
const (
|
||||
Input Direction = iota
|
||||
Output
|
||||
)
|
||||
|
||||
// State of pin, High / Low
|
||||
const (
|
||||
Low State = iota
|
||||
High
|
||||
)
|
||||
|
||||
// Arrays for 8 / 32 bit access to memory and a semaphore for write locking
|
||||
var (
|
||||
memlock sync.Mutex
|
||||
mem []uint32
|
||||
mem8 []uint8
|
||||
)
|
||||
|
||||
// Set pin as Input
|
||||
func (pin Pin) Input() {
|
||||
PinMode(pin, Input)
|
||||
}
|
||||
|
||||
// Set pin as Output
|
||||
func (pin Pin) Output() {
|
||||
PinMode(pin, Output)
|
||||
}
|
||||
|
||||
// Set pin High
|
||||
func (pin Pin) High() {
|
||||
WritePin(pin, High)
|
||||
}
|
||||
|
||||
// Set pin Low
|
||||
func (pin Pin) Low() {
|
||||
WritePin(pin, Low)
|
||||
}
|
||||
|
||||
// Toggle pin state
|
||||
func (pin Pin) Toggle() {
|
||||
TogglePin(pin)
|
||||
}
|
||||
|
||||
// Set pin Direction
|
||||
func (pin Pin) Mode(dir Direction) {
|
||||
PinMode(pin, dir)
|
||||
}
|
||||
|
||||
// Set pin state (high/low)
|
||||
func (pin Pin) Write(state State) {
|
||||
WritePin(pin, state)
|
||||
}
|
||||
|
||||
// Read pin state (high/low)
|
||||
func (pin Pin) Read() State {
|
||||
return ReadPin(pin)
|
||||
}
|
||||
|
||||
// WritePin sets the direction of a given pin (Input or Output)
|
||||
func PinMode(pin Pin, direction Direction) {
|
||||
|
||||
// Pin fsel register, 0 or 1 depending on bank
|
||||
fsel := uint8(pin) / 10
|
||||
shift := (uint8(pin) % 10) * 3
|
||||
|
||||
memlock.Lock()
|
||||
|
||||
if direction == Input {
|
||||
mem[fsel] = mem[fsel] &^ (pinMask << shift)
|
||||
} else {
|
||||
mem[fsel] = (mem[fsel] &^ (pinMask << shift)) | (1 << shift)
|
||||
}
|
||||
|
||||
memlock.Unlock()
|
||||
}
|
||||
|
||||
// WritePin sets a given pin High or Low
|
||||
// by setting the clear or set registers respectively
|
||||
func WritePin(pin Pin, state State) {
|
||||
|
||||
p := uint8(pin)
|
||||
|
||||
// Clear register, 10 / 11 depending on bank
|
||||
// Set register, 7 / 8 depending on bank
|
||||
clearReg := p/32 + 10
|
||||
setReg := p/32 + 7
|
||||
|
||||
memlock.Lock()
|
||||
|
||||
if state == Low {
|
||||
mem[clearReg] = 1 << (p & 31)
|
||||
} else {
|
||||
mem[setReg] = 1 << (p & 31)
|
||||
}
|
||||
|
||||
memlock.Unlock()
|
||||
}
|
||||
|
||||
// Read the state of a
|
||||
func ReadPin(pin Pin) State {
|
||||
// Input level register offset (13 / 14 depending on bank)
|
||||
levelReg := uint8(pin)/32 + 13
|
||||
|
||||
if (mem[levelReg] & (1 << uint8(pin))) != 0 {
|
||||
return High
|
||||
}
|
||||
|
||||
return Low
|
||||
}
|
||||
|
||||
// Toggle a pin state (high -> low -> high)
|
||||
// TODO: probably possible to do this much faster without read
|
||||
func TogglePin(pin Pin) {
|
||||
switch ReadPin(pin) {
|
||||
case Low:
|
||||
pin.High()
|
||||
case High:
|
||||
pin.Low()
|
||||
}
|
||||
}
|
||||
|
||||
// Open and memory map GPIO memory range from /dev/mem .
|
||||
// Some reflection magic is used to convert it to a unsafe []uint32 pointer
|
||||
func Open() (err error) {
|
||||
var file *os.File
|
||||
|
||||
// Open fd for rw mem access
|
||||
file, err = os.OpenFile(
|
||||
"/dev/mem",
|
||||
os.O_RDWR|os.O_SYNC,
|
||||
0)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// FD can be closed after memory mapping
|
||||
defer file.Close()
|
||||
|
||||
memlock.Lock()
|
||||
|
||||
// Memory map GPIO registers to byte array
|
||||
mem8, err = syscall.Mmap(
|
||||
int(file.Fd()),
|
||||
gpioBase,
|
||||
memLength,
|
||||
syscall.PROT_READ|syscall.PROT_WRITE,
|
||||
syscall.MAP_SHARED)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Convert mapped byte memory to unsafe []uint32 pointer, adjust length as needed
|
||||
header := *(*reflect.SliceHeader)(unsafe.Pointer(&mem8))
|
||||
header.Len /= (32 / 8) // (32 bit = 4 bytes)
|
||||
header.Cap /= (32 / 8)
|
||||
|
||||
mem = *(*[]uint32)(unsafe.Pointer(&header))
|
||||
|
||||
memlock.Unlock()
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close unmaps GPIO memory
|
||||
func Close() {
|
||||
memlock.Lock()
|
||||
syscall.Munmap(mem8)
|
||||
memlock.Unlock()
|
||||
}
|
151
rpio/rpio.go
151
rpio/rpio.go
|
@ -1,151 +0,0 @@
|
|||
package rpio
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"sync"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type Direction uint8
|
||||
type Pin uint8
|
||||
type State uint8
|
||||
|
||||
// Memory offsets for gpio, see the spec for more details
|
||||
// http://www.raspberrypi.org/wp-content/uploads/2012/02/BCM2835-ARM-Peripherals.pdf
|
||||
const (
|
||||
bcm2835Base = 0x20000000
|
||||
gpioBase = bcm2835Base + 0x200000
|
||||
|
||||
pinmask uint32 = 7 // 0b111
|
||||
)
|
||||
|
||||
// Pin modes (input, output)
|
||||
const (
|
||||
INPUT Direction = iota
|
||||
OUTPUT
|
||||
)
|
||||
|
||||
// State of pin, high/low
|
||||
const (
|
||||
LOW State = iota
|
||||
HIGH
|
||||
)
|
||||
|
||||
var (
|
||||
mem []uint32
|
||||
mem8 []uint8
|
||||
memlock sync.Mutex
|
||||
)
|
||||
|
||||
func (pin Pin) High() {
|
||||
WritePin(uint8(pin), HIGH)
|
||||
}
|
||||
|
||||
func (pin Pin) Low() {
|
||||
WritePin(uint8(pin), LOW)
|
||||
}
|
||||
|
||||
func (pin Pin) Mode(dir Direction) {
|
||||
PinMode(uint8(pin), dir)
|
||||
}
|
||||
|
||||
func (pin Pin) Write(state State) {
|
||||
WritePin(uint8(pin), state)
|
||||
}
|
||||
|
||||
func (pin Pin) Read() State {
|
||||
return ReadPin(uint8(pin))
|
||||
}
|
||||
|
||||
func PinMode(pin uint8, direction Direction) {
|
||||
fsel := pin / 10
|
||||
shift := (pin % 10) * 3
|
||||
|
||||
fmt.Println(pin, fsel, shift)
|
||||
fmt.Printf("0b%b \n", mem[fsel])
|
||||
|
||||
memlock.Lock()
|
||||
|
||||
if direction == INPUT {
|
||||
fmt.Printf("0b%b\n\n", mem[fsel]&^(pinmask<<shift))
|
||||
} else {
|
||||
fmt.Printf("0b%b\n\n", mem[fsel]&^(pinmask<<shift)|(1<<shift))
|
||||
}
|
||||
|
||||
memlock.Unlock()
|
||||
}
|
||||
|
||||
func WritePin(pin uint8, state State) {
|
||||
|
||||
clearReg := pin/32 + 10
|
||||
setReg := pin/32 + 7
|
||||
|
||||
memlock.Lock()
|
||||
|
||||
if state == LOW {
|
||||
fmt.Printf("0b%b \n", mem[clearReg])
|
||||
fmt.Printf("0b%b\n\n", 1<<(pin&31))
|
||||
} else {
|
||||
fmt.Printf("0b%b \n", mem[setReg])
|
||||
fmt.Printf("0b%b\n\n", 1<<(pin&31))
|
||||
}
|
||||
|
||||
memlock.Unlock()
|
||||
}
|
||||
|
||||
func ReadPin(pin uint8) State {
|
||||
// input level register offset
|
||||
levelReg := pin/32 + 13
|
||||
|
||||
//memlock.Lock()
|
||||
fmt.Printf("0b%b \n", mem[levelReg])
|
||||
fmt.Printf("0b%b \n", mem[levelReg]&(1<<pin))
|
||||
|
||||
if (mem[levelReg] & (1 << pin)) != 0 {
|
||||
return HIGH
|
||||
}
|
||||
|
||||
return LOW
|
||||
}
|
||||
|
||||
func Open() (err error) {
|
||||
var file *os.File
|
||||
|
||||
// Open fd for rw mem access
|
||||
file, err = os.OpenFile("/dev/mem", os.O_RDWR|os.O_SYNC, 0)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Can be closed after memory mapping
|
||||
defer file.Close()
|
||||
|
||||
memlock.Lock()
|
||||
|
||||
// Memory map GPIO registers to byte array
|
||||
mem8, err = syscall.Mmap(int(file.Fd()), gpioBase, 4*1024, syscall.PROT_READ, syscall.MAP_SHARED)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Convert mapped byte memory to unsafe []uint32 pointer, adjust length as needed
|
||||
header := *(*reflect.SliceHeader)(unsafe.Pointer(&mem8))
|
||||
header.Len /= (32 / 8) // (32 bit = 4 bytes)
|
||||
header.Cap /= (32 / 8)
|
||||
|
||||
mem = *(*[]uint32)(unsafe.Pointer(&header))
|
||||
|
||||
memlock.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func Close() {
|
||||
// Unmap memory
|
||||
memlock.Lock()
|
||||
syscall.Munmap(mem8)
|
||||
memlock.Unlock()
|
||||
}
|
Loading…
Reference in New Issue
Block a user