1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 04:40:04 +01:00

spi: variables/method name, err handling changes to follow idiomatic go

This commit is contained in:
kunalpowar 2014-05-08 23:09:31 +05:30
parent 4191327632
commit 1bb2bac8f1
5 changed files with 37 additions and 45 deletions

View File

@ -8,17 +8,15 @@ import (
)
type mcp3008 struct {
mode int
mode byte
bus embd.SPIBus
}
const (
SingleMode = int(1)
DifferenceMode = int(0)
)
var SingleMode byte = 1
var DifferenceMode byte = 0
func New(mode, spiChan, speed int) (*mcp3008, error) {
func New(mode byte, spiChan, speed int) (*mcp3008, error) {
if err := embd.InitSPI(); err != nil {
return nil, err
}
@ -28,7 +26,7 @@ func New(mode, spiChan, speed int) (*mcp3008, error) {
}
func (m *mcp3008) AnalogValueAt(chanNum int) (int, error) {
data := make([]uint8, 3)
var data [3]uint8
data[0] = 1
data[1] = uint8(m.mode)<<7 | uint8(chanNum)<<4
data[2] = 0

View File

@ -96,7 +96,7 @@ var ledMap = embd.LEDMap{
"beaglebone:green:usr3": []string{"3", "USR3", "usr3"},
}
var spiDeviceMinor = byte(1)
var spiDeviceMinor byte = 1
func ensureFeatureEnabled(id string) error {
glog.V(3).Infof("bbb: enabling feature %v", id)

View File

@ -12,23 +12,23 @@ import (
)
const (
spiIocWrMode = 0x40016B01
spiIocWrBitsPerWord = 0x40016B03
spiIocWrMaxSpeedHz = 0x40046B04
spiIOCWrMode = 0x40016B01
spiIOCWrBitsPerWord = 0x40016B03
spiIOCWrMaxSpeedHz = 0x40046B04
spiIocRdMode = 0x80016B01
spiIocRdBitsPerWord = 0x80016B03
spiIocRdMaxSpeedHz = 0x80046B04
spiIOCRdMode = 0x80016B01
spiIOCRdBitsPerWord = 0x80016B03
spiIOCRdMaxSpeedHz = 0x80046B04
spiIocMessage0 = 1073769216 //0x40006B00
spiIocIncrementor = 2097152 //0x200000
spiIOCMessage0 = 1073769216 //0x40006B00
spiIOCIncrementor = 2097152 //0x200000
defaultDelayms = uint16(0)
defaultSpiBpw = uint8(8)
defaultSpiSpeed = uint32(1000000)
defaultSPIBPW = uint8(8)
defaultSPISpeed = uint32(1000000)
)
type spiIocTransfer struct {
type spiIOCTransfer struct {
txBuf uint64
rxBuf uint64
@ -51,15 +51,15 @@ type spiBus struct {
mu sync.Mutex
spiTransferData spiIocTransfer
spiTransferData spiIOCTransfer
initialized bool
shouldInitialize bool
initializer func() error
}
func spiIocMessageN(n uint32) uint32 {
return (spiIocMessage0 + (n * spiIocIncrementor))
func spiIOCMessageN(n uint32) uint32 {
return (spiIOCMessage0 + (n * spiIOCIncrementor))
}
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
}
b.spiTransferData = spiIocTransfer{}
b.spiTransferData = spiIOCTransfer{}
err = b.setSpeed()
if err != nil {
return err
}
err = b.setBpw()
err = b.setBPW()
if err != nil {
return err
}
@ -113,7 +113,7 @@ func (b *spiBus) init() error {
b.setDelay()
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
return nil
@ -124,7 +124,7 @@ func (b *spiBus) setMode() error {
var err error
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 {
err = syscall.Errno(errno)
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 {
speed = uint32(b.speed)
} else {
speed = defaultSpiSpeed
speed = defaultSPISpeed
}
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 {
err := syscall.Errno(errno)
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
}
func (b *spiBus) setBpw() error {
func (b *spiBus) setBPW() error {
var bpw uint8
if b.bpw > 0 {
bpw = uint8(b.bpw)
} else {
bpw = defaultSpiBpw
bpw = defaultSPIBPW
}
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 {
err := syscall.Errno(errno)
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])))
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 {
err := syscall.Errno(errno)
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)
var err error
err = b.TransferAndRecieveData(data)
if err != nil {
if err := b.TransferAndRecieveData(data); err != nil {
return nil, err
}
return data, nil

View File

@ -11,9 +11,7 @@ import (
)
func main() {
var err error
err = embd.InitSPI()
if err != nil {
if err := embd.InitSPI(); err != nil {
panic(err)
}
defer embd.CloseSPI()
@ -23,30 +21,29 @@ func main() {
dataBuf := []uint8{1, 2, 3}
err = spiBus.TransferAndRecieveData(dataBuf)
if err != nil {
if err := spiBus.TransferAndRecieveData(dataBuf); err != nil {
panic(err)
}
fmt.Println("Recived data is: %v", dataBuf)
fmt.Println("received data is: %v", dataBuf)
dataReceived, err := spiBus.ReceiveData(3)
if err != nil {
panic(err)
}
fmt.Println("Recived data is: %v", dataReceived)
fmt.Println("received data is: %v", dataReceived)
dataByte := byte(1)
receivedByte, err := spiBus.TransferAndReceiveByte(dataByte)
if err != nil {
panic(err)
}
fmt.Println("Recived byte is: %v", receivedByte)
fmt.Println("received byte is: %v", receivedByte)
receivedByte, err = spiBus.ReceiveByte()
if err != nil {
panic(err)
}
fmt.Println("Recived byte is: %v", receivedByte)
fmt.Println("received byte is: %v", receivedByte)
}

View File

@ -18,7 +18,6 @@ func main() {
}
bus := embd.NewSPIBus(embd.SpiMode0, 0, 1000000, 8, 0)
defer clean(bus)
for i := 0; i < 30; i++ {