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

spi: changes in fields/symbols to follow idiomatic go

This commit is contained in:
Kunal Powar 2014-04-28 03:13:57 +05:30
parent 12db8443b0
commit faa686f01c
5 changed files with 47 additions and 50 deletions

View File

@ -12,30 +12,30 @@ import (
) )
const ( const (
SPI_IOC_WR_MODE = 0x40016B01 spiIocWrMode = 0x40016B01
SPI_IOC_WR_BITS_PER_WORD = 0x40016B03 spiIocWrBitsPerWord = 0x40016B03
SPI_IOC_WR_MAX_SPEED_HZ = 0x40046B04 spiIocWrMaxSpeedHz = 0x40046B04
SPI_IOC_RD_MODE = 0x80016B01 spiIocRdMode = 0x80016B01
SPI_IOC_RD_BITS_PER_WORD = 0x80016B03 spiIocRdBitsPerWord = 0x80016B03
SPI_IOC_RD_MAX_SPEED_HZ = 0x80046B04 spiIocRdMaxSpeedHz = 0x80046B04
SPI_IOC_MESSAGE_0 = 1073769216 //0x40006B00 spiIocMessage0 = 1073769216 //0x40006B00
SPI_IOC_INCREMENTER = 2097152 //0x200000 spiIocIncrementor = 2097152 //0x200000
DEFAULT_DELAYMS = uint16(0) defaultDelayms = uint16(0)
DEFAULT_SPI_BPW = uint8(8) defaultSpiBpw = uint8(8)
DEFAULT_SPI_SPEED = uint32(1000000) defaultSpiSpeed = uint32(1000000)
) )
type spiIocTransfer struct { type spiIocTransfer struct {
tx_buf uint64 txBuf uint64
rx_buf uint64 rxBuf uint64
length uint32 length uint32
speed_hz uint32 speedHz uint32
delay_usecs uint16 delayus uint16
bits_per_word uint8 bitsPerWord uint8
} }
type spiBus struct { type spiBus struct {
@ -55,8 +55,8 @@ type spiBus struct {
initialized bool initialized bool
} }
func spi_ioc_message_n(n uint32) uint32 { func spiIocMessageN(n uint32) uint32 {
return (SPI_IOC_MESSAGE_0 + (n * SPI_IOC_INCREMENTER)) return (spiIocMessage0 + (n * spiIocIncrementor))
} }
func NewSPIBus(spiDevMinor, mode, channel byte, speed, bpw, delay int) embd.SPIBus { func NewSPIBus(spiDevMinor, mode, channel byte, speed, bpw, delay int) embd.SPIBus {
@ -103,7 +103,6 @@ func (b *spiBus) init() error {
b.initialized = true b.initialized = true
return nil return nil
} }
func (b *spiBus) setMode() error { func (b *spiBus) setMode() error {
@ -111,7 +110,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(), SPI_IOC_WR_MODE, 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())
@ -126,18 +125,18 @@ func (b *spiBus) setSpeed() error {
if b.speed > 0 { if b.speed > 0 {
speed = uint32(b.speed) speed = uint32(b.speed)
} else { } else {
speed = DEFAULT_SPI_SPEED speed = defaultSpiSpeed
} }
glog.V(3).Infof("spi: setting spi speed_max to %v", speed) glog.V(3).Infof("spi: setting spi speedMax to %v", speed)
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), SPI_IOC_WR_MAX_SPEED_HZ, 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 speed_max due to %v", err.Error()) glog.V(3).Infof("spi: failed to set speedMax due to %v", err.Error())
return err return err
} }
glog.V(3).Infof("spi: speed_max set to %v", speed) glog.V(3).Infof("spi: speedMax set to %v", speed)
b.spiTransferData.speed_hz = speed b.spiTransferData.speedHz = speed
return nil return nil
} }
@ -148,18 +147,18 @@ func (b *spiBus) setBpw() error {
if b.bpw > 0 { if b.bpw > 0 {
bpw = uint8(b.bpw) bpw = uint8(b.bpw)
} else { } else {
bpw = DEFAULT_SPI_BPW 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(), SPI_IOC_WR_BITS_PER_WORD, 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())
return err return err
} }
glog.V(3).Infof("spi: bpw set to %v", bpw) glog.V(3).Infof("spi: bpw set to %v", bpw)
b.spiTransferData.bits_per_word = uint8(bpw) b.spiTransferData.bitsPerWord = uint8(bpw)
return nil return nil
} }
@ -169,10 +168,10 @@ func (b *spiBus) setDelay() {
if b.delayms > 0 { if b.delayms > 0 {
delay = uint16(b.delayms) delay = uint16(b.delayms)
} else { } else {
delay = DEFAULT_DELAYMS delay = defaultDelayms
} }
glog.V(3).Infof("spi: delay_ms set to %v", delay) glog.V(3).Infof("spi: delayms set to %v", delay)
b.spiTransferData.delay_usecs = delay b.spiTransferData.delayus = delay
} }
func (b *spiBus) TransferAndRecieveData(dataBuffer []uint8) error { func (b *spiBus) TransferAndRecieveData(dataBuffer []uint8) error {
@ -180,10 +179,10 @@ func (b *spiBus) TransferAndRecieveData(dataBuffer []uint8) error {
dataCarrier := b.spiTransferData dataCarrier := b.spiTransferData
dataCarrier.length = uint32(len) dataCarrier.length = uint32(len)
dataCarrier.tx_buf = uint64(uintptr(unsafe.Pointer(&dataBuffer[0]))) dataCarrier.txBuf = uint64(uintptr(unsafe.Pointer(&dataBuffer[0])))
dataCarrier.rx_buf = uint64(uintptr(unsafe.Pointer(&dataBuffer[0]))) dataCarrier.rxBuf = uint64(uintptr(unsafe.Pointer(&dataBuffer[0])))
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), uintptr(spi_ioc_message_n(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())

1
samples/.gitignore vendored
View File

@ -23,3 +23,4 @@ tmp006
universalblinker universalblinker
us020 us020
watersensor watersensor
spi

View File

@ -16,13 +16,10 @@ func main() {
} }
defer embd.CloseSPI() defer embd.CloseSPI()
spiBus := embd.NewSPIBus(embd.SPI_MODE_0, 0, 1000000, 8, 0) spiBus := embd.NewSPIBus(embd.SpiMode0, 0, 1000000, 8, 0)
defer spiBus.Close() defer spiBus.Close()
dataBuf := make([]uint8, 3) dataBuf := []uint8{1, 2, 3}
dataBuf[0] = uint8(1)
dataBuf[1] = uint8(2)
dataBuf[2] = uint8(3)
err = spiBus.TransferAndRecieveData(dataBuf) err = spiBus.TransferAndRecieveData(dataBuf)
if err != nil { if err != nil {

12
spi.go
View File

@ -1,13 +1,13 @@
package embd package embd
const ( const (
spi_cpha = 0x01 spiCpha = 0x01
spi_cpol = 0x02 spiCpol = 0x02
SPI_MODE_0 = (0 | 0) SpiMode0 = (0 | 0)
SPI_MODE_1 = (0 | spi_cpha) SpiMode1 = (0 | spiCpha)
SPI_MODE_2 = (spi_cpol | 0) SpiMode2 = (spiCpol | 0)
SPI_MODE_3 = (spi_cpol | spi_cpha) SpiMode3 = (spiCpol | spiCpha)
) )
type SPIBus interface { type SPIBus interface {

View File

@ -10,13 +10,13 @@ type spiDriver struct {
busMap map[byte]SPIBus busMap map[byte]SPIBus
busMapLock sync.Mutex busMapLock sync.Mutex
spf spiBusFactory sbf spiBusFactory
} }
func NewSPIDriver(spiDevMinor byte, sbf spiBusFactory) SPIDriver { func NewSPIDriver(spiDevMinor byte, sbf spiBusFactory) SPIDriver {
return &spiDriver{ return &spiDriver{
spiDevMinor: spiDevMinor, spiDevMinor: spiDevMinor,
spf: sbf, sbf: sbf,
} }
} }
@ -24,7 +24,7 @@ func (s *spiDriver) Bus(mode, channel byte, speed, bpw, delay int) SPIBus {
s.busMapLock.Lock() s.busMapLock.Lock()
defer s.busMapLock.Unlock() defer s.busMapLock.Unlock()
b := s.spf(s.spiDevMinor, mode, channel, speed, bpw, delay) b := s.sbf(s.spiDevMinor, mode, channel, speed, bpw, delay)
s.busMap[channel] = b s.busMap[channel] = b
return b return b
} }