import "golang.org/x/exp/io/spi"
Package spi allows users to read from and write to an SPI device.
Example illustrates a program that drives an APA-102 LED strip.
Code:
dev, err := spi.Open(&spi.Devfs{}, 0, 1, spi.Mode3, 500000) // opens /dev/spidev0.1.
if err != nil {
panic(err)
}
defer dev.Close()
if err := dev.Transfer([]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)
}
type Devfs struct{}
Devfs is an SPI driver that works against the devfs. You need to load the "spidev" module to use this driver.
Open Devfs /dev/spidev<bus>.<chip> and returns a connection.
type Device struct {
// contains filtered or unexported fields
}
Open opens a device with the specified bus and chip select by using the given driver. If a nil driver is provided, the default driver (devfs) is used. 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 overriden by the device's driver. Speed is the max clock speed (Hz) and can be overriden by the device's driver.
Close closes the SPI device and releases the related resources.
SetBitOrder sets the bit justification used to transfer SPI words. Valid values are MSBFirst and LSBFirst.
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.
SetDelay sets the amount of pause will be added after each frame write.
SetMaxSpeed sets the maximum clock speed in Hz. The value can be overriden by SPI device's driver.
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.
Transfer performs a duplex transmission to write to the SPI device and read len(rx) bytes to rx. User should not mutate the tx and rx until this call returns.
Mode represents the SPI mode number where clock parity (CPOL) is the high order and clock edge (CPHA) is the low order bit.
Order is the bit justification to be used while transfering words to the SPI device. MSB-first encoding is more popular than LSB-first.
Path | Synopsis |
---|---|
driver | Package driver contains interfaces to be implemented by various SPI implementations. |
Package spi imports 6 packages (graph). Updated 2 days ago. Refresh now. Tools for package owners.