1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 04:40:04 +01:00

spi: finishing touches

This commit is contained in:
kunalpowar 2014-05-22 02:19:01 +05:30
parent 0f23150374
commit 67d316851a
7 changed files with 49 additions and 45 deletions

View File

@ -3,11 +3,10 @@ package mcp3008
import (
"github.com/golang/glog"
"github.com/kidoman/embd"
_ "github.com/kidoman/embd/host/all"
)
type mcp3008 struct {
// MCP3008 represents a mcp3008 8bit DAC
type MCP3008 struct {
mode byte
bus embd.SPIBus
@ -16,18 +15,22 @@ type mcp3008 struct {
var SingleMode byte = 1
var DifferenceMode byte = 0
func New(mode byte, bus embd.SPIBus) *mcp3008 {
return &mcp3008{mode, bus}
func New(mode byte, bus embd.SPIBus) *MCP3008 {
return &MCP3008{mode, bus}
}
func (m *mcp3008) AnalogValueAt(chanNum int) (int, error) {
data := make([]uint8, 3)
data[0] = 1
const (
startBit = 1
)
func (m *MCP3008) AnalogValueAt(chanNum int) (int, error) {
var data [3]uint8
data[0] = startBit
data[1] = uint8(m.mode)<<7 | uint8(chanNum)<<4
data[2] = 0
glog.V(2).Infof("mcp3008: sendingdata buffer %v", data)
if err := m.bus.TransferAndRecieveData(data); err != nil {
if err := m.bus.TransferAndRecieveData(data[:]); err != nil {
return 0, err
}

View File

@ -16,7 +16,6 @@ import (
"strings"
"github.com/golang/glog"
"github.com/kidoman/embd"
"github.com/kidoman/embd/host/generic"
)

View File

@ -129,11 +129,9 @@ func (b *spiBus) setMode() error {
}
func (b *spiBus) setSpeed() error {
var speed uint32
speed := defaultSPISpeed
if b.speed > 0 {
speed = uint32(b.speed)
} else {
speed = defaultSPISpeed
}
glog.V(3).Infof("spi: setting spi speedMax to %v", speed)
@ -150,12 +148,9 @@ func (b *spiBus) setSpeed() error {
}
func (b *spiBus) setBPW() error {
var bpw uint8
bpw := defaultSPIBPW
if b.bpw > 0 {
bpw = uint8(b.bpw)
} else {
bpw = defaultSPIBPW
}
glog.V(3).Infof("spi: setting spi bpw to %v", bpw)
@ -171,13 +166,11 @@ func (b *spiBus) setBPW() error {
}
func (b *spiBus) setDelay() {
var delay uint16
delay := defaultDelayms
if b.delayms > 0 {
delay = uint16(b.delayms)
} else {
delay = defaultDelayms
}
glog.V(3).Infof("spi: delayms set to %v", delay)
b.spiTransferData.delayus = delay
}
@ -210,11 +203,11 @@ func (b *spiBus) ReceiveData(len int) ([]uint8, error) {
return nil, err
}
data := make([]uint8, len)
if err := b.TransferAndRecieveData(data); err != nil {
var data [len]uint8
if err := b.TransferAndRecieveData(data[:]); err != nil {
return nil, err
}
return data, nil
return data[:], nil
}
func (b *spiBus) TransferAndReceiveByte(data byte) (byte, error) {
@ -222,9 +215,8 @@ func (b *spiBus) TransferAndReceiveByte(data byte) (byte, error) {
return 0, err
}
d := make([]uint8, 1)
d[0] = uint8(data)
if err := b.TransferAndRecieveData(d); err != nil {
d := [1]uint8{uint8(data)}
if err := b.TransferAndRecieveData(d[:]); err != nil {
return 0, err
}
return d[0], nil
@ -235,9 +227,8 @@ func (b *spiBus) ReceiveByte() (byte, error) {
return 0, err
}
d := make([]uint8, 1)
err := b.TransferAndRecieveData(d)
if err != nil {
var d [1]uint8
if err := b.TransferAndRecieveData(d[:]); err != nil {
return 0, err
}
return byte(d[0]), nil

View File

@ -1,5 +1,6 @@
// +build ignore
// this sample uses the mcp3008 package to interface with the 8-bit ADC and works without code change on bbb and rpi
package main
import (
@ -7,9 +8,9 @@ import (
"fmt"
"time"
"github.com/kidoman/embd/convertors/mcp3008"
"github.com/kidoman/embd"
"github.com/kidoman/embd/convertors/mcp3008"
_ "github.com/kidoman/embd/host/all"
)
func main() {
@ -21,7 +22,11 @@ func main() {
}
defer embd.CloseSPI()
spiBus := embd.NewSPIBus(embd.SpiMode0, 0, 1000000, 8, 0)
channel := 0
speed := 1000000
bpw := 8
delay := 0
spiBus := embd.NewSPIBus(embd.SPIMode0, channel, speed, bpw, delay)
defer spiBus.Close()
adc := mcp3008.New(mcp3008.SingleMode, spiBus)

View File

@ -6,7 +6,6 @@ import (
"fmt"
"github.com/kidoman/embd"
_ "github.com/kidoman/embd/host/all"
)
@ -16,7 +15,7 @@ func main() {
}
defer embd.CloseSPI()
spiBus := embd.NewSPIBus(embd.SpiMode0, 0, 1000000, 8, 0)
spiBus := embd.NewSPIBus(embd.SPIMode0, 0, 1000000, 8, 0)
defer spiBus.Close()
dataBuf := []uint8{1, 2, 3}

View File

@ -18,12 +18,19 @@ func main() {
}
defer embd.CloseSPI()
bus := embd.NewSPIBus(embd.SpiMode0, 0, 1000000, 8, 0)
channel := 0
speed := 1000000
bpw := 8
delay := 0
bus := embd.NewSPIBus(embd.SPIMode0, channel, speed, bpw, delay)
defer bus.Close()
for i := 0; i < 30; i++ {
time.Sleep(1 * time.Second)
val, _ := getSensorValue(bus)
val, err := getSensorValue(bus)
if err != nil {
panic(err)
}
fmt.Printf("value is: %v\n", val)
}
}

16
spi.go
View File

@ -6,17 +6,17 @@ const (
spiCpha = 0x01
spiCpol = 0x02
// SpiMode0 represents the mode0 operation (CPOL=0 CPHA=0) of spi.
SpiMode0 = (0 | 0)
// SPIMode0 represents the mode0 operation (CPOL=0 CPHA=0) of spi.
SPIMode0 = (0 | 0)
// SpiMode0 represents the mode0 operation (CPOL=0 CPHA=1) of spi.
SpiMode1 = (0 | spiCpha)
// SPIMode1 represents the mode0 operation (CPOL=0 CPHA=1) of spi.
SPIMode1 = (0 | spiCpha)
// SpiMode0 represents the mode0 operation (CPOL=1 CPHA=0) of spi.
SpiMode2 = (spiCpol | 0)
// SPIMode2 represents the mode0 operation (CPOL=1 CPHA=0) of spi.
SPIMode2 = (spiCpol | 0)
// SpiMode0 represents the mode0 operation (CPOL=1 CPHA=1) of spi.
SpiMode3 = (spiCpol | spiCpha)
// SPIMode3 represents the mode0 operation (CPOL=1 CPHA=1) of spi.
SPIMode3 = (spiCpol | spiCpha)
)
// SPI interface allows interaction with the SPI bus.