2017-08-25 10:11:11 +08:00
|
|
|
package spi
|
|
|
|
|
|
|
|
import (
|
2018-07-03 21:24:30 +08:00
|
|
|
"periph.io/x/periph/conn/physic"
|
2018-04-12 00:59:01 +08:00
|
|
|
xspi "periph.io/x/periph/conn/spi"
|
2018-04-16 15:56:17 +08:00
|
|
|
xsysfs "periph.io/x/periph/host/sysfs"
|
2017-08-25 10:11:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2018-04-12 02:22:59 +08:00
|
|
|
// NotInitialized is the initial value for a bus/chip
|
|
|
|
NotInitialized = -1
|
2017-08-25 10:11:11 +08:00
|
|
|
)
|
|
|
|
|
2018-04-16 19:29:35 +08:00
|
|
|
// Operations are the wrappers around the actual functions used by the SPI device interface
|
2018-04-12 00:59:01 +08:00
|
|
|
type Operations interface {
|
2017-08-25 10:11:11 +08:00
|
|
|
Close() error
|
|
|
|
Tx(w, r []byte) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connector lets Adaptors provide the interface for Drivers
|
|
|
|
// to get access to the SPI buses on platforms that support SPI.
|
|
|
|
type Connector interface {
|
2018-04-12 00:59:01 +08:00
|
|
|
// GetSpiConnection returns a connection to a SPI device at the specified bus and chip.
|
2017-09-12 14:51:01 +08:00
|
|
|
// Bus numbering starts at index 0, the range of valid buses is
|
2018-04-12 00:59:01 +08:00
|
|
|
// platform specific. Same with chip numbering.
|
2018-04-16 19:29:35 +08:00
|
|
|
GetSpiConnection(busNum, chip, mode, bits int, maxSpeed int64) (device Connection, err error)
|
2017-08-25 10:11:11 +08:00
|
|
|
|
2018-04-12 00:59:01 +08:00
|
|
|
// GetSpiDefaultBus returns the default SPI bus index
|
2017-08-25 10:11:11 +08:00
|
|
|
GetSpiDefaultBus() int
|
|
|
|
|
2018-04-12 00:59:01 +08:00
|
|
|
// GetSpiDefaultChip returns the default SPI chip index
|
|
|
|
GetSpiDefaultChip() int
|
|
|
|
|
2017-08-25 10:11:11 +08:00
|
|
|
// GetDefaultMode returns the default SPI mode (0/1/2/3)
|
|
|
|
GetSpiDefaultMode() int
|
|
|
|
|
2018-04-12 00:59:01 +08:00
|
|
|
// GetDefaultMode returns the default SPI number of bits (8)
|
|
|
|
GetSpiDefaultBits() int
|
|
|
|
|
2017-08-25 10:11:11 +08:00
|
|
|
// GetSpiDefaultMaxSpeed returns the max SPI speed
|
|
|
|
GetSpiDefaultMaxSpeed() int64
|
|
|
|
}
|
|
|
|
|
2018-04-16 19:29:35 +08:00
|
|
|
// Connection is a connection to a SPI device with a specific bus/chip.
|
|
|
|
// Provided by an Adaptor, usually just by calling the spi package's GetSpiConnection() function.
|
2018-04-12 00:59:01 +08:00
|
|
|
type Connection Operations
|
2017-08-25 10:11:11 +08:00
|
|
|
|
2018-04-16 15:56:17 +08:00
|
|
|
// SpiConnection is the implementation of the SPI interface using the periph.io
|
2018-04-16 19:29:35 +08:00
|
|
|
// sysfs implementation for Linux.
|
2017-08-25 10:11:11 +08:00
|
|
|
type SpiConnection struct {
|
2018-04-16 19:29:35 +08:00
|
|
|
Operations
|
2018-04-12 00:59:01 +08:00
|
|
|
port xspi.PortCloser
|
|
|
|
dev xspi.Conn
|
|
|
|
bus int
|
|
|
|
chip int
|
|
|
|
bits int
|
2017-08-25 10:11:11 +08:00
|
|
|
mode int
|
|
|
|
maxSpeed int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConnection creates and returns a new connection to a specific
|
2018-04-16 19:29:35 +08:00
|
|
|
// spi device on a bus/chip using the periph.io interface.
|
2018-04-12 00:59:01 +08:00
|
|
|
func NewConnection(port xspi.PortCloser, conn xspi.Conn) (connection *SpiConnection) {
|
|
|
|
return &SpiConnection{port: port, dev: conn}
|
2017-08-25 10:11:11 +08:00
|
|
|
}
|
|
|
|
|
2018-04-16 19:29:35 +08:00
|
|
|
// Close the SPI connection.
|
2017-08-25 10:11:11 +08:00
|
|
|
func (c *SpiConnection) Close() error {
|
2018-04-12 00:59:01 +08:00
|
|
|
return c.port.Close()
|
2017-08-25 10:11:11 +08:00
|
|
|
}
|
|
|
|
|
2018-04-12 00:59:01 +08:00
|
|
|
// Tx uses the SPI device to send/receive data.
|
2017-08-25 10:11:11 +08:00
|
|
|
func (c *SpiConnection) Tx(w, r []byte) error {
|
2018-04-12 00:59:01 +08:00
|
|
|
return c.dev.Tx(w, r)
|
2017-08-25 10:11:11 +08:00
|
|
|
}
|
2017-12-13 20:16:50 +08:00
|
|
|
|
2018-04-16 19:29:35 +08:00
|
|
|
// GetSpiConnection is a helper to return a SPI device.
|
|
|
|
func GetSpiConnection(busNum, chipNum, mode, bits int, maxSpeed int64) (Connection, error) {
|
2018-04-16 15:56:17 +08:00
|
|
|
p, err := xsysfs.NewSPI(busNum, chipNum)
|
2018-04-12 00:59:01 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-12-13 20:16:50 +08:00
|
|
|
}
|
2018-07-03 21:24:30 +08:00
|
|
|
c, err := p.Connect(physic.Frequency(maxSpeed)*physic.Hertz, xspi.Mode(mode), bits)
|
2017-12-13 20:16:50 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-04-16 15:56:17 +08:00
|
|
|
return NewConnection(p, c), nil
|
2017-12-13 20:16:50 +08:00
|
|
|
}
|