2014-05-08 20:16:09 +02:00
|
|
|
// SPI support.
|
|
|
|
|
2014-04-27 21:48:14 +02:00
|
|
|
package embd
|
|
|
|
|
2015-01-13 22:43:23 +01:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2014-04-27 21:48:14 +02:00
|
|
|
const (
|
2014-04-27 23:43:57 +02:00
|
|
|
spiCpha = 0x01
|
|
|
|
spiCpol = 0x02
|
2014-04-27 21:48:14 +02:00
|
|
|
|
2014-05-21 22:49:01 +02:00
|
|
|
// SPIMode0 represents the mode0 operation (CPOL=0 CPHA=0) of spi.
|
|
|
|
SPIMode0 = (0 | 0)
|
2014-05-08 20:16:09 +02:00
|
|
|
|
2014-05-21 22:49:01 +02:00
|
|
|
// SPIMode1 represents the mode0 operation (CPOL=0 CPHA=1) of spi.
|
|
|
|
SPIMode1 = (0 | spiCpha)
|
2014-05-08 20:16:09 +02:00
|
|
|
|
2014-05-21 22:49:01 +02:00
|
|
|
// SPIMode2 represents the mode0 operation (CPOL=1 CPHA=0) of spi.
|
|
|
|
SPIMode2 = (spiCpol | 0)
|
2014-05-08 20:16:09 +02:00
|
|
|
|
2014-05-21 22:49:01 +02:00
|
|
|
// SPIMode3 represents the mode0 operation (CPOL=1 CPHA=1) of spi.
|
|
|
|
SPIMode3 = (spiCpol | spiCpha)
|
2014-04-27 21:48:14 +02:00
|
|
|
)
|
|
|
|
|
2014-05-22 11:11:07 +02:00
|
|
|
// SPIBus interface allows interaction with the SPI bus.
|
2014-04-27 21:48:14 +02:00
|
|
|
type SPIBus interface {
|
2015-01-13 22:43:23 +01:00
|
|
|
io.Writer
|
|
|
|
|
2014-05-08 20:16:09 +02:00
|
|
|
// TransferAndRecieveData transmits data in a buffer(slice) and receives into it.
|
2014-04-27 21:48:14 +02:00
|
|
|
TransferAndRecieveData(dataBuffer []uint8) error
|
|
|
|
|
2014-05-08 20:16:09 +02:00
|
|
|
// ReceiveData receives data of length len into a slice.
|
2014-04-27 21:48:14 +02:00
|
|
|
ReceiveData(len int) ([]uint8, error)
|
|
|
|
|
2014-05-08 20:16:09 +02:00
|
|
|
// TransferAndReceiveByte transmits a byte data and receives a byte.
|
2014-04-27 21:48:14 +02:00
|
|
|
TransferAndReceiveByte(data byte) (byte, error)
|
|
|
|
|
2014-05-08 20:16:09 +02:00
|
|
|
// ReceiveByte receives a byte data.
|
2014-04-27 21:48:14 +02:00
|
|
|
ReceiveByte() (byte, error)
|
|
|
|
|
2014-05-08 20:16:09 +02:00
|
|
|
// Close releases the resources associated with the bus.
|
2014-04-27 21:48:14 +02:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2014-05-08 20:16:09 +02:00
|
|
|
// SPIDriver interface interacts with the host descriptors to allow us
|
|
|
|
// control of SPI communication.
|
2014-04-27 21:48:14 +02:00
|
|
|
type SPIDriver interface {
|
2014-05-21 23:01:42 +02:00
|
|
|
// Bus returns a SPIBus interface which allows us to use spi functionalities
|
2014-04-27 21:48:14 +02:00
|
|
|
Bus(byte, byte, int, int, int) SPIBus
|
|
|
|
|
2014-05-21 23:01:42 +02:00
|
|
|
// Close cleans up all the initialized SPIbus
|
2014-04-27 21:48:14 +02:00
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
var spiDriverInitialized bool
|
|
|
|
var spiDriverInstance SPIDriver
|
|
|
|
|
2014-05-08 20:16:09 +02:00
|
|
|
// InitSPI initializes the SPI driver.
|
2014-04-27 21:48:14 +02:00
|
|
|
func InitSPI() error {
|
|
|
|
if spiDriverInitialized {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
desc, err := DescribeHost()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if desc.SPIDriver == nil {
|
|
|
|
return ErrFeatureNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
spiDriverInstance = desc.SPIDriver()
|
|
|
|
spiDriverInitialized = true
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-08 20:16:09 +02:00
|
|
|
// CloseSPI releases resources associated with the SPI driver.
|
2014-04-27 21:48:14 +02:00
|
|
|
func CloseSPI() error {
|
|
|
|
return spiDriverInstance.Close()
|
|
|
|
}
|
|
|
|
|
2014-05-08 20:16:09 +02:00
|
|
|
// NewSPIBus returns a SPIBus.
|
2014-04-27 21:48:14 +02:00
|
|
|
func NewSPIBus(mode, channel byte, speed, bpw, delay int) SPIBus {
|
|
|
|
if err := InitSPI(); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return spiDriverInstance.Bus(mode, channel, speed, bpw, delay)
|
|
|
|
}
|