go-rpio: github.com/stianeikeland/go-rpio Index | Files

package rpio

import "github.com/stianeikeland/go-rpio"

Package rpio provides GPIO access on the Raspberry PI without any need for external c libraries (eg. WiringPi or BCM2835).

Supports simple operations such as:

- Pin mode/direction (input/output/clock/pwm)
- Pin write (high/low)
- Pin read (high/low)
- Pull up/down/off

And clock/pwm related oparations:

- Set Clock frequency
- Set Duty cycle

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, and not the wiringPi convention.

          Rev 2 and 3 Raspberry Pi                        Rev 1 Raspberry Pi (legacy)
+-----+---------+----------+---------+-----+      +-----+--------+----------+--------+-----+
| BCM |   Name  | Physical | Name    | BCM |      | BCM | Name   | Physical | Name   | BCM |
+-----+---------+----++----+---------+-----+      +-----+--------+----++----+--------+-----+
|     |    3.3v |  1 || 2  | 5v      |     |      |     | 3.3v   |  1 ||  2 | 5v     |     |
|   2 |   SDA 1 |  3 || 4  | 5v      |     |      |   0 | SDA    |  3 ||  4 | 5v     |     |
|   3 |   SCL 1 |  5 || 6  | 0v      |     |      |   1 | SCL    |  5 ||  6 | 0v     |     |
|   4 | GPIO  7 |  7 || 8  | TxD     | 14  |      |   4 | GPIO 7 |  7 ||  8 | TxD    |  14 |
|     |      0v |  9 || 10 | RxD     | 15  |      |     | 0v     |  9 || 10 | RxD    |  15 |
|  17 | GPIO  0 | 11 || 12 | GPIO  1 | 18  |      |  17 | GPIO 0 | 11 || 12 | GPIO 1 |  18 |
|  27 | GPIO  2 | 13 || 14 | 0v      |     |      |  21 | GPIO 2 | 13 || 14 | 0v     |     |
|  22 | GPIO  3 | 15 || 16 | GPIO  4 | 23  |      |  22 | GPIO 3 | 15 || 16 | GPIO 4 |  23 |
|     |    3.3v | 17 || 18 | GPIO  5 | 24  |      |     | 3.3v   | 17 || 18 | GPIO 5 |  24 |
|  10 |    MOSI | 19 || 20 | 0v      |     |      |  10 | MOSI   | 19 || 20 | 0v     |     |
|   9 |    MISO | 21 || 22 | GPIO  6 | 25  |      |   9 | MISO   | 21 || 22 | GPIO 6 |  25 |
|  11 |    SCLK | 23 || 24 | CE0     | 8   |      |  11 | SCLK   | 23 || 24 | CE0    |   8 |
|     |      0v | 25 || 26 | CE1     | 7   |      |     | 0v     | 25 || 26 | CE1    |   7 |
|   0 |   SDA 0 | 27 || 28 | SCL 0   | 1   |      +-----+--------+----++----+--------+-----+
|   5 | GPIO 21 | 29 || 30 | 0v      |     |
|   6 | GPIO 22 | 31 || 32 | GPIO 26 | 12  |
|  13 | GPIO 23 | 33 || 34 | 0v      |     |
|  19 | GPIO 24 | 35 || 36 | GPIO 27 | 16  |
|  26 | GPIO 25 | 37 || 38 | GPIO 28 | 20  |
|     |      0v | 39 || 40 | GPIO 29 | 21  |
+-----+---------+----++----+---------+-----+

See the spec for full details of the BCM2835 controller:

https://www.raspberrypi.org/documentation/hardware/raspberrypi/bcm2835/BCM2835-ARM-Peripherals.pdf and https://elinux.org/BCM2835_datasheet_errata - for errors in that spec

Index

Package Files

rpio.go

func Close Uses

func Close() error

Close unmaps GPIO memory

func Open Uses

func Open() (err error)

Open and memory map GPIO memory range from /dev/mem . Some reflection magic is used to convert it to a unsafe []uint32 pointer

func PinMode Uses

func PinMode(pin Pin, mode Mode)

PinMode sets the mode (direction) of a given pin (Input, Output, Clock or Pwm)

Clock is possible only for pins 4, 5, 6, 20, 21. Pwm is possible only for pins 12, 13, 18, 19.

func PullMode Uses

func PullMode(pin Pin, pull Pull)

func SetDutyCycle Uses

func SetDutyCycle(pin Pin, dutyLen, cycleLen uint32)

Set cycle length (range) and duty length (data) for Pwm pin in M/S mode

 |<- duty ->|
  __________
_/          \_____________/
 |<------- cycle -------->|

Output frequency is computed as pwm clock frequency divided by cycle length. So, to set Pwm pin to freqency 38kHz with duty cycle 1/4, use this combination:

pin.Pwm()
pin.DutyCycle(1, 4)
pin.Freq(38000*4)

Note that some pins share common pwm channel, so calling this function will set same duty cycle for all pins belonging to channel. The channels are:

channel 1 (pwm0) for pins 12, 18, 40
channel 2 (pwm1) for pins 13, 19, 41, 45.

func SetFreq Uses

func SetFreq(pin Pin, freq int)

Set clock speed for given pin in Clock or Pwm mode

Param freq should be in range 4688Hz - 19.2MHz to prevent unexpected behavior, however output frequency of Pwm pins can be further adjusted with SetDutyCycle. So for smaller frequencies use Pwm pin with large cycle range. (Or implement custom software clock using output pin and sleep.)

Note that some pins share the same clock source, it means that changing frequency for one pin will change it also for all pins within a group. The groups are:

gp_clk0: pins 4, 20, 32, 34
gp_clk1: pins 5, 21, 42, 43
gp_clk2: pins 6 and 43
pwm_clk: pins 12, 13, 18, 19, 40, 41, 45

func StartPwm Uses

func StartPwm()

Start pwm for both channels

func StopPwm Uses

func StopPwm()

Stop pwm for both channels

func TogglePin Uses

func TogglePin(pin Pin)

Toggle a pin state (high -> low -> high) TODO: probably possible to do this much faster without read

func WritePin Uses

func WritePin(pin Pin, state State)

WritePin sets a given pin High or Low by setting the clear or set registers respectively

type Mode Uses

type Mode uint8
const (
    Input Mode = iota
    Output
    Clock
    Pwm
)

Pin mode, a pin can be set in Input or Output, Clock or Pwm mode

type Pin Uses

type Pin uint8

func (Pin) Clock Uses

func (pin Pin) Clock()

Set pin as Clock

func (Pin) DutyCycle Uses

func (pin Pin) DutyCycle(dutyLen, cycleLen uint32)

Set duty cycle for Pwm pin (see doc of SetDutyCycle)

func (Pin) Freq Uses

func (pin Pin) Freq(freq int)

Set frequency of Clock or Pwm pin (see doc of SetFreq)

func (Pin) High Uses

func (pin Pin) High()

Set pin High

func (Pin) Input Uses

func (pin Pin) Input()

Set pin as Input

func (Pin) Low Uses

func (pin Pin) Low()

Set pin Low

func (Pin) Mode Uses

func (pin Pin) Mode(mode Mode)

Set pin Mode

func (Pin) Output Uses

func (pin Pin) Output()

Set pin as Output

func (Pin) Pull Uses

func (pin Pin) Pull(pull Pull)

Set a given pull up/down mode

func (Pin) PullDown Uses

func (pin Pin) PullDown()

Pull down pin

func (Pin) PullOff Uses

func (pin Pin) PullOff()

Disable pullup/down on pin

func (Pin) PullUp Uses

func (pin Pin) PullUp()

Pull up pin

func (Pin) Pwm Uses

func (pin Pin) Pwm()

Set pin as Pwm

func (Pin) Read Uses

func (pin Pin) Read() State

Read pin state (high/low)

func (Pin) Toggle Uses

func (pin Pin) Toggle()

Toggle pin state

func (Pin) Write Uses

func (pin Pin) Write(state State)

Set pin state (high/low)

type Pull Uses

type Pull uint8
const (
    PullOff Pull = iota
    PullDown
    PullUp
)

Pull Up / Down / Off

type State Uses

type State uint8
const (
    Low State = iota
    High
)

State of pin, High / Low

func ReadPin Uses

func ReadPin(pin Pin) State

Read the state of a pin

Package rpio imports 8 packages (graph) and is imported by 47 packages. Updated 2018-03-25. Refresh now. Tools for package owners.