2014-04-27 21:48:14 +02:00
|
|
|
package generic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
"github.com/kidoman/embd"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2014-05-08 19:39:31 +02:00
|
|
|
spiIOCWrMode = 0x40016B01
|
|
|
|
spiIOCWrBitsPerWord = 0x40016B03
|
|
|
|
spiIOCWrMaxSpeedHz = 0x40046B04
|
2014-04-27 21:48:14 +02:00
|
|
|
|
2014-05-08 19:39:31 +02:00
|
|
|
spiIOCRdMode = 0x80016B01
|
|
|
|
spiIOCRdBitsPerWord = 0x80016B03
|
|
|
|
spiIOCRdMaxSpeedHz = 0x80046B04
|
2014-04-27 21:48:14 +02:00
|
|
|
|
2014-05-08 19:39:31 +02:00
|
|
|
spiIOCMessage0 = 1073769216 //0x40006B00
|
|
|
|
spiIOCIncrementor = 2097152 //0x200000
|
2014-04-27 21:48:14 +02:00
|
|
|
|
2014-05-21 23:28:15 +02:00
|
|
|
defaultDelayms = 0
|
|
|
|
defaultSPIBPW = 8
|
|
|
|
defaultSPISpeed = 1000000
|
2014-04-27 21:48:14 +02:00
|
|
|
)
|
|
|
|
|
2014-05-08 19:39:31 +02:00
|
|
|
type spiIOCTransfer struct {
|
2014-04-27 23:43:57 +02:00
|
|
|
txBuf uint64
|
|
|
|
rxBuf uint64
|
2014-04-27 21:48:14 +02:00
|
|
|
|
2014-04-27 23:43:57 +02:00
|
|
|
length uint32
|
|
|
|
speedHz uint32
|
|
|
|
delayus uint16
|
|
|
|
bitsPerWord uint8
|
2015-11-03 14:54:27 +01:00
|
|
|
csChange uint8
|
|
|
|
pad uint32
|
2014-04-27 21:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type spiBus struct {
|
|
|
|
file *os.File
|
|
|
|
|
|
|
|
spiDevMinor byte
|
|
|
|
|
|
|
|
channel byte
|
|
|
|
mode byte
|
|
|
|
speed int
|
|
|
|
bpw int
|
|
|
|
delayms int
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
|
2014-05-08 19:39:31 +02:00
|
|
|
spiTransferData spiIOCTransfer
|
2014-04-27 21:48:14 +02:00
|
|
|
initialized bool
|
2014-05-01 19:14:41 +02:00
|
|
|
|
2014-05-21 21:51:09 +02:00
|
|
|
initializer func() error
|
2014-04-27 21:48:14 +02:00
|
|
|
}
|
|
|
|
|
2014-05-08 19:39:31 +02:00
|
|
|
func spiIOCMessageN(n uint32) uint32 {
|
|
|
|
return (spiIOCMessage0 + (n * spiIOCIncrementor))
|
2014-04-27 21:48:14 +02:00
|
|
|
}
|
|
|
|
|
2014-05-21 21:51:09 +02:00
|
|
|
func NewSPIBus(spiDevMinor, mode, channel byte, speed, bpw, delay int, i func() error) embd.SPIBus {
|
2014-04-27 21:48:14 +02:00
|
|
|
return &spiBus{
|
2014-05-21 21:51:09 +02:00
|
|
|
spiDevMinor: spiDevMinor,
|
|
|
|
mode: mode,
|
|
|
|
channel: channel,
|
|
|
|
speed: speed,
|
|
|
|
bpw: bpw,
|
|
|
|
delayms: delay,
|
|
|
|
initializer: i,
|
2014-04-27 21:48:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *spiBus) init() error {
|
2014-05-03 18:05:09 +02:00
|
|
|
if b.initialized {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-21 21:51:09 +02:00
|
|
|
if b.initializer != nil {
|
2014-05-01 19:14:41 +02:00
|
|
|
if err := b.initializer(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 21:48:14 +02:00
|
|
|
var err error
|
|
|
|
if b.file, err = os.OpenFile(fmt.Sprintf("/dev/spidev%v.%v", b.spiDevMinor, b.channel), os.O_RDWR, os.ModeExclusive); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-03 18:05:09 +02:00
|
|
|
glog.V(3).Infof("spi: sucessfully opened file /dev/spidev%v.%v", b.spiDevMinor, b.channel)
|
2014-04-27 21:48:14 +02:00
|
|
|
|
2014-05-21 21:51:09 +02:00
|
|
|
if err = b.setMode(); err != nil {
|
2014-04-27 21:48:14 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-05-08 19:39:31 +02:00
|
|
|
b.spiTransferData = spiIOCTransfer{}
|
2014-04-27 21:48:14 +02:00
|
|
|
|
2014-05-21 21:51:09 +02:00
|
|
|
if err = b.setSpeed(); err != nil {
|
2014-04-27 21:48:14 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-05-21 21:51:09 +02:00
|
|
|
if err = b.setBPW(); err != nil {
|
2014-04-27 21:48:14 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b.setDelay()
|
|
|
|
|
|
|
|
glog.V(2).Infof("spi: bus %v initialized", b.channel)
|
2014-05-08 19:39:31 +02:00
|
|
|
glog.V(3).Infof("spi: bus %v initialized with spiIOCTransfer as %v", b.channel, b.spiTransferData)
|
2014-04-27 21:48:14 +02:00
|
|
|
|
|
|
|
b.initialized = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *spiBus) setMode() error {
|
|
|
|
var mode = uint8(b.mode)
|
|
|
|
glog.V(3).Infof("spi: setting spi mode to %v", mode)
|
|
|
|
|
2014-05-08 19:39:31 +02:00
|
|
|
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), spiIOCWrMode, uintptr(unsafe.Pointer(&mode)))
|
2014-04-27 21:48:14 +02:00
|
|
|
if errno != 0 {
|
2014-05-21 23:28:15 +02:00
|
|
|
err := syscall.Errno(errno)
|
2014-04-27 21:48:14 +02:00
|
|
|
glog.V(3).Infof("spi: failed to set mode due to %v", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.V(3).Infof("spi: mode set to %v", mode)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *spiBus) setSpeed() error {
|
2014-05-21 23:28:15 +02:00
|
|
|
var speed uint32 = defaultSPISpeed
|
2014-04-27 21:48:14 +02:00
|
|
|
if b.speed > 0 {
|
|
|
|
speed = uint32(b.speed)
|
|
|
|
}
|
|
|
|
|
2014-04-27 23:43:57 +02:00
|
|
|
glog.V(3).Infof("spi: setting spi speedMax to %v", speed)
|
2014-05-08 19:39:31 +02:00
|
|
|
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), spiIOCWrMaxSpeedHz, uintptr(unsafe.Pointer(&speed)))
|
2014-04-27 21:48:14 +02:00
|
|
|
if errno != 0 {
|
|
|
|
err := syscall.Errno(errno)
|
2014-04-27 23:43:57 +02:00
|
|
|
glog.V(3).Infof("spi: failed to set speedMax due to %v", err.Error())
|
2014-04-27 21:48:14 +02:00
|
|
|
return err
|
|
|
|
}
|
2014-04-27 23:43:57 +02:00
|
|
|
glog.V(3).Infof("spi: speedMax set to %v", speed)
|
|
|
|
b.spiTransferData.speedHz = speed
|
2014-04-27 21:48:14 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-08 19:39:31 +02:00
|
|
|
func (b *spiBus) setBPW() error {
|
2014-05-21 23:28:15 +02:00
|
|
|
var bpw uint8 = defaultSPIBPW
|
2014-04-27 21:48:14 +02:00
|
|
|
if b.bpw > 0 {
|
|
|
|
bpw = uint8(b.bpw)
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(3).Infof("spi: setting spi bpw to %v", bpw)
|
2014-05-08 19:39:31 +02:00
|
|
|
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), spiIOCWrBitsPerWord, uintptr(unsafe.Pointer(&bpw)))
|
2014-04-27 21:48:14 +02:00
|
|
|
if errno != 0 {
|
|
|
|
err := syscall.Errno(errno)
|
|
|
|
glog.V(3).Infof("spi: failed to set bpw due to %v", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
glog.V(3).Infof("spi: bpw set to %v", bpw)
|
2014-04-27 23:43:57 +02:00
|
|
|
b.spiTransferData.bitsPerWord = uint8(bpw)
|
2014-04-27 21:48:14 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *spiBus) setDelay() {
|
2014-05-21 23:28:15 +02:00
|
|
|
var delay uint16 = defaultDelayms
|
2014-04-27 21:48:14 +02:00
|
|
|
if b.delayms > 0 {
|
|
|
|
delay = uint16(b.delayms)
|
|
|
|
}
|
2014-05-21 22:49:01 +02:00
|
|
|
|
2014-04-27 23:43:57 +02:00
|
|
|
glog.V(3).Infof("spi: delayms set to %v", delay)
|
|
|
|
b.spiTransferData.delayus = delay
|
2014-04-27 21:48:14 +02:00
|
|
|
}
|
|
|
|
|
2015-12-22 18:54:33 +01:00
|
|
|
func (b *spiBus) TransferAndReceiveData(dataBuffer []uint8) error {
|
2014-05-03 18:05:09 +02:00
|
|
|
if err := b.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-04-27 21:48:14 +02:00
|
|
|
len := len(dataBuffer)
|
|
|
|
dataCarrier := b.spiTransferData
|
|
|
|
|
|
|
|
dataCarrier.length = uint32(len)
|
2014-04-27 23:43:57 +02:00
|
|
|
dataCarrier.txBuf = uint64(uintptr(unsafe.Pointer(&dataBuffer[0])))
|
|
|
|
dataCarrier.rxBuf = uint64(uintptr(unsafe.Pointer(&dataBuffer[0])))
|
2014-04-27 21:48:14 +02:00
|
|
|
|
2014-05-03 18:05:09 +02:00
|
|
|
glog.V(3).Infof("spi: sending dataBuffer %v with carrier %v", dataBuffer, dataCarrier)
|
2014-05-08 19:39:31 +02:00
|
|
|
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), uintptr(spiIOCMessageN(1)), uintptr(unsafe.Pointer(&dataCarrier)))
|
2014-04-27 21:48:14 +02:00
|
|
|
if errno != 0 {
|
|
|
|
err := syscall.Errno(errno)
|
|
|
|
glog.V(3).Infof("spi: failed to read due to %v", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
2014-05-03 18:05:09 +02:00
|
|
|
glog.V(3).Infof("spi: read into dataBuffer %v", dataBuffer)
|
2014-04-27 21:48:14 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *spiBus) ReceiveData(len int) ([]uint8, error) {
|
2014-05-03 18:05:09 +02:00
|
|
|
if err := b.init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-05-21 23:33:22 +02:00
|
|
|
data := make([]uint8, len)
|
2015-12-22 18:54:33 +01:00
|
|
|
if err := b.TransferAndReceiveData(data); err != nil {
|
2014-04-27 21:48:14 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-21 23:33:22 +02:00
|
|
|
return data, nil
|
2014-04-27 21:48:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *spiBus) TransferAndReceiveByte(data byte) (byte, error) {
|
2014-05-03 18:05:09 +02:00
|
|
|
if err := b.init(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2014-05-21 22:49:01 +02:00
|
|
|
d := [1]uint8{uint8(data)}
|
2015-12-22 18:54:33 +01:00
|
|
|
if err := b.TransferAndReceiveData(d[:]); err != nil {
|
2014-04-27 21:48:14 +02:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return d[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *spiBus) ReceiveByte() (byte, error) {
|
2014-05-03 18:05:09 +02:00
|
|
|
if err := b.init(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2014-05-21 22:49:01 +02:00
|
|
|
var d [1]uint8
|
2015-12-22 18:54:33 +01:00
|
|
|
if err := b.TransferAndReceiveData(d[:]); err != nil {
|
2014-04-27 21:48:14 +02:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return byte(d[0]), nil
|
|
|
|
}
|
|
|
|
|
2015-01-13 22:43:23 +01:00
|
|
|
func (b *spiBus) Write(data []byte) (n int, err error) {
|
|
|
|
if err := b.init(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return b.file.Write(data)
|
|
|
|
}
|
|
|
|
|
2014-04-27 21:48:14 +02:00
|
|
|
func (b *spiBus) Close() error {
|
|
|
|
b.mu.Lock()
|
|
|
|
defer b.mu.Unlock()
|
|
|
|
|
|
|
|
if !b.initialized {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.file.Close()
|
|
|
|
}
|