1
0
mirror of https://github.com/kidoman/embd synced 2024-10-31 23:38:44 +01:00
embd/spi.go

93 lines
2.0 KiB
Go
Raw Normal View History

2014-05-08 20:16:09 +02:00
// SPI support.
package embd
import (
"io"
)
const (
spiCpha = 0x01
spiCpol = 0x02
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-05-22 11:11:07 +02:00
// SPIBus interface allows interaction with the SPI bus.
type SPIBus interface {
io.Writer
// TransferAndReceiveData transmits data in a buffer(slice) and receives into it.
TransferAndReceiveData(dataBuffer []uint8) error
2014-05-08 20:16:09 +02:00
// ReceiveData receives data of length len into a slice.
ReceiveData(len int) ([]uint8, error)
2014-05-08 20:16:09 +02:00
// TransferAndReceiveByte transmits a byte data and receives a byte.
TransferAndReceiveByte(data byte) (byte, error)
2014-05-08 20:16:09 +02:00
// ReceiveByte receives a byte data.
ReceiveByte() (byte, error)
2014-05-08 20:16:09 +02:00
// Close releases the resources associated with the bus.
Close() error
}
2014-05-08 20:16:09 +02:00
// SPIDriver interface interacts with the host descriptors to allow us
// control of SPI communication.
type SPIDriver interface {
2014-05-21 23:01:42 +02:00
// Bus returns a SPIBus interface which allows us to use spi functionalities
Bus(byte, byte, int, int, int) SPIBus
2014-05-21 23:01:42 +02:00
// Close cleans up all the initialized SPIbus
Close() error
}
var spiDriverInitialized bool
var spiDriverInstance SPIDriver
2014-05-08 20:16:09 +02:00
// InitSPI initializes the SPI driver.
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.
func CloseSPI() error {
return spiDriverInstance.Close()
}
2014-05-08 20:16:09 +02:00
// NewSPIBus returns a SPIBus.
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)
}