mirror of
https://github.com/kidoman/embd
synced 2024-12-22 12:50:19 +01:00
spi: variables/method name, err handling changes to follow idiomatic go
This commit is contained in:
parent
4191327632
commit
1bb2bac8f1
@ -8,17 +8,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type mcp3008 struct {
|
type mcp3008 struct {
|
||||||
mode int
|
mode byte
|
||||||
|
|
||||||
bus embd.SPIBus
|
bus embd.SPIBus
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
var SingleMode byte = 1
|
||||||
SingleMode = int(1)
|
var DifferenceMode byte = 0
|
||||||
DifferenceMode = int(0)
|
|
||||||
)
|
|
||||||
|
|
||||||
func New(mode, spiChan, speed int) (*mcp3008, error) {
|
func New(mode byte, spiChan, speed int) (*mcp3008, error) {
|
||||||
if err := embd.InitSPI(); err != nil {
|
if err := embd.InitSPI(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -28,7 +26,7 @@ func New(mode, spiChan, speed int) (*mcp3008, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *mcp3008) AnalogValueAt(chanNum int) (int, error) {
|
func (m *mcp3008) AnalogValueAt(chanNum int) (int, error) {
|
||||||
data := make([]uint8, 3)
|
var data [3]uint8
|
||||||
data[0] = 1
|
data[0] = 1
|
||||||
data[1] = uint8(m.mode)<<7 | uint8(chanNum)<<4
|
data[1] = uint8(m.mode)<<7 | uint8(chanNum)<<4
|
||||||
data[2] = 0
|
data[2] = 0
|
||||||
|
@ -96,7 +96,7 @@ var ledMap = embd.LEDMap{
|
|||||||
"beaglebone:green:usr3": []string{"3", "USR3", "usr3"},
|
"beaglebone:green:usr3": []string{"3", "USR3", "usr3"},
|
||||||
}
|
}
|
||||||
|
|
||||||
var spiDeviceMinor = byte(1)
|
var spiDeviceMinor byte = 1
|
||||||
|
|
||||||
func ensureFeatureEnabled(id string) error {
|
func ensureFeatureEnabled(id string) error {
|
||||||
glog.V(3).Infof("bbb: enabling feature %v", id)
|
glog.V(3).Infof("bbb: enabling feature %v", id)
|
||||||
|
@ -12,23 +12,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
spiIocWrMode = 0x40016B01
|
spiIOCWrMode = 0x40016B01
|
||||||
spiIocWrBitsPerWord = 0x40016B03
|
spiIOCWrBitsPerWord = 0x40016B03
|
||||||
spiIocWrMaxSpeedHz = 0x40046B04
|
spiIOCWrMaxSpeedHz = 0x40046B04
|
||||||
|
|
||||||
spiIocRdMode = 0x80016B01
|
spiIOCRdMode = 0x80016B01
|
||||||
spiIocRdBitsPerWord = 0x80016B03
|
spiIOCRdBitsPerWord = 0x80016B03
|
||||||
spiIocRdMaxSpeedHz = 0x80046B04
|
spiIOCRdMaxSpeedHz = 0x80046B04
|
||||||
|
|
||||||
spiIocMessage0 = 1073769216 //0x40006B00
|
spiIOCMessage0 = 1073769216 //0x40006B00
|
||||||
spiIocIncrementor = 2097152 //0x200000
|
spiIOCIncrementor = 2097152 //0x200000
|
||||||
|
|
||||||
defaultDelayms = uint16(0)
|
defaultDelayms = uint16(0)
|
||||||
defaultSpiBpw = uint8(8)
|
defaultSPIBPW = uint8(8)
|
||||||
defaultSpiSpeed = uint32(1000000)
|
defaultSPISpeed = uint32(1000000)
|
||||||
)
|
)
|
||||||
|
|
||||||
type spiIocTransfer struct {
|
type spiIOCTransfer struct {
|
||||||
txBuf uint64
|
txBuf uint64
|
||||||
rxBuf uint64
|
rxBuf uint64
|
||||||
|
|
||||||
@ -51,15 +51,15 @@ type spiBus struct {
|
|||||||
|
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
|
|
||||||
spiTransferData spiIocTransfer
|
spiTransferData spiIOCTransfer
|
||||||
initialized bool
|
initialized bool
|
||||||
|
|
||||||
shouldInitialize bool
|
shouldInitialize bool
|
||||||
initializer func() error
|
initializer func() error
|
||||||
}
|
}
|
||||||
|
|
||||||
func spiIocMessageN(n uint32) uint32 {
|
func spiIOCMessageN(n uint32) uint32 {
|
||||||
return (spiIocMessage0 + (n * spiIocIncrementor))
|
return (spiIOCMessage0 + (n * spiIOCIncrementor))
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSPIBus(spiDevMinor, mode, channel byte, speed, bpw, delay int, shouldInitialize bool, i func() error) embd.SPIBus {
|
func NewSPIBus(spiDevMinor, mode, channel byte, speed, bpw, delay int, shouldInitialize bool, i func() error) embd.SPIBus {
|
||||||
@ -98,14 +98,14 @@ func (b *spiBus) init() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
b.spiTransferData = spiIocTransfer{}
|
b.spiTransferData = spiIOCTransfer{}
|
||||||
|
|
||||||
err = b.setSpeed()
|
err = b.setSpeed()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = b.setBpw()
|
err = b.setBPW()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -113,7 +113,7 @@ func (b *spiBus) init() error {
|
|||||||
b.setDelay()
|
b.setDelay()
|
||||||
|
|
||||||
glog.V(2).Infof("spi: bus %v initialized", b.channel)
|
glog.V(2).Infof("spi: bus %v initialized", b.channel)
|
||||||
glog.V(3).Infof("spi: bus %v initialized with spiIocTransfer as %v", b.channel, b.spiTransferData)
|
glog.V(3).Infof("spi: bus %v initialized with spiIOCTransfer as %v", b.channel, b.spiTransferData)
|
||||||
|
|
||||||
b.initialized = true
|
b.initialized = true
|
||||||
return nil
|
return nil
|
||||||
@ -124,7 +124,7 @@ func (b *spiBus) setMode() error {
|
|||||||
var err error
|
var err error
|
||||||
glog.V(3).Infof("spi: setting spi mode to %v", mode)
|
glog.V(3).Infof("spi: setting spi mode to %v", mode)
|
||||||
|
|
||||||
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), spiIocWrMode, uintptr(unsafe.Pointer(&mode)))
|
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), spiIOCWrMode, uintptr(unsafe.Pointer(&mode)))
|
||||||
if errno != 0 {
|
if errno != 0 {
|
||||||
err = syscall.Errno(errno)
|
err = syscall.Errno(errno)
|
||||||
glog.V(3).Infof("spi: failed to set mode due to %v", err.Error())
|
glog.V(3).Infof("spi: failed to set mode due to %v", err.Error())
|
||||||
@ -139,11 +139,11 @@ func (b *spiBus) setSpeed() error {
|
|||||||
if b.speed > 0 {
|
if b.speed > 0 {
|
||||||
speed = uint32(b.speed)
|
speed = uint32(b.speed)
|
||||||
} else {
|
} else {
|
||||||
speed = defaultSpiSpeed
|
speed = defaultSPISpeed
|
||||||
}
|
}
|
||||||
|
|
||||||
glog.V(3).Infof("spi: setting spi speedMax to %v", speed)
|
glog.V(3).Infof("spi: setting spi speedMax to %v", speed)
|
||||||
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), spiIocWrMaxSpeedHz, uintptr(unsafe.Pointer(&speed)))
|
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), spiIOCWrMaxSpeedHz, uintptr(unsafe.Pointer(&speed)))
|
||||||
if errno != 0 {
|
if errno != 0 {
|
||||||
err := syscall.Errno(errno)
|
err := syscall.Errno(errno)
|
||||||
glog.V(3).Infof("spi: failed to set speedMax due to %v", err.Error())
|
glog.V(3).Infof("spi: failed to set speedMax due to %v", err.Error())
|
||||||
@ -155,17 +155,17 @@ func (b *spiBus) setSpeed() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *spiBus) setBpw() error {
|
func (b *spiBus) setBPW() error {
|
||||||
var bpw uint8
|
var bpw uint8
|
||||||
|
|
||||||
if b.bpw > 0 {
|
if b.bpw > 0 {
|
||||||
bpw = uint8(b.bpw)
|
bpw = uint8(b.bpw)
|
||||||
} else {
|
} else {
|
||||||
bpw = defaultSpiBpw
|
bpw = defaultSPIBPW
|
||||||
}
|
}
|
||||||
|
|
||||||
glog.V(3).Infof("spi: setting spi bpw to %v", bpw)
|
glog.V(3).Infof("spi: setting spi bpw to %v", bpw)
|
||||||
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), spiIocWrBitsPerWord, uintptr(unsafe.Pointer(&bpw)))
|
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), spiIOCWrBitsPerWord, uintptr(unsafe.Pointer(&bpw)))
|
||||||
if errno != 0 {
|
if errno != 0 {
|
||||||
err := syscall.Errno(errno)
|
err := syscall.Errno(errno)
|
||||||
glog.V(3).Infof("spi: failed to set bpw due to %v", err.Error())
|
glog.V(3).Infof("spi: failed to set bpw due to %v", err.Error())
|
||||||
@ -201,7 +201,7 @@ func (b *spiBus) TransferAndRecieveData(dataBuffer []uint8) error {
|
|||||||
dataCarrier.rxBuf = uint64(uintptr(unsafe.Pointer(&dataBuffer[0])))
|
dataCarrier.rxBuf = uint64(uintptr(unsafe.Pointer(&dataBuffer[0])))
|
||||||
|
|
||||||
glog.V(3).Infof("spi: sending dataBuffer %v with carrier %v", dataBuffer, dataCarrier)
|
glog.V(3).Infof("spi: sending dataBuffer %v with carrier %v", dataBuffer, dataCarrier)
|
||||||
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), uintptr(spiIocMessageN(1)), uintptr(unsafe.Pointer(&dataCarrier)))
|
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), uintptr(spiIOCMessageN(1)), uintptr(unsafe.Pointer(&dataCarrier)))
|
||||||
if errno != 0 {
|
if errno != 0 {
|
||||||
err := syscall.Errno(errno)
|
err := syscall.Errno(errno)
|
||||||
glog.V(3).Infof("spi: failed to read due to %v", err.Error())
|
glog.V(3).Infof("spi: failed to read due to %v", err.Error())
|
||||||
@ -217,9 +217,7 @@ func (b *spiBus) ReceiveData(len int) ([]uint8, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
data := make([]uint8, len)
|
data := make([]uint8, len)
|
||||||
var err error
|
if err := b.TransferAndRecieveData(data); err != nil {
|
||||||
err = b.TransferAndRecieveData(data)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return data, nil
|
return data, nil
|
||||||
|
@ -11,9 +11,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
if err := embd.InitSPI(); err != nil {
|
||||||
err = embd.InitSPI()
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
defer embd.CloseSPI()
|
defer embd.CloseSPI()
|
||||||
@ -23,30 +21,29 @@ func main() {
|
|||||||
|
|
||||||
dataBuf := []uint8{1, 2, 3}
|
dataBuf := []uint8{1, 2, 3}
|
||||||
|
|
||||||
err = spiBus.TransferAndRecieveData(dataBuf)
|
if err := spiBus.TransferAndRecieveData(dataBuf); err != nil {
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Recived data is: %v", dataBuf)
|
fmt.Println("received data is: %v", dataBuf)
|
||||||
|
|
||||||
dataReceived, err := spiBus.ReceiveData(3)
|
dataReceived, err := spiBus.ReceiveData(3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Recived data is: %v", dataReceived)
|
fmt.Println("received data is: %v", dataReceived)
|
||||||
|
|
||||||
dataByte := byte(1)
|
dataByte := byte(1)
|
||||||
receivedByte, err := spiBus.TransferAndReceiveByte(dataByte)
|
receivedByte, err := spiBus.TransferAndReceiveByte(dataByte)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Println("Recived byte is: %v", receivedByte)
|
fmt.Println("received byte is: %v", receivedByte)
|
||||||
|
|
||||||
receivedByte, err = spiBus.ReceiveByte()
|
receivedByte, err = spiBus.ReceiveByte()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
fmt.Println("Recived byte is: %v", receivedByte)
|
fmt.Println("received byte is: %v", receivedByte)
|
||||||
}
|
}
|
||||||
|
@ -18,7 +18,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bus := embd.NewSPIBus(embd.SpiMode0, 0, 1000000, 8, 0)
|
bus := embd.NewSPIBus(embd.SpiMode0, 0, 1000000, 8, 0)
|
||||||
|
|
||||||
defer clean(bus)
|
defer clean(bus)
|
||||||
|
|
||||||
for i := 0; i < 30; i++ {
|
for i := 0; i < 30; i++ {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user