Skip to content

Commit

Permalink
raspi: extract shared SPI init code into spi package
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Dec 13, 2017
1 parent 8e28bcc commit 6b960a0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 46 deletions.
41 changes: 6 additions & 35 deletions platforms/raspi/raspi_adaptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/drivers/spi"
"gobot.io/x/gobot/sysfs"
xspi "golang.org/x/exp/io/spi"
)

var readFile = func() ([]byte, error) {
Expand Down Expand Up @@ -209,46 +208,18 @@ func (r *Adaptor) GetDefaultBus() int {
// GetSpiConnection returns an spi connection to a device on a specified bus.
// Valid bus number is [0..1] which corresponds to /dev/spidev0.0 through /dev/spidev0.1.
func (r *Adaptor) GetSpiConnection(busNum, mode int, maxSpeed int64) (connection spi.Connection, err error) {
r.mutex.Lock()
defer r.mutex.Unlock()

if (busNum < 0) || (busNum > 1) {
return nil, fmt.Errorf("Bus number %d out of range", busNum)
}
device, err := r.getSpiBus(busNum, mode, maxSpeed)
return spi.NewConnection(device), err
}

func (r *Adaptor) getSpiBus(busNum, mode int, maxSpeed int64) (_ spi.SPIDevice, err error) {
r.mutex.Lock()
defer r.mutex.Unlock()

if r.spiBuses[busNum] == nil {
var spiMode xspi.Mode
switch mode {
case 0:
spiMode = xspi.Mode0
case 1:
spiMode = xspi.Mode1
case 2:
spiMode = xspi.Mode2
case 3:
spiMode = xspi.Mode3
default:
spiMode = xspi.Mode0
}
dev := fmt.Sprintf("/dev/spidev0.%d", busNum)
devfs := &xspi.Devfs{
Dev: dev,
Mode: spiMode,
MaxSpeed: maxSpeed,
}
if r.spiBuses[busNum] == nil {
bus, err := xspi.Open(devfs)
if err != nil {
return nil, err
}
r.spiBuses[busNum] = spi.NewConnection(bus)
}
r.spiBuses[busNum], err = spi.GetSpiBus(busNum, mode, maxSpeed)
}
return r.spiBuses[busNum], err

return spi.NewConnection(r.spiBuses[busNum]), err
}

// GetSpiDefaultBus returns the default spi bus for this platform.
Expand Down
20 changes: 9 additions & 11 deletions platforms/raspi/raspi_adaptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import (
"strings"
"testing"

"runtime"
"strconv"
"sync"

"gobot.io/x/gobot"
"gobot.io/x/gobot/drivers/gpio"
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/drivers/spi"
"gobot.io/x/gobot/gobottest"
"gobot.io/x/gobot/sysfs"
"runtime"
"strconv"
"sync"
)

// make sure that this Adaptor fullfills all the required interfaces
Expand Down Expand Up @@ -189,18 +190,15 @@ func TestAdaptorSPI(t *testing.T) {
})
sysfs.SetFilesystem(fs)
sysfs.SetSyscall(&sysfs.MockSyscall{})
// TODO: find a better way to test this
_, err := a.GetSpiConnection(1, 0, 500000)
gobottest.Assert(t, err, err)

gobottest.Assert(t, a.GetSpiDefaultBus(), 1)
gobottest.Assert(t, a.GetSpiDefaultMode(), 0)
gobottest.Assert(t, a.GetSpiDefaultMaxSpeed(), int64(500000))

_, err = a.GetSpiConnection(1, 1, 500000)
_, err = a.GetSpiConnection(1, 2, 500000)
_, err = a.GetSpiConnection(1, 3, 500000)
_, err = a.GetSpiConnection(1, 5, 500000)
_, err = a.GetSpiConnection(4, 0, 500000)
_, err := a.GetSpiConnection(10, 0, 500000)
gobottest.Assert(t, err.Error(), "Bus number 10 out of range")

// TODO: test tx/rx here...
}

func TestAdaptorDigitalPinConcurrency(t *testing.T) {
Expand Down

0 comments on commit 6b960a0

Please sign in to comment.