1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 12:50:19 +01:00
embd/spi.go

93 lines
2.0 KiB
Go
Raw Normal View History

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