package spi
import "golang.org/x/exp/io/spi"
Package spi allows users to read from and write to an SPI device.
Deprecated
This package is not maintained anymore. An actively supported cross-platform
alternative is https://periph.io/.
Example illustrates a program that drives an APA-102 LED strip.
Code:play
Example¶
package main
import (
"golang.org/x/exp/io/spi"
)
func main() {
dev, err := spi.Open(&spi.Devfs{
Dev: "/dev/spidev0.1",
Mode: spi.Mode3,
MaxSpeed: 500000,
})
if err != nil {
panic(err)
}
defer dev.Close()
if err := dev.Tx([]byte{
0, 0, 0, 0,
0xff, 200, 0, 200,
0xff, 200, 0, 200,
0xe0, 200, 0, 200,
0xff, 200, 0, 200,
0xff, 8, 50, 0,
0xff, 200, 0, 0,
0xff, 0, 0, 0,
0xff, 200, 0, 200,
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff,
}, nil); err != nil {
panic(err)
}
}
Index ¶
- Constants
- type Devfs
- type Device
- func Open(o driver.Opener) (*Device, error)
- func (d *Device) Close() error
- func (d *Device) SetBitOrder(o Order) error
- func (d *Device) SetBitsPerWord(bits int) error
- func (d *Device) SetCSChange(leaveEnabled bool) error
- func (d *Device) SetDelay(t time.Duration) error
- func (d *Device) SetMaxSpeed(speed int) error
- func (d *Device) SetMode(mode Mode) error
- func (d *Device) Tx(w, r []byte) error
- type Mode
- type Order
Examples ¶
Constants ¶
Types ¶
type Devfs ¶
type Devfs struct { // Dev is the device to be opened. // Device name is usually in the /dev/spidev<bus>.<chip> format. // Required. Dev string // Mode is the SPI mode. SPI mode is a combination of polarity and phases. // CPOL is the high order bit, CPHA is the low order. Pre-computed mode // values are Mode0, Mode1, Mode2 and Mode3. The value of the mode argument // can be overridden by the device's driver. // Required. Mode Mode // MaxSpeed is the max clock speed (Hz) and can be overridden by the device's driver. // Required. MaxSpeed int64 }
Devfs is an SPI driver that works against the devfs. You need to have loaded the "spidev" Linux module to use this driver.
func (*Devfs) Open ¶
Open opens the provided device with the specified options and returns a connection.
type Device ¶
type Device struct {
// contains filtered or unexported fields
}
func Open ¶
func (*Device) Close ¶
Close closes the SPI device and releases the related resources.
func (*Device) SetBitOrder ¶
SetBitOrder sets the bit justification used to transfer SPI words. Valid values are MSBFirst and LSBFirst.
func (*Device) SetBitsPerWord ¶
SetBitsPerWord sets how many bits it takes to represent a word, e.g. 8 represents 8-bit words. The default is 8 bits per word.
func (*Device) SetCSChange ¶
SetCSChange sets whether to leave the chipselect enabled after a Tx.
func (*Device) SetDelay ¶
SetDelay sets the amount of pause will be added after each frame write.
func (*Device) SetMaxSpeed ¶
SetMaxSpeed sets the maximum clock speed in Hz. The value can be overridden by SPI device's driver.
func (*Device) SetMode ¶
SetMode sets the SPI mode. SPI mode is a combination of polarity and phases. CPOL is the high order bit, CPHA is the low order. Pre-computed mode values are Mode0, Mode1, Mode2 and Mode3. The value can be changed by SPI device's driver.
func (*Device) Tx ¶
Tx performs a duplex transmission to write w to the SPI device and read len(r) bytes to r. User should not mutate the w and r until this call returns.
type Mode ¶
type Mode int
Mode represents the SPI mode number where clock parity (CPOL) is the high order and clock edge (CPHA) is the low order bit.
type Order ¶
type Order int
Order is the bit justification to be used while transferring words to the SPI device. MSB-first encoding is more popular than LSB-first.
Source Files ¶
devfs.go spi.go
Directories ¶
Path | Synopsis |
---|---|
io/spi/driver | Package driver contains interfaces to be implemented by various SPI implementations. |
- Version
- v0.0.0-20250218142911-aa4b98e5adaa (latest)
- Published
- Feb 18, 2025
- Platform
- linux/amd64
- Imports
- 6 packages
- Last checked
- 2 days ago –
Tools for package owners.