periph: Index | Examples | Files

package spireg

import ""

Package spireg defines the SPI registry for SPI ports discovered on the host.

SPI ports discovered on the host are automatically registered in the SPI registry by host.Init().


// Make sure periph is initialized.
if _, err := host.Init(); err != nil {

// A command line tool may let the user choose a SPI port, yet default to the
// first port known.
name := flag.String("spi", "", "SPI port to use")
p, err := spireg.Open(*name)
if err != nil {
defer p.Close()

// Convert the spi.Port into a spi.Conn so it can be used for communication.
c, err := p.Connect(physic.MegaHertz, spi.Mode3, 8)
if err != nil {

// Write 0x10 to the device, and read a byte right after.
write := []byte{0x10, 0x00}
read := make([]byte, len(write))
if err := c.Tx(write, read); err != nil {
// Use read.
fmt.Printf("%v\n", read[1:])



Package Files


func Open Uses

func Open(name string) (spi.PortCloser, error)

Open opens a SPI port by its name, an alias or its number and returns an handle to it.

Specify the empty string "" to get the first available port. This is the recommended default value unless an application knows the exact port to use.

Each port can register multiple aliases, each leading to the same port handle.

"Bus number" is a generic concept that is highly dependent on the platform and OS. On some platform, the first port may have the number 0, 1 or as high as 32766. Bus numbers are not necessarily continuous and may not start at 0. It was observed that the bus number as reported by the OS may change across OS revisions.

A SPI port is constructed of the bus number and the chip select (CS) number.

When the SPI port is provided by an off board plug and play bus like USB via a FT232H USB device, there can be no associated number.


// Make sure periph is initialized.
if _, err := host.Init(); err != nil {

// On Linux, the following calls will likely open the same port.
_, _ = spireg.Open("/dev/spidev1.0")
_, _ = spireg.Open("SPI1.0")
_, _ = spireg.Open("1")

// Opens the first default SPI bus found:
_, _ = spireg.Open("")

// Wondering what to do with the opened spi.PortCloser? Look at the package's
// example above.

func Register Uses

func Register(name string, aliases []string, number int, o Opener) error

Register registers a SPI port.

Registering the same port name twice is an error, e.g. o.Name(). o.Number() can be -1 to signify that the port doesn't have an inherent "bus number". A good example is a port provided over a FT232H device connected on an USB bus. In this case, the port name should be created from the serial number of the device for unique identification.

Only ports with the CS #0 are registered with their number.

func Unregister Uses

func Unregister(name string) error

Unregister removes a previously registered SPI port.

This can happen when a SPI port is exposed via an USB device and the device is unplugged.

type Opener Uses

type Opener func() (spi.PortCloser, error)

Opener opens an handle to a port.

It is provided by the actual port driver.

type Ref Uses

type Ref struct {
    // Name of the port.
    // It must not be a sole number. It must be unique across the host.
    Name string
    // Aliases are the alternative names that can be used to reference this port.
    Aliases []string
    // Number of the bus or -1 if the bus doesn't have any "native" number.
    // Buses provided by the CPU normally have a 0 based number. Buses provided
    // via an addon (like over USB) generally are not numbered.
    // The port is a bus number plus a CS line.
    Number int
    // Open is the factory to open an handle to this SPI port.
    Open Opener

Ref references a SPI port.

It is returned by All() to enumerate all registered ports.

func All Uses

func All() []*Ref

All returns a copy of all the registered references to all know SPI ports available on this host.

The list is sorted by the port name.


// Make sure periph is initialized.
if _, err := host.Init(); err != nil {

// Enumerate all SPI ports available and the corresponding pins.
fmt.Print("SPI ports available:\n")
for _, ref := range spireg.All() {
    fmt.Printf("- %s\n", ref.Name)
    if ref.Number != -1 {
        fmt.Printf("  %d\n", ref.Number)
    if len(ref.Aliases) != 0 {
        fmt.Printf("  %s\n", strings.Join(ref.Aliases, " "))

    p, err := ref.Open()
    if err != nil {
        fmt.Printf("  Failed to open: %v", err)
    if p, ok := p.(spi.Pins); ok {
        fmt.Printf("  CLK : %s", p.CLK())
        fmt.Printf("  MOSI: %s", p.MOSI())
        fmt.Printf("  MISO: %s", p.MISO())
        fmt.Printf("  CS  : %s", p.CS())
    if err := p.Close(); err != nil {
        fmt.Printf("  Failed to close: %v", err)

Package spireg imports 5 packages (graph) and is imported by 43 packages. Updated 2018-08-12. Refresh now. Tools for package owners.