Skip to content

Commit

Permalink
spi: switch to using periph.io for SPI interfaces
Browse files Browse the repository at this point in the history
Signed-off-by: Ron Evans <[email protected]>
  • Loading branch information
deadprogram committed Apr 12, 2018
1 parent ecfe801 commit 7637d90
Show file tree
Hide file tree
Showing 22 changed files with 238 additions and 153 deletions.
43 changes: 33 additions & 10 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@

[[constraint]]
branch = "master"
name = "golang.org/x/exp"
name = "periph.io/x/periph"

[[constraint]]
branch = "master"
Expand Down
4 changes: 3 additions & 1 deletion drivers/spi/apa102.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ func (d *APA102Driver) Connection() gobot.Connection { return d.connection.(gobo
// Start initializes the driver.
func (d *APA102Driver) Start() (err error) {
bus := d.connector.GetSpiDefaultBus()
chip := d.connector.GetSpiDefaultChip()
mode := d.connector.GetSpiDefaultMode()
bits := d.connector.GetSpiDefaultBits()
maxSpeed := d.connector.GetSpiDefaultMaxSpeed()
d.connection, err = d.connector.GetSpiConnection(bus, mode, maxSpeed)
d.connection, err = d.connector.GetSpiConnection(bus, chip, mode, bits, maxSpeed)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/spi/doc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Package spi provides Gobot drivers for spi devices.
Uses "golang.org/x/exp/io/spi" for spi
Uses periph.io for spi
Installing:
go get -d -u gobot.io/x/gobot
For further information refer to spi README:
Expand Down
4 changes: 3 additions & 1 deletion drivers/spi/mcp3002.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ func (d *MCP3002Driver) Connection() gobot.Connection { return d.connection.(gob
// Start initializes the driver.
func (d *MCP3002Driver) Start() (err error) {
bus := d.connector.GetSpiDefaultBus()
chip := d.connector.GetSpiDefaultChip()
mode := d.connector.GetSpiDefaultMode()
bits := d.connector.GetSpiDefaultBits()
maxSpeed := d.connector.GetSpiDefaultMaxSpeed()
d.connection, err = d.connector.GetSpiConnection(bus, mode, maxSpeed)
d.connection, err = d.connector.GetSpiConnection(bus, chip, mode, bits, maxSpeed)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion drivers/spi/mcp3004.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ func (d *MCP3004Driver) Connection() gobot.Connection { return d.connection.(gob
// Start initializes the driver.
func (d *MCP3004Driver) Start() (err error) {
bus := d.connector.GetSpiDefaultBus()
chip := d.connector.GetSpiDefaultChip()
mode := d.connector.GetSpiDefaultMode()
bits := d.connector.GetSpiDefaultBits()
maxSpeed := d.connector.GetSpiDefaultMaxSpeed()
d.connection, err = d.connector.GetSpiConnection(bus, mode, maxSpeed)
d.connection, err = d.connector.GetSpiConnection(bus, chip, mode, bits, maxSpeed)
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions drivers/spi/mcp3008.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ func (d *MCP3008Driver) Connection() gobot.Connection { return d.connection.(gob
// Start initializes the driver.
func (d *MCP3008Driver) Start() (err error) {
bus := d.connector.GetSpiDefaultBus()
chip := d.connector.GetSpiDefaultChip()
mode := d.connector.GetSpiDefaultMode()
bits := d.connector.GetSpiDefaultBits()
maxSpeed := d.connector.GetSpiDefaultMaxSpeed()
d.connection, err = d.connector.GetSpiConnection(bus, mode, maxSpeed)
d.connection, err = d.connector.GetSpiConnection(bus, chip, mode, bits, maxSpeed)
if err != nil {
return err
}
Expand Down Expand Up @@ -72,7 +74,7 @@ func (d *MCP3008Driver) Read(channel int) (result int, err error) {

err = d.connection.Tx(tx, rx)
if err == nil && len(rx) == 3 {
result = int(((rx[1] & 0x3) << 8) + rx[2])
result = int((rx[1]&0x3))<<8 + int(rx[2])
}

return result, err
Expand Down
4 changes: 3 additions & 1 deletion drivers/spi/mcp3202.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ func (d *MCP3202Driver) Connection() gobot.Connection { return d.connection.(gob
// Start initializes the driver.
func (d *MCP3202Driver) Start() (err error) {
bus := d.connector.GetSpiDefaultBus()
chip := d.connector.GetSpiDefaultChip()
mode := d.connector.GetSpiDefaultMode()
bits := d.connector.GetSpiDefaultBits()
maxSpeed := d.connector.GetSpiDefaultMaxSpeed()
d.connection, err = d.connector.GetSpiConnection(bus, mode, maxSpeed)
d.connection, err = d.connector.GetSpiConnection(bus, chip, mode, bits, maxSpeed)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion drivers/spi/mcp3204.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ func (d *MCP3204Driver) Connection() gobot.Connection { return d.connection.(gob
// Start initializes the driver.
func (d *MCP3204Driver) Start() (err error) {
bus := d.connector.GetSpiDefaultBus()
chip := d.connector.GetSpiDefaultChip()
mode := d.connector.GetSpiDefaultMode()
bits := d.connector.GetSpiDefaultBits()
maxSpeed := d.connector.GetSpiDefaultMaxSpeed()
d.connection, err = d.connector.GetSpiConnection(bus, mode, maxSpeed)
d.connection, err = d.connector.GetSpiConnection(bus, chip, mode, bits, maxSpeed)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion drivers/spi/mcp3208.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ func (d *MCP3208Driver) Connection() gobot.Connection { return d.connection.(gob
// Start initializes the driver.
func (d *MCP3208Driver) Start() (err error) {
bus := d.connector.GetSpiDefaultBus()
chip := d.connector.GetSpiDefaultChip()
mode := d.connector.GetSpiDefaultMode()
bits := d.connector.GetSpiDefaultBits()
maxSpeed := d.connector.GetSpiDefaultMaxSpeed()
d.connection, err = d.connector.GetSpiConnection(bus, mode, maxSpeed)
d.connection, err = d.connector.GetSpiConnection(bus, chip, mode, bits, maxSpeed)
if err != nil {
return err
}
Expand Down
4 changes: 3 additions & 1 deletion drivers/spi/mcp3304.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ func (d *MCP3304Driver) Connection() gobot.Connection { return d.connection.(gob
// Start initializes the driver.
func (d *MCP3304Driver) Start() (err error) {
bus := d.connector.GetSpiDefaultBus()
chip := d.connector.GetSpiDefaultChip()
mode := d.connector.GetSpiDefaultMode()
bits := d.connector.GetSpiDefaultBits()
maxSpeed := d.connector.GetSpiDefaultMaxSpeed()
d.connection, err = d.connector.GetSpiConnection(bus, mode, maxSpeed)
d.connection, err = d.connector.GetSpiConnection(bus, chip, mode, bits, maxSpeed)
if err != nil {
return err
}
Expand Down
106 changes: 48 additions & 58 deletions drivers/spi/spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,103 +2,93 @@ package spi

import (
"fmt"
"time"
"log"

xspi "golang.org/x/exp/io/spi"
xspi "periph.io/x/periph/conn/spi"
"periph.io/x/periph/conn/spi/spireg"
"periph.io/x/periph/host"
)

const (
// BusNotInitialized is the initial value for a bus
BusNotInitialized = -1
)

type SPIOperations interface {
// Operations are the wrappers around the actual functions used by the SPI Device interface
type Operations interface {
Close() error
SetBitOrder(o xspi.Order) error
SetBitsPerWord(bits int) error
SetCSChange(leaveEnabled bool) error
SetDelay(t time.Duration) error
SetMaxSpeed(speed int) error
SetMode(mode xspi.Mode) error
Tx(w, r []byte) error
}

// SPIDevice is the interface to a specific spi bus
type SPIDevice interface {
SPIOperations
// Device is the interface to a specific spi bus/chip
type Device interface {
Operations
}

// Connector lets Adaptors provide the interface for Drivers
// to get access to the SPI buses on platforms that support SPI.
type Connector interface {
// GetConnection returns a connection to device at the specified bus.
// GetSpiConnection returns a connection to a SPI device at the specified bus and chip.
// Bus numbering starts at index 0, the range of valid buses is
// platform specific.
GetSpiConnection(busNum, mode int, maxSpeed int64) (device Connection, err error)
// platform specific. Same with chip numbering.
GetSpiConnection(busNum, chip, mode, bits int, maxSpeed int64) (device Device, err error)

// GetDefaultBus returns the default SPI bus index
// GetSpiDefaultBus returns the default SPI bus index
GetSpiDefaultBus() int

// GetSpiDefaultChip returns the default SPI chip index
GetSpiDefaultChip() int

// GetDefaultMode returns the default SPI mode (0/1/2/3)
GetSpiDefaultMode() int

// GetDefaultMode returns the default SPI number of bits (8)
GetSpiDefaultBits() int

// GetSpiDefaultMaxSpeed returns the max SPI speed
GetSpiDefaultMaxSpeed() int64
}

// Connection is a connection to an SPI device with a specified address
// on a specific bus. Used as an alternative to the SPI interface.
// Connection is a connection to an SPI device with a specified bus
// on a specific chip.
// Implements SPIOperations to talk to the device, wrapping the
// calls in SetAddress to always target the specified device.
// Provided by an Adaptor by implementing the SPIConnector interface.
type Connection SPIOperations
type Connection Operations

type SpiConnection struct {
bus SPIDevice
Connection
port xspi.PortCloser
dev xspi.Conn
bus int
chip int
bits int
mode int
maxSpeed int64
}

// NewConnection creates and returns a new connection to a specific
// spi device on a bus and address
func NewConnection(bus SPIDevice) (connection *SpiConnection) {
return &SpiConnection{bus: bus}
// spi device on a bus/chip using the periph.io interface
func NewConnection(port xspi.PortCloser, conn xspi.Conn) (connection *SpiConnection) {
return &SpiConnection{port: port, dev: conn}
}

// Close the SPI connection
func (c *SpiConnection) Close() error {
return c.bus.Close()
}

func (c *SpiConnection) SetBitOrder(o xspi.Order) error {
return c.bus.SetBitOrder(o)
}

func (c *SpiConnection) SetBitsPerWord(bits int) error {
return c.bus.SetBitsPerWord(bits)
}

func (c *SpiConnection) SetCSChange(leaveEnabled bool) error {
return c.bus.SetCSChange(leaveEnabled)
}

func (c *SpiConnection) SetDelay(t time.Duration) error {
return c.bus.SetDelay(t)
}

func (c *SpiConnection) SetMaxSpeed(speed int) error {
return c.bus.SetMaxSpeed(speed)
}

func (c *SpiConnection) SetMode(mode xspi.Mode) error {
return c.bus.SetMode(mode)
return c.port.Close()
}

// Tx uses the SPI device to send/receive data.
func (c *SpiConnection) Tx(w, r []byte) error {
return c.bus.Tx(w, r)
return c.dev.Tx(w, r)
}

// GetSPIBus is a helper to return a SPI bus
func GetSpiBus(busNum, mode int, maxSpeed int64) (spiDevice SPIDevice, err error) {
// GetSpiConnection is a helper to return a SPI device
func GetSpiConnection(busNum, chipNum, mode, bits int, maxSpeed int64) (spiDevice Device, err error) {
if _, err := host.Init(); err != nil {
log.Fatal(err)
}

var spiMode xspi.Mode
switch mode {
case 0:
Expand All @@ -112,17 +102,17 @@ func GetSpiBus(busNum, mode int, maxSpeed int64) (spiDevice SPIDevice, err error
default:
spiMode = xspi.Mode0
}
dev := fmt.Sprintf("/dev/spidev0.%d", busNum)
devfs := &xspi.Devfs{
Dev: dev,
Mode: spiMode,
MaxSpeed: maxSpeed,
devName := fmt.Sprintf("/dev/spidev%d.%d", busNum, chipNum)
p, err := spireg.Open(devName)
if err != nil {
return nil, err
}
bus, err := xspi.Open(devfs)

c, err := p.Connect(maxSpeed, spiMode, bits)
if err != nil {
return nil, err
}
spiDevice = NewConnection(bus)
spiDevice = NewConnection(p, c)

return
}
Loading

0 comments on commit 7637d90

Please sign in to comment.