2014-05-08 23:46:09 +05:30
|
|
|
// SPI support.
|
|
|
|
|
2014-04-28 01:18:14 +05:30
|
|
|
package embd
|
|
|
|
|
2015-01-13 13:43:23 -08:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2014-04-28 01:18:14 +05:30
|
|
|
const (
|
2014-04-28 03:13:57 +05:30
|
|
|
spiCpha = 0x01
|
|
|
|
spiCpol = 0x02
|
2014-04-28 01:18:14 +05:30
|
|
|
|
2014-05-22 02:19:01 +05:30
|
|
|
// SPIMode0 represents the mode0 operation (CPOL=0 CPHA=0) of spi.
|
|
|
|
SPIMode0 = (0 | 0)
|
2014-05-08 23:46:09 +05:30
|
|
|
|
2014-05-22 02:19:01 +05:30
|
|
|
// SPIMode1 represents the mode0 operation (CPOL=0 CPHA=1) of spi.
|
|
|
|
SPIMode1 = (0 | spiCpha)
|
2014-05-08 23:46:09 +05:30
|
|
|
|
2014-05-22 02:19:01 +05:30
|
|
|
// SPIMode2 represents the mode0 operation (CPOL=1 CPHA=0) of spi.
|
|
|
|
SPIMode2 = (spiCpol | 0)
|
2014-05-08 23:46:09 +05:30
|
|
|
|
2014-05-22 02:19:01 +05:30
|
|
|
// SPIMode3 represents the mode0 operation (CPOL=1 CPHA=1) of spi.
|
|
|
|
SPIMode3 = (spiCpol | spiCpha)
|
2014-04-28 01:18:14 +05:30
|
|
|
)
|
|
|
|
|
2014-05-22 14:41:07 +05:30
|
|
|
// SPIBus interface allows interaction with the SPI bus.
|
2014-04-28 01:18:14 +05:30
|
|
|
type SPIBus interface {
|
2015-01-13 13:43:23 -08:00
|
|
|
io.Writer
|
|
|
|
|
2015-12-22 12:54:33 -05:00
|
|
|
// TransferAndReceiveData transmits data in a buffer(slice) and receives into it.
|
|
|
|
TransferAndReceiveData(dataBuffer []uint8) error
|
2014-04-28 01:18:14 +05:30
|
|
|
|
2014-05-08 23:46:09 +05:30
|
|
|
// ReceiveData receives data of length len into a slice.
|
2014-04-28 01:18:14 +05:30
|
|
|
ReceiveData(len int) ([]uint8, error)
|
|
|
|
|
2014-05-08 23:46:09 +05:30
|
|
|
// TransferAndReceiveByte transmits a byte data and receives a byte.
|
2014-04-28 01:18:14 +05:30
|
|
|
TransferAndReceiveByte(data byte) (byte, error)
|
|
|
|
|
2014-05-08 23:46:09 +05:30
|
|
|
// ReceiveByte receives a byte data.
|
2014-04-28 01:18:14 +05:30
|
|
|
ReceiveByte() (byte, error)
|
|
|
|
|
2014-05-08 23:46:09 +05:30
|
|
|
// Close releases the resources associated with the bus.
|
2014-04-28 01:18:14 +05:30
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2014-05-08 23:46:09 +05:30
|
|
|
// SPIDriver interface interacts with the host descriptors to allow us
|
|
|
|
// control of SPI communication.
|
2014-04-28 01:18:14 +05:30
|
|
|
type SPIDriver interface {
|
2014-05-22 02:31:42 +05:30
|
|
|
// Bus returns a SPIBus interface which allows us to use spi functionalities
|
2014-04-28 01:18:14 +05:30
|
|
|
Bus(byte, byte, int, int, int) SPIBus
|
|
|
|
|
2014-05-22 02:31:42 +05:30
|
|
|
// Close cleans up all the initialized SPIbus
|
2014-04-28 01:18:14 +05:30
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
var spiDriverInitialized bool
|
|
|
|
var spiDriverInstance SPIDriver
|
|
|
|
|
2014-05-08 23:46:09 +05:30
|
|
|
// InitSPI initializes the SPI driver.
|
2014-04-28 01:18:14 +05:30
|
|
|
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 23:46:09 +05:30
|
|
|
// CloseSPI releases resources associated with the SPI driver.
|
2014-04-28 01:18:14 +05:30
|
|
|
func CloseSPI() error {
|
|
|
|
return spiDriverInstance.Close()
|
|
|
|
}
|
|
|
|
|
2014-05-08 23:46:09 +05:30
|
|
|
// NewSPIBus returns a SPIBus.
|
2014-04-28 01:18:14 +05:30
|
|
|
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)
|
|
|
|
}
|